Skip to content

Commit 49b6221

Browse files
committed
purge fmem and use SDL png functionality instead (much nicer!)
1 parent 5fff423 commit 49b6221

File tree

120 files changed

+68
-3385
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+68
-3385
lines changed

RTEA.vcxproj

Lines changed: 15 additions & 15 deletions
Large diffs are not rendered by default.

Source/Managers/ActivityMan.cpp

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
#include "zip.h"
2929
#include "unzip.h"
3030

31-
#include "fmem.h"
31+
#include "SDL3/SDL_surface.h"
32+
#include <SDL3_image/SDL_image.h>
3233

3334
using namespace RTE;
3435

@@ -150,26 +151,37 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
150151

151152
for (const SceneLayerInfo& layerInfo : *sceneLayerInfos)
152153
{
153-
// A bit of a finicky workaround, but to save a png to memory we create a memory stream and send that into allegro to save into
154-
fmem memStructure;
155-
fmem_init(&memStructure);
154+
// Save png into a memory buffer
155+
SDL_IOStream* stream = SDL_IOFromDynamicMem();
156+
SDL_Surface* image = SDL_CreateSurfaceFrom(layerInfo.bitmap->w, layerInfo.bitmap->h, SDL_PIXELFORMAT_INDEX8, layerInfo.bitmap->dat, layerInfo.bitmap->w);
156157

157-
// Save the png to our memory stream
158-
FILE* stream = fmem_open(&memStructure, "w");
159-
save_stream_png(stream, layerInfo.bitmap.get(), palette);
160-
fflush(stream);
158+
SDL_Palette* palette = ContentFile::DefaultPaletteToSDL();
159+
SDL_SetSurfacePalette(image, palette);
160+
161+
bool result = IMG_SavePNG_IO(image, stream, false);
162+
SDL_FlushIO(stream);
163+
164+
SDL_DestroyPalette(palette);
165+
SDL_DestroySurface(image);
166+
167+
if (!result) {
168+
g_ConsoleMan.PrintString("ERROR: Failed to save scenelayers to PNG!");
169+
continue;
170+
}
161171

162172
// Actually get the memory
163-
void* buffer;
164-
size_t size;
165-
fmem_mem(&memStructure, &buffer, &size);
173+
void* buffer = SDL_GetPointerProperty(SDL_GetIOProperties(stream), SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, nullptr);
174+
size_t size = static_cast<size_t>(SDL_GetIOSize(stream));
175+
if (!buffer || size < 0) {
176+
g_ConsoleMan.PrintString("ERROR: Failed to save scenelayers to PNG!");
177+
continue;
178+
}
166179

167180
zipOpenNewFileInZip(zippedSaveFile, ("Save " + layerInfo.name + ".png").c_str(), &zfi, nullptr, 0, nullptr, 0, nullptr, Z_DEFLATED, defaultCompression);
168181
zipWriteInFileInZip(zippedSaveFile, static_cast<const char*>(buffer), size);
169182
zipCloseFileInZip(zippedSaveFile);
170183

171-
fclose(stream);
172-
fmem_term(&memStructure);
184+
SDL_CloseIO(stream);
173185
}
174186

175187
zipClose(zippedSaveFile, fileName.c_str());
@@ -219,6 +231,30 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
219231
unzCloseCurrentFile(zippedSaveFile);
220232
};
221233

234+
auto loadMemPngIntoBitmap = [](void* buffer, size_t size) {
235+
BITMAP* bitmap = nullptr; // just to help the compiler deduce the type, we can't early-return nullptr or it thiks we return std::nullptr_t
236+
237+
SDL_IOStream* stream = SDL_IOFromConstMem(buffer, size);
238+
SDL_Surface* image = stream ? IMG_LoadPNG_IO(stream) : nullptr;
239+
if (!image) {
240+
return bitmap;
241+
}
242+
243+
SDL_CloseIO(stream);
244+
245+
bitmap = create_bitmap_ex(SDL_BITSPERPIXEL(image->format), image->w, image->h);
246+
247+
// Allegro doesn't align lines, SDL does 4byte alignment
248+
for (int y = 0; y < image->h; y++) {
249+
memcpy(bitmap->line[y],
250+
static_cast<unsigned char*>(image->pixels) + image->pitch * y,
251+
image->w * SDL_BYTESPERPIXEL(image->format));
252+
}
253+
254+
SDL_DestroySurface(image);
255+
return bitmap;
256+
};
257+
222258
unzipFileIntoBuffer("Save.ini");
223259

224260
Reader reader(std::make_unique<std::istringstream>(buffer), filePath + "/Save.ini", true, nullptr, false);
@@ -253,20 +289,20 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
253289
get_palette(palette);
254290

255291
unzipFileIntoBuffer("Save Mat.png");
256-
ContentFile::ManuallyLoadDataBitmap(filePath + "/Save Mat.png", load_memory_png(buffer, info.uncompressed_size, palette));
292+
ContentFile::ManuallyLoadDataBitmap(filePath + "/Save Mat.png", loadMemPngIntoBitmap(buffer, info.uncompressed_size));
257293
free(buffer);
258294

259295
unzipFileIntoBuffer("Save FG.png");
260-
ContentFile::ManuallyLoadDataBitmap(filePath + "/Save FG.png", load_memory_png(buffer, info.uncompressed_size, palette));
296+
ContentFile::ManuallyLoadDataBitmap(filePath + "/Save FG.png", loadMemPngIntoBitmap(buffer, info.uncompressed_size));
261297
free(buffer);
262298

263299
unzipFileIntoBuffer("Save BG.png");
264-
ContentFile::ManuallyLoadDataBitmap(filePath + "/Save BG.png", load_memory_png(buffer, info.uncompressed_size, palette));
300+
ContentFile::ManuallyLoadDataBitmap(filePath + "/Save BG.png", loadMemPngIntoBitmap(buffer, info.uncompressed_size));
265301
free(buffer);
266302

267303
for (int i = 0; i < numberOfTeams; ++i) {
268304
unzipFileIntoBuffer(std::format("Save UST%i.png", i));
269-
ContentFile::ManuallyLoadDataBitmap(filePath + std::format("/Save UST%i", i), load_memory_png(buffer, info.uncompressed_size, palette));
305+
ContentFile::ManuallyLoadDataBitmap(filePath + std::format("/Save UST%i", i), loadMemPngIntoBitmap(buffer, info.uncompressed_size));
270306
free(buffer);
271307
}
272308

external/sources/fmem/.appveyor.yml

Lines changed: 0 additions & 62 deletions
This file was deleted.

external/sources/fmem/.cmake/FindCriterion.cmake

Lines changed: 0 additions & 27 deletions
This file was deleted.

external/sources/fmem/.github/workflows/cmake-multi-platform.yml

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)