Folded sdlrenderer code into Game(), since we'll be doing rendering on a per-state basis

This commit is contained in:
Anna Rose 2011-06-27 22:35:52 -04:00
parent 5522cbc64c
commit 2bddf92001
7 changed files with 97 additions and 84 deletions

View File

@ -4,7 +4,7 @@ PROJECT=graphgame
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 sdlrenderer.o OBJECTS=drawutils.o game.o graph.o main.o mainevent.o mathutils.o gamedata.o
all: $(PROJECT) all: $(PROJECT)

View File

@ -1,13 +1,22 @@
#include "game.h" #include "game.h"
#include "mathutils.h" #include "mathutils.h"
#include "drawutils.h"
#include "debug.h" #include "debug.h"
#include <SDL.h> #include <SDL.h>
int Game::NODE_RADIUS = 12; int Game::NODE_RADIUS = 12;
Game::Game(SDL_Surface* display)
: GameState(display)
{
background = NULL;
}
Game::~Game() Game::~Game()
{ {
renderer.cleanup(); SDL_FreeSurface(background);
} }
@ -16,13 +25,71 @@ void Game::execute(stack<GameState*> &state_stack) throw(StateExit)
SDL_Event event; SDL_Event event;
while(SDL_PollEvent(&event)) while(SDL_PollEvent(&event))
handle_event(&event); handle_event(&event);
renderer.render(data); render();
} }
bool Game::init() 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<Vertex*> 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<Vertex*>::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<Vertex*>::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);
} }

8
game.h
View File

@ -18,7 +18,7 @@ using std::stack;
class Game : public GameState class Game : public GameState
{ {
public: public:
Game() {} Game(SDL_Surface* display);
~Game(); ~Game();
bool init(); bool init();
@ -34,13 +34,15 @@ class Game : public GameState
private: private:
void render(); void render();
SDLRenderer renderer;
// data // data
GameData data; GameData data;
static int NODE_RADIUS; static int NODE_RADIUS;
static int MAX_MOVE_DISTANCE; static int MAX_MOVE_DISTANCE;
// surfaces containing textures to draw
SDL_Surface* background;
}; };
#endif #endif

View File

@ -7,6 +7,7 @@
#define _GAME_STATE_H_ #define _GAME_STATE_H_
#include "mainevent.h" #include "mainevent.h"
#include <SDL.h>
#include <exception> #include <exception>
#include <stack> #include <stack>
@ -18,11 +19,14 @@ class StateExit : public exception {};
class GameState : public MainEvent class GameState : public MainEvent
{ {
public: public:
GameState() {} GameState(SDL_Surface* display) { this->display = display; }
~GameState() {} ~GameState() {}
virtual bool init() = 0; virtual bool init() = 0;
virtual void execute(stack<GameState*> &game_state) throw(StateExit) = 0; virtual void execute(stack<GameState*> &game_state) throw(StateExit) = 0;
protected:
SDL_Surface* display;
}; };

View File

@ -2,6 +2,7 @@
#include "gamestate.h" #include "gamestate.h"
#include "game.h" #include "game.h"
#include "debug.h"
#include <SDL.h> #include <SDL.h>
int main(int argc, char** argv) 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; if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false;
SDL_WM_SetCaption("TreeWars","TreeWars"); 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<GameState*> state_stack; stack<GameState*> state_stack;
// initialize the stack by initting and pushing the initial state(s) // initialize the stack by initting and pushing the initial state(s)
GameState* tmpstate = new Game(); GameState* tmpstate = new Game(display);
if (!tmpstate->init()) exit(1); if (!tmpstate->init()) return 1;
state_stack.push(tmpstate); state_stack.push(tmpstate);
while (!state_stack.empty()) while (!state_stack.empty())
@ -37,6 +48,7 @@ int main(int argc, char** argv)
} }
} }
SDL_FreeSurface(display);
SDL_Quit(); SDL_Quit();
return 0; return 0;

View File

@ -2,88 +2,22 @@
#include "drawutils.h" #include "drawutils.h"
#include "debug.h" #include "debug.h"
SDLRenderer::SDLRenderer() SDLRenderer::SDLRenderer(SDL_Surface* display)
{ {
display = NULL; this->display = display;
background = NULL;
} }
bool SDLRenderer::init() 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) 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<Vertex*> 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<Vertex*>::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<Vertex*>::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() void SDLRenderer::cleanup()
{ {
SDL_FreeSurface(background);
SDL_FreeSurface(display);
} }

View File

@ -25,12 +25,6 @@ class SDLRenderer
void cleanup(); void cleanup();
private: private:
// Main surface - our window
SDL_Surface* display;
// surfaces containing textures to draw
SDL_Surface* background;
}; };
#endif #endif