Refactored all the rendering code into its own class
This commit is contained in:
parent
5ed05fc829
commit
837b3466a5
2
Makefile
2
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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
77
gamecore.cpp
77
gamecore.cpp
|
@ -1,15 +1,12 @@
|
|||
#include "gamecore.h"
|
||||
#include "drawutils.h"
|
||||
#include "mathutils.h"
|
||||
#include "debug.h"
|
||||
#include <SDL/SDL.h>
|
||||
|
||||
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<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()
|
||||
{
|
||||
|
||||
return renderer.init();
|
||||
}
|
||||
|
||||
|
||||
void GameCore::cleanup()
|
||||
{
|
||||
SDL_FreeSurface(background);
|
||||
SDL_FreeSurface(display);
|
||||
renderer.cleanup();
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
|
|
|
@ -9,9 +9,9 @@
|
|||
#ifndef _GAME_CORE_H_
|
||||
#define _GAME_CORE_H_
|
||||
|
||||
#include <SDL/SDL.h>
|
||||
#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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user