1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-04-15 11:46:56 +02:00

Update the originalPalette, and use SDL_Palette methods to improve memory management.

This commit is contained in:
toneyisnow 2020-01-26 00:01:48 -08:00
parent 0f4a9e5106
commit 468584d469
2 changed files with 36 additions and 16 deletions

View File

@ -70,6 +70,9 @@ public:
class SDLImage : public IImage class SDLImage : public IImage
{ {
public: public:
const static int DEFAULT_PALETTE_COLORS = 256;
//Surface without empty borders //Surface without empty borders
SDL_Surface * surf; SDL_Surface * surf;
//size of left and top borders //size of left and top borders
@ -112,7 +115,7 @@ public:
friend class SDLImageLoader; friend class SDLImageLoader;
private: private:
std::unique_ptr<SDL_Palette> originalPalette; SDL_Palette * originalPalette;
}; };
class SDLImageLoader class SDLImageLoader
@ -506,8 +509,8 @@ void SDLImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_C
image->fullSize = FullSize; image->fullSize = FullSize;
//Prepare surface //Prepare surface
SDL_Palette * p = SDL_AllocPalette(256); SDL_Palette * p = SDL_AllocPalette(SDLImage::DEFAULT_PALETTE_COLORS);
SDL_SetPaletteColors(p, pal, 0, 256); SDL_SetPaletteColors(p, pal, 0, SDLImage::DEFAULT_PALETTE_COLORS);
SDL_SetSurfacePalette(image->surf, p); SDL_SetSurfacePalette(image->surf, p);
SDL_FreePalette(p); SDL_FreePalette(p);
@ -557,7 +560,8 @@ IImage::~IImage() = default;
SDLImage::SDLImage(CDefFile * data, size_t frame, size_t group) SDLImage::SDLImage(CDefFile * data, size_t frame, size_t group)
: surf(nullptr), : surf(nullptr),
margins(0, 0), margins(0, 0),
fullSize(0, 0) fullSize(0, 0),
originalPalette(nullptr)
{ {
SDLImageLoader loader(this); SDLImageLoader loader(this);
data->loadFrame(frame, group, loader); data->loadFrame(frame, group, loader);
@ -568,7 +572,8 @@ SDLImage::SDLImage(CDefFile * data, size_t frame, size_t group)
SDLImage::SDLImage(SDL_Surface * from, bool extraRef) SDLImage::SDLImage(SDL_Surface * from, bool extraRef)
: surf(nullptr), : surf(nullptr),
margins(0, 0), margins(0, 0),
fullSize(0, 0) fullSize(0, 0),
originalPalette(nullptr)
{ {
surf = from; surf = from;
if (surf == nullptr) if (surf == nullptr)
@ -585,7 +590,8 @@ SDLImage::SDLImage(SDL_Surface * from, bool extraRef)
SDLImage::SDLImage(const JsonNode & conf) SDLImage::SDLImage(const JsonNode & conf)
: surf(nullptr), : surf(nullptr),
margins(0, 0), margins(0, 0),
fullSize(0, 0) fullSize(0, 0),
originalPalette(nullptr)
{ {
std::string filename = conf["file"].String(); std::string filename = conf["file"].String();
@ -618,7 +624,8 @@ SDLImage::SDLImage(const JsonNode & conf)
SDLImage::SDLImage(std::string filename) SDLImage::SDLImage(std::string filename)
: surf(nullptr), : surf(nullptr),
margins(0, 0), margins(0, 0),
fullSize(0, 0) fullSize(0, 0),
originalPalette(nullptr)
{ {
surf = BitmapHandler::loadBitmap(filename); surf = BitmapHandler::loadBitmap(filename);
@ -759,11 +766,13 @@ void SDLImage::verticalFlip()
void SDLImage::savePalette() void SDLImage::savePalette()
{ {
// For some images that don't have palette, skip this // For some images that don't have palette, skip this
if(surf->format->palette == NULL) if(surf->format->palette == nullptr)
return; return;
originalPalette.reset(new SDL_Palette()); if(originalPalette == nullptr)
memcpy(originalPalette.get(), surf->format->palette, sizeof(SDL_Palette)); originalPalette = SDL_AllocPalette(DEFAULT_PALETTE_COLORS);
SDL_SetPaletteColors(originalPalette, surf->format->palette->colors, 0, DEFAULT_PALETTE_COLORS);
} }
void SDLImage::shiftPalette(int from, int howMany) void SDLImage::shiftPalette(int from, int howMany)
@ -785,8 +794,13 @@ void SDLImage::shiftPalette(int from, int howMany)
void SDLImage::adjustPalette(const ColorShifter * shifter) void SDLImage::adjustPalette(const ColorShifter * shifter)
{ {
if(originalPalette == nullptr)
return;
SDL_Palette* palette = surf->format->palette; SDL_Palette* palette = surf->format->palette;
for (int i = 0; i < 255; i++)
// Note: here we skip the first 8 colors in the palette that predefined in H3Palette
for(int i = 8; i < palette->ncolors; i++)
{ {
palette->colors[i] = shifter->shiftColor(originalPalette->colors[i]); palette->colors[i] = shifter->shiftColor(originalPalette->colors[i]);
} }
@ -794,11 +808,11 @@ void SDLImage::adjustPalette(const ColorShifter * shifter)
void SDLImage::resetPalette() void SDLImage::resetPalette()
{ {
SDL_Palette * pal = new SDL_Palette(); if(originalPalette == nullptr)
memcpy(pal, originalPalette.get(), sizeof(SDL_Palette)); return;
// Always keept the original palette not changed, copy a new palette to assign to surface // Always keept the original palette not changed, copy a new palette to assign to surface
SDL_SetPaletteColors(surf->format->palette, originalPalette->colors, 0, 255); SDL_SetPaletteColors(surf->format->palette, originalPalette->colors, 0, originalPalette->ncolors);
} }
void SDLImage::setBorderPallete(const IImage::BorderPallete & borderPallete) void SDLImage::setBorderPallete(const IImage::BorderPallete & borderPallete)
@ -812,6 +826,12 @@ void SDLImage::setBorderPallete(const IImage::BorderPallete & borderPallete)
SDLImage::~SDLImage() SDLImage::~SDLImage()
{ {
SDL_FreeSurface(surf); SDL_FreeSurface(surf);
if(originalPalette != nullptr)
{
SDL_FreePalette(originalPalette);
originalPalette = nullptr;
}
} }
std::shared_ptr<IImage> CAnimation::getFromExtraDef(std::string filename) std::shared_ptr<IImage> CAnimation::getFromExtraDef(std::string filename)