Refactored a wee bit of code in the GameStack... stack, and started building the title screen code

This commit is contained in:
Anna Rose 2011-06-28 21:38:18 -04:00
parent 9961d177fd
commit 6a4b1c4cf2
7 changed files with 76 additions and 14 deletions

View File

@ -4,7 +4,7 @@ PROJECT=treewars
CXX=g++ CXX=g++
CXXFLAGS=-DDEBUG -g `sdl-config --cflags` CXXFLAGS=-DDEBUG -g `sdl-config --cflags`
LDFLAGS=`sdl-config --libs` LDFLAGS=`sdl-config --libs`
OBJECTS=drawutils.o game.o graph.o main.o mainevent.o mathutils.o gamedata.o OBJECTS=drawutils.o game.o graph.o main.o mainevent.o mathutils.o gamedata.o gamestate.o
all: $(PROJECT) all: $(PROJECT)

View File

@ -20,15 +20,6 @@ Game::~Game()
} }
void Game::execute(stack<GameState*> &state_stack) throw(StateExit)
{
SDL_Event event;
while(SDL_PollEvent(&event))
handle_event(&event);
render();
}
bool Game::init() bool Game::init()
{ {
background = DrawUtils::load("background.bmp"); background = DrawUtils::load("background.bmp");

5
game.h
View File

@ -21,9 +21,11 @@ class Game : public GameState
~Game(); ~Game();
bool init(); bool init();
void execute(stack<GameState*> &state_stack) throw(StateExit);
protected: protected:
void render();
void iterate() {}
// event handlers // event handlers
void on_exit(); void on_exit();
void on_lbutton_down(int x, int y); void on_lbutton_down(int x, int y);
@ -31,7 +33,6 @@ class Game : public GameState
void on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode); void on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode);
private: private:
void render();
// data // data
GameData data; GameData data;

View File

@ -0,0 +1,10 @@
#include "gamestate.h"
void GameState::execute(stack<GameState*> &state_stack) throw(StateExit)
{
SDL_Event event;
while(SDL_PollEvent(&event))
handle_event(&event);
iterate();
render();
}

View File

@ -20,12 +20,15 @@ class GameState : public MainEvent
{ {
public: public:
GameState(SDL_Surface* display) { this->display = display; } GameState(SDL_Surface* display) { this->display = display; }
~GameState() {} virtual ~GameState() {}
virtual bool init() = 0; virtual bool init() = 0;
virtual void execute(stack<GameState*> &game_state) throw(StateExit) = 0; void execute(stack<GameState*> &game_state) throw(StateExit);
protected: protected:
virtual void iterate() = 0;
virtual void render() = 0;
SDL_Surface* display; SDL_Surface* display;
}; };

30
titlescreen.cpp Normal file
View File

@ -0,0 +1,30 @@
#include "titlescreen.h"
TitleScreen::TitleScreen(SDL_Surface* display)
: GameState(display)
{
background = NULL;
title_banner = NULL;
}
GameState::~GameState()
{
if (background != NULL)
SDL_FreeSurface(background);
if (background != NULL)
SDL_FreeSurface(title_banner);
}
bool TitleScreen::init()
{
}
void TitleScreen::execute(stack<TitleScreen*> &game_state) throw(StateExit)
{
}

27
titlescreen.h Normal file
View File

@ -0,0 +1,27 @@
/*
This renders and handles the title screen / main menu
*/
#ifndef _TITLE_SCREEN_H_
#define _TITLE_SCREEN_H_
#include "gamestate.h"
class TitleScreen : public GameState
{
public:
GameState(SDL_Surface* display);
~GameState();
bool init();
void execute(stack<GameState*> &game_state) throw(StateExit);
protected:
SDL_Surface* display;
SDL_Surface* background;
SDL_Surface* title_banner;
};
#endif