Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Source/Managers/ActivityMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
27 changes: 17 additions & 10 deletions Source/System/ContentFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; });

Expand Down Expand Up @@ -249,8 +249,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());
Expand Down Expand Up @@ -302,11 +302,19 @@ void ContentFile::GetAsAnimation(std::vector<BITMAP*>& vectorToFill, int frameCo
}
}
}
SDL_Palette* ContentFile::DefaultPaletteToSDL() {
SDL_Palette* ContentFile::DefaultPaletteToSDL(bool preMask) {
SDL_Palette* palette = SDL_CreatePalette(256);
std::array<SDL_Color, 256> 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;
Expand All @@ -330,7 +338,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);
Expand Down Expand Up @@ -391,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);
Expand Down Expand Up @@ -443,15 +451,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<unsigned char*>(newImage->pixels) + y * newImage->pitch, newImage->w * SDL_GetPixelFormatDetails(newImage->format)->bytes_per_pixel);
memcpy(newBitmap->line[y], static_cast<unsigned char*>(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));
Expand Down
3 changes: 2 additions & 1 deletion Source/System/ContentFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down