/* 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 "main.h"
#include "world.h"
#include "display.h"
#include "input.h"

#ifdef x86
#include "SDL/SDL.h"
#include "SDL/SDL_thread.h"
SDL_Thread *update_thread = NULL;
#endif
int quit = 0;

#ifndef x86
int thread = 0;
#endif

#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;


int update_loop();

/* 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);
  printf("Abount to call update loop\n");
  //  update_loop();  
  
  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;

#define NUM_COLORS 256


int update_loop() {
  printf("Before update loop\n");
  while (!quit) {
    printf("thread looped\n");
    update(default_display, default_world);
#ifdef x86
    SDL_Delay(200);
#endif
#ifndef x86
    sceKernelDelayThread(2000);
#endif
  }
}



int main() {

  //FILE *out;
  fclose(stdout);
  //  out = fopen("/dev/null", "w");

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

#ifdef x86
  /* initiate SDL */
  if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
    printf("SDL failed to init!\n");
    return 1;
  }
  SDL_WM_SetCaption("PSPLords v0.1 SDL version", NULL);
#endif

  setup_default_display();
  default_world = create_world();
  load_world_text(default_world, "world2.txt");
  printf("Loaded world\n");
  
  /* not sure if this is the best way to go about it...? */
  main_menu();


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

  /* create an army */
  //  create_test_army(default_world);
  //  printf("Created test army\n");

#ifdef x86
  printf("before creating thread\n");
  //  update_thread = SDL_CreateThread(update_loop, NULL);
  printf("after creating thread\n");
  sdl_input_loop();
#endif  

  //  printf("%d\n", sizeof(struct town));

#ifndef x86
  //  thread = sceKernelCreateThread("update_thread", update_loop, 0x18, 0x10000, 0, NULL);
  //  if(thread >= 0) {
  //    sceKernelStartThread(thread, 0, 0);
  //  }
  input_loop();
  
  sceKernelSleepThread();
#endif

  return 0;
}


void shutdown() {
  /* insert nice shutting down stuff here */
  quit = 1;
  exit(0);
}

