From ca60c61ce61396253b76948eacd55f5023e0b7ca Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Thu, 30 Jun 2011 17:21:46 -0400 Subject: [PATCH] Implemented and integrated title screen - now game crashes at launch, though --- Makefile | 2 +- game.cpp | 10 ++-------- game.h | 3 +-- gamestate.cpp | 8 +++++++- gamestate.h | 8 ++++++-- main.cpp | 7 ++++--- titlescreen.cpp | 32 +++++++++++++++++++++++++++----- titlescreen.h | 17 +++++++++++------ 8 files changed, 59 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 2f2db51..ef5bac1 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 gamestate.o +OBJECTS=main.o drawutils.o mathutils.o graph.o gamedata.o mainevent.o gamestate.o game.o titlescreen.o all: $(PROJECT) diff --git a/game.cpp b/game.cpp index 11a3ebe..11924de 100644 --- a/game.cpp +++ b/game.cpp @@ -7,8 +7,8 @@ int Game::NODE_RADIUS = 10; -Game::Game(SDL_Surface* display) - : GameState(display) +Game::Game(stack* state_stack, SDL_Surface* display) + : GameState(state_stack, display) { background = NULL; } @@ -83,12 +83,6 @@ void Game::render() } -void Game::on_exit() -{ - throw StateExit(); -} - - void Game::on_lbutton_down(int x, int y) { data.do_vertex(x, y, NODE_RADIUS); diff --git a/game.h b/game.h index 12bcb5c..120f2d3 100644 --- a/game.h +++ b/game.h @@ -17,7 +17,7 @@ using std::stack; class Game : public GameState { public: - Game(SDL_Surface* display); + Game(stack* state_stack, SDL_Surface* display); ~Game(); bool init(); @@ -27,7 +27,6 @@ class Game : public GameState void iterate() {} // event handlers - void on_exit(); void on_lbutton_down(int x, int y); void on_rbutton_down(int mX, int mY); void on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode); diff --git a/gamestate.cpp b/gamestate.cpp index 92d85fb..b96e3ea 100644 --- a/gamestate.cpp +++ b/gamestate.cpp @@ -1,6 +1,6 @@ #include "gamestate.h" -void GameState::execute(stack &state_stack) throw(StateExit) +void GameState::execute() throw(StateExit) { SDL_Event event; while(SDL_PollEvent(&event)) @@ -8,3 +8,9 @@ void GameState::execute(stack &state_stack) throw(StateExit) iterate(); render(); } + + +void GameState::on_exit() +{ + throw StateExit(); +} diff --git a/gamestate.h b/gamestate.h index fe883c7..e974d5f 100644 --- a/gamestate.h +++ b/gamestate.h @@ -19,16 +19,20 @@ class StateExit : public exception {}; class GameState : public MainEvent { public: - GameState(SDL_Surface* display) { this->display = display; } + GameState(stack* state_stack, SDL_Surface* display) + { this->state_stack = state_stack, this->display = display; } virtual ~GameState() {} virtual bool init() = 0; - void execute(stack &game_state) throw(StateExit); + void execute() throw(StateExit); protected: virtual void iterate() = 0; virtual void render() = 0; + void on_exit(); + + stack* state_stack; SDL_Surface* display; }; diff --git a/main.cpp b/main.cpp index 28ae896..c6b65ba 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ /* Do basic initialization, get the loop going */ #include "gamestate.h" -#include "game.h" +#include "titlescreen.h" #include "debug.h" #include @@ -24,15 +24,16 @@ int main(int argc, char** argv) stack state_stack; // initialize the stack by initting and pushing the initial state(s) - GameState* tmpstate = new Game(display); + 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(state_stack); + state->execute(); } catch (StateExit& e) { // remove the old state diff --git a/titlescreen.cpp b/titlescreen.cpp index 7f4ee86..8cf8886 100644 --- a/titlescreen.cpp +++ b/titlescreen.cpp @@ -1,15 +1,18 @@ #include "titlescreen.h" #include "drawutils.h" +#include "game.h" +#include "debug.h" -TitleScreen::TitleScreen(SDL_Surface* display) - : GameState(display) +TitleScreen::TitleScreen(stack* state_stack, + SDL_Surface* display) + : GameState(state_stack, display) { background = NULL; title_banner = NULL; } -GameState::~GameState() +TitleScreen::~TitleScreen() { if (background != NULL) SDL_FreeSurface(background); @@ -29,7 +32,7 @@ bool TitleScreen::init() return false; } - title_banner = DrawUtils::load("title.bmp"); + title_banner = DrawUtils::load("title_banner.bmp"); if (title_banner == NULL) { @@ -39,7 +42,26 @@ bool TitleScreen::init() } -void TitleScreen::execute(stack &game_state) throw(StateExit) +void TitleScreen::render() { + DrawUtils::draw(display, background, 0, 0); + int x = display->w / 2 - (title_banner->w / 2); + int y = display->h / 2 - (title_banner->h / 2); + + DrawUtils::draw(display, title_banner, x, y); + + SDL_Flip(display); +} + + +void TitleScreen::on_lbutton_down(int x, int y) +{ + state_stack->push(new Game(state_stack, display)); +} + + +void TitleScreen::on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode) +{ + if (sym == SDLK_q && mod & KMOD_CTRL) throw StateExit(); } diff --git a/titlescreen.h b/titlescreen.h index 3da0f30..70f32fb 100644 --- a/titlescreen.h +++ b/titlescreen.h @@ -10,15 +10,20 @@ class TitleScreen : public GameState { public: - GameState(SDL_Surface* display); - ~GameState(); + TitleScreen(stack* state_stack, SDL_Surface* display); + ~TitleScreen(); bool init(); - void execute(stack &game_state) throw(StateExit); - - protected: - SDL_Surface* display; + protected: + void render(); + void iterate() {} + + // signal handlers + void on_lbutton_down(int x, int y); + void on_key_down(SDLKey sym, SDLMod mod, Uint16 unicode); + + private: SDL_Surface* background; SDL_Surface* title_banner; };