Save screenshots as JPGs when building with SDL_Image 2.0.2 or newer

This functionality was added in that release. Since it's only 4 months old, it's
too early to make minimum; it won't be available on some Linux distros yet.

There's currently no way to choose between JPG or PNG files. That can (and should)
be added later. For now, it defaults to JPG files if SDL_Image is of the appropriate
version.

I've also left the BMP fallback path, but that should never be reached now, since
PNG saving is always available

Closes #1448.
This commit is contained in:
Charles Dang 2018-03-15 03:02:01 +11:00
parent dfc42e8a8d
commit 686aca2667
4 changed files with 27 additions and 3 deletions

View file

@ -61,6 +61,8 @@
* Fixed regression in 1.13.11 where completed events could fire again when
reloading a save.
* Fixed PNG images with an embedded palette displaying incorrectly.
* Screenshots are now saved as JPG files by default. If the game has not
been built with the appropriate libraries, it will fall back to a PNG file.
### Music and sound effects
* Updated a few UI sounds.

View file

@ -265,6 +265,11 @@ version_table_manager::version_table_manager()
features.back().enabled = true;
#endif
features.emplace_back(N_("feature^JPG screenshots"));
#if SDL_IMAGE_VERSION_ATLEAST(2, 0, 2)
features.back().enabled = true;
#endif
features.emplace_back(N_("feature^Lua console completion"));
#ifdef HAVE_HISTORY
features.back().enabled = true;

View file

@ -34,6 +34,8 @@
#include "utils/functional.hpp"
#include <SDL_image.h>
#include <cassert>
#include <ios>
@ -57,7 +59,13 @@ template<typename TFunc>
void make_screenshot(const std::string& name, const TFunc& func)
{
std::string filename = filesystem::get_screenshot_dir() + "/" + name + "_";
// TODO: there should be a way to configure which of these is used by default.
#if SDL_IMAGE_VERSION_ATLEAST(2, 0, 2)
const std::string ext = ".jpg";
#else
const std::string ext = ".png";
#endif
filename = filesystem::get_next_filename(filename, ext);
const bool res = func(filename);

View file

@ -1331,14 +1331,23 @@ bool save_image(const surface& surf, const std::string& filename)
return false;
}
if(!filesystem::ends_with(filename, ".bmp")) {
LOG_DP << "Writing a png image to " << filename << std::endl;
#if SDL_IMAGE_VERSION_ATLEAST(2, 0, 2)
if(filesystem::ends_with(filename, ".jpg")) {
LOG_DP << "Writing a JPG image to " << filename << std::endl;
const int err = IMG_SaveJPG_RW(surf, filesystem::make_write_RWops(filename).release(), true, 50); // SDL takes ownership of the RWops
return err == 0;
}
#endif
if(filesystem::ends_with(filename, ".png")) {
LOG_DP << "Writing a PNG image to " << filename << std::endl;
const int err = IMG_SavePNG_RW(surf, filesystem::make_write_RWops(filename).release(), true); // SDL takes ownership of the RWops
return err == 0;
}
LOG_DP << "Writing a bmp image to " << filename << std::endl;
LOG_DP << "Writing a BMP image to " << filename << std::endl;
return SDL_SaveBMP(surf, filename.c_str()) == 0;
}