From 837b3466a5bc98572b41d47cc1bd3615dc8dfd6c Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 24 Jun 2011 09:40:13 -0400 Subject: [PATCH] Refactored all the rendering code into its own class --- Makefile | 2 +- drawutils.h | 8 +++++- gamecore.cpp | 77 ++++------------------------------------------------ gamecore.h | 8 ++---- 4 files changed, 15 insertions(+), 80 deletions(-) diff --git a/Makefile b/Makefile index 0ea3831..29fd734 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ PROJECT=graphgame CXX=g++ CXXFLAGS=-DDEBUG -g 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) diff --git a/drawutils.h b/drawutils.h index a87b1ed..10e9aed 100644 --- a/drawutils.h +++ b/drawutils.h @@ -12,16 +12,22 @@ class DrawUtils { public: DrawUtils() {} + 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, 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_circle_filled(SDL_Surface* dest, Sint16 x, Sint16 y, Uint16 r, Uint32 colour); // 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 diff --git a/gamecore.cpp b/gamecore.cpp index d5e8fba..dcfc663 100644 --- a/gamecore.cpp +++ b/gamecore.cpp @@ -1,15 +1,12 @@ #include "gamecore.h" -#include "drawutils.h" #include "mathutils.h" -#include "debug.h" +#include int GameCore::MAX_MOVE_DISTANCE = 100; int GameCore::NODE_RADIUS = 12; GameCore::GameCore() { - display = NULL; - background = NULL; is_running = true; } @@ -23,8 +20,8 @@ int GameCore::execute() { while(SDL_PollEvent(&event)) handle_event(&event); - iterate(); - render(); + // iterate(); + renderer.render(data, MAX_MOVE_DISTANCE); } cleanup(); @@ -37,77 +34,13 @@ bool GameCore::init() { if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false; - display = SDL_SetVideoMode(1024,768,32, SDL_HWSURFACE | SDL_DOUBLEBUF); - 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 vertices = data.get_vertices(); - list 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::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 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() -{ - + return renderer.init(); } void GameCore::cleanup() { - SDL_FreeSurface(background); - SDL_FreeSurface(display); + renderer.cleanup(); SDL_Quit(); } diff --git a/gamecore.h b/gamecore.h index 4d13d24..a69e500 100644 --- a/gamecore.h +++ b/gamecore.h @@ -9,9 +9,9 @@ #ifndef _GAME_CORE_H_ #define _GAME_CORE_H_ -#include #include "mainevent.h" #include "gamedata.h" +#include "sdlrenderer.h" class GameCore : public MainEvent { @@ -28,16 +28,12 @@ class GameCore : public MainEvent private: bool init(); - void iterate(); // updates the game state void render(); void cleanup(); bool is_running; - SDL_Surface* display; - - // textures to draw - SDL_Surface* background; + SDLRenderer renderer; // data GameData data;