Implemented and integrated title screen - now game crashes at launch, though
This commit is contained in:
parent
d2322a7caf
commit
ca60c61ce6
2
Makefile
2
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)
|
||||
|
||||
|
|
10
game.cpp
10
game.cpp
|
@ -7,8 +7,8 @@
|
|||
int Game::NODE_RADIUS = 10;
|
||||
|
||||
|
||||
Game::Game(SDL_Surface* display)
|
||||
: GameState(display)
|
||||
Game::Game(stack<GameState*>* 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);
|
||||
|
|
3
game.h
3
game.h
|
@ -17,7 +17,7 @@ using std::stack;
|
|||
class Game : public GameState
|
||||
{
|
||||
public:
|
||||
Game(SDL_Surface* display);
|
||||
Game(stack<GameState*>* 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);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
#include "gamestate.h"
|
||||
|
||||
void GameState::execute(stack<GameState*> &state_stack) throw(StateExit)
|
||||
void GameState::execute() throw(StateExit)
|
||||
{
|
||||
SDL_Event event;
|
||||
while(SDL_PollEvent(&event))
|
||||
|
@ -8,3 +8,9 @@ void GameState::execute(stack<GameState*> &state_stack) throw(StateExit)
|
|||
iterate();
|
||||
render();
|
||||
}
|
||||
|
||||
|
||||
void GameState::on_exit()
|
||||
{
|
||||
throw StateExit();
|
||||
}
|
||||
|
|
|
@ -19,16 +19,20 @@ class StateExit : public exception {};
|
|||
class GameState : public MainEvent
|
||||
{
|
||||
public:
|
||||
GameState(SDL_Surface* display) { this->display = display; }
|
||||
GameState(stack<GameState*>* state_stack, SDL_Surface* display)
|
||||
{ this->state_stack = state_stack, this->display = display; }
|
||||
virtual ~GameState() {}
|
||||
|
||||
virtual bool init() = 0;
|
||||
void execute(stack<GameState*> &game_state) throw(StateExit);
|
||||
void execute() throw(StateExit);
|
||||
|
||||
protected:
|
||||
virtual void iterate() = 0;
|
||||
virtual void render() = 0;
|
||||
|
||||
void on_exit();
|
||||
|
||||
stack<GameState*>* state_stack;
|
||||
SDL_Surface* display;
|
||||
};
|
||||
|
||||
|
|
7
main.cpp
7
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 <SDL.h>
|
||||
|
||||
|
@ -24,15 +24,16 @@ int main(int argc, char** argv)
|
|||
stack<GameState*> 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
|
||||
|
|
|
@ -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<GameState*>* 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<TitleScreen*> &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();
|
||||
}
|
||||
|
|
|
@ -10,15 +10,20 @@
|
|||
class TitleScreen : public GameState
|
||||
{
|
||||
public:
|
||||
GameState(SDL_Surface* display);
|
||||
~GameState();
|
||||
TitleScreen(stack<GameState*>* state_stack, SDL_Surface* display);
|
||||
~TitleScreen();
|
||||
|
||||
bool init();
|
||||
void execute(stack<GameState*> &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;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user