treewars/sdlrenderer.cpp

88 lines
1.7 KiB
C++
Raw Normal View History

#include "sdlrenderer.h"
#include "drawutils.h"
#include "debug.h"
SDLRenderer::SDLRenderer()
{
display = NULL;
background = NULL;
}
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;
}
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, range, range_colour);
}
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 SDLRenderer::cleanup()
{
SDL_FreeSurface(background);
SDL_FreeSurface(display);
}