Skip to content

Commit 4dfefb8

Browse files
committed
only add alpha to palette on 8to32 conversion
1 parent 4d461c6 commit 4dfefb8

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

Source/System/ContentFile.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ 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")
67-
MatchProperty("Path", { SetDataPath(reader.ReadPropValue()); });
66+
MatchForwards("FilePath")
67+
MatchProperty("Path", { SetDataPath(reader.ReadPropValue()); });
6868
MatchProperty("IsMemoryPNG", { reader >> m_IsMemoryPNG; });
6969

7070
EndPropertyList;
@@ -199,8 +199,10 @@ void ContentFile::ReadAndStoreBMPFileInfo(FILE* imageFile) {
199199

200200
void ContentFile::ManuallyLoadDataPNG(const std::string& filePath, SDL_Surface* surface) {
201201
s_MemoryPNGs[filePath] = surface;
202+
std::cout << filePath << " ";
202203

203204
int bitDepth = SDL_GetPixelFormatDetails(surface->format)->bits_per_pixel;
205+
std::cout << bitDepth << std::endl;
204206
BITMAP* bitmap = create_bitmap_ex(bitDepth, surface->w, surface->h);
205207

206208
// Allegro doesn't align lines, SDL does 4byte alignment
@@ -249,8 +251,8 @@ BITMAP* ContentFile::GetAsBitmap(int conversionMode, bool storeBitmap, const std
249251
SDL_DestroySurface(surface);
250252
s_MemoryPNGs.erase(dataPathToLoad);
251253
}
252-
}
253-
254+
}
255+
254256
if (returnBitmap == nullptr) {
255257
if (!System::PathExistsCaseSensitive(dataPathToLoad)) {
256258
const std::string dataPathWithoutExtension = dataPathToLoad.substr(0, dataPathToLoad.length() - m_DataPathExtension.length());
@@ -302,11 +304,19 @@ void ContentFile::GetAsAnimation(std::vector<BITMAP*>& vectorToFill, int frameCo
302304
}
303305
}
304306
}
305-
SDL_Palette* ContentFile::DefaultPaletteToSDL() {
307+
SDL_Palette* ContentFile::DefaultPaletteToSDL(bool preMask) {
306308
SDL_Palette* palette = SDL_CreatePalette(256);
307309
std::array<SDL_Color, 256> paletteColor;
308310
const PALETTE& defaultPalette = g_FrameMan.GetDefaultPalette();
309-
paletteColor[0] = {.r = 0, .g = 0, .b = 0, .a = 0};
311+
if (preMask) {
312+
paletteColor[0] = {.r = 0, .g = 0, .b = 0, .a = 0};
313+
} else {
314+
paletteColor[0] = {.r = defaultPalette[0].r,
315+
.g = defaultPalette[0].g,
316+
.b = defaultPalette[0].b,
317+
.a = 255
318+
};
319+
}
310320
for (size_t i = 1; i < paletteColor.size(); ++i) {
311321
paletteColor[i].r = defaultPalette[i].r;
312322
paletteColor[i].g = defaultPalette[i].g;
@@ -330,7 +340,7 @@ SDL_Surface* ContentFile::LoadImageAsSurface(int conversionMode, const std::stri
330340
image = newImage;
331341
bitDepth = 8;
332342
} else if (bitDepth != 8 || convert8To32) {
333-
SDL_Palette* palette = DefaultPaletteToSDL();
343+
SDL_Palette* palette = DefaultPaletteToSDL(true);
334344
if (SDL_GetPixelFormatDetails(image->format)->bits_per_pixel == 8) {
335345
SDL_SetSurfacePalette(image, palette);
336346
SDL_SetSurfaceColorKey(image, true, 0);
@@ -443,15 +453,14 @@ void ContentFile::ReloadBitmap(const std::string& filePath, int conversionMode)
443453

444454
SDL_Surface* newImage = LoadImageAsSurface(conversionMode, filePath);
445455

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

449458
// 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.
450459
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);
460+
memcpy(newBitmap->line[y], static_cast<unsigned char*>(newImage->pixels) + y * newImage->pitch, newImage->w * SDL_GetPixelFormatDetails(newImage->format)->bytes_per_pixel);
452461
}
453462

454-
//AddAlphaChannel(newBitmap);
463+
// AddAlphaChannel(newBitmap);
455464
BITMAP swap;
456465

457466
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)