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

#include "canvas.h"

#ifndef _PSP
#pragma mark Private for Canvas Class
#endif

void *Canvas::drawFrame(void)
{
   u32* vram = (u32*) vrambase;
   switch (this->mode) {
      case SINGLE_BUFFER:
         if (this->frame == 1) vram += FRAMEBUFFER_SIZE / PIXEL_SIZE;
         break;
         
      case DOUBLE_BUFFER:
         if (this->frame == 0) vram += FRAMEBUFFER_SIZE / PIXEL_SIZE;
         break;
   }
   return (void *)vram;
}

void *Canvas::displayFrame(void)
{
   u32 *vram = (u32*) vrambase;
   if (this->frame == 1) vram += FRAMEBUFFER_SIZE / PIXEL_SIZE;
   return (void *)vram;
}

void *Canvas::pixelFrame(int x,int y)
{
   u32 *vram = (u32 *)this->drawFrame();
   return (void *)(vram+(LINE_SIZE * y + x));
}



#ifndef _PSP
#pragma mark Methods for Canvas Class
#endif



void Canvas::doubleBuffer(bool arg)
{
   if (arg)
      this->mode=DOUBLE_BUFFER;
   else
      this->mode=SINGLE_BUFFER;
}

void Canvas::screenSwap(void)
{
#ifdef _PSP
   if(this->mode==SINGLE_BUFFER) return;
#else
   // Make Mac OS X screen refresh in none double frame buffering mode
   if(this->mode==SINGLE_BUFFER)
   {
      sceDisplaySetFrameBuf(this->displayFrame(),LINE_SIZE,PIXELFORMAT,0);
      return;
   }
#endif
	this->frame ^= 1;
	sceDisplayWaitVblankStart();
	sceDisplaySetFrameBuf(this->displayFrame(),LINE_SIZE,PIXELFORMAT,0);
}
