/* Do basic initialization, get the loop going */

#include "gamestate.h"
#include "titlescreen.h"
#include "debug.h"
#include <SDL.h>

int main(int argc, char** argv)
{
    // Barebones setup for our game
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false;
    SDL_WM_SetCaption("TreeWars","TreeWars");

    SDL_Surface* display = SDL_SetVideoMode(800, 600, 32,
					    SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (display == NULL)
    {
#ifdef DEBUG
	std::cerr << "debug: main: error: Couldn't create main surface\n";
#endif
	return 1;
    }

    stack<GameState*> state_stack;

    // initialize the stack by initting and pushing the initial state(s)
    GameState* tmpstate = new TitleScreen(&state_stack, display);
    if (!tmpstate->init()) return 1;
    state_stack.push(tmpstate);

    while (!state_stack.empty())
    {
	GameState* state = state_stack.top();
	try {
	    state->execute();
	} catch (StateExit& e) {

	    // remove the old state
	    state_stack.pop();
	    delete state;
	    
	    // init the new state, discarding it if we fail
	    while (!(state_stack.empty() || state_stack.top()->init()))
	    {
		state_stack.pop();
		delete state;
	    }
	}
    }

    SDL_FreeSurface(display);
    SDL_Quit();

    return 0;
}