diff --git a/src/gui/core/canvas.cpp b/src/gui/core/canvas.cpp index f841ad449e506..33771253b4c5a 100644 --- a/src/gui/core/canvas.cpp +++ b/src/gui/core/canvas.cpp @@ -1068,7 +1068,8 @@ void image_shape::draw( return; } - image_ = image::get_texture(name); + // NOTE: if we need a key to specify NN scaling it can be added later. + image_ = image::get_texture(name, image::LINEAR); if(!image_) { ERR_GUI_D << "Image: '" << name << "' not found and won't be drawn." << std::endl; diff --git a/src/image.cpp b/src/image.cpp index d692a171723d1..b38670df8e1fb 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -31,6 +31,7 @@ #include "preferences/general.hpp" #include "serialization/string_utils.hpp" #include "sdl/rect.hpp" +#include "sdl/render_utils.hpp" #include "sdl/texture.hpp" #include "utils/general.hpp" @@ -180,7 +181,9 @@ image::image_cache * Texture caches. * Note that the latter two are temporary and should be removed once we have OGL and shader support. */ -image::texture_cache +using texture_cache_map = std::map; + +texture_cache_map textures_, textures_hexed_, texture_tod_colored_; @@ -1271,6 +1274,15 @@ bool update_from_preferences() * are so different. Might move this to a file of its own in the future. */ +/** Sets the texture scale quality hint. Must be called *before* creating textures! */ +static void set_scale_quality_pre_texture_creation(SCALE_QUALITY quality) +{ + static const std::string n_scale_str = "nearest"; + static const std::string l_scale_str = "linear"; + + set_texture_scale_quality(quality == NEAREST ? n_scale_str : l_scale_str); +} + /** Loads a new texture directly from disk. */ static texture create_texture_from_file(const image::locator& loc) { @@ -1416,8 +1428,13 @@ static texture create_texture_post_surface_op(const image::locator& i_locator, T return texture(surf); } -/** Returns a texture for the corresponding image. */ texture get_texture(const image::locator& i_locator, TYPE type) +{ + return get_texture(i_locator, NEAREST, type); +} + +/** Returns a texture for the corresponding image. */ +texture get_texture(const image::locator& i_locator, SCALE_QUALITY quality, TYPE type) { texture res; @@ -1436,13 +1453,13 @@ texture get_texture(const image::locator& i_locator, TYPE type) switch(type) { case HEXED: - cache = &textures_hexed_; + cache = &textures_hexed_[quality]; break; case TOD_COLORED: - cache = &texture_tod_colored_; + cache = &texture_tod_colored_[quality]; break; default: - cache = &textures_; + cache = &textures_[quality]; } // @@ -1464,10 +1481,12 @@ texture get_texture(const image::locator& i_locator, TYPE type) } // - // No texture was cached. In that case, create a new one. The explicit cases require special + // No texture was cached. In that case, create a new one. The explicit cases require special // handling with surfaces in order to generate the desired effect. This shouldn't be the case // once we get OGL and shader support. // + set_scale_quality_pre_texture_creation(quality); + switch(type) { case TOD_COLORED: case HEXED: diff --git a/src/image.hpp b/src/image.hpp index 3fae4c7dbd87f..70e0fbece5276 100644 --- a/src/image.hpp +++ b/src/image.hpp @@ -190,10 +190,13 @@ namespace image { /// BRIGHTENED : same as TOD_COLORED but also brightened enum TYPE { UNSCALED, SCALED_TO_ZOOM, HEXED, SCALED_TO_HEX, TOD_COLORED, BRIGHTENED}; + enum SCALE_QUALITY { NEAREST, LINEAR }; + ///function to get the surface corresponding to an image. surface get_image(const locator& i_locator, TYPE type=UNSCALED); texture get_texture(const image::locator& i_locator, TYPE type = UNSCALED); + texture get_texture(const image::locator& i_locator, SCALE_QUALITY quality, TYPE type = UNSCALED); ///function to get the surface corresponding to an image. ///after applying the lightmap encoded in ls diff --git a/src/sdl/render_utils.hpp b/src/sdl/render_utils.hpp index 454d6c05707d1..6ef4bf00a169a 100644 --- a/src/sdl/render_utils.hpp +++ b/src/sdl/render_utils.hpp @@ -127,3 +127,14 @@ inline void set_texture_blend_mode(texture& t, SDL_BlendMode mode) { SDL_SetTextureBlendMode(t, mode); } + +/** + * Sets the texture scale quality. Note this should be called *before* a texture + * is created, since the hint has no effect on existing textures or render ops. + * + * @param value The scaling mode. Use either 'linear' or 'nearest'. + */ +inline void set_texture_scale_quality(const std::string& value) +{ + SDL_SetHintWithPriority(SDL_HINT_RENDER_SCALE_QUALITY, value.c_str(), SDL_HINT_OVERRIDE); +} diff --git a/src/sdl/window.cpp b/src/sdl/window.cpp index c9e546208a490..896490cd67913 100644 --- a/src/sdl/window.cpp +++ b/src/sdl/window.cpp @@ -53,9 +53,6 @@ window::window(const std::string& title, // Set default blend mode to blend. SDL_SetRenderDrawBlendMode(*this, SDL_BLENDMODE_BLEND); - // Use linear scaling when rendering, if applicable. - SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); - fill(0,0,0); render();