Skip to content
Browse files

Sprite/Video: create some SDL_surface manipulation methods

SDLSprite only has its surface to worry about and SDLVideo has many so lets have the surface manipulation be part of SDLVideo instead.
make SetSurfacePalette, SetSurfacePixel, and GetSurfacePixel static methods of SDLVideo class.
  • Loading branch information...
1 parent 4abe685 commit 3baa1ee3e07a34c30193d99e257b636da5f11187 @bradallred bradallred committed Jun 21, 2013
View
44 gemrb/plugins/SDLVideo/SDLSurfaceSprite2D.cpp
@@ -19,6 +19,7 @@
*/
#include "SDLSurfaceSprite2D.h"
+#include "SDLVideo.h"
#include "System/Logging.h"
@@ -76,7 +77,7 @@ void SDLSurfaceSprite2D::SetPalette(Palette* pal)
void SDLSurfaceSprite2D::SetPalette(Color* pal)
{
- SDLSurfaceSprite2D::SetSurfacePalette(surface, (SDL_Color*)pal, 0x01 << Bpp);
+ SDLVideoDriver::SetSurfacePalette(surface, (SDL_Color*)pal, 0x01 << Bpp);
}
ieDword SDLSurfaceSprite2D::GetColorKey() const
@@ -103,39 +104,9 @@ void SDLSurfaceSprite2D::SetColorKey(ieDword ck)
Color SDLSurfaceSprite2D::GetPixel(unsigned short x, unsigned short y) const
{
Color c = { 0, 0, 0, 0 };
-
if (x >= Width || y >= Height) return c;
- SDL_LockSurface( surface );
- Uint8 Bpp = surface->format->BytesPerPixel;
- unsigned char * pixels = ( ( unsigned char * ) surface->pixels ) +
- ( ( y * surface->w + x) * Bpp );
- long val = 0;
-
- if (Bpp == 1) {
- val = *pixels;
- } else if (Bpp == 2) {
- val = *(Uint16 *)pixels;
- } else if (Bpp == 3) {
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
- val = pixels[0] + ((unsigned int)pixels[1] << 8) + ((unsigned int)pixels[2] << 16);
-#else
- val = pixels[2] + ((unsigned int)pixels[1] << 8) + ((unsigned int)pixels[0] << 16);
-#endif
- } else if (Bpp == 4) {
- val = *(Uint32 *)pixels;
- }
-
- SDL_UnlockSurface( surface );
-
- SDL_GetRGBA( val, surface->format, (Uint8 *) &c.r, (Uint8 *) &c.g, (Uint8 *) &c.b, (Uint8 *) &c.a );
- if (c.a == 0 && (c.r || c.g || c.b)) {
- c.a = 0xff;
- }
- ieDword ck = GetColorKey();
- if (ck) {
- ck;
- }
+ SDLVideoDriver::GetSurfacePixel(surface, x, y, c);
return c;
}
@@ -195,13 +166,4 @@ void SDLSurfaceSprite2D::SetSurfaceRLE(bool rle)
assert(RLE == false);
}
-void SDLSurfaceSprite2D::SetSurfacePalette(SDL_Surface* surf, SDL_Color* pal, int numcolors)
-{
-#if SDL_VERSION_ATLEAST(1,3,0)
- SDL_SetPaletteColors( surf->format->palette, pal, 0, numcolors );
-#else
- SDL_SetPalette( surf, SDL_LOGPAL | SDL_RLEACCEL, pal, 0, numcolors );
-#endif
-}
-
}
View
2 gemrb/plugins/SDLVideo/SDLSurfaceSprite2D.h
@@ -51,8 +51,6 @@ class SDLSurfaceSprite2D : public Sprite2D {
ieDword bmask, ieDword amask);
SDL_Surface* GetSurface() const { return surface; };
-
- static void SetSurfacePalette(SDL_Surface* surf, SDL_Color* pal, int numcolors = 256);
};
}
View
135 gemrb/plugins/SDLVideo/SDLVideo.cpp
@@ -509,9 +509,9 @@ void SDLVideoDriver::BlitSprite(const Sprite2D* spr, int x, int y, bool anchor,
SDL_Surface* surf = ((SDLSurfaceSprite2D*)spr)->GetSurface();
if (palette) {
SDL_Color* palColors = (SDL_Color*)spr->GetPaletteColors();
- SDLSurfaceSprite2D::SetSurfacePalette(surf, (SDL_Color*)palette->col);
+ SetSurfacePalette(surf, (SDL_Color*)palette->col);
SDL_BlitSurface( surf, srect, backBuf, &drect );
- SDLSurfaceSprite2D::SetSurfacePalette(surf, palColors);
+ SetSurfacePalette(surf, palColors);
} else {
SDL_BlitSurface( surf, srect, backBuf, &drect );
}
@@ -866,7 +866,7 @@ void SDLVideoDriver::DrawRect(const Region& rgn, const Color& color, bool fill,
c.r = color.r;
c.b = color.b;
c.g = color.g;
- SDLSurfaceSprite2D::SetSurfacePalette(rectsurf, &c, 1);
+ SetSurfacePalette(rectsurf, &c, 1);
SetSurfaceAlpha(rectsurf, color.a);
SDL_BlitSurface( rectsurf, NULL, backBuf, &drect );
SDL_FreeSurface( rectsurf );
@@ -902,49 +902,13 @@ void SDLVideoDriver::DrawRectSprite(const Region& rgn, const Color& color, const
c.r = color.r;
c.b = color.b;
c.g = color.g;
- SDLSurfaceSprite2D::SetSurfacePalette(rectsurf, &c, 1);
+ SDLVideoDriver::SetSurfacePalette(rectsurf, &c, 1);
SetSurfaceAlpha(rectsurf, color.a);
SDL_BlitSurface( rectsurf, NULL, surf, &drect );
SDL_FreeSurface( rectsurf );
}
}
-inline void ReadPixel(long &val, Pixel* pixels , int BytesPerPixel) {
- if (BytesPerPixel == 1) {
- val = *pixels;
- } else if (BytesPerPixel == 2) {
- val = *(Uint16 *)pixels;
- } else if (BytesPerPixel == 3) {
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
- val = pixels[0] + ((unsigned int)pixels[1] << 8) + ((unsigned int)pixels[2] << 16);
-#else
- val = pixels[2] + ((unsigned int)pixels[1] << 8) + ((unsigned int)pixels[0] << 16);
-#endif
- } else if (BytesPerPixel == 4) {
- val = *(Uint32 *)pixels;
- }
-}
-
-inline void WritePixel(const long val, Pixel* pixels, int BytesPerPixel) {
- if (BytesPerPixel == 1) {
- *pixels = (unsigned char)val;
- } else if (BytesPerPixel == 2) {
- *(Uint16 *)pixels = (Uint16)val;
- } else if (BytesPerPixel == 3) {
-#if SDL_BYTEORDER == SDL_LIL_ENDIAN
- pixels[0] = val & 0xff;
- pixels[1] = (val >> 8) & 0xff;
- pixels[2] = (val >> 16) & 0xff;
-#else
- pixels[2] = val & 0xff;
- pixels[1] = (val >> 8) & 0xff;
- pixels[0] = (val >> 16) & 0xff;
-#endif
- } else if (BytesPerPixel == 4) {
- *(Uint32 *)pixels = val;
- }
-}
-
void SDLVideoDriver::SetPixel(short x, short y, const Color& color, bool clipped)
{
//print("x: %d; y: %d; XC: %d; YC: %d, VX: %d, VY: %d, VW: %d, VH: %d", x, y, xCorr, yCorr, Viewport.x, Viewport.y, Viewport.w, Viewport.h);
@@ -966,25 +930,12 @@ void SDLVideoDriver::SetPixel(short x, short y, const Color& color, bool clipped
}
}
- unsigned char * pixels = ( ( unsigned char * ) backBuf->pixels ) +
- ( ( y * disp->w + x) * disp->format->BytesPerPixel );
-
- long val = SDL_MapRGBA( backBuf->format, color.r, color.g, color.b, color.a );
- SDL_LockSurface( backBuf );
- WritePixel(val, pixels, backBuf->format->BytesPerPixel);
- SDL_UnlockSurface( backBuf );
+ SDLVideoDriver::SetSurfacePixel(backBuf, x, y, color);
}
void SDLVideoDriver::GetPixel(short x, short y, Color& c)
{
- SDL_LockSurface( backBuf );
- unsigned char * pixels = ( ( unsigned char * ) backBuf->pixels ) +
- ( ( y * disp->w + x) * disp->format->BytesPerPixel );
- long val = 0;
- ReadPixel(val, pixels, backBuf->format->BytesPerPixel);
- SDL_UnlockSurface( backBuf );
-
- SDL_GetRGBA( val, backBuf->format, (Uint8 *) &c.r, (Uint8 *) &c.g, (Uint8 *) &c.b, (Uint8 *) &c.a );
+ SDLVideoDriver::GetSurfacePixel(backBuf, x, y, c);
}
/*
@@ -1597,3 +1548,77 @@ void SDLVideoDriver::DrawMovieSubtitle(ieDword strRef)
backBuf = temp;
}
}
+
+// static class methods
+
+void SDLVideoDriver::SetSurfacePalette(SDL_Surface* surf, SDL_Color* pal, int numcolors)
+{
+#if SDL_VERSION_ATLEAST(1,3,0)
+ SDL_SetPaletteColors( surf->format->palette, pal, 0, numcolors );
+#else
+ SDL_SetPalette( surf, SDL_LOGPAL | SDL_RLEACCEL, pal, 0, numcolors );
+#endif
+}
+
+void SDLVideoDriver::SetSurfacePixel(SDL_Surface* surface, short x, short y, const Color& color)
+{
+ SDL_PixelFormat* fmt = surface->format;
+ unsigned char * pixels = ( ( unsigned char * ) surface->pixels ) +
+ ( ( y * surface->w + x) * fmt->BytesPerPixel );
+
+ Uint32 val = SDL_MapRGBA( fmt, color.r, color.g, color.b, color.a );
+ SDL_LockSurface( surface );
+
+ switch (fmt->BytesPerPixel) {
+ case 1:
+ *pixels = (unsigned char)val;
+ break;
+ case 2:
+ *(Uint16 *)pixels = (Uint16)val;
+ break;
+ case 3:
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+ pixels[0] = val & 0xff;
+ pixels[1] = (val >> 8) & 0xff;
+ pixels[2] = (val >> 16) & 0xff;
+#else
+ pixels[2] = val & 0xff;
+ pixels[1] = (val >> 8) & 0xff;
+ pixels[0] = (val >> 16) & 0xff;
+#endif
+ break;
+ case 4:
+ *(Uint32 *)pixels = val;
+ default:
+ Log(ERROR, "SDLSurfaceSprite2D", "Working with unknown pixel format: %s", SDL_GetError());
+ break;
+ }
+
+ SDL_UnlockSurface( surface );
+}
+
+void SDLVideoDriver::GetSurfacePixel(SDL_Surface* surface, short x, short y, Color& c)
+{
+ SDL_LockSurface( surface );
+ Uint8 Bpp = surface->format->BytesPerPixel;
+ unsigned char * pixels = ( ( unsigned char * ) surface->pixels ) +
+ ( ( y * surface->w + x) * Bpp );
+ Uint32 val = 0;
+
+ if (Bpp == 1) {
+ val = *pixels;
+ } else if (Bpp == 2) {
+ val = *(Uint16 *)pixels;
+ } else if (Bpp == 3) {
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+ val = pixels[0] + ((unsigned int)pixels[1] << 8) + ((unsigned int)pixels[2] << 16);
+#else
+ val = pixels[2] + ((unsigned int)pixels[1] << 8) + ((unsigned int)pixels[0] << 16);
+#endif
+ } else if (Bpp == 4) {
+ val = *(Uint32 *)pixels;
+ }
+
+ SDL_UnlockSurface( surface );
+ SDL_GetRGBA( val, surface->format, (Uint8 *) &c.r, (Uint8 *) &c.g, (Uint8 *) &c.b, (Uint8 *) &c.a );
+}
View
6 gemrb/plugins/SDLVideo/SDLVideo.h
@@ -168,8 +168,10 @@ class SDLVideoDriver : public Video {
virtual int ProcessEvent(const SDL_Event & event);
public:
- void SetGamma(int brightness, int contrast)=0;
- void SetMouseScrollSpeed(int speed);
+ // static functions for manipulating surfaces
+ static void SetSurfacePalette(SDL_Surface* surf, SDL_Color* pal, int numcolors = 256);
+ static void SetSurfacePixel(SDL_Surface* surf, short x, short y, const Color& color);
+ static void GetSurfacePixel(SDL_Surface* surf, short x, short y, Color& c);
};
}

0 comments on commit 3baa1ee

Please sign in to comment.
Something went wrong with that request. Please try again.