/*
 *  Universal Application for Mac Intel, Mac PPC & PSP
 *
 *  Created by XArt on 15/03/2007.
 *  Copyright 2007 XArt. All rights reserved.
 *
 */
   
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include "endian.c"

#ifndef _PSP
// Emulate PSP Functions for Mac OS X
#include "psp.h"
#else
#include <pspsdk.h>
#include <pspkernel.h>
#include <pspctrl.h>
#include <pspdisplay.h>
#include <pspiofilemgr.h>
#include <psppower.h>
#include <pspumd.h>
#include <pspusb.h>
#include <pspusbstor.h>
#include <pspdisplay.h>
#include <psputils.h>
#endif

#ifdef _PSP
PSP_MODULE_INFO("Application", 0x1000, 1, 1);
PSP_MAIN_THREAD_ATTR(0);

static int exit_callback(int arg1, int arg2, void *common);
static int CallbackThread(SceSize args, void *argp);
static int SetupCallbacks(void);

// Exit callback
static int exit_callback(int arg1, int arg2, void *common) {
	sceKernelExitGame();
	return 0;
}

// Callback thread 
static int CallbackThread(SceSize args, void *argp) {
	int cbid;
   
	cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
	sceKernelRegisterExitCallback(cbid);
   
	sceKernelSleepThreadCB();
   
	return 0;
}

// Sets up the callback thread and returns its thread id
static int SetupCallbacks(void) {
	int thid = 0;
   
	thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
	if(thid >= 0)
		sceKernelStartThread(thid, 0, 0);
   
	return thid;
}
#endif

#include "graphics.h"
#include "effects.h"

int main(int argc, char *argv[])
{
	
	int done;
   int sm=0;
   int x,y;
   SceCtrlData ctrldata;
   unsigned int buttons=0;
   
   sceGuInit();
   pspDebugScreenInit();
   
#ifdef _PSP
   // Init the controll pad & analog
	sceCtrlSetSamplingCycle(0);
	sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);
   SetupCallbacks();
#endif
   Graphics *graphics=new Graphics;
   
   Image *imgPicture=new Image;
   
#ifndef _PSP
   imgPicture=graphics->loadPicture("../../picture.png");
#else
   imgPicture=graphics->loadPicture("picture.png");
#endif

   graphics->doubleBuffer(false);
   
	done = 0;
   while (!done) {
      graphics->foreColor=RGB(rand()%256,rand()%256,rand()%256);
      graphics->fillColor=RGB(rand()%256,rand()%256,rand()%256);
      switch (sm)
      {
         case 0:
            graphics->drawLine(rand()%SCREEN_WIDTH,rand()%SCREEN_HEIGHT,rand()%SCREEN_WIDTH,rand()%SCREEN_HEIGHT);
            break;
            
         case 1:
            x=rand()%SCREEN_WIDTH;
            y=rand()%SCREEN_HEIGHT;
            graphics->drawCircle(x,y,rand()%128);
            break;
            
         case 2:
            x=rand()%SCREEN_WIDTH;
            y=rand()%SCREEN_HEIGHT;
            graphics->drawRect(x,y,rand()%(SCREEN_WIDTH-x),rand()%(SCREEN_HEIGHT-y));
            break;            
            
         case 3:
            x=rand()%SCREEN_WIDTH;
            y=rand()%SCREEN_HEIGHT;
            graphics->fillCircle(x,y,rand()%128);
            break;
   
         case 4:
            x=rand()%SCREEN_WIDTH;
            y=rand()%SCREEN_HEIGHT;
            graphics->fillRect(x,y,rand()%(SCREEN_WIDTH-x),rand()%(SCREEN_HEIGHT-y));
            break;
            
         case 5:
            x=rand()%SCREEN_WIDTH;
            y=rand()%SCREEN_HEIGHT;
            graphics->drawHorizontalLine(x,y,rand()%(SCREEN_WIDTH-x));
            break;
            
         case 6:
            x=rand()%SCREEN_WIDTH;
            y=rand()%SCREEN_HEIGHT;
            graphics->drawVerticalLine(x,y,rand()%(SCREEN_HEIGHT-y));
            break;
            
         case 7:
            x=rand()%(SCREEN_WIDTH-imgPicture->width);
            y=rand()%(SCREEN_HEIGHT-imgPicture->height);
            graphics->drawPicture(imgPicture,x,y);
            break;
      }
      graphics->screenSwap();
      
      sceCtrlReadBufferPositive(&ctrldata, 1);
      
      if (ctrldata.Buttons & PSP_CTRL_CROSS) {
         // Pressed
         if (!(buttons & PSP_CTRL_CROSS)) {
         }
         buttons |= PSP_CTRL_CROSS;
      } else {
         // DePressed
         if (buttons & PSP_CTRL_CROSS) {
            graphics->clearRect(0,0,SCREEN_WIDTH,SCREEN_HEIGHT);
            sm=(sm!=7)?sm+1:0;
         }
         buttons &= ~PSP_CTRL_CROSS; 
      }
      
      
      if (ctrldata.Buttons & PSP_CTRL_SQUARE) {
         // Pressed
         if (!(buttons & PSP_CTRL_SQUARE)) {
#ifndef _PSP
            graphics->saveScreen("../../screen.png");
#else
            graphics->saveScreen("screen.png");
#endif
         }
         buttons |= PSP_CTRL_SQUARE;
      } else {
         // DePressed
         if (buttons & PSP_CTRL_SQUARE) {
         }
         buttons &= ~PSP_CTRL_SQUARE; 
      }
      
      if (ctrldata.Buttons & PSP_CTRL_HOME) done=1;
      
	}
	
   sceKernelExitGame();
	return(0);
}
