Permalink
Browse files

Sprite: add ConvertFormatTo method

returns true if sprite converted successfully
  • Loading branch information...
1 parent 0d3d529 commit b6e2900b8f1510caa69b3b4e0c7ffd81b2bc2fb9 @bradallred bradallred committed Jun 16, 2013
View
@@ -72,6 +72,8 @@ class GEM_EXPORT Sprite2D {
virtual Color GetPixel(unsigned short x, unsigned short y) const = 0;
virtual ieDword GetColorKey() const = 0;
virtual void SetColorKey(ieDword ck) = 0;
+ virtual bool ConvertFormatTo(int /*bpp*/, ieDword /*rmask*/, ieDword /*gmask*/,
+ ieDword /*bmask*/, ieDword /*amask*/) { return false; }; // not pure virtual!
void acquire() { ++RefCount; }
void release();
@@ -20,6 +20,8 @@
#include "SDLSurfaceSprite2D.h"
+#include "System/Logging.h"
+
#include <SDL/SDL.h>
namespace GemRB {
@@ -153,6 +155,44 @@ Color SDLSurfaceSprite2D::GetPixel(unsigned short x, unsigned short y) const
return c;
}
+bool SDLSurfaceSprite2D::ConvertFormatTo(int bpp, ieDword rmask, ieDword gmask,
+ ieDword bmask, ieDword amask)
+{
+ if (bpp >= 8) {
+#if SDL_VERSION_ATLEAST(1,3,0)
+ Uint32 fmt = SDL_MasksToPixelFormatEnum(bpp, rmask, gmask, bmask, amask);
+ if (fmt != SDL_PIXELFORMAT_UNKNOWN) {
+ SDL_Surface* ns = SDL_ConvertSurfaceFormat( surface, fmt, 0);
+#else
+ SDL_Surface* tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, Width, Height, bpp, rmask, gmask, bmask, amask);
+ if (tmp) {
+ SDL_Surface* ns = SDL_ConvertSurface( surface, tmp->format, 0);
+ SDL_FreeSurface(tmp);
+#endif
+ if (ns) {
+ SDL_FreeSurface(surface);
+ if (freePixels) {
+ free((void*)pixels);
+ }
+ freePixels = false;
+ surface = ns;
+ pixels = surface->pixels;
+ Bpp = bpp;
+ return true;
+ } else {
+ Log(MESSAGE, "SDLSurfaceSprite2D",
+#if SDL_VERSION_ATLEAST(1,3,0)
+ "Cannot convert sprite to format: %s\nError: %s", SDL_GetPixelFormatName(fmt),
+#else
+ "Cannot convert sprite to format: %s",
+#endif
+ SDL_GetError());
+ }
+ }
+ }
+ return false;
+}
+
void SDLSurfaceSprite2D::SetSurfacePalette(SDL_Surface* surf, SDL_Color* pal, int numcolors)
{
#if SDL_VERSION_ATLEAST(1,3,0)
@@ -47,6 +47,8 @@ class SDLSurfaceSprite2D : public Sprite2D {
ieDword GetColorKey() const;
void SetColorKey(ieDword ck);
Color GetPixel(unsigned short x, unsigned short y) const;
+ bool ConvertFormatTo(int bpp, ieDword rmask, ieDword gmask,
+ ieDword bmask, ieDword amask);
SDL_Surface* GetSurface() const { return surface; };
@@ -951,19 +951,14 @@ Sprite2D* SDLVideoDriver::GetScreenshot( Region r )
void SDLVideoDriver::ConvertToVideoFormat(Sprite2D* sprite)
{
if (!sprite->BAM) {
- SDL_Surface* ss = ( SDL_Surface* ) sprite->vptr;
+ // TODO: is this really true and if so should we move this to SDLSurfaceSprite2D::ConvertFormatTo()?
+ SDL_Surface* ss = ((SDLSurfaceSprite2D*)sprite)->GetSurface();
if (ss->format->Amask != 0) //Surface already converted
{
return;
}
- SDL_Surface* ns = SDL_ConvertSurface( ss, disp->format, 0);
- if (ns == NULL) {
- return;
- }
- SDL_FreeSurface( ss );
- free( (void*)sprite->pixels );
- sprite->pixels = NULL;
- sprite->vptr = ns;
+ SDL_PixelFormat* fmt = disp->format;
+ sprite->ConvertFormatTo(fmt->BitsPerPixel, fmt->Rmask, fmt->Gmask, fmt->Bmask, fmt->Amask);
}
}

0 comments on commit b6e2900

Please sign in to comment.