/**
 * Nametry
 * (c) Kenta Cho (cs8k-cyu@asahi-net.or.jp)
 * http://www.asahi-net.or.jp/~cs8k-cyu/
 *
 * nametry.c(main routine)
 */

#include <sys/bios.h>
#include <stdlib.h>

#include "nametry.h"

Boolean loopf = TRUE;
int timer;

static int startApplication(void) {
  initDisplay();
  initSound();
  randomize();
  if ( !loadPrefs() ) {
    initRanking();
  }
  gotoTitle();
}

static void stopApplication(void) {
  closeSound();
  closeDisplay();
  savePrefs();
}

/**
 * Proceed one frame.
 * gameStatus.status represents a state of game.
 */
static void loop() {
  unsigned long startTick = sys_get_tick_count();
  unsigned loopTick;
  switch ( gameStatus.status ) {
  case GAME_IN_PROGRESS:
    handleGameStatus();
    break;
  case TITLE:
    handleTitle();
    break;
  case GAMEOVER:
    handleGameover();
    break;
  case GAME_GAMEOVER:
    handleGameGameover();
    break;
  case RANKING:
    handleRanking();
    break;
  case PAUSE:
    handlePause();
    break;
  }
  loopTick = sys_get_tick_count()-startTick;
  if ( loopTick < INTERVAL ) {
    sys_wait(INTERVAL-loopTick);
  }
  timer++;
}

void main(int argc, char *argv[]) {
  startApplication();
  while ( loopf ) {
    loop();
  }
  stopApplication();
  bios_exit();
}
