treewars/sdlrenderer.cpp

90 lines
1.8 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;
}
// 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);
}