Use PHYSFS for saving screenshots

1164-fix
melex750 2020-01-22 12:12:24 -05:00 committed by Mateusz Przybył
parent f449d9c800
commit fb9fa49ce8
2 changed files with 21 additions and 8 deletions

View File

@ -22,6 +22,7 @@
#include "common/make_unique.h"
#include "common/resources/outputstream.h"
#include "common/resources/resourcemanager.h"
#include "math/func.h"
@ -93,8 +94,8 @@ bool PNGSaveSurface(const char *filename, SDL_Surface *surf)
PNG_ERROR = "";
/* Opening output file */
FILE *fp = fopen(filename, "wb");
if (fp == nullptr)
COutputStream ostr(filename);
if (!ostr.is_open())
{
PNG_ERROR = std::string("Could not open file '") + std::string(filename) + std::string("' for saving");
return false;
@ -104,7 +105,7 @@ bool PNGSaveSurface(const char *filename, SDL_Surface *surf)
png_structp pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, PNGUserError, nullptr);
if (pngPtr == nullptr)
{
fclose(fp);
ostr.close();
return false;
}
@ -113,18 +114,30 @@ bool PNGSaveSurface(const char *filename, SDL_Surface *surf)
{
png_destroy_write_struct(&pngPtr, static_cast<png_infopp>(nullptr));
PNG_ERROR = "png_create_info_struct() error!";
fclose(fp);
ostr.close();
return false;
}
if (setjmp(png_jmpbuf(pngPtr)))
{
png_destroy_write_struct(&pngPtr, &infoPtr);
fclose(fp);
ostr.close();
return false;
}
png_init_io(pngPtr, fp);
png_set_write_fn(
pngPtr, static_cast<std::ostream*>(&ostr),
[](png_structp pngPtr, png_bytep data, png_size_t length)
{
auto* file = static_cast<std::ostream*>(png_get_io_ptr(pngPtr));
file->write(reinterpret_cast<char*>(data), length);
},
[](png_structp pngPtr)
{
auto* file = static_cast<std::ostream*>(png_get_io_ptr(pngPtr));
file->flush();
}
);
int colortype = PNGColortypeFromSurface(surf);
png_set_IHDR(pngPtr, infoPtr, surf->w, surf->h, 8, colortype, PNG_INTERLACE_NONE,
@ -141,7 +154,7 @@ bool PNGSaveSurface(const char *filename, SDL_Surface *surf)
png_write_end(pngPtr, infoPtr);
png_destroy_write_struct(&pngPtr, &infoPtr);
fclose(fp);
ostr.close();
return true;
}

View File

@ -4753,7 +4753,7 @@ bool CRobotMain::IOWriteScene(std::string filename, std::string filecbot, std::s
m_engine->SetScreenshotMode(true);
m_engine->Render(); // update (but don't show, we're not swapping buffers here!)
m_engine->WriteScreenShot(CResourceManager::GetSaveLocation() + "/" + filescreenshot); //TODO: Use PHYSFS?
m_engine->WriteScreenShot(filescreenshot);
m_shotSaving++;
m_engine->SetScreenshotMode(false);