Permalink
Browse files

Sprites: refactor sprite destruction

previously the video driver managed destroying private sprite data. rewrite to make each subclass responsible for its own data
  • Loading branch information...
1 parent 7125ea0 commit 1c53aee653a77bb649f05da4325bb0187e77fd6f @bradallred bradallred committed Jun 16, 2013
View
@@ -83,8 +83,10 @@ Color Sprite2D::GetPixel(unsigned short x, unsigned short y) const
void Sprite2D::release()
{
- Sprite2D *that = this;
- core->GetVideoDriver()->FreeSprite(that);
+ assert(RefCount > 0);
+ if (--RefCount == 0) {
+ delete this;
+ }
}
}
View
@@ -49,6 +49,8 @@ class AnimationFactory;
class GEM_EXPORT Sprite2D {
public:
static const TypeID ID;
+private:
+ int RefCount;
public:
int XPos, YPos, Width, Height, Bpp;
/** Pointer to the Driver Video Structure */
@@ -69,9 +71,7 @@ class GEM_EXPORT Sprite2D {
virtual Color GetPixel(unsigned short x, unsigned short y) const;
virtual ieDword GetColorKey() const = 0;
virtual void SetColorKey(ieDword ck) = 0;
-public: // public only for SDLVideo
- int RefCount;
-public:
+
void acquire() { ++RefCount; }
void release();
};
@@ -30,6 +30,7 @@ SDLSurfaceSprite2D::SDLSurfaceSprite2D(int Width, int Height, int Bpp, void* pix
surface = SDL_CreateRGBSurfaceFrom( pixels, Width, Height, Bpp < 8 ? 8 : Bpp, Width * ( Bpp / 8 ),
0, 0, 0, 0 );
vptr = surface;
+ freePixels = true;
#if SDL_VERSION_ATLEAST(1,3,0)
SDL_SetSurfaceRLE(surface, SDL_TRUE);
#endif
@@ -42,6 +43,7 @@ SDLSurfaceSprite2D::SDLSurfaceSprite2D (int Width, int Height, int Bpp, void* pi
surface = SDL_CreateRGBSurfaceFrom( pixels, Width, Height, Bpp < 8 ? 8 : Bpp, Width * ( Bpp / 8 ),
rmask, gmask, bmask, amask );
vptr = surface;
+ freePixels = true;
#if SDL_VERSION_ATLEAST(1,3,0)
SDL_SetSurfaceRLE(surface, SDL_TRUE);
#endif
@@ -51,6 +53,8 @@ SDLSurfaceSprite2D::SDLSurfaceSprite2D(const SDLSurfaceSprite2D &obj)
: Sprite2D(obj)
{
surface = SDL_ConvertSurface(obj.surface, obj.surface->format, obj.surface->flags);
+ pixels = surface->pixels;
+ freePixels = false;
vptr = surface;
#if SDL_VERSION_ATLEAST(1,3,0)
SDL_SetSurfaceRLE(surface, SDL_TRUE);
@@ -65,6 +69,10 @@ SDLSurfaceSprite2D* SDLSurfaceSprite2D::copy() const
SDLSurfaceSprite2D::~SDLSurfaceSprite2D()
{
SDL_FreeSurface(surface);
+ if (freePixels) {
+ // FIXME: casting away const.
+ free((void*)pixels);
+ }
}
/** Get the Palette of a Sprite */
@@ -30,6 +30,7 @@ namespace GemRB {
class SDLSurfaceSprite2D : public Sprite2D {
private:
+ bool freePixels;
SDL_Surface* surface;
public:
SDLSurfaceSprite2D(int Width, int Height, int Bpp, void* pixels);
@@ -423,23 +423,10 @@ Sprite2D* SDLVideoDriver::CreatePalettedSprite(int w, int h, int bpp, void* pixe
void SDLVideoDriver::FreeSprite(Sprite2D*& spr)
{
- if(!spr)
- return;
- assert(spr->RefCount > 0);
- if (--spr->RefCount > 0) {
+ if (spr) {
+ spr->release();
spr = NULL;
- return;
- }
-
- if (spr->BAM) {
- // currently in the process of removing vptr, but it needs to be set for some methods to work
- // so i am setting it to the object itself for bam sprites for now
- assert(spr->vptr == spr);
- } else {
- free( (void*)spr->pixels );
}
- delete spr;
- spr = NULL;
}
Sprite2D* SDLVideoDriver::DuplicateSprite(const Sprite2D* sprite)

0 comments on commit 1c53aee

Please sign in to comment.