Skip to content

Commit 2abaff3

Browse files
committed
Parallelized PNG writing
1 parent 682e56b commit 2abaff3

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

Source/Managers/ActivityMan.cpp

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
159159
delete sceneLayerInfos;
160160
return;
161161
}
162-
162+
163163
std::stringstream* mainStream = static_cast<std::stringstream*>(mainWriter->GetStream());
164164
std::stringstream* indexStream = static_cast<std::stringstream*>(indexWriter->GetStream());
165165
mainStream->flush();
@@ -178,40 +178,41 @@ bool ActivityMan::SaveCurrentGame(const std::string& fileName) {
178178
zipWriteInFileInZip(zippedSaveFile, mainStreamView.data(), mainStreamView.size());
179179
zipCloseFileInZip(zippedSaveFile);
180180

181-
for (const SceneLayerInfo& layerInfo : *sceneLayerInfos)
182-
{
183-
// Save png into a memory buffer
184-
SDL_IOStream* stream = SDL_IOFromDynamicMem();
185-
SDL_Surface* image = SDL_CreateSurfaceFrom(layerInfo.bitmap->w, layerInfo.bitmap->h, SDL_PIXELFORMAT_INDEX8, layerInfo.bitmap->dat, layerInfo.bitmap->w);
181+
std::for_each(std::execution::par_unseq,
182+
sceneLayerInfos->begin(), sceneLayerInfos->end(),
183+
[&](const SceneLayerInfo& layerInfo) {
184+
// Save png into a memory buffer
185+
SDL_IOStream* stream = SDL_IOFromDynamicMem();
186+
SDL_Surface* image = SDL_CreateSurfaceFrom(layerInfo.bitmap->w, layerInfo.bitmap->h, SDL_PIXELFORMAT_INDEX8, layerInfo.bitmap->dat, layerInfo.bitmap->w);
186187

187-
SDL_Palette* palette = ContentFile::DefaultPaletteToSDL();
188-
SDL_SetSurfacePalette(image, palette);
188+
SDL_Palette* palette = ContentFile::DefaultPaletteToSDL();
189+
SDL_SetSurfacePalette(image, palette);
189190

190-
bool result = IMG_SavePNG_IO(image, stream, false);
191-
SDL_FlushIO(stream);
191+
bool result = IMG_SavePNG_IO(image, stream, false);
192+
SDL_FlushIO(stream);
192193

193-
SDL_DestroyPalette(palette);
194-
SDL_DestroySurface(image);
194+
SDL_DestroyPalette(palette);
195+
SDL_DestroySurface(image);
195196

196-
if (!result) {
197-
g_ConsoleMan.PrintString("ERROR: Failed to save scenelayers to PNG!");
198-
continue;
199-
}
197+
if (!result) {
198+
g_ConsoleMan.PrintString("ERROR: Failed to save scenelayers to PNG!");
199+
return;
200+
}
200201

201-
// Actually get the memory
202-
void* buffer = SDL_GetPointerProperty(SDL_GetIOProperties(stream), SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, nullptr);
203-
size_t size = static_cast<size_t>(SDL_GetIOSize(stream));
204-
if (!buffer || size < 0) {
205-
g_ConsoleMan.PrintString("ERROR: Failed to save scenelayers to PNG!");
206-
continue;
207-
}
202+
// Actually get the memory
203+
void* buffer = SDL_GetPointerProperty(SDL_GetIOProperties(stream), SDL_PROP_IOSTREAM_DYNAMIC_MEMORY_POINTER, nullptr);
204+
size_t size = static_cast<size_t>(SDL_GetIOSize(stream));
205+
if (!buffer || size < 0) {
206+
g_ConsoleMan.PrintString("ERROR: Failed to save scenelayers to PNG!");
207+
return;
208+
}
208209

209-
zipOpenNewFileInZip(zippedSaveFile, ("Save " + layerInfo.name + ".png").c_str(), &zfi, nullptr, 0, nullptr, 0, nullptr, MZ_COMPRESS_METHOD_STORE, MZ_COMPRESS_LEVEL_FAST);
210-
zipWriteInFileInZip(zippedSaveFile, static_cast<const char*>(buffer), size);
211-
zipCloseFileInZip(zippedSaveFile);
210+
zipOpenNewFileInZip(zippedSaveFile, ("Save " + layerInfo.name + ".png").c_str(), &zfi, nullptr, 0, nullptr, 0, nullptr, MZ_COMPRESS_METHOD_STORE, MZ_COMPRESS_LEVEL_FAST);
211+
zipWriteInFileInZip(zippedSaveFile, static_cast<const char*>(buffer), size);
212+
zipCloseFileInZip(zippedSaveFile);
212213

213-
SDL_CloseIO(stream);
214-
}
214+
SDL_CloseIO(stream);
215+
});
215216

216217
zipClose(zippedSaveFile, fileName.c_str());
217218

@@ -301,8 +302,7 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
301302
}
302303
}
303304

304-
if (!unzipFileIntoBuffer("Save.ini"))
305-
{
305+
if (!unzipFileIntoBuffer("Save.ini")) {
306306
RTEError::ShowMessageBox("Game loading failed! This save looks invalid or corrupted.");
307307
return false;
308308
}

0 commit comments

Comments
 (0)