//**************************
// Filename: main.cpp
// Author: Aaron Rogers
// Updated: 09/04/02
// Purpose: Tic Tac Toe game
//**************************

// 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 finish_vbl_func(); // finish VBL function

// Global Variables
TICTACTOE game; // The main game object of the TICTACTOE class
bool newframe = FALSE; // Used for playing sound to keep track of new frames


//*************************************
// 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()
{
	// Seed the random function
	srand(F_VCNT_CURRENT_SCANLINE);

	// 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() != TICTACTOE::QUIT)
	{
		// Each loop, check the game state and react accordingly
		switch(game.gs())
		{
		case TICTACTOE::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() == TICTACTOE::START) {
				if(newframe) {
					// Update the mixer once per frame
					ham_UpdateMixer();
					// Make sure the sounds are playing
					game.check_samples();
					// Reset newframe
					newframe = FALSE;
				}
			}

			// 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 TICTACTOE::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() == TICTACTOE::MAIN) {
				if(newframe) {
					// Update the mixer once per frame
					ham_UpdateMixer();
					// Make sure the sounds are playing
					game.check_samples();
					// Reset newframe
					newframe = FALSE;
				}
			}

			// 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 TICTACTOE::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() == TICTACTOE::PLAY) {
				if(newframe) {
					// Update the mixer once per frame
					ham_UpdateMixer();
					// Make sure the sounds are playing
					game.check_samples();
					// Reset newframe
					newframe = FALSE;
				}
			}

			// 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 TICTACTOE::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() == TICTACTOE::FINISH) {
				if(newframe) {
					// Update the mixer once per frame
					ham_UpdateMixer();
					// Make sure the sounds are playing
					game.check_samples();
					// Swap newframe
					newframe = FALSE;
				}
			}

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

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

			break;
		case TICTACTOE::OPTION:
			break;
		case TICTACTOE::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()
{
	// Sync the mixer at the beginning of the VBL
	ham_SyncMixer();

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

	// Set newframe to true once per frame
	newframe = TRUE;

	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()
{
	// Sync the mixer at the beginning of the VBL
	ham_SyncMixer();

	// Update sprites
	ham_CopyObjToOAM();

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

	// Set newframe to true once per frame
	newframe = TRUE;

	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()
{
	// Sync the mixer at the beginning of the VBL
	ham_SyncMixer();

	// Update sprites
	ham_CopyObjToOAM();

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

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

	// Set newframe to true once per frame
	newframe = TRUE;

	return;
} // End of in_game_vbl_func()


//*************************************
// Function: finish_vbl_func()
// Purpose: Handles control of the game
//          during the finish screen
//*************************************
void finish_vbl_func()
{
	// Sync the mixer at the beginning of the VBL
	ham_SyncMixer();

	// Update sprites
	ham_CopyObjToOAM();

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

	// Set newframe to true once per frame
	newframe = TRUE;

	return;
} // End of finish_vbl_func()