Skip to content

Instantly share code, notes, and snippets.

@i8degrees
Last active December 18, 2015 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save i8degrees/5711070 to your computer and use it in GitHub Desktop.
Save i8degrees/5711070 to your computer and use it in GitHub Desktop.
Abstracted SDL_Drawables Example
namespace nom
{
class SDL_Drawable
{
public:
virtual void Draw ( SDL_Surface *video_buffer ) const = 0;
};
// Canvas
// Image
// Font
// ...
class Rectangle: public SDL_Drawable
{
public:
Rectangle ( void ) : coords ( 0, 0, 0, 0 ), color ( 0, 0, 0, -1 )
{}
Rectangle ( const Coords& coords, const Color& color )
{
this->coords = coords;
this->color = color;
}
void Draw ( SDL_Surface *video_buffer ) const
{
SDL_Rect rectangle = this->coords.getSDL_Rect();
unsigned int rectangle_color = 0;
rectangle_color = this->color.getColorAsInt ( video_buffer->format );
if ( SDL_FillRect ( video_buffer, &rectangle, rectangle_color ) != 0 )
{
std::cout << "ERR in Gfx::DrawRectangle(): " << SDL_GetError() << std::endl;
return;
}
return;
}
private:
Coords coords; // "component entity" coords wrapper class within nom
Color color; // "component entity" color wrapper class within nom
};
}
// TTcards.h
class TTcards
{
public:
// ...
void Draw ( void );
private:
// ...
void Load ( void );
typedef std::vector<nom::SDL_Drawable*> drawable_t;
drawable_t drawableRects;
drawable_t drawableBackgrounds;
void TTcards::~TTcards ( void )
{
// Do not forget to clean up yo shit when you are done with it..!
for ( auto it = drawableRects.begin(); it != drawableRects.end(); it++ )
{
delete *it; // goodbye Rectangles!
}
for ( drawable_t::const_iterator it = drawableBackgrounds.begin(); it != drawableBackgrounds.end(); it++ )
{
nom::SDL_Drawable *obj = *it;
delete obj; // goodbye drawableBackgrounds!
}
}
void TTcards::Load ( void )
{
// nothing to see here, move along! (only another thousand preliminary init tasks)
drawableRects.push_back ( new nom::Rectangle ( nom::Coords ( 320, 0, 16, 16 ), nom::Color ( 188, 203, 236 ) ) );
drawableRects.push_back ( new nom::Rectangle ( nom::Coords ( 40, 0, 16, 16 ), nom::Color ( 222, 196, 205 ) ) );
drawableBackgrounds.push_back ( new nom::Image ( "yo-mama-haxxed.png", nom::Color ( 0, 0, 0, -1 ) ) ); // filename, colorkey
}
void TTcards::Draw ( void )
{
// ...
// static image backgrounds example; here std::list<SDL_Drawable*> drawable_t works well (read: both efficiently and elegantly)
for ( drawable_t::const_iterator it = drawableBackgrounds.begin(); it != drawableBackgrounds.end(); ++it )
{
nom::SDL_Drawable *obj = *it;
obj->Draw ( video_buffer ); // could also be represented as obj->Draw ( SDL_GetVideoSurface() );
}
// player state example; std::list<SDL_Drawable*> drawable_t doesn't work quite so nice here
if ( this->turn == 0 ) // player1
drawableRects[0]->Draw ( video_buffer );
else // player2
drawableRects[1]->Draw ( video_buffer );
// onwards, progress, or so it seems!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment