/* $Id: graphics.hpp,v 1.4 2005/10/20 19:46:52 chfreund Exp $ */ #ifndef _GRAPHICS_HPP_ #define _GRAPHICS_HPP_ #include #include class Graphics { public: static void setPixel( SDL_Surface* surface, const int x, const int y, const Uint32 color ); static Uint32 getPixel( SDL_Surface *surface, const int x, const int y ); static void setPixelClip( SDL_Surface* surface, const int x, const int y, const Uint32 color ) { if( x >= 0 && y >= 0 && x < surface->w && y < surface->h ) setPixel( surface, x, y, color ); } static void drawLine( SDL_Surface* surface, int x1, int y1, int x2, int y2, const Uint32 color ) { int i; int sx, sy; // step positive or negative (1 or -1) int dx, dy; // delta (difference in X and Y between points) int e; dx = abs( x2 - x1 ); sx = (x2 - x1 > 0) ? 1 : -1; dy = abs( y2 - y1 ); sy = (y2 - y1 > 0) ? 1 : -1; if( dy <= dx ) { e = (dy << 1) - dx; for( i = 0; i <= dx; i++ ) { setPixelClip( surface, x1, y1, color ); if( e >= 0 ) { y1 += sy; e -= (dx << 1); } x1 += sx; e += (dy << 1); } } else { #define SWAP(a,b) {int tmpswap = a; a = b; b = tmpswap;} SWAP(x1, y1); SWAP(dx, dy); SWAP(sx, sy); #undef SWAP e = (dy << 1) - dx; for( i = 0; i <= dx; i++ ) { setPixelClip( surface, y1, x1, color ); if( e >= 0 ) { y1 += sy; e -= (dx << 1); } x1 += sx; e += (dy << 1); } } } static void drawCircle( SDL_Surface* surface, int xc, int yc, int r, const Uint32 color ) { Sint32 x = 0; Sint32 y = r; double d = 1.25 - static_cast( r ); setPixelClip( surface, xc, yc + r, color ); setPixelClip( surface, xc, yc - r, color ); setPixelClip( surface, xc + r, yc, color ); setPixelClip( surface, xc - r, yc, color ); while ( y > x ) { x++; if ( d > 0 ) { y--; d += 2.0 * (x - y); } else { d += 2.0 * x + 1.0; } setPixelClip( surface, xc - x, yc + y, color ); setPixelClip( surface, xc + x, yc + y, color ); setPixelClip( surface, xc - x, yc - y, color ); setPixelClip( surface, xc + x, yc - y, color ); setPixelClip( surface, xc - y, yc + x, color ); setPixelClip( surface, xc + y, yc + x, color ); setPixelClip( surface, xc - y, yc - x, color ); setPixelClip( surface, xc + y, yc - x, color ); } } }; #endif // _GRAPHICS_HPP_