From 593ac7b240a517d8412fc806cce9680f924ab5f0 Mon Sep 17 00:00:00 2001 From: Nicolay Korslund Date: Sun, 19 Sep 2010 11:57:09 +0200 Subject: [PATCH] Added pixel() to Rend2D::Sprite --- rend2d/servers/sdl_driver.cpp | 32 ++++++++++++++++++++++++++++++++ rend2d/servers/sdl_driver.hpp | 3 +++ rend2d/sprite.hpp | 5 +++++ 3 files changed, 40 insertions(+) diff --git a/rend2d/servers/sdl_driver.cpp b/rend2d/servers/sdl_driver.cpp index 89c1c79c2..d86cf53e4 100644 --- a/rend2d/servers/sdl_driver.cpp +++ b/rend2d/servers/sdl_driver.cpp @@ -7,6 +7,38 @@ using namespace Mangle::Rend2D; +// This is a really crappy and slow implementation +void SDL_Sprite::pixel(int x, int y, int color) +{ + SDL_LockSurface(surface); + + int bpp = surface->format->BytesPerPixel; + char *p = (char*)surface->pixels + y*surface->pitch + x*bpp; + + switch(bpp) + { + case 1: *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: + *(int*)p = color; + break; + } + SDL_UnlockSurface(surface); +} + void SDL_Sprite::draw(Sprite *s, // Must be SDL_Sprite int x, int y, // Destination position int sx, int sy, // Source position diff --git a/rend2d/servers/sdl_driver.hpp b/rend2d/servers/sdl_driver.hpp index 9aeed2f92..4cab45c2e 100644 --- a/rend2d/servers/sdl_driver.hpp +++ b/rend2d/servers/sdl_driver.hpp @@ -34,6 +34,9 @@ namespace Mangle // Fill with a given pixel value void fill(int value); + // Set one pixel + void pixel(int x, int y, int value); + private: // The SDL surface SDL_Surface* surface; diff --git a/rend2d/sprite.hpp b/rend2d/sprite.hpp index 150d97e4e..7f6b9a220 100644 --- a/rend2d/sprite.hpp +++ b/rend2d/sprite.hpp @@ -28,6 +28,11 @@ namespace Mangle /// Fill the sprite with the given pixel value. The pixel format /// depends on the format of the sprite. virtual void fill(int value) = 0; + + /// Set one pixel value. The pixel format depends on the sprite + /// format. This is not expected to be fast, and in some + /// implementations may not work at all. + virtual void pixel(int x, int y, int value) {} }; } }