/* main.c
 *
 * PSPlords 0.0.1 for PSP
 *
 * (c) 2006, Joseph Curtis
 */

#include "x86.h"

#ifndef x86
#include <pspdisplay.h>
#include <pspctrl.h>
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <png.h>
#include <pspthreadman.h>
#endif

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

#include "world.h"
#include "display.h"
#include "input.h"


#ifndef x86
#include "graphics.h"


#define printf pspDebugScreenPrintf



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

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


extern struct display *default_display;
extern struct world *default_world;


/* 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;
}
#endif


extern struct world *default_world;
extern struct display *default_display;

int main() {

#ifndef x86
  pspDebugScreenInit();
  SetupCallbacks();
  initGraphics();
#endif  

  setup_default_display();
  default_world = create_world();
  load_world_text(default_world, "world2.txt");
  printf("Loaded world\n");
  
  /* create an army */
  create_test_army(default_world);
  printf("Created test army\n");

  /* create a player */
  create_test_player(default_world);
  printf("Created test player\n");

  //  update_main(default_display, default_world);

  //  printf("%d\n", sizeof(struct town));
#ifndef x86
  input_loop();
  
  sceKernelSleepThread();
#endif
  return 0;
}

