//*****************************************
// Filename: main.cpp
// Author: Aaron Rogers
// Updated: 11/02/02
// Purpose: Hearts game AI
//*****************************************
// This file is part of HAM Tutorial Hearts
// Copyright 2002 Aaron Rogers
// See README.txt for more information
//*****************************************


// Library Includes
#include "ai.h"
#include "game.h"
#include "mygba.h"

// Function prototypes
void start_screen_vbl_func(); // start screen VBL function
void main_screen_vbl_func(); // main screen VBL function
void in_game_vbl_func(); // in game VBL function
void score_vbl_func(); // score VBL function
void finish_vbl_func(); // finish VBL function
void options_vbl_func(); // options VBL function

// Global Variables
HEARTS game; // The main game object of the HEARTS class


//*************************************
// Function: main()
// Purpose: The starting point for the
// Tic Tac Toe game. A new game is
// created, initialized and controled
// by the VBL functions
//*************************************
int main()
{
  // Initialize the game
  game.init();

  // Here's the main game state loop. As long as the
  // game state isn't set to QUIT, keep looping
  while (game.gs() != HEARTS::QUIT)
  {
   // Each loop, check the game state and react accordingly
   switch(game.gs())
   {
   case HEARTS::START:
   // Initialize the start screen
   game.start_screen_init();

   // Start the VBL interrupt handler
   ham_StartIntHandler(INT_TYPE_VBL,(void*)&start_screen_vbl_func);

   // Loop while we are still at the start screen
   while(game.gs() == HEARTS::START) {}

   // At this point the start screen is over
   // Clean up after the start screen
   game.start_screen_deinit();

   // Stop the VBL interrupt handler
   ham_StopIntHandler(INT_TYPE_VBL);
  
   break;
   case HEARTS::MAIN:
   // Initialize the main screen
   game.main_screen_init();

   // Start the VBL interrupt handler
   ham_StartIntHandler(INT_TYPE_VBL,(void*)&main_screen_vbl_func);

   // Loop while we are still at the main screen
   while(game.gs() == HEARTS::MAIN) {}

   // At this point the main screen is over
   // Clean up after the main screen
   game.main_screen_deinit();

   // Stop the VBL interrupt handler
   ham_StopIntHandler(INT_TYPE_VBL);
  
   break;
   case HEARTS::PLAY:
   // Initialize the game loop
   game.in_game_init();

   // Start the VBL interrupt handler
   ham_StartIntHandler(INT_TYPE_VBL,(void*)&in_game_vbl_func);

   // Loop while we are still playing the game
   while(game.gs() == HEARTS::PLAY) {}

   // At this point the game is over
   // Clean up after the game loop
   game.in_game_deinit();

   // Stop the VBL interrupt handler
   ham_StopIntHandler(INT_TYPE_VBL);

   break;
   case HEARTS::SCORE:
   // Initialize the score screen
   game.score_init();

   // Start the VBL interrupt handler
   ham_StartIntHandler(INT_TYPE_VBL,(void*)&score_vbl_func);

   // Loop while we are still at the score screen
   while(game.gs() == HEARTS::SCORE) {}

   // At this point the score screen is over
   // Clean up after the game loop
   game.score_deinit();

   // Stop the VBL interrupt handler
   ham_StopIntHandler(INT_TYPE_VBL);

   break;
   case HEARTS::FINISH:
   // Initialize the finish screen loop
   game.finish_init();

   // Start the VBL interrupt handler
   ham_StartIntHandler(INT_TYPE_VBL,(void*)&finish_vbl_func);
  
   // Loop while we are still at the finish screen
   while(game.gs() == HEARTS::FINISH) {}

   // Clean up after the finish screen
   game.finish_deinit();

   // Stop the VBL interrupt handler
   ham_StopIntHandler(INT_TYPE_VBL);

   break;
   case HEARTS::OPTIONS:
   // Initialize the options screen loop
   game.options_init();

   // Start the VBL interrupt handler
   ham_StartIntHandler(INT_TYPE_VBL,(void*)&options_vbl_func);
  
   // Loop while we are still at the finish screen
   while(game.gs() == HEARTS::OPTIONS) {}

   // Clean up after the finish screen
   game.options_deinit();

   // Stop the VBL interrupt handler
   ham_StopIntHandler(INT_TYPE_VBL);

   break;
   case HEARTS::QUIT:
   break;
   default: // This should never happen
   break;
   }

  } // End of while(game_state != QUIT)

  return 0;
} // End of main()


//*************************************
// Function: start_screen_vbl_func()
// Purpose: Handles control of the game
// during the start screen
//*************************************
void start_screen_vbl_func()
{
  // Check for keypad input
  game.start_screen_query_keys();

  return;
} // End of start_screen_vbl_func()


//*************************************
// Function: main_screen_vbl_func()
// Purpose: Handles control of the game
// during the main screen
//*************************************
void main_screen_vbl_func()
{
  // Update sprites
  ham_CopyObjToOAM();

  // Check for keypad input
  game.main_screen_query_keys();

  // Update sprite, etc. info
  game.main_screen_redraw();

  return;
} // End of main_screen_vbl_func()


//********************************
// Function: in_game_vbl_func()
// Purpose: Handles control of the
// game during play
//********************************
void in_game_vbl_func()
{
  // Update sprites
  ham_CopyObjToOAM();

  // Check for keypad input
  game.in_game_query_keys();

  // Update sprite, etc. info
  game.in_game_redraw();

  return;
} // End of in_game_vbl_func()


//**********************************
// Function: score_vbl_func()
// Purpose: Handles control of the
// game during score screen
//**********************************
void score_vbl_func()
{
  // Update sprites
  ham_CopyObjToOAM();

  // Check for keypad input
  game.score_query_keys();

  // Update sprite, etc. info
  game.score_redraw();

  return;
} // End of score_vbl_func()


//*************************************
// Function: finish_vbl_func()
// Purpose: Handles control of the game
// during the finish screen
//*************************************
void finish_vbl_func()
{
  // Update sprites
  ham_CopyObjToOAM();

  // Check for keypad input
  game.finish_query_keys();

  return;
} // End of finish_vbl_func()


//**********************************
// Function: options_vbl_func()
// Purpose: Handles control of the
// game during options screen
//**********************************
void options_vbl_func()
{
  // Update sprites
  ham_CopyObjToOAM();

  // Check for keypad input
  game.options_query_keys();

  // Update sprite, etc. info
  game.options_redraw();

  return;
} // End of options_vbl_func()