diff --git a/Makefile b/Makefile index 6dcfd49..de78694 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ PROJECT=graphgame 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 sdlrenderer.o +OBJECTS=drawutils.o game.o graph.o main.o mainevent.o mathutils.o gamedata.o all: $(PROJECT) diff --git a/game.cpp b/game.cpp index 55e9353..89093fc 100644 --- a/game.cpp +++ b/game.cpp @@ -1,13 +1,22 @@ #include "game.h" #include "mathutils.h" +#include "drawutils.h" #include "debug.h" #include int Game::NODE_RADIUS = 12; + +Game::Game(SDL_Surface* display) + : GameState(display) +{ + background = NULL; +} + + Game::~Game() { - renderer.cleanup(); + SDL_FreeSurface(background); } @@ -16,13 +25,71 @@ void Game::execute(stack &state_stack) throw(StateExit) SDL_Event event; while(SDL_PollEvent(&event)) handle_event(&event); - renderer.render(data); + render(); } bool Game::init() { - return renderer.init(); + background = DrawUtils::load("background.bmp"); + + if (background == NULL) + { +#ifdef DEBUG + std::cerr << "debug: Game::init(): error: Couldn't load background image\n"; +#endif + return false; + } + + return true; +} + + +void Game::render() +{ + int range = data.get_range(); + int range_colour = 0x888888; + + switch(data.get_mode()) + { + case MODE_MOVE: + range_colour = 0x0000ff; + break; + case MODE_ATTACK: + range_colour = 0xff0000; + break; + } + + // Background image first + DrawUtils::draw(display, background, 0, 0); + + list vertices = data.get_vertices(); + + // Now paint on the targeting circle + if (data.get_current_vertex() != NULL) + { + Vertex* v = data.get_current_vertex(); + DrawUtils::draw_circle_filled(display, v->x, v->y, range, + range_colour); + } + + // Now paint each vertex, and any edges that it needs + for (list::iterator cursor = vertices.begin(); + cursor != vertices.end(); cursor++) + { + Vertex* v = *cursor; + DrawUtils::draw_circle_filled(display, v->x, v->y, v->r, + v->colour); + for (list::iterator subcursor = v->neighbors.begin(); + subcursor != v->neighbors.end(); subcursor++) + { + Vertex* v1 = *subcursor; + DrawUtils::draw_line(display, v->x, v->y, v1->x, v1->y, 2, + v->colour); + } + } + + SDL_Flip(display); } diff --git a/game.h b/game.h index d71d055..527bd06 100644 --- a/game.h +++ b/game.h @@ -18,7 +18,7 @@ using std::stack; class Game : public GameState { public: - Game() {} + Game(SDL_Surface* display); ~Game(); bool init(); @@ -34,13 +34,15 @@ class Game : public GameState private: void render(); - SDLRenderer renderer; - // data GameData data; static int NODE_RADIUS; static int MAX_MOVE_DISTANCE; + + + // surfaces containing textures to draw + SDL_Surface* background; }; #endif diff --git a/gamestate.h b/gamestate.h index 2082ab4..9ec4f42 100644 --- a/gamestate.h +++ b/gamestate.h @@ -7,6 +7,7 @@ #define _GAME_STATE_H_ #include "mainevent.h" +#include #include #include @@ -18,11 +19,14 @@ class StateExit : public exception {}; class GameState : public MainEvent { public: - GameState() {} + GameState(SDL_Surface* display) { this->display = display; } ~GameState() {} virtual bool init() = 0; virtual void execute(stack &game_state) throw(StateExit) = 0; + + protected: + SDL_Surface* display; }; diff --git a/main.cpp b/main.cpp index edecd29..28ae896 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include "gamestate.h" #include "game.h" +#include "debug.h" #include int main(int argc, char** argv) @@ -10,11 +11,21 @@ int main(int argc, char** argv) if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false; SDL_WM_SetCaption("TreeWars","TreeWars"); + SDL_Surface* display = SDL_SetVideoMode(800, 600, 32, + SDL_HWSURFACE | SDL_DOUBLEBUF); + if (display == NULL) + { +#ifdef DEBUG + std::cerr << "debug: main: error: Couldn't create main surface\n"; +#endif + return 1; + } + stack state_stack; // initialize the stack by initting and pushing the initial state(s) - GameState* tmpstate = new Game(); - if (!tmpstate->init()) exit(1); + GameState* tmpstate = new Game(display); + if (!tmpstate->init()) return 1; state_stack.push(tmpstate); while (!state_stack.empty()) @@ -37,6 +48,7 @@ int main(int argc, char** argv) } } + SDL_FreeSurface(display); SDL_Quit(); return 0; diff --git a/sdlrenderer.cpp b/sdlrenderer.cpp index 2a0384c..be03cf4 100644 --- a/sdlrenderer.cpp +++ b/sdlrenderer.cpp @@ -2,88 +2,22 @@ #include "drawutils.h" #include "debug.h" -SDLRenderer::SDLRenderer() +SDLRenderer::SDLRenderer(SDL_Surface* display) { - display = NULL; - background = NULL; + this->display = display; } bool SDLRenderer::init() { - display = SDL_SetVideoMode(1024,768,32, SDL_HWSURFACE | SDL_DOUBLEBUF); - if (display == NULL) - { -#ifdef DEBUG - std::cerr << "SDLRenderer::init(): error: Couldn't create main surface\n"; -#endif - return false; - } - - background = DrawUtils::load("background.bmp"); - - if (background == NULL) - { -#ifdef DEBUG - std::cerr << "SDLRenderer::init(): error: Couldn't load background image\n"; -#endif - return false; - } - - return true; } void SDLRenderer::render(GameData& data) { - int range = data.get_range(); - int range_colour = 0x888888; - - switch(data.get_mode()) - { - case MODE_MOVE: - range_colour = 0x0000ff; - break; - case MODE_ATTACK: - range_colour = 0xff0000; - break; - } - - // Background image first - DrawUtils::draw(display, background, 0, 0); - - list vertices = data.get_vertices(); - - // Now paint on the targeting circle - if (data.get_current_vertex() != NULL) - { - Vertex* v = data.get_current_vertex(); - DrawUtils::draw_circle_filled(display, v->x, v->y, range, - range_colour); - } - - // Now paint each vertex, and any edges that it needs - for (list::iterator cursor = vertices.begin(); - cursor != vertices.end(); cursor++) - { - Vertex* v = *cursor; - DrawUtils::draw_circle_filled(display, v->x, v->y, v->r, - v->colour); - for (list::iterator subcursor = v->neighbors.begin(); - subcursor != v->neighbors.end(); subcursor++) - { - Vertex* v1 = *subcursor; - DrawUtils::draw_line(display, v->x, v->y, v1->x, v1->y, 2, - v->colour); - } - } - - SDL_Flip(display); } void SDLRenderer::cleanup() { - SDL_FreeSurface(background); - SDL_FreeSurface(display); } diff --git a/sdlrenderer.h b/sdlrenderer.h index f818318..3e05a04 100644 --- a/sdlrenderer.h +++ b/sdlrenderer.h @@ -25,12 +25,6 @@ class SDLRenderer void cleanup(); private: - // Main surface - our window - SDL_Surface* display; - - // surfaces containing textures to draw - SDL_Surface* background; - }; #endif