Folded sdlrenderer code into Game(), since we'll be doing rendering on a per-state basis
This commit is contained in:
parent
5522cbc64c
commit
2bddf92001
2
Makefile
2
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)
|
||||
|
||||
|
|
73
game.cpp
73
game.cpp
|
@ -1,13 +1,22 @@
|
|||
#include "game.h"
|
||||
#include "mathutils.h"
|
||||
#include "drawutils.h"
|
||||
#include "debug.h"
|
||||
#include <SDL.h>
|
||||
|
||||
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<GameState*> &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<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
8
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
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#define _GAME_STATE_H_
|
||||
|
||||
#include "mainevent.h"
|
||||
#include <SDL.h>
|
||||
#include <exception>
|
||||
#include <stack>
|
||||
|
||||
|
@ -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<GameState*> &game_state) throw(StateExit) = 0;
|
||||
|
||||
protected:
|
||||
SDL_Surface* display;
|
||||
};
|
||||
|
||||
|
||||
|
|
16
main.cpp
16
main.cpp
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "gamestate.h"
|
||||
#include "game.h"
|
||||
#include "debug.h"
|
||||
#include <SDL.h>
|
||||
|
||||
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<GameState*> 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;
|
||||
|
|
|
@ -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<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()
|
||||
{
|
||||
SDL_FreeSurface(background);
|
||||
SDL_FreeSurface(display);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user