From 6a4b1c4cf2d7a39fbf3f68923cc12a80d714ebf1 Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Tue, 28 Jun 2011 21:38:18 -0400 Subject: [PATCH] Refactored a wee bit of code in the GameStack... stack, and started building the title screen code --- Makefile | 2 +- game.cpp | 9 --------- game.h | 5 +++-- gamestate.cpp | 10 ++++++++++ gamestate.h | 7 +++++-- titlescreen.cpp | 30 ++++++++++++++++++++++++++++++ titlescreen.h | 27 +++++++++++++++++++++++++++ 7 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 titlescreen.cpp create mode 100644 titlescreen.h diff --git a/Makefile b/Makefile index 1f1e6a2..2f2db51 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ PROJECT=treewars CXX=g++ CXXFLAGS=-DDEBUG -g `sdl-config --cflags` 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) diff --git a/game.cpp b/game.cpp index 89093fc..f81c806 100644 --- a/game.cpp +++ b/game.cpp @@ -20,15 +20,6 @@ Game::~Game() } -void Game::execute(stack &state_stack) throw(StateExit) -{ - SDL_Event event; - while(SDL_PollEvent(&event)) - handle_event(&event); - render(); -} - - bool Game::init() { background = DrawUtils::load("background.bmp"); diff --git a/game.h b/game.h index 9e3dc05..ff5ba93 100644 --- a/game.h +++ b/game.h @@ -21,9 +21,11 @@ class Game : public GameState ~Game(); bool init(); - void execute(stack &state_stack) throw(StateExit); protected: + void render(); + void iterate() {} + // event handlers void on_exit(); 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); private: - void render(); // data GameData data; diff --git a/gamestate.cpp b/gamestate.cpp index e69de29..92d85fb 100644 --- a/gamestate.cpp +++ b/gamestate.cpp @@ -0,0 +1,10 @@ +#include "gamestate.h" + +void GameState::execute(stack &state_stack) throw(StateExit) +{ + SDL_Event event; + while(SDL_PollEvent(&event)) + handle_event(&event); + iterate(); + render(); +} diff --git a/gamestate.h b/gamestate.h index 9ec4f42..fe883c7 100644 --- a/gamestate.h +++ b/gamestate.h @@ -20,12 +20,15 @@ class GameState : public MainEvent { public: GameState(SDL_Surface* display) { this->display = display; } - ~GameState() {} + virtual ~GameState() {} virtual bool init() = 0; - virtual void execute(stack &game_state) throw(StateExit) = 0; + void execute(stack &game_state) throw(StateExit); protected: + virtual void iterate() = 0; + virtual void render() = 0; + SDL_Surface* display; }; diff --git a/titlescreen.cpp b/titlescreen.cpp new file mode 100644 index 0000000..12d5b17 --- /dev/null +++ b/titlescreen.cpp @@ -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 &game_state) throw(StateExit) +{ + +} diff --git a/titlescreen.h b/titlescreen.h new file mode 100644 index 0000000..3da0f30 --- /dev/null +++ b/titlescreen.h @@ -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 &game_state) throw(StateExit); + + protected: + SDL_Surface* display; + + SDL_Surface* background; + SDL_Surface* title_banner; +}; + + +#endif