/* A main file for me learning teh psp haxor! */

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

#include "graphics.h"

#define printf pspDebugScreenPrintf
#define MAX(X, Y) ((X) > (Y) ? (X) : (Y))

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

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

/* Callback thread */
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 */
int SetupCallbacks(void) {
  int thid = 0;
  
  thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
  if(thid >= 0) {
    sceKernelStartThread(thid, 0, 0);
  }
  
  return thid;
}


void fyre() {
  srand(1);
  int i;
  int x, y;
  int red;

  //printTextScreen(100, 0, "FYRE??", GU_ARGB(0xFF, 0xFF, 0xFF, 0x00));

  // drawLineScreen(0, 250, SCREEN_WIDTH, 250, GU_ARGB(0xFF, 0xBB, 0x00, 0x00));
  while (1) {
    /* Draw the initial line */
    for (i=0;i<SCREEN_WIDTH;i++) {
      red = (rand()%128)+128;
      putPixelScreen(GU_ARGB(0xFF, red, 0x00, 0x00), i, SCREEN_HEIGHT-1);
    }
    
    /* more lines */
    for (y=0;y<SCREEN_HEIGHT;y++) {
      for(x=0;x<SCREEN_WIDTH;x++) {
	if(R(getPixelScreen(x, y+1)) > 0)
	  putPixelScreen(GU_ARGB(0xFF, R(getPixelScreen(x, y+1))-1, 0x00, 0x00), x, y);
      }
    }
    
    //sceKernelDelayThread(1000000);
    
    flipScreen();
  }
    
  return;
}

int main() {

  pspDebugScreenInit();
  SetupCallbacks();
  initGraphics();
  
  fyre();

  sceKernelSleepThread();
  return 0;
}

