/* First for PSP */

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspgu.h>
#include <stdio.h>

PSP_MODULE_INFO("Colour",0,1,1);

#define printf pspDebugScreenPrintf
#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))


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


int InputThread(SceSize args, void *argp) {
  
  SceCtrlData pad;
  int i;

  while(1) {
    sceCtrlReadBufferPositive(&pad, 1);
    if (pad.Buttons & PSP_CTRL_CROSS) {
      printf(">< ");
    } else if (pad.Buttons & PSP_CTRL_CIRCLE) {
      printf("() ");
    } else if (pad.Buttons & PSP_CTRL_SQUARE) {
      printf("[] ");
    } else if (pad.Buttons & PSP_CTRL_TRIANGLE) {
      printf("/\\ ");
    }
    for (i=0;i<10;i++) {
      
    }
  }

}

int CallbackThread(SceSize args, void *argp) {

  int cbid;

  cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
  sceKernelRegisterExitCallback(cbid);

  sceKernelSleepThreadCB();

  return 0;
}

int SetupCallbacks(void) {
  int thid = 0;
  int input_thread_id = 0;

  input_thread_id = sceKernelCreateThread("input_thread", InputThread, 
0x11, 0xFA0, 0, 0);
  if (input_thread_id >= 0) {
    sceKernelStartThread(input_thread_id, 0, 0);
  }


  thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
  if (thid >= 0) {
    sceKernelStartThread(thid, 0, 0);
  }
  return thid;
}

void initGraphics() {
  sceDisplaySetMode(0, 320, 240);
 
}

int main () {  
  SceCtrlData pad;

//  scePowerSetClockFrequency(333, 333, 166);
  pspDebugScreenInit();
  SetupCallbacks();  
//  initGraphics();

  printf("BUTTONS!!\n\n");
  

  sceKernelSleepThread();

  return 0;
  
}

