Skip to content

Commit

Permalink
Refactor video driver sprite creation
Browse files Browse the repository at this point in the history
CreateSprite8() now enforces 8bpp as the name implies and also takes a Palette* instead of void* (Color*)
A new method CreatePalettedSprite() will fill in for creating non 8bpp paletted sprites using an array of Colors
  • Loading branch information
bradallred committed Jun 16, 2013
1 parent a1fdb4b commit d7a6a40
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 31 deletions.
6 changes: 4 additions & 2 deletions gemrb/core/Video.h
Expand Up @@ -134,8 +134,10 @@ class GEM_EXPORT Video : public Plugin {
virtual Sprite2D* CreateSprite(int w, int h, int bpp, ieDword rMask,
ieDword gMask, ieDword bMask, ieDword aMask, void* pixels,
bool cK = false, int index = 0) = 0;
virtual Sprite2D* CreateSprite8(int w, int h, int bpp, void* pixels,
void* palette, bool cK = false, int index = 0) = 0;
virtual Sprite2D* CreateSprite8(int w, int h, void* pixels,
Palette* palette, bool cK = false, int index = 0) = 0;
virtual Sprite2D* CreatePalettedSprite(int w, int h, int bpp, void* pixels,
Color* palette, bool cK = false, int index = 0) = 0;
virtual bool SupportsBAMSprites() { return false; }
virtual void FreeSprite(Sprite2D* &spr) = 0;
virtual Sprite2D* DuplicateSprite(const Sprite2D* spr) = 0;
Expand Down
2 changes: 1 addition & 1 deletion gemrb/plugins/BAMImporter/BAMFont.cpp
Expand Up @@ -55,7 +55,7 @@ BAMFont::BAMFont(AnimationFactory* af, int* baseline)
pal->release();
first->release();

blank = core->GetVideoDriver()->CreateSprite8(0, 0, 8, NULL, palette->col);
blank = core->GetVideoDriver()->CreateSprite8(0, 0, NULL, palette);
}

BAMFont::~BAMFont()
Expand Down
6 changes: 3 additions & 3 deletions gemrb/plugins/BAMImporter/BAMImporter.cpp
Expand Up @@ -153,8 +153,8 @@ Sprite2D* BAMImporter::GetFrameInternal(unsigned short findex, unsigned char mod
} else {
void* pixels = GetFramePixels(findex);
spr = core->GetVideoDriver()->CreateSprite8(
frames[findex].Width, frames[findex].Height, 8,
pixels, palette->col, true, 0 );
frames[findex].Width, frames[findex].Height,
pixels, palette, true, 0 );
}

spr->XPos = (ieWordSigned)frames[findex].XPos;
Expand Down Expand Up @@ -287,7 +287,7 @@ Sprite2D* BAMImporter::GetPalette()
for (int i = 0; i < 256; i++) {
*p++ = ( unsigned char ) i;
}
return core->GetVideoDriver()->CreateSprite8( 16, 16, 8, pixels, palette->col, false );
return core->GetVideoDriver()->CreateSprite8( 16, 16, pixels, palette );
}

#include "BAMFontManager.h"
Expand Down
4 changes: 2 additions & 2 deletions gemrb/plugins/BMPImporter/BMPImporter.cpp
Expand Up @@ -234,8 +234,8 @@ Sprite2D* BMPImporter::GetSprite2D()
} else if (BitCount == 8) {
void* p = malloc( Width* Height );
memcpy( p, pixels, Width * Height );
spr = core->GetVideoDriver()->CreateSprite8( Width, Height, NumColors==16?4:8,
p, Palette, true, 0 );
spr = core->GetVideoDriver()->CreatePalettedSprite(Width, Height, NumColors == 16 ? 4 : 8,
p, Palette, true, 0);
}
return spr;
}
Expand Down
3 changes: 1 addition & 2 deletions gemrb/plugins/PNGImporter/PNGImporter.cpp
Expand Up @@ -180,8 +180,7 @@ Sprite2D* PNGImporter::GetSprite2D()
Color pal[256];
GetPalette(256, pal);
// TODO: colorkey
spr = core->GetVideoDriver()->CreateSprite8(Width, Height, 8,
buffer, pal, false, 0);
spr = core->GetVideoDriver()->CreatePalettedSprite(Width, Height, 8, buffer, pal);
} else {
spr = core->GetVideoDriver()->CreateSprite(Width, Height, 32,
red_mask, green_mask,
Expand Down
25 changes: 12 additions & 13 deletions gemrb/plugins/SDLVideo/SDLVideo.cpp
Expand Up @@ -393,29 +393,28 @@ void SDLVideoDriver::AddPolygonToSpriteCover(SpriteCover* sc, Wall_Polygon* poly
}
}


Sprite2D* SDLVideoDriver::CreateSprite(int w, int h, int bpp, ieDword rMask,
ieDword gMask, ieDword bMask, ieDword aMask, void* pixels, bool cK, int index)
{
SDL_Surface* p = SDL_CreateRGBSurfaceFrom( pixels, w, h, bpp, w*( bpp / 8 ),
rMask, gMask, bMask, aMask );
rMask, gMask, bMask, aMask );
if (cK) {
SDL_SetColorKey( p, SDL_SRCCOLORKEY | SDL_RLEACCEL, index );
}
return new Sprite2D(w, h, bpp, p, pixels);
}

Sprite2D* SDLVideoDriver::CreateSprite8(int w, int h, int bpp, void* pixels,
void* palette, bool cK, int index)
Sprite2D* SDLVideoDriver::CreateSprite8(int w, int h, void* pixels,
Palette* palette, bool cK, int index)
{
return CreatePalettedSprite(w, h, 8, pixels, palette->col, cK, index);
}

Sprite2D* SDLVideoDriver::CreatePalettedSprite(int w, int h, int bpp, void* pixels,
Color* palette, bool cK, int index)
{
SDL_Surface* p = SDL_CreateRGBSurfaceFrom( pixels, w, h, 8, w, 0, 0, 0, 0 );
int colorcount;
if (bpp == 8) {
colorcount = 256;
} else {
colorcount = 16;
}
SetSurfacePalette( p, ( SDL_Color * ) palette, colorcount );
SetSurfacePalette( p, (SDL_Color*)palette, 0x01 << bpp );
if (cK) {
SDL_SetColorKey( p, SDL_SRCCOLORKEY, index );
}
Expand Down Expand Up @@ -457,8 +456,8 @@ Sprite2D* SDLVideoDriver::DuplicateSprite(const Sprite2D* sprite)

SDL_LockSurface( tmp );
memcpy(newpixels, sprite->pixels, sprite->Width*sprite->Height);
dest = CreateSprite8(sprite->Width, sprite->Height, 8,
newpixels, tmp->format->palette->colors, true, 0);
dest = CreatePalettedSprite(sprite->Width, sprite->Height, 8, newpixels,
(Color*)tmp->format->palette->colors, true, 0);
SDL_UnlockSurface( tmp );
} else {
dest = sprite->copy();
Expand Down
6 changes: 4 additions & 2 deletions gemrb/plugins/SDLVideo/SDLVideo.h
Expand Up @@ -87,8 +87,10 @@ class SDLVideoDriver : public Video {
Sprite2D* CreateSprite(int w, int h, int bpp, ieDword rMask,
ieDword gMask, ieDword bMask, ieDword aMask, void* pixels,
bool cK = false, int index = 0);
Sprite2D* CreateSprite8(int w, int h, int bpp, void* pixels,
void* palette, bool cK = false, int index = 0);
Sprite2D* CreateSprite8(int w, int h, void* pixels,
Palette* palette, bool cK, int index);
Sprite2D* CreatePalettedSprite(int w, int h, int bpp, void* pixels,
Color* palette, bool cK = false, int index = 0);
bool SupportsBAMSprites() { return true; }
void FreeSprite(Sprite2D* &spr);
Sprite2D* DuplicateSprite(const Sprite2D* spr);
Expand Down
4 changes: 2 additions & 2 deletions gemrb/plugins/TISImporter/TISImporter.cpp
Expand Up @@ -107,7 +107,7 @@ Sprite2D* TISImporter::GetTile(int index)
memset(pixels, 0, 4096);
memset(Palette, 0, 256 * sizeof(Color));
Palette[0].g = 200;
Sprite2D* spr = core->GetVideoDriver()->CreateSprite8( 64, 64, 8, pixels, Palette, false, 0 );
Sprite2D* spr = core->GetVideoDriver()->CreatePalettedSprite( 64, 64, 8, pixels, Palette );
spr->XPos = spr->YPos = 0;
return spr;
}
Expand All @@ -130,7 +130,7 @@ Sprite2D* TISImporter::GetTile(int index)
}
}
str->Read( pixels, 4096 );
Sprite2D* spr = core->GetVideoDriver()->CreateSprite8( 64, 64, 8, pixels, Palette, transparent, transindex );
Sprite2D* spr = core->GetVideoDriver()->CreatePalettedSprite( 64, 64, 8, pixels, Palette, transparent, transindex );
spr->XPos = spr->YPos = 0;
return spr;
}
Expand Down
8 changes: 4 additions & 4 deletions gemrb/plugins/TTFImporter/TTFFont.cpp
Expand Up @@ -158,7 +158,7 @@ const Sprite2D* TTFFont::GetCharSprite(ieWord chr) const

// TODO: do an underline if requested

Sprite2D* spr = core->GetVideoDriver()->CreateSprite8(sprWidth, sprHeight, 8, pixels, palette, true, 0);
Sprite2D* spr = core->GetVideoDriver()->CreateSprite8(sprWidth, sprHeight, pixels, palette, true, 0);
// for some reason BAM fonts are all based of a YPos of 13
spr->YPos = 13 - yoffset;
// cache the glyph
Expand Down Expand Up @@ -266,11 +266,11 @@ TTFFont::TTFFont(FT_Face face, ieWord ptSize, FontStyle style, Palette* pal)
SetPalette(pal);

// TODO: ttf fonts have a "box" glyph they use for this
blank = core->GetVideoDriver()->CreateSprite8(0, 0, 8, NULL, palette->col);
blank = core->GetVideoDriver()->CreateSprite8(0, 0, NULL, palette);
// ttf fonts dont produce glyphs for whitespace
int SpaceWidth = core->TLKEncoding.zerospace ? 1 : (ptSize * 0.25);
Sprite2D* space = core->GetVideoDriver()->CreateSprite8(SpaceWidth, 0, 8, NULL, palette->col);;
Sprite2D* tab = core->GetVideoDriver()->CreateSprite8((space->Width)*4, 0, 8, NULL, palette->col);
Sprite2D* space = core->GetVideoDriver()->CreateSprite8(SpaceWidth, 0, NULL, palette);
Sprite2D* tab = core->GetVideoDriver()->CreateSprite8((space->Width)*4, 0, NULL, palette);

// now cache these glyphs for quick access
// WARNING: if we ever did something to purge the cache these would be lost
Expand Down

0 comments on commit d7a6a40

Please sign in to comment.