/*
 *  graphics.h
 *  Cross Platform Graphics for PSP & Mac OS X PPC/Intel
 *
 *  Created by XArt on 25/01/2007.
 *  Copyright 2007 XArt. All rights reserved.
 *
 */

#ifndef _GRAPHICS_H_
#define _GRAPHICS_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef _PSP
#include <malloc.h>
#include <pspdisplay.h>
#include <psputils.h>
#include <pspgu.h>
#else
extern int stricmp(const char *dest, const char *src);
#endif

#include "canvas.h"
#include "picture.h"

#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

#define COLUMNS 60
#define ROWS 34

#define R(n) (n)
#define G(n) (n<<8)
#define B(n) (n<<16)
#define A(n) (n<<24)
//#define Y(r,g,b)

#define RGB(r,g,b) ( R(r) | G(g) | B(b) ) | 0xff000000
#define RGBA(r,g,b,a) ( R(r) | G(g) | B(b) | A(a) )

/*
 From RGB to YUV
 
 Y = 0.299R + 0.587G + 0.114B
 U = 0.492 (B-Y)
 V = 0.877 (R-Y)
 
 It can also be represented as:
 
 Y =  0.299R + 0.587G + 0.114B
 U = -0.147R - 0.289G + 0.436B
 V =  0.615R - 0.515G - 0.100B
 
 
 From YUV to RGB
 
 R = Y + 1.140V
 G = Y - 0.395U - 0.581V
 B = Y + 2.032U
 */

typedef unsigned int    Color;

#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

class Graphics : public Canvas, public Picture
{
private:
   u32 dummy;
   
   int drawGlyph(int x, int y,char ascii);
   int glyphWidth(char ascii);
   char *resolveLink(char *filename, const char *linkfile);      
   u32 alphaBlend(u32 dest, u32 src);
protected:
   
   
public:
   bool bold,italic,kerning;
   enum { LEFT, RIGHT, CENTRE } alignment;
   u8   leading,tracking;      
   u32  foreColor,fillColor;
   
   Graphics(void)
   {
      /* Initialize text variables */
      this->fillColor=0x00000000; /* ABGR */
      this->foreColor=0xFFFFFFFF; /* ABGR */
      this->alignment=LEFT; /* Text alignment defaulted to left alignment */
      this->tracking=0;
      this->leading=8;
      this->kerning=true;
   }
   
   void* pixel(int x, int y);
   void blitAlpha(int sx, int sy, int width, int height, int srcw, void *src, int dx, int dy, int destw, void *dest);
   void clearRect(int x, int y, int width, int height);
   void drawLine(int x1, int y1, int x2, int y2);
   void drawHorizontalLine(int x, int y, int width);
   void drawVerticalLine(int x, int y, int height);
   //void drawOval(int x, int y, int width, int height);
   void drawCircle(int x, int y, int radius);
  void drawRect(int x, int y, int width, int height);
   void drawString(const char *text, int x, int y);
   void drawString(const char *text, int x, int y, int wrapWidth);
   void drawString(const char *text, int x, int y, int wrapWidth, bool condence);
   int  stringWidth(const char *text);
   void fillRect(int x, int y, int width, int height);
   //void fillOval(int x, int y, int width, int height);
   void fillCircle(int x, int y, int radius);
   void fillScreen(void);
   void drawPicture(Image *image);
   void drawPicture(Image *image, int x, int y);
   void drawPicture(Image *image, int x, int y, int destWidth, int destHeight);
   void drawPicture(Image *image, int x, int y, int destWidth, int destHeight, int sourceX, int sourceY);
   void drawPicture(Image *image, int x, int y, int destWidth, int destHeight, int sourceX, int sourceY, int sourceWidth, int sourceHeight);
   void loadScreen(const char *filename);
   void saveScreen(const char *filename);
   void storeScreen(void *pixels);
   void storeScreen(void *pixels, bool displayFrameBuffer);
   void restoreScreen(void *pixels);
   void restoreScreen(void *pixels, bool displayFrameBuffer);
};
#endif
