implement image prefs using function pointers, for efficiency

This commit is contained in:
Chris Beck 2014-11-14 03:50:51 -05:00
parent b309afde7e
commit 13a873df01
4 changed files with 38 additions and 12 deletions

View file

@ -106,6 +106,8 @@ void tadvanced_graphics_options::scale_button_callback(std::string pref_id, SCAL
ttoggle_button * b = &find_widget<ttoggle_button>(&window, pref_id + "_" + SCALING_ALGORITHM_to_string(static_cast<SCALING_ALGORITHM>(x)), false); ttoggle_button * b = &find_widget<ttoggle_button>(&window, pref_id + "_" + SCALING_ALGORITHM_to_string(static_cast<SCALING_ALGORITHM>(x)), false);
b->set_value(x == me); b->set_value(x == me);
} }
image::update_from_preferences();
} }
void tadvanced_graphics_options::setup_scale_case(const std::string & i, twindow & window) void tadvanced_graphics_options::setup_scale_case(const std::string & i, twindow & window)

View file

@ -42,6 +42,7 @@
#include "SDL_image.h" #include "SDL_image.h"
#include <boost/bind.hpp>
#include <boost/functional/hash.hpp> #include <boost/functional/hash.hpp>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
@ -172,6 +173,11 @@ std::vector<std::string> team_colors;
int zoom = tile_size; int zoom = tile_size;
int cached_zoom = 0; int cached_zoom = 0;
/** Algorithm choices */
typedef boost::function<surface(const surface &, int, int)> scaling_function;
scaling_function scale_to_zoom_func;
scaling_function scale_to_hex_func;
} // end anon namespace } // end anon namespace
namespace image { namespace image {
@ -801,12 +807,7 @@ static surface get_scaled_to_hex(const locator& i_locator)
//return scale_surface(img, zoom, zoom); //return scale_surface(img, zoom, zoom);
if (!img.null()) { if (!img.null()) {
gui2::tadvanced_graphics_options::SCALING_ALGORITHM algo = gui2::tadvanced_graphics_options::LINEAR; return scale_to_hex_func(img, zoom, zoom);
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);
} else { } else {
return surface(NULL); return surface(NULL);
} }
@ -826,12 +827,7 @@ static surface get_scaled_to_zoom(const locator& i_locator)
surface res(get_image(i_locator, UNSCALED)); surface res(get_image(i_locator, UNSCALED));
// For some reason haloes seems to have invalid images, protect against crashing // For some reason haloes seems to have invalid images, protect against crashing
if(!res.null()) { if(!res.null()) {
gui2::tadvanced_graphics_options::SCALING_ALGORITHM algo = gui2::tadvanced_graphics_options::LINEAR; return scale_to_zoom_func(res, ((res.get()->w * zoom) / tile_size), ((res.get()->h * zoom) / tile_size));
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);
} else { } else {
return surface(NULL); return surface(NULL);
} }
@ -1298,5 +1294,24 @@ std::string describe_versions()
return ss.str(); 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 } // end namespace image

View file

@ -248,6 +248,9 @@ namespace image {
std::string describe_versions(); 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 locator& i_locator, const std::string& outfile);
void save_image(const surface& surf, const std::string& outfile); void save_image(const surface& surf, const std::string& outfile);
} }

View file

@ -572,6 +572,12 @@ static int do_gameloop(const std::vector<std::string>& args)
return 1; 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()) { if(preferences::joystick_support_enabled()) {
res = game->init_joystick(); res = game->init_joystick();
if(res == false) { if(res == false) {