From ed46a272b869de851c393474d5dce34d1279cf0e Mon Sep 17 00:00:00 2001 From: Anna Wiggins Date: Fri, 24 Jun 2011 21:55:43 -0400 Subject: [PATCH] Left some important files out of the repo --- sdlrenderer.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++++++++ sdlrenderer.h | 36 ++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 sdlrenderer.cpp create mode 100644 sdlrenderer.h diff --git a/sdlrenderer.cpp b/sdlrenderer.cpp new file mode 100644 index 0000000..1b442c9 --- /dev/null +++ b/sdlrenderer.cpp @@ -0,0 +1,87 @@ +#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 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, range, range_colour); + } + + 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 SDLRenderer::cleanup() +{ + SDL_FreeSurface(background); + SDL_FreeSurface(display); +} diff --git a/sdlrenderer.h b/sdlrenderer.h new file mode 100644 index 0000000..f818318 --- /dev/null +++ b/sdlrenderer.h @@ -0,0 +1,36 @@ +/* This class handles all of the messy details of SDL rendering + * We typically want to call init() when we start up, and + * render() when we want to render a frame + * + * Note that this class does NOT call SDL_init() - that should be + * done before init()ing here + * + * cleanup() does the SDL destructor work - call it explicitly in case + * we want to keep the object around and re-init it... + */ + +#ifndef _SDLRENDERER_H_ +#define _SDLRENDERER_H_ + +#include "gamedata.h" +#include + +class SDLRenderer +{ + public: + SDLRenderer(); + + bool init(); + void render(GameData& data); + void cleanup(); + + private: + // Main surface - our window + SDL_Surface* display; + + // surfaces containing textures to draw + SDL_Surface* background; + +}; + +#endif