treewars/main.cpp

61 lines
1.3 KiB
C++
Raw Normal View History

/* Do basic initialization, get the loop going */
#include "gamestate.h"
#include "titlescreen.h"
#include "debug.h"
#include <SDL.h>
#include <SDL_ttf.h>
int main(int argc, char** argv)
{
// Barebones setup for our game
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false;
if (TTF_Init() < 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();
// init the new state, discarding it if we fail
if (!(state->initted() || state->init()))
{
state_stack.pop();
delete state;
continue;
}
try {
state->execute();
} catch (StateExit& e) {
// remove the old state
state_stack.pop();
delete state;
}
}
SDL_FreeSurface(display);
TTF_Quit();
SDL_Quit();
return 0;
}