/* $Id: graphics.cpp,v 1.3 2005/06/28 13:55:19 chfreund Exp $ */ #include "graphics.hpp" void Graphics::setPixel( SDL_Surface* surface, const int x, const int y, const Uint32 color ) { int bpp = surface->format->BytesPerPixel; Uint8 *p = static_cast( surface->pixels ) + y * surface->pitch + x * bpp; switch ( bpp ) { case 1: *p = color; break; case 2: *( reinterpret_cast( p ) ) = color; break; case 3: if ( SDL_BYTEORDER == SDL_BIG_ENDIAN ) { p[0] = ( color >> 16 ) & 0xff; p[1] = ( color >> 8 ) & 0xff; p[2] = color & 0xff; } else { p[0] = color & 0xff; p[1] = ( color >> 8 ) & 0xff; p[2] = ( color >> 16 ) & 0xff; } break; case 4: *( reinterpret_cast( p ) ) = color; break; } } Uint32 Graphics::getPixel( SDL_Surface *surface, const int x, const int y ) { int bpp = surface->format->BytesPerPixel; Uint8 *p = static_cast( surface->pixels ) + y * surface->pitch + x * bpp; switch ( bpp ) { case 1: return *p; case 2: return *( reinterpret_cast( p ) ); case 3: if ( SDL_BYTEORDER == SDL_BIG_ENDIAN ) return p[0] << 16 | p[1] << 8 | p[2]; else return p[0] | p[1] << 8 | p[2] << 16; case 4: return *( reinterpret_cast( p ) ); default: return 0; } }