Slight refactoring of default prefs value storage, and clamp font scaling

This commit is contained in:
Charles Dang 2016-07-29 04:32:36 +11:00
parent 17edeed029
commit 13da8dac07
3 changed files with 36 additions and 31 deletions

View file

@ -57,6 +57,22 @@ config prefs;
namespace preferences {
/*
* Stores all the static, default values for certain game preferences. The values
* are kept here for easy modification without a lnegthy rebuild.
*
* Add any variables of similar type here.
*/
const int min_window_width = 800;
const int min_window_height = 600;
const int def_window_width = 1024;
const int def_window_height = 768;
const int min_font_scaling = 80;
const int max_font_scaling = 150;
class prefs_event_handler : public events::sdl_handler {
public:
virtual void handle_event(const SDL_Event &) {}
@ -137,9 +153,7 @@ void prefs_event_handler::handle_window_event(const SDL_Event& event)
void write_preferences()
{
#ifndef _WIN32
bool prefs_file_existed = access(filesystem::get_prefs_file().c_str(), F_OK) == 0;
#endif
try {
@ -149,9 +163,7 @@ void write_preferences()
ERR_FS << "error writing to preferences file '" << filesystem::get_prefs_file() << "'" << std::endl;
}
#ifndef _WIN32
if(!prefs_file_existed) {
if(chmod(filesystem::get_prefs_file().c_str(), 0600) == -1) {
@ -159,10 +171,7 @@ void write_preferences()
}
}
#endif
}
void set(const std::string &key, bool value)
@ -348,16 +357,6 @@ void set_scroll_to_action(bool ison)
prefs["scroll_to_action"] = ison;
}
int min_allowed_width()
{
return 800;
}
int min_allowed_height()
{
return 600;
}
std::pair<int,int> resolution()
{
const std::string& x = prefs["xresolution"], y = prefs["yresolution"];
@ -365,12 +364,12 @@ std::pair<int,int> resolution()
if (!x.empty() && !y.empty()) {
try {
return std::make_pair(
std::max(std::stoi(x), min_allowed_width()),
std::max(std::stoi(y), min_allowed_height()));
std::max(std::stoi(x), min_window_width),
std::max(std::stoi(y), min_window_height));
} catch(std::invalid_argument) {}
}
}
return std::pair<int,int>(1024,768);
return std::pair<int,int>(def_window_width, def_window_height);
}
bool maximized()
@ -425,13 +424,13 @@ void save_turbo_speed(const double speed)
int font_scaling()
{
// Clip at 50 because if it's too low it'll cause crashes
return std::max<int>(50, prefs["font_scale"].to_int(100));
// Clip at 80 because if it's too low it'll cause crashes
return std::max<int>(std::min<int>(prefs["font_scale"].to_int(100), max_font_scaling), min_font_scaling);
}
void set_font_scaling(int scale)
{
prefs["font_scale"] = scale;
prefs["font_scale"] = std::max(std::min(scale, max_font_scaling), min_font_scaling);
}
int font_scaled(int size)

View file

@ -39,6 +39,15 @@ namespace preferences {
~base_manager();
};
extern const int min_window_width;
extern const int min_window_height;
extern const int def_window_width;
extern const int def_window_height;
extern const int min_font_scaling;
extern const int max_font_scaling;
void write_preferences();
void set(const std::string& key, const std::string &value);
@ -64,9 +73,6 @@ namespace preferences {
bool scroll_to_action();
void set_scroll_to_action(bool ison);
int min_allowed_width();
int min_allowed_height();
std::pair<int,int> resolution();
void _set_resolution(const std::pair<int,int>& res);
@ -81,7 +87,7 @@ namespace preferences {
double turbo_speed();
void save_turbo_speed(const double speed);
int font_scaling();
void set_font_scaling(int scale);
int font_scaled(int size);

View file

@ -415,8 +415,8 @@ bool CVideo::init_window()
std::cerr << "Setting mode to " << w << "x" << h << std::endl;
window->set_minimum_size(
preferences::min_allowed_width(),
preferences::min_allowed_height()
preferences::min_window_width,
preferences::min_window_height
);
event_handler_.join_global();
@ -584,7 +584,7 @@ std::vector<std::pair<int, int> > CVideo::get_available_resolutions(const bool i
return result;
}
const std::pair<int,int> min_res = std::make_pair(preferences::min_allowed_width(),preferences::min_allowed_height());
const std::pair<int,int> min_res = std::make_pair(preferences::min_window_width, preferences::min_window_height);
SDL_DisplayMode mode;
for (int i = 0; i < modes; ++i) {