From a05046026ec9edb1e528fac2c70f887239302237 Mon Sep 17 00:00:00 2001 From: Nicolay Korslund Date: Sun, 19 Sep 2010 12:36:19 +0200 Subject: [PATCH] More direct pixel functions. --- rend2d/servers/sdl_driver.cpp | 30 +++++++++++++++++++++++++++++- rend2d/servers/sdl_driver.hpp | 6 ++++++ rend2d/sprite.hpp | 18 ++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/rend2d/servers/sdl_driver.cpp b/rend2d/servers/sdl_driver.cpp index d86cf53e4d..aa1ff6c6dc 100644 --- a/rend2d/servers/sdl_driver.cpp +++ b/rend2d/servers/sdl_driver.cpp @@ -7,7 +7,34 @@ using namespace Mangle::Rend2D; -// This is a really crappy and slow implementation +const SpriteData *SDL_Sprite::lock() +{ + // Make sure we aren't already locked + assert(!data.pixels); + + // Lock the surface and set up the data structure + SDL_LockSurface(surface); + + data.pixels = surface->pixels; + data.w = surface->w; + data.h = surface->h; + data.pitch = surface->pitch; + data.bypp = surface->format->BytesPerPixel; + + return &data; +} + +void SDL_Sprite::unlock() +{ + if(data.pixels) + { + SDL_UnlockSurface(surface); + data.pixels = NULL; + } +} + +// This is a really crappy and slow implementation, only intended for +// testing purposes. Use lock/unlock for faster pixel drawing. void SDL_Sprite::pixel(int x, int y, int color) { SDL_LockSurface(surface); @@ -83,6 +110,7 @@ SDL_Sprite::SDL_Sprite(SDL_Surface *s, bool autoDelete) : surface(s), autoDel(autoDelete) { assert(surface != NULL); + data.pixels = NULL; } SDL_Sprite::~SDL_Sprite() diff --git a/rend2d/servers/sdl_driver.hpp b/rend2d/servers/sdl_driver.hpp index 4cab45c2e8..0f205ba34c 100644 --- a/rend2d/servers/sdl_driver.hpp +++ b/rend2d/servers/sdl_driver.hpp @@ -37,10 +37,16 @@ namespace Mangle // Set one pixel void pixel(int x, int y, int value); + const SpriteData *lock(); + void unlock(); + private: // The SDL surface SDL_Surface* surface; + // Used for locking + SpriteData data; + // If true, delete this surface when the canvas is destructed bool autoDel; }; diff --git a/rend2d/sprite.hpp b/rend2d/sprite.hpp index 7f6b9a2206..f49da6cb6d 100644 --- a/rend2d/sprite.hpp +++ b/rend2d/sprite.hpp @@ -5,6 +5,17 @@ namespace Mangle { namespace Rend2D { + /** + A pointer to sprite data for direct drawing. Only to be used + while the corresponding sprite is locked. + */ + struct SpriteData + { + void *pixels; // Pixel data + int w, h; // Width and height + int pitch, bypp; // Pitch (bytes) and bytes per pixel + }; + /** A Sprite is either a bitmap to be drawn or an output of area for blitting other bitmaps, or both. They are created by the @@ -33,6 +44,13 @@ namespace Mangle /// 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) {} + + /// Lock sprite for direct drawing, and return a struct + /// containing the necessary pointer. When finished, unlock the + /// sprite with unlock(). May return NULL, if so then direct + /// drawing is not possible. + virtual const SpriteData *lock() { return NULL; } + virtual void unlock() {} }; } }