Skip to content

Commit 6da1d35

Browse files
authored
Merge pull request #255 from cortex-command-community/fix-save-palette
Fix save palette
2 parents 4d461c6 + 88cde74 commit 6da1d35

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

Source/Managers/ActivityMan.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
206206

207207
SDL_Palette* palette = ContentFile::DefaultPaletteToSDL();
208208
SDL_SetSurfacePalette(image, palette);
209+
SDL_SetSurfaceColorKey(image, false, 0);
209210

210211
bool result = IMG_SavePNG_IO(image, stream, false);
211212
SDL_FlushIO(stream);
@@ -310,11 +311,16 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
310311
SDL_Surface* image = stream ? IMG_LoadPNG_IO(stream) : nullptr;
311312
SDL_CloseIO(stream);
312313

314+
int bitDepth = SDL_GetPixelFormatDetails(image->format)->bits_per_pixel;
313315
SDL_Palette* palette = ContentFile::DefaultPaletteToSDL();
314-
SDL_Surface* newImage = SDL_ConvertSurfaceAndColorspace(image, SDL_PIXELFORMAT_INDEX8, palette, SDL_COLORSPACE_UNKNOWN, 0);
316+
if (bitDepth != 8) {
317+
SDL_Surface* newImage = SDL_ConvertSurfaceAndColorspace(image, SDL_PIXELFORMAT_INDEX8, palette, SDL_COLORSPACE_UNKNOWN, 0);
318+
SDL_DestroySurface(image);
319+
image = newImage;
320+
} else {
321+
SDL_SetSurfacePalette(image, palette);
322+
}
315323
SDL_DestroyPalette(palette);
316-
SDL_DestroySurface(image);
317-
image = newImage;
318324

319325
free(buffer);
320326
return image;

Source/System/ContentFile.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void ContentFile::FreeAllLoaded() {
6363
int ContentFile::ReadProperty(const std::string_view& propName, Reader& reader) {
6464
StartPropertyList(return Serializable::ReadProperty(propName, reader));
6565

66-
MatchForwards("FilePath")
66+
MatchForwards("FilePath")
6767
MatchProperty("Path", { SetDataPath(reader.ReadPropValue()); });
6868
MatchProperty("IsMemoryPNG", { reader >> m_IsMemoryPNG; });
6969

@@ -249,8 +249,8 @@ BITMAP* ContentFile::GetAsBitmap(int conversionMode, bool storeBitmap, const std
249249
SDL_DestroySurface(surface);
250250
s_MemoryPNGs.erase(dataPathToLoad);
251251
}
252-
}
253-
252+
}
253+
254254
if (returnBitmap == nullptr) {
255255
if (!System::PathExistsCaseSensitive(dataPathToLoad)) {
256256
const std::string dataPathWithoutExtension = dataPathToLoad.substr(0, dataPathToLoad.length() - m_DataPathExtension.length());
@@ -302,11 +302,19 @@ void ContentFile::GetAsAnimation(std::vector<BITMAP*>& vectorToFill, int frameCo
302302
}
303303
}
304304
}
305-
SDL_Palette* ContentFile::DefaultPaletteToSDL() {
305+
SDL_Palette* ContentFile::DefaultPaletteToSDL(bool preMask) {
306306
SDL_Palette* palette = SDL_CreatePalette(256);
307307
std::array<SDL_Color, 256> paletteColor;
308308
const PALETTE& defaultPalette = g_FrameMan.GetDefaultPalette();
309-
paletteColor[0] = {.r = 0, .g = 0, .b = 0, .a = 0};
309+
if (preMask) {
310+
paletteColor[0] = {.r = 0, .g = 0, .b = 0, .a = 0};
311+
} else {
312+
paletteColor[0] = {.r = defaultPalette[0].r,
313+
.g = defaultPalette[0].g,
314+
.b = defaultPalette[0].b,
315+
.a = 255
316+
};
317+
}
310318
for (size_t i = 1; i < paletteColor.size(); ++i) {
311319
paletteColor[i].r = defaultPalette[i].r;
312320
paletteColor[i].g = defaultPalette[i].g;
@@ -330,7 +338,7 @@ SDL_Surface* ContentFile::LoadImageAsSurface(int conversionMode, const std::stri
330338
image = newImage;
331339
bitDepth = 8;
332340
} else if (bitDepth != 8 || convert8To32) {
333-
SDL_Palette* palette = DefaultPaletteToSDL();
341+
SDL_Palette* palette = DefaultPaletteToSDL(true);
334342
if (SDL_GetPixelFormatDetails(image->format)->bits_per_pixel == 8) {
335343
SDL_SetSurfacePalette(image, palette);
336344
SDL_SetSurfaceColorKey(image, true, 0);
@@ -391,7 +399,7 @@ FMOD::Sound* ContentFile::LoadAndReleaseSound(bool abortGameForInvalidSound, boo
391399
}
392400
if (!System::PathExistsCaseSensitive(m_DataPath)) {
393401
bool foundAltExtension = false;
394-
for (const std::string& altFileExtension: c_SupportedAudioFormats) {
402+
for (const char* altFileExtension: c_SupportedAudioFormats) {
395403
const std::string altDataPathToLoad = m_DataPathWithoutExtension + altFileExtension;
396404
if (System::PathExistsCaseSensitive(altDataPathToLoad)) {
397405
g_ConsoleMan.AddLoadWarningLogExtensionMismatchEntry(m_DataPath, m_FormattedReaderPosition, altFileExtension);
@@ -443,15 +451,14 @@ void ContentFile::ReloadBitmap(const std::string& filePath, int conversionMode)
443451

444452
SDL_Surface* newImage = LoadImageAsSurface(conversionMode, filePath);
445453

446-
447454
BITMAP* newBitmap = create_bitmap_ex(SDL_GetPixelFormatDetails(newImage->format)->bits_per_pixel, newImage->w, newImage->h);
448455

449456
// allegro doesn't (always) align lines to 4byte, so copy line by line. SDL_Surface.pitch is the size in bytes per line + alignment padding.
450457
for (int y = 0; y < newImage->h; y++) {
451-
memcpy(newBitmap->line[y], static_cast<unsigned char*>(newImage->pixels) + y * newImage->pitch, newImage->w * SDL_GetPixelFormatDetails(newImage->format)->bytes_per_pixel);
458+
memcpy(newBitmap->line[y], static_cast<unsigned char*>(newImage->pixels) + y * newImage->pitch, newImage->w * SDL_GetPixelFormatDetails(newImage->format)->bytes_per_pixel);
452459
}
453460

454-
//AddAlphaChannel(newBitmap);
461+
// AddAlphaChannel(newBitmap);
455462
BITMAP swap;
456463

457464
std::memcpy(&swap, loadedBitmap, sizeof(BITMAP));

Source/System/ContentFile.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ namespace RTE {
157157
#pragma endregion
158158

159159
/// Copies the default palette to an sdl palette.
160-
static SDL_Palette* DefaultPaletteToSDL();
160+
/// @param preMask Whether to replace mask color with 0 alpha (necessary for loading indexed to 32-bit image)
161+
static SDL_Palette* DefaultPaletteToSDL(bool preMask = false);
161162

162163
private:
163164
/// Enumeration for loading BITMAPs by bit depth. NOTE: This can't be lower down because s_LoadedBitmaps relies on this definition.

0 commit comments

Comments
 (0)