From 4dfefb8d86d22b35f5d7800e8c4de85d1e5bc532 Mon Sep 17 00:00:00 2001 From: HeliumAnt Date: Thu, 1 Jan 2026 14:55:52 +0100 Subject: [PATCH 1/4] only add alpha to palette on 8to32 conversion --- Source/System/ContentFile.cpp | 29 +++++++++++++++++++---------- Source/System/ContentFile.h | 3 ++- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Source/System/ContentFile.cpp b/Source/System/ContentFile.cpp index 6313408117..4d1df524fd 100644 --- a/Source/System/ContentFile.cpp +++ b/Source/System/ContentFile.cpp @@ -63,8 +63,8 @@ void ContentFile::FreeAllLoaded() { int ContentFile::ReadProperty(const std::string_view& propName, Reader& reader) { StartPropertyList(return Serializable::ReadProperty(propName, reader)); - MatchForwards("FilePath") - MatchProperty("Path", { SetDataPath(reader.ReadPropValue()); }); + MatchForwards("FilePath") + MatchProperty("Path", { SetDataPath(reader.ReadPropValue()); }); MatchProperty("IsMemoryPNG", { reader >> m_IsMemoryPNG; }); EndPropertyList; @@ -199,8 +199,10 @@ void ContentFile::ReadAndStoreBMPFileInfo(FILE* imageFile) { void ContentFile::ManuallyLoadDataPNG(const std::string& filePath, SDL_Surface* surface) { s_MemoryPNGs[filePath] = surface; + std::cout << filePath << " "; int bitDepth = SDL_GetPixelFormatDetails(surface->format)->bits_per_pixel; + std::cout << bitDepth << std::endl; BITMAP* bitmap = create_bitmap_ex(bitDepth, surface->w, surface->h); // Allegro doesn't align lines, SDL does 4byte alignment @@ -249,8 +251,8 @@ BITMAP* ContentFile::GetAsBitmap(int conversionMode, bool storeBitmap, const std SDL_DestroySurface(surface); s_MemoryPNGs.erase(dataPathToLoad); } - } - + } + if (returnBitmap == nullptr) { if (!System::PathExistsCaseSensitive(dataPathToLoad)) { const std::string dataPathWithoutExtension = dataPathToLoad.substr(0, dataPathToLoad.length() - m_DataPathExtension.length()); @@ -302,11 +304,19 @@ void ContentFile::GetAsAnimation(std::vector& vectorToFill, int frameCo } } } -SDL_Palette* ContentFile::DefaultPaletteToSDL() { +SDL_Palette* ContentFile::DefaultPaletteToSDL(bool preMask) { SDL_Palette* palette = SDL_CreatePalette(256); std::array paletteColor; const PALETTE& defaultPalette = g_FrameMan.GetDefaultPalette(); - paletteColor[0] = {.r = 0, .g = 0, .b = 0, .a = 0}; + if (preMask) { + paletteColor[0] = {.r = 0, .g = 0, .b = 0, .a = 0}; + } else { + paletteColor[0] = {.r = defaultPalette[0].r, + .g = defaultPalette[0].g, + .b = defaultPalette[0].b, + .a = 255 + }; + } for (size_t i = 1; i < paletteColor.size(); ++i) { paletteColor[i].r = defaultPalette[i].r; paletteColor[i].g = defaultPalette[i].g; @@ -330,7 +340,7 @@ SDL_Surface* ContentFile::LoadImageAsSurface(int conversionMode, const std::stri image = newImage; bitDepth = 8; } else if (bitDepth != 8 || convert8To32) { - SDL_Palette* palette = DefaultPaletteToSDL(); + SDL_Palette* palette = DefaultPaletteToSDL(true); if (SDL_GetPixelFormatDetails(image->format)->bits_per_pixel == 8) { SDL_SetSurfacePalette(image, palette); SDL_SetSurfaceColorKey(image, true, 0); @@ -443,15 +453,14 @@ void ContentFile::ReloadBitmap(const std::string& filePath, int conversionMode) SDL_Surface* newImage = LoadImageAsSurface(conversionMode, filePath); - BITMAP* newBitmap = create_bitmap_ex(SDL_GetPixelFormatDetails(newImage->format)->bits_per_pixel, newImage->w, newImage->h); // 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. for (int y = 0; y < newImage->h; y++) { - memcpy(newBitmap->line[y], static_cast(newImage->pixels) + y * newImage->pitch, newImage->w * SDL_GetPixelFormatDetails(newImage->format)->bytes_per_pixel); + memcpy(newBitmap->line[y], static_cast(newImage->pixels) + y * newImage->pitch, newImage->w * SDL_GetPixelFormatDetails(newImage->format)->bytes_per_pixel); } - //AddAlphaChannel(newBitmap); + // AddAlphaChannel(newBitmap); BITMAP swap; std::memcpy(&swap, loadedBitmap, sizeof(BITMAP)); diff --git a/Source/System/ContentFile.h b/Source/System/ContentFile.h index bfcd119d69..454511d13b 100644 --- a/Source/System/ContentFile.h +++ b/Source/System/ContentFile.h @@ -157,7 +157,8 @@ namespace RTE { #pragma endregion /// Copies the default palette to an sdl palette. - static SDL_Palette* DefaultPaletteToSDL(); + /// @param preMask Whether to replace mask color with 0 alpha (necessary for loading indexed to 32-bit image) + static SDL_Palette* DefaultPaletteToSDL(bool preMask = false); private: /// Enumeration for loading BITMAPs by bit depth. NOTE: This can't be lower down because s_LoadedBitmaps relies on this definition. From cbf5ca86f63975fdd4d0cda1c43e373881ccfbbb Mon Sep 17 00:00:00 2001 From: HeliumAnt Date: Thu, 1 Jan 2026 14:56:15 +0100 Subject: [PATCH 2/4] skip unecessary format conversion on load --- Source/Managers/ActivityMan.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Source/Managers/ActivityMan.cpp b/Source/Managers/ActivityMan.cpp index 95f8ebb010..65a2d41b61 100644 --- a/Source/Managers/ActivityMan.cpp +++ b/Source/Managers/ActivityMan.cpp @@ -206,6 +206,7 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) { SDL_Palette* palette = ContentFile::DefaultPaletteToSDL(); SDL_SetSurfacePalette(image, palette); + SDL_SetSurfaceColorKey(image, false, 0); bool result = IMG_SavePNG_IO(image, stream, false); SDL_FlushIO(stream); @@ -310,11 +311,16 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) { SDL_Surface* image = stream ? IMG_LoadPNG_IO(stream) : nullptr; SDL_CloseIO(stream); + int bitDepth = SDL_GetPixelFormatDetails(image->format)->bits_per_pixel; SDL_Palette* palette = ContentFile::DefaultPaletteToSDL(); - SDL_Surface* newImage = SDL_ConvertSurfaceAndColorspace(image, SDL_PIXELFORMAT_INDEX8, palette, SDL_COLORSPACE_UNKNOWN, 0); + if (bitDepth != 8) { + SDL_Surface* newImage = SDL_ConvertSurfaceAndColorspace(image, SDL_PIXELFORMAT_INDEX8, palette, SDL_COLORSPACE_UNKNOWN, 0); + SDL_DestroySurface(image); + image = newImage; + } else { + SDL_SetSurfacePalette(image, palette); + } SDL_DestroyPalette(palette); - SDL_DestroySurface(image); - image = newImage; free(buffer); return image; From 99d4545b678032bed0df15557c9f216912377ebd Mon Sep 17 00:00:00 2001 From: HeliumAnt Date: Thu, 1 Jan 2026 15:00:45 +0100 Subject: [PATCH 3/4] remove debug print --- Source/System/ContentFile.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Source/System/ContentFile.cpp b/Source/System/ContentFile.cpp index 4d1df524fd..754afcdc26 100644 --- a/Source/System/ContentFile.cpp +++ b/Source/System/ContentFile.cpp @@ -63,8 +63,8 @@ void ContentFile::FreeAllLoaded() { int ContentFile::ReadProperty(const std::string_view& propName, Reader& reader) { StartPropertyList(return Serializable::ReadProperty(propName, reader)); - MatchForwards("FilePath") - MatchProperty("Path", { SetDataPath(reader.ReadPropValue()); }); + MatchForwards("FilePath"); + MatchProperty("Path", { SetDataPath(reader.ReadPropValue()); }); MatchProperty("IsMemoryPNG", { reader >> m_IsMemoryPNG; }); EndPropertyList; @@ -199,10 +199,8 @@ void ContentFile::ReadAndStoreBMPFileInfo(FILE* imageFile) { void ContentFile::ManuallyLoadDataPNG(const std::string& filePath, SDL_Surface* surface) { s_MemoryPNGs[filePath] = surface; - std::cout << filePath << " "; int bitDepth = SDL_GetPixelFormatDetails(surface->format)->bits_per_pixel; - std::cout << bitDepth << std::endl; BITMAP* bitmap = create_bitmap_ex(bitDepth, surface->w, surface->h); // Allegro doesn't align lines, SDL does 4byte alignment @@ -401,7 +399,7 @@ FMOD::Sound* ContentFile::LoadAndReleaseSound(bool abortGameForInvalidSound, boo } if (!System::PathExistsCaseSensitive(m_DataPath)) { bool foundAltExtension = false; - for (const std::string& altFileExtension: c_SupportedAudioFormats) { + for (const char* altFileExtension: c_SupportedAudioFormats) { const std::string altDataPathToLoad = m_DataPathWithoutExtension + altFileExtension; if (System::PathExistsCaseSensitive(altDataPathToLoad)) { g_ConsoleMan.AddLoadWarningLogExtensionMismatchEntry(m_DataPath, m_FormattedReaderPosition, altFileExtension); From 88cde741fb50b1bad156a3cc63ba76a0235c8309 Mon Sep 17 00:00:00 2001 From: HeliumAnt Date: Thu, 1 Jan 2026 15:35:08 +0100 Subject: [PATCH 4/4] remove matchforwards semicolon --- Source/System/ContentFile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/System/ContentFile.cpp b/Source/System/ContentFile.cpp index 754afcdc26..3d7b9b4b8f 100644 --- a/Source/System/ContentFile.cpp +++ b/Source/System/ContentFile.cpp @@ -63,7 +63,7 @@ void ContentFile::FreeAllLoaded() { int ContentFile::ReadProperty(const std::string_view& propName, Reader& reader) { StartPropertyList(return Serializable::ReadProperty(propName, reader)); - MatchForwards("FilePath"); + MatchForwards("FilePath") MatchProperty("Path", { SetDataPath(reader.ReadPropValue()); }); MatchProperty("IsMemoryPNG", { reader >> m_IsMemoryPNG; });