Refactored code heavily to be able to use a state stack - main function now does the basic init, looping, and shutdown, and each state is a pointer to an object that has its own event handling, rendering, etc

This commit is contained in:
2011-06-27 22:07:52 -04:00
parent a77a8575c5
commit 5522cbc64c
7 changed files with 136 additions and 94 deletions

52
game.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "game.h"
#include "mathutils.h"
#include "debug.h"
#include <SDL.h>
int Game::NODE_RADIUS = 12;
Game::~Game()
{
renderer.cleanup();
}
void Game::execute(stack<GameState*> &state_stack) throw(StateExit)
{
SDL_Event event;
while(SDL_PollEvent(&event))
handle_event(&event);
renderer.render(data);
}
bool Game::init()
{
return renderer.init();
}
void Game::on_exit()
{
throw StateExit();
}
void Game::on_lbutton_down(int x, int y)
{
data.do_vertex(x, y, NODE_RADIUS);
}
void Game::on_rbutton_down(int mX, int mY)
{
data.clear_current_vertex();
}
void Game::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode)
{
if (sym == SDLK_q && mod & KMOD_CTRL) throw StateExit();
if (sym == SDLK_a) data.set_mode(MODE_ATTACK);
if (sym == SDLK_m) data.set_mode(MODE_MOVE);
}