Refactored all the rendering code into its own class

This commit is contained in:
Anna Rose 2011-06-24 09:40:13 -04:00
parent 5ed05fc829
commit 837b3466a5
4 changed files with 15 additions and 80 deletions

View File

@ -4,7 +4,7 @@ PROJECT=graphgame
CXX=g++ CXX=g++
CXXFLAGS=-DDEBUG -g CXXFLAGS=-DDEBUG -g
LDFLAGS=-lSDL LDFLAGS=-lSDL
OBJECTS=drawutils.o gamecore.o graph.o main.o mainevent.o mathutils.o gamedata.o OBJECTS=drawutils.o gamecore.o graph.o main.o mainevent.o mathutils.o gamedata.o sdlrenderer.o
all: $(PROJECT) all: $(PROJECT)

View File

@ -12,16 +12,22 @@ class DrawUtils
{ {
public: public:
DrawUtils() {} DrawUtils() {}
static SDL_Surface* load(string file); static SDL_Surface* load(string file);
static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y); static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y);
static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y, static bool draw(SDL_Surface* dest, SDL_Surface* drawable, int x, int y,
int x2, int y2, int w, int h); int x2, int y2, int w, int h);
static void draw_line(SDL_Surface* dest, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour); static void draw_line(SDL_Surface* dest, Sint16 x1, Sint16 y1, Sint16 x2, Sint16 y2, Uint16 width, Uint32 colour);
static void draw_circle_filled(SDL_Surface* dest, Sint16 x, Sint16 y, Uint16 r, Uint32 colour); static void draw_circle_filled(SDL_Surface* dest, Sint16 x, Sint16 y, Uint16 r, Uint32 colour);
// transpare (v) - to make transparent // transpare (v) - to make transparent
static bool transpare(SDL_Surface* surface, int r, int g, int b); // this function makes a particular color key on a surface transparent
// the traditional color key is 255, 0, 255 (bright pink), so we default
// to that
static bool transpare(SDL_Surface* surface, int r = 255, int g = 0,
int b = 255);
}; };
#endif #endif

View File

@ -1,15 +1,12 @@
#include "gamecore.h" #include "gamecore.h"
#include "drawutils.h"
#include "mathutils.h" #include "mathutils.h"
#include "debug.h" #include <SDL/SDL.h>
int GameCore::MAX_MOVE_DISTANCE = 100; int GameCore::MAX_MOVE_DISTANCE = 100;
int GameCore::NODE_RADIUS = 12; int GameCore::NODE_RADIUS = 12;
GameCore::GameCore() GameCore::GameCore()
{ {
display = NULL;
background = NULL;
is_running = true; is_running = true;
} }
@ -23,8 +20,8 @@ int GameCore::execute()
{ {
while(SDL_PollEvent(&event)) while(SDL_PollEvent(&event))
handle_event(&event); handle_event(&event);
iterate(); // iterate();
render(); renderer.render(data, MAX_MOVE_DISTANCE);
} }
cleanup(); cleanup();
@ -37,77 +34,13 @@ bool GameCore::init()
{ {
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false; if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false;
display = SDL_SetVideoMode(1024,768,32, SDL_HWSURFACE | SDL_DOUBLEBUF); return renderer.init();
if (display == NULL)
{
#ifdef DEBUG
std::cerr << "GameCore::init(): error: Couldn't create main surface\n";
#endif
return false;
}
background = DrawUtils::load("background.bmp");
if (background == NULL)
{
#ifdef DEBUG
std::cerr << "GameCore::init(): error: Couldn't load background image\n";
#endif
return false;
}
// DrawUtils::transpare(node, 255, 0, 255);
// DrawUtils::transpare(move_template, 255, 0, 255);
// SDL_SetAlpha(move_template, SDL_SRCALPHA, 128);
return true;
}
void GameCore::render()
{
DrawUtils::draw(display, background, 0, 0);
list<Vertex*> vertices = data.get_vertices();
list<Edge> edges = data.get_edges();
if (data.get_current_vertex() != NULL)
{
Vertex* v = data.get_current_vertex();
DrawUtils::draw_circle_filled(display, v->x, v->y,
MAX_MOVE_DISTANCE, 0xcb1919);
}
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<Edge>::iterator cursor = edges.begin();
cursor != edges.end(); cursor++)
{
Edge e = *cursor;
DrawUtils::draw_line(display, e.a->x, e.a->y, e.b->x, e.b->y, 2,
e.a->colour);
}
SDL_Flip(display);
}
void GameCore::iterate()
{
} }
void GameCore::cleanup() void GameCore::cleanup()
{ {
SDL_FreeSurface(background); renderer.cleanup();
SDL_FreeSurface(display);
SDL_Quit(); SDL_Quit();
} }

View File

@ -9,9 +9,9 @@
#ifndef _GAME_CORE_H_ #ifndef _GAME_CORE_H_
#define _GAME_CORE_H_ #define _GAME_CORE_H_
#include <SDL/SDL.h>
#include "mainevent.h" #include "mainevent.h"
#include "gamedata.h" #include "gamedata.h"
#include "sdlrenderer.h"
class GameCore : public MainEvent class GameCore : public MainEvent
{ {
@ -28,16 +28,12 @@ class GameCore : public MainEvent
private: private:
bool init(); bool init();
void iterate(); // updates the game state
void render(); void render();
void cleanup(); void cleanup();
bool is_running; bool is_running;
SDL_Surface* display; SDLRenderer renderer;
// textures to draw
SDL_Surface* background;
// data // data
GameData data; GameData data;