#ifndef __OBJECT_H
#define __OBJECT_H


#include "types.h"
#include "video.h"

////// Object Manipulation Macros //////

// On-screen Position
#define	OBJ_SET_X(n,x)		do { OAM[ (n) << 2]      = (x) } while (0)
#define	OBJ_SET_Y(n,y)		do { OAM[((n) << 2) + 2] = (y) } while (0)
#define	OBJ_SET_POS(n,x,y)	OBJ_SET_X((n),(x)) OBJ_SET_Y((n),(y))
// Parallax
#define OBJ_SET_P(n,p)		do { OAM[((n) << 2) + 1] = (OAM[((n) << 2) + 1] & 0xC000) | (p) } while (0)
// Character
#define	OBJ_SET_CHAR(n,c)	do { OAM[((n) << 2) + 3] = (OAM[((n) << 2) + 3] & 0xF000) | (u16)(c) } while (0)
// Horiz. or Vert. Flipped
#define OBJ_SET_H(n,h)		do { if (h) OAM[((n) << 2) + 3] |= 0x2000; else OAM[((n) << 2) + 1] &= 0xDFFF; } while (0)
#define	OBJ_SET_V(n,v)		do { if (v) OAM[((n) << 2) + 3] |= 0x1000; else OAM[((n) << 2) + 1] &= 0xEFFF; } while (0)
// Palette
#define OBJ_SET_PAL(n,pal)	do { OAM[((n) << 2) + 3] = (((pal) << 8) & 0xC000) | (OAM[((n) << 2) + 3] & 0x3FFF) } while (0)
// Left, Right or Both eye visibility (l, r, and v are boolean)
#define OBJ_SET_LON(n,l)	do { if (l) OAM[((n) << 2) + 1] |= 0x8000; else OAM[((n) << 2) + 1] &= 0x7FFF; } while (0)
#define OBJ_SET_RON(n,r)	do { if (r) OAM[((n) << 2) + 1] |= 0x4000; else OAM[((n) << 2) + 1] &= 0xBFFF; } while (0)
#define OBJ_SET_VIS(n,v)	do { if (v) OAM[((n) << 2) + 1] |= 0xC000; else OAM[((n) << 2) + 1] &= 0x3FFF; } while (0)

////// Object Functions //////

// (Obsoleted by the OBJ_* macros...)
void vbSetObject (u16 n, u8 header, s16 x, s16 p, s16 y, u16 chr)
{
	n <<= 2;
	OAM[n    ] = (u16)x;
	OAM[n + 1] = (u16)p | ((u16)header << 8);
	OAM[n + 2] = (u16)y;
	OAM[n + 3] = ((header & 0x3C) << 10) | (chr & 0x7FF);
}

/* Object attributes */
#define	JX		0			// Display pointer X
#define	JP		2			// Paralax
#define	JY		4			// Display pointer Y
#define	JCA		6			// Character No.

// "vbSetObject" and OBJ_x header flags //	// (OR these together to build an Object Header)
#define	OBJ_ON		0x00C0
#define	OBJ_LON		0x0080
#define	OBJ_RON		0x0040

#define	OBJ_PAL0	0x0000
#define	OBJ_PAL1	0x0010
#define	OBJ_PAL2	0x0020
#define	OBJ_PAL3	0x0030

#define	OBJ_HFLIP	0x0008
#define	OBJ_VFLIP	0x0004


#endif
