/*
 *  canvas.h
 *  Core Class for Graphics Class
 *
 *  Created by XArt on 18/03/2007.
 *  Copyright 2007 XArt. All rights reserved.
 *
 */

#ifndef _CANVAS_H_
#define _CANVAS_H_

#ifdef _PSP
#include <malloc.h>
#include <pspdisplay.h>
#include <psputils.h>
#include <pspgu.h>
#else
#include <SDL/SDL.h>
extern void sceDisplaySetFrameBuf(void *topaddr, int bufferwidth, int pixelformat, int sync);
extern int sceDisplaySetMode(int mode, int width, int height);
extern void sceDisplayWaitVblankStart(void);
#endif

#ifdef PSP
#include <psptypes.h>
#else
#ifndef _VAR_TYPEDEFS_
#define _VAR_TYPEDEFS_
typedef unsigned char	u8;
typedef char	         s8;
typedef unsigned short	u16;
typedef short	         s16;
typedef unsigned int	   u32;
typedef int             s32;
#endif
#endif

#define SCREEN_WIDTH  480
#define SCREEN_HEIGHT 272

#define PIXEL_SIZE	     4
#define LINE_SIZE         512			        
#define FRAMEBUFFER_SIZE  (LINE_SIZE * SCREEN_HEIGHT * PIXEL_SIZE)
#define PIXELFORMAT       3  // (1=RGBA 5551 3=RGBA 88888)

#define SINGLE_BUFFER 0
#define DOUBLE_BUFFER 1

#ifndef _PSP
extern SDL_Surface *surface;
extern char *_vram; // Virtual PSP VRAM for Mac OS X
#endif

class Canvas
{
private:
protected:
   int   frame;
   u32  *vrambase;
   int mode;
   
   void *drawFrame(void);
   void *displayFrame(void);   
   void *pixelFrame(int x,int y);
   
public:
      
   Canvas(void)
   {
      this->frame = 0;
      this->mode=DOUBLE_BUFFER;
         
#ifdef _PSP
      this->vrambase=(u32 *) (0x40000000 | 0x04000000);
#else
      this->vrambase=(u32 *)_vram;
#endif
      sceDisplaySetMode(0,SCREEN_WIDTH,SCREEN_HEIGHT);
      sceDisplaySetFrameBuf(this->displayFrame(),LINE_SIZE,PIXELFORMAT,0);
   }
   
   void doubleBuffer(bool arg);
   void screenSwap(void);
};

#endif
