#ifndef __PG_H
#define __PG_H

#include <pspgu.h>
#include <pspctrl.h>

// primitive graphics for Hello World PSP


#define AY8(a,y) ( (a<<24) | (y * 0x10101) )
#define RGB888(r,g,b) ( (b<<16)  | (g<<8) | (r) | 0xff000000 )
#define RGB444(r,g,b) ( ((b<<4)&0xf00)  | (g&0xf0) | ((r>>4)&0xf) | 0xf000 )
#define RGB565(r,g,b) ( ((b<<8) & 0xf800) | ((g<<3) & 0x7e0) | ((r>>3) & 0x1F) )

#define WHITE RGB888(255,255,255)
#define GREY  RGB888(127,127,127)
#define RED   RGB888(255,0,0)
#define GREEN RGB888(0,255,0)
#define BLUE  RGB888(0,0,255)
#define BLACK RGB888(0,0,0)
#define YELLOW RGB888(255,255,0)
#define ORANGE RGB888(255,127,0)
#define VIOLET RGB888(255,0,255)
#define TEAL RGB888(0,255,255)

int Lx, Ly;
 
typedef struct {
	u16 u1, v1;	// origin
	u16 u2, v2;	// clip size
	u16 tw, th;	// tex size
	u16 *b;		// buffer
	float s;	// scale
	u16 f;		// format
} bltrect;

u16 *pgGetVramAddr(u32 x,u32 y);
void pgInit();
void pgWaitV();
void pgWaitVn(u32 count);
void pgStartFrame();
void pgEndFrame();
void pgFillvram(u32 color);
void pgBitBltD(u16 *db, u16 ds, u16 dx, u16 dy,
               u16 *sb, u16 ss, u16 sx, u16 sy, u16 sw, u16 sh);
void pgBitBltRM(bltrect *sr, u16 dx, u16 dy, u32 col);
#define pgBitBltR(sr,dx,dy) pgBitBltRM(sr,dx,dy,WHITE)
void pgBitBlt(u32 x,u32 y,u32 w,u32 h,u32 mag,const u16 *d);

#define SCREEN_WIDTH  480
#define SCREEN_HEIGHT 272
#define         PIXELSIZE       1                               //in short
#define         LINESIZE        512                             //in short
#define         FRAMESIZE       0x44000                 //in byte
#define CMAX_X 60
#define CMAX_Y 34
#define CMAX2_X 30
#define CMAX2_Y 17
#define CMAX4_X 15
#define CMAX4_Y 8 

//LUDO:
# if 0
# define GE_TPSM_8888 GU_PSM_8888
# define GE_PSM_8888  GU_PSM_8888

# define GU_STATE_SCISSOR GU_SCISSOR_TEST
# define GU_STATE_TEXTURE GU_TEXTURE
# define GU_STATE_ALPHA   GU_ALPHA_TEST

# define GE_FACE_CW        GU_CW
# define GE_CLEAR_COLOR    GU_COLOR_BUFFER_BIT
# define GE_CLEAR_DEPTH    GU_DEPTH_BUFFER_BIT

# define GE_ALPHA_ADD      GU_ADD
# define GE_ALPHA_FIX      GU_FIX

# define GU_PRIM_SPRITES   GU_SPRITES

# define GE_SETREG_VTYPE(tt, ct, nt, mt, wt, it, nw, vb, dm) \
    ((u32)(tt)      | ((u32)(ct) << 2)      | ((u32)(nt) << 5) | \
    ((u32)(mt) << 7)| ((u32)(wt) << 9)      | ((u32)(it) << 11) | \
    ((u32)(nw) <<14)| ((u32)(vb) << 18)     | ((u32)(dm) << 23))
# else
#define GU_PRIM_POINTS          (0)
#define GU_PRIM_LINES           (1)
#define GU_PRIM_LINESTRIPS      (2)
#define GU_PRIM_TRIANGLES       (3)
#define GU_PRIM_TRISTRIPS       (4)
#define GU_PRIM_TRIFANS         (5)
#define GU_PRIM_SPRITES         (6)

#define GU_STATE_ATE            (0) // Alpha Test
#define GU_STATE_ZTE            (1) // Depth Test
#define GU_STATE_SCISSOR        (2)
#define GU_STATE_UNKNOWN3       (3)
#define GU_STATE_ALPHA          (4) // Alpha Blend
#define GU_STATE_CULL           (5)
#define GU_STATE_UNKNOWN6       (6)
#define GU_STATE_UNKNOWN7       (7)
#define GU_STATE_UNKNOWN8       (8)
#define GU_STATE_TEXTURE        (9)
#define GU_STATE_LIGHTING       (10)
#define GU_STATE_LIGHT0         (11) // Light 0 Enable
#define GU_STATE_LIGHT1         (12) // Light 1 Enable
#define GU_STATE_LIGHT2         (13) // Light 2 Enable
#define GU_STATE_LIGHT3         (14) // Light 3 Enable
#define GU_STATE_UNKNOWN15      (15)
#define GU_STATE_UNKNOWN16      (16)
#define GU_STATE_UNKNOWN17      (17)
#define GU_STATE_UNKNOWN18      (18)
#define GU_STATE_UNKNOWN19      (19)
#define GU_STATE_PATCH_FACE     (20) // sceGuPatchFrontFace()
#define GU_STATE_UNKNOWN21      (21)

#define GU_DISPLAY_OFF          (0)
#define GU_DISPLAY_ON           (1)

#define GU_MATRIX_PROJECTION    (0)
#define GU_MATRIX_VIEW          (1)
#define GU_MATRIX_WORLD         (2)
#define GU_MATRIX_TEXTURE       (3) /* this is untested, only assumed */

/* TODO: should the GE_ defines move to pspge.h? It's not like they are exclusive to pspgu */

/* vertex type by Neovangelist */
#define GE_SETREG_VTYPE(tt, ct, nt, mt, wt, it, nw, vb, dm) \
        ((u32)(tt)      | ((u32)(ct) << 2)      | ((u32)(nt) << 5) | \
        ((u32)(mt) << 7)| ((u32)(wt) << 9)      | ((u32)(it) << 11) | \
        ((u32)(nw) <<14)| ((u32)(vb) << 18)     | ((u32)(dm) << 23))

#define GE_TT_NONE              0x00
#define GE_TT_8BIT              0x01
#define GE_TT_16BIT             0x02
#define GE_TT_32BITF            0x03

#define GE_CT_NONE              0x00
#define GE_CT_RES1              0x01
#define GE_CT_RES2              0x02
#define GE_CT_RES3              0x03
#define GE_CT_5650              0x04
#define GE_CT_5551              0x05
#define GE_CT_4444              0x06
#define GE_CT_8888              0x07

#define GE_NT_NONE              0x00
#define GE_NT_8BIT              0x01
#define GE_NT_16BIT             0x02
#define GE_NT_32BITF            0x03

#define GE_MT_RES               0x00
#define GE_MT_8BIT              0x01
#define GE_MT_16BIT             0x02
#define GE_MT_32BITF            0x03

#define GE_WT_NONE              0x00
#define GE_WT_8BIT              0x01
#define GE_WT_16BIT             0x02
#define GE_WT_32BITF            0x03

#define GE_IT_NONE              0x00
#define GE_IT_8BIT              0x01
#define GE_IT_16BIT             0x02
#define GE_IT_RES               0x03

#define GE_BM_3D                0x00
#define GE_BM_2D                0x01

#define GE_PSM_5650             (0)
#define GE_PSM_5551             (1)
#define GE_PSM_4444             (2)
#define GE_PSM_8888             (3)

#define GE_TPSM_5650            (0)
#define GE_TPSM_5551            (1)
#define GE_TPSM_4444            (2)
#define GE_TPSM_8888            (3)
#define GE_TPSM_T4              (4)
#define GE_TPSM_T8              (5)

#define GE_SHADE_FLAT           (0)
#define GE_SHADE_GOURAUD        (1)

#define GE_FILTER_POINT         (0)
#define GE_FILTER_LINEAR        (1)

#define GE_WRAP_REPEAT          (0)
#define GE_WRAP_CLAMP           (1)

#define GE_FACE_CW              (0)
#define GE_FACE_CCW             (1)

#define GE_TEST_NEVER           (0)
#define GE_TEST_ALWAYS          (1)
#define GE_TEST_FUNCTION2       (2) /* figure these out */
#define GE_TEST_FUNCTION3       (3)
#define GE_TEST_LESS            (4)
#define GE_TEST_LEQUAL          (5)
#define GE_TEST_GREATER         (6)
#define GE_TEST_GEQUAL          (7)

#define GE_CLEAR_COLOR          (1)
#define GE_CLEAR_DEPTH          (4)

#define GE_TFX_MODULATE         (0)
#define GE_TFX_REPLACE          (3)
#define GE_TFX_ADD              (4)

#define GE_TCC_RGB              (0)
#define GE_TCC_RGBA             (1)

#define GE_ALPHA_ADD            (0)
#define GE_ALPHA_SUBTRACT       (1)

#define GE_ALPHA_SRC_COLOR              (0)
#define GE_ALPHA_ONE_MINUS_SRC_COLOR    (1)
#define GE_ALPHA_SRC_ALPHA              (2)
#define GE_ALPHA_ONE_MINUS_SRC_ALPHA    (3)

#define GE_ALPHA_DST_COLOR              (0)
#define GE_ALPHA_ONE_MINUS_DST_COLOR    (1)

#define GE_ALPHA_FIX                    (10)


# endif


void readpad(int HomeInhibit);

extern u32 new_pad, now_pad, old_pad;
extern SceCtrlData paddata;

#define UPPER_THRESHOLD  0xcf
#define LOWER_THRESHOLD  0x2f

void pgBattery(int x, int y);
void pgDrawFrame(unsigned long x1, unsigned long y1, unsigned long x2, unsigned long y2, unsigned long color);
void pgFillBox(unsigned long x1, unsigned long y1, unsigned long x2, unsigned long y2, unsigned long color);
void mh_print(int x,int y,const char *msg, u32 col);

#endif
