From 13a873df01a0b8d73d000a1a0c4a2bb60593cee5 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Fri, 14 Nov 2014 03:50:51 -0500 Subject: [PATCH] implement image prefs using function pointers, for efficiency --- src/gui/dialogs/advanced_graphics_options.cpp | 2 + src/image.cpp | 39 +++++++++++++------ src/image.hpp | 3 ++ src/wesnoth.cpp | 6 +++ 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/gui/dialogs/advanced_graphics_options.cpp b/src/gui/dialogs/advanced_graphics_options.cpp index 1007d077c56..799949dc5e1 100644 --- a/src/gui/dialogs/advanced_graphics_options.cpp +++ b/src/gui/dialogs/advanced_graphics_options.cpp @@ -106,6 +106,8 @@ void tadvanced_graphics_options::scale_button_callback(std::string pref_id, SCAL ttoggle_button * b = &find_widget(&window, pref_id + "_" + SCALING_ALGORITHM_to_string(static_cast(x)), false); b->set_value(x == me); } + + image::update_from_preferences(); } void tadvanced_graphics_options::setup_scale_case(const std::string & i, twindow & window) diff --git a/src/image.cpp b/src/image.cpp index be0b01b819c..24c1cfd5bdf 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -42,6 +42,7 @@ #include "SDL_image.h" +#include #include #include @@ -172,6 +173,11 @@ std::vector team_colors; int zoom = tile_size; int cached_zoom = 0; +/** Algorithm choices */ +typedef boost::function scaling_function; +scaling_function scale_to_zoom_func; +scaling_function scale_to_hex_func; + } // end anon namespace namespace image { @@ -801,12 +807,7 @@ static surface get_scaled_to_hex(const locator& i_locator) //return scale_surface(img, zoom, zoom); if (!img.null()) { - gui2::tadvanced_graphics_options::SCALING_ALGORITHM algo = gui2::tadvanced_graphics_options::LINEAR; - try { - algo = gui2::tadvanced_graphics_options::string_to_SCALING_ALGORITHM(preferences::get("scale_hex")); - } catch (bad_enum_cast &) {} - - return scale_surface_algorithm(img, zoom, zoom, algo); + return scale_to_hex_func(img, zoom, zoom); } else { return surface(NULL); } @@ -826,12 +827,7 @@ static surface get_scaled_to_zoom(const locator& i_locator) surface res(get_image(i_locator, UNSCALED)); // For some reason haloes seems to have invalid images, protect against crashing if(!res.null()) { - gui2::tadvanced_graphics_options::SCALING_ALGORITHM algo = gui2::tadvanced_graphics_options::LINEAR; - try { - algo = gui2::tadvanced_graphics_options::string_to_SCALING_ALGORITHM(preferences::get("scale_zoom")); - } catch (bad_enum_cast &) {} - - return scale_surface_algorithm(res, ((res.get()->w * zoom) / tile_size), ((res.get()->h * zoom) / tile_size), algo); + return scale_to_zoom_func(res, ((res.get()->w * zoom) / tile_size), ((res.get()->h * zoom) / tile_size)); } else { return surface(NULL); } @@ -1298,5 +1294,24 @@ std::string describe_versions() return ss.str(); } +bool update_from_preferences() +{ + gui2::tadvanced_graphics_options::SCALING_ALGORITHM algo = gui2::tadvanced_graphics_options::LINEAR; + try { + algo = gui2::tadvanced_graphics_options::string_to_SCALING_ALGORITHM(preferences::get("scale_hex")); + } catch (bad_enum_cast &) {} + + scale_to_hex_func = boost::bind(& scale_surface_algorithm, _1, _2, _3, algo); + + algo = gui2::tadvanced_graphics_options::LINEAR; + try { + algo = gui2::tadvanced_graphics_options::string_to_SCALING_ALGORITHM(preferences::get("scale_zoom")); + } catch (bad_enum_cast &) {} + + scale_to_zoom_func = boost::bind(& scale_surface_algorithm, _1, _2, _3, algo); + + return true; +} + } // end namespace image diff --git a/src/image.hpp b/src/image.hpp index 96453ea63bd..20f596cda26 100644 --- a/src/image.hpp +++ b/src/image.hpp @@ -248,6 +248,9 @@ namespace image { std::string describe_versions(); + /// initialize any private data, e.g. algorithm choices from preferences + bool update_from_preferences(); + void save_image(const locator& i_locator, const std::string& outfile); void save_image(const surface& surf, const std::string& outfile); } diff --git a/src/wesnoth.cpp b/src/wesnoth.cpp index b9d43fb0465..758a80a0ba7 100644 --- a/src/wesnoth.cpp +++ b/src/wesnoth.cpp @@ -572,6 +572,12 @@ static int do_gameloop(const std::vector& args) return 1; } + res = image::update_from_preferences(); + if(res == false) { + std::cerr << "could not initialize image preferences\n"; + return 1; + } + if(preferences::joystick_support_enabled()) { res = game->init_joystick(); if(res == false) {