Skip to content

Commit 2760371

Browse files
SmallJokersfan5
authored andcommittedJan 29, 2021
Settings: Purge getDefault, clean FontEngine
1 parent 37a05ec commit 2760371

File tree

8 files changed

+38
-60
lines changed

8 files changed

+38
-60
lines changed
 

Diff for: ‎src/client/clientlauncher.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ bool ClientLauncher::run(GameStartData &start_data, const Settings &cmd_args)
175175
}
176176
}
177177
#endif
178-
g_fontengine = new FontEngine(g_settings, guienv);
178+
g_fontengine = new FontEngine(guienv);
179179
FATAL_ERROR_IF(g_fontengine == NULL, "Font engine creation failed.");
180180

181181
#if (IRRLICHT_VERSION_MAJOR >= 1 && IRRLICHT_VERSION_MINOR >= 8) || IRRLICHT_VERSION_MAJOR >= 2

Diff for: ‎src/client/fontengine.cpp

+32-33
Original file line numberDiff line numberDiff line change
@@ -42,43 +42,42 @@ static void font_setting_changed(const std::string &name, void *userdata)
4242
}
4343

4444
/******************************************************************************/
45-
FontEngine::FontEngine(Settings* main_settings, gui::IGUIEnvironment* env) :
46-
m_settings(main_settings),
45+
FontEngine::FontEngine(gui::IGUIEnvironment* env) :
4746
m_env(env)
4847
{
4948

5049
for (u32 &i : m_default_size) {
5150
i = (FontMode) FONT_SIZE_UNSPECIFIED;
5251
}
5352

54-
assert(m_settings != NULL); // pre-condition
53+
assert(g_settings != NULL); // pre-condition
5554
assert(m_env != NULL); // pre-condition
5655
assert(m_env->getSkin() != NULL); // pre-condition
5756

5857
readSettings();
5958

6059
if (m_currentMode == FM_Standard) {
61-
m_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
62-
m_settings->registerChangedCallback("font_bold", font_setting_changed, NULL);
63-
m_settings->registerChangedCallback("font_italic", font_setting_changed, NULL);
64-
m_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
65-
m_settings->registerChangedCallback("font_path_bold", font_setting_changed, NULL);
66-
m_settings->registerChangedCallback("font_path_italic", font_setting_changed, NULL);
67-
m_settings->registerChangedCallback("font_path_bolditalic", font_setting_changed, NULL);
68-
m_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
69-
m_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
60+
g_settings->registerChangedCallback("font_size", font_setting_changed, NULL);
61+
g_settings->registerChangedCallback("font_bold", font_setting_changed, NULL);
62+
g_settings->registerChangedCallback("font_italic", font_setting_changed, NULL);
63+
g_settings->registerChangedCallback("font_path", font_setting_changed, NULL);
64+
g_settings->registerChangedCallback("font_path_bold", font_setting_changed, NULL);
65+
g_settings->registerChangedCallback("font_path_italic", font_setting_changed, NULL);
66+
g_settings->registerChangedCallback("font_path_bolditalic", font_setting_changed, NULL);
67+
g_settings->registerChangedCallback("font_shadow", font_setting_changed, NULL);
68+
g_settings->registerChangedCallback("font_shadow_alpha", font_setting_changed, NULL);
7069
}
7170
else if (m_currentMode == FM_Fallback) {
72-
m_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
73-
m_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
74-
m_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
75-
m_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
71+
g_settings->registerChangedCallback("fallback_font_size", font_setting_changed, NULL);
72+
g_settings->registerChangedCallback("fallback_font_path", font_setting_changed, NULL);
73+
g_settings->registerChangedCallback("fallback_font_shadow", font_setting_changed, NULL);
74+
g_settings->registerChangedCallback("fallback_font_shadow_alpha", font_setting_changed, NULL);
7675
}
7776

78-
m_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
79-
m_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
80-
m_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
81-
m_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
77+
g_settings->registerChangedCallback("mono_font_path", font_setting_changed, NULL);
78+
g_settings->registerChangedCallback("mono_font_size", font_setting_changed, NULL);
79+
g_settings->registerChangedCallback("screen_dpi", font_setting_changed, NULL);
80+
g_settings->registerChangedCallback("gui_scaling", font_setting_changed, NULL);
8281
}
8382

8483
/******************************************************************************/
@@ -205,9 +204,9 @@ unsigned int FontEngine::getFontSize(FontMode mode)
205204
void FontEngine::readSettings()
206205
{
207206
if (USE_FREETYPE && g_settings->getBool("freetype")) {
208-
m_default_size[FM_Standard] = m_settings->getU16("font_size");
209-
m_default_size[FM_Fallback] = m_settings->getU16("fallback_font_size");
210-
m_default_size[FM_Mono] = m_settings->getU16("mono_font_size");
207+
m_default_size[FM_Standard] = g_settings->getU16("font_size");
208+
m_default_size[FM_Fallback] = g_settings->getU16("fallback_font_size");
209+
m_default_size[FM_Mono] = g_settings->getU16("mono_font_size");
211210

212211
/*~ DO NOT TRANSLATE THIS LITERALLY!
213212
This is a special string. Put either "no" or "yes"
@@ -220,15 +219,15 @@ void FontEngine::readSettings()
220219
m_currentMode = is_yes(gettext("needs_fallback_font")) ?
221220
FM_Fallback : FM_Standard;
222221

223-
m_default_bold = m_settings->getBool("font_bold");
224-
m_default_italic = m_settings->getBool("font_italic");
222+
m_default_bold = g_settings->getBool("font_bold");
223+
m_default_italic = g_settings->getBool("font_italic");
225224

226225
} else {
227226
m_currentMode = FM_Simple;
228227
}
229228

230-
m_default_size[FM_Simple] = m_settings->getU16("font_size");
231-
m_default_size[FM_SimpleMono] = m_settings->getU16("mono_font_size");
229+
m_default_size[FM_Simple] = g_settings->getU16("font_size");
230+
m_default_size[FM_SimpleMono] = g_settings->getU16("mono_font_size");
232231

233232
cleanCache();
234233
updateFontCache();
@@ -244,7 +243,7 @@ void FontEngine::updateSkin()
244243
m_env->getSkin()->setFont(font);
245244
else
246245
errorstream << "FontEngine: Default font file: " <<
247-
"\n\t\"" << m_settings->get("font_path") << "\"" <<
246+
"\n\t\"" << g_settings->get("font_path") << "\"" <<
248247
"\n\trequired for current screen configuration was not found" <<
249248
" or was invalid file format." <<
250249
"\n\tUsing irrlicht default font." << std::endl;
@@ -292,7 +291,7 @@ gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
292291
setting_suffix.append("_italic");
293292

294293
u32 size = std::floor(RenderingEngine::getDisplayDensity() *
295-
m_settings->getFloat("gui_scaling") * spec.size);
294+
g_settings->getFloat("gui_scaling") * spec.size);
296295

297296
if (size == 0) {
298297
errorstream << "FontEngine: attempt to use font size 0" << std::endl;
@@ -311,8 +310,8 @@ gui::IGUIFont *FontEngine::initFont(const FontSpec &spec)
311310

312311
std::string fallback_settings[] = {
313312
wanted_font_path,
314-
m_settings->get("fallback_font_path"),
315-
m_settings->getDefault(setting_prefix + "font_path")
313+
g_settings->get("fallback_font_path"),
314+
Settings::getLayer(SL_DEFAULTS)->get(setting_prefix + "font_path")
316315
};
317316

318317
#if USE_FREETYPE
@@ -346,7 +345,7 @@ gui::IGUIFont *FontEngine::initSimpleFont(const FontSpec &spec)
346345
assert(spec.mode == FM_Simple || spec.mode == FM_SimpleMono);
347346
assert(spec.size != FONT_SIZE_UNSPECIFIED);
348347

349-
const std::string &font_path = m_settings->get(
348+
const std::string &font_path = g_settings->get(
350349
(spec.mode == FM_SimpleMono) ? "mono_font_path" : "font_path");
351350

352351
size_t pos_dot = font_path.find_last_of('.');
@@ -364,7 +363,7 @@ gui::IGUIFont *FontEngine::initSimpleFont(const FontSpec &spec)
364363

365364
u32 size = std::floor(
366365
RenderingEngine::getDisplayDensity() *
367-
m_settings->getFloat("gui_scaling") *
366+
g_settings->getFloat("gui_scaling") *
368367
spec.size);
369368

370369
irr::gui::IGUIFont *font = nullptr;

Diff for: ‎src/client/fontengine.h

+1-7
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class FontEngine
6262
{
6363
public:
6464

65-
FontEngine(Settings* main_settings, gui::IGUIEnvironment* env);
65+
FontEngine(gui::IGUIEnvironment* env);
6666

6767
~FontEngine();
6868

@@ -128,9 +128,6 @@ class FontEngine
128128
/** get font size for a specific mode */
129129
unsigned int getFontSize(FontMode mode);
130130

131-
/** initialize font engine */
132-
void initialize(Settings* main_settings, gui::IGUIEnvironment* env);
133-
134131
/** update internal parameters from settings */
135132
void readSettings();
136133

@@ -150,9 +147,6 @@ class FontEngine
150147
/** clean cache */
151148
void cleanCache();
152149

153-
/** pointer to settings for registering callbacks or reading config */
154-
Settings* m_settings = nullptr;
155-
156150
/** pointer to irrlicht gui environment */
157151
gui::IGUIEnvironment* m_env = nullptr;
158152

Diff for: ‎src/main.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,6 @@ static bool read_config_file(const Settings &cmd_args)
527527
// Path of configuration file in use
528528
sanity_check(g_settings_path == ""); // Sanity check
529529

530-
531530
if (cmd_args.exists("config")) {
532531
bool r = g_settings->readConfigFile(cmd_args.get("config").c_str());
533532
if (!r) {

Diff for: ‎src/script/scripting_mainmenu.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3131
extern "C" {
3232
#include "lualib.h"
3333
}
34-
#include "settings.h"
3534
#define MAINMENU_NUM_ASYNC_THREADS 4
3635

3736

Diff for: ‎src/settings.cpp

+3-15
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3333
#include <cctype>
3434
#include <algorithm>
3535

36-
Settings *g_settings = nullptr;
36+
Settings *g_settings = nullptr; // Populated in main()
3737
std::string g_settings_path;
3838

3939
Settings *Settings::s_layers[SL_TOTAL_COUNT] = {0}; // Zeroed by compiler
@@ -85,6 +85,7 @@ Settings & Settings::operator = (const Settings &other)
8585
if (&other == this)
8686
return *this;
8787

88+
// TODO: Avoid copying Settings objects. Make this private.
8889
FATAL_ERROR_IF(m_settingslayer != SL_TOTAL_COUNT && other.m_settingslayer != SL_TOTAL_COUNT,
8990
("Tried to copy unique Setting layer " + std::to_string(m_settingslayer)).c_str());
9091

@@ -208,6 +209,7 @@ void Settings::writeLines(std::ostream &os, u32 tab_depth) const
208209
for (const auto &setting_it : m_settings)
209210
printEntry(os, setting_it.first, setting_it.second, tab_depth);
210211

212+
// For groups this must be "}" !
211213
if (!m_end_tag.empty()) {
212214
for (u32 i = 0; i < tab_depth; i++)
213215
os << "\t";
@@ -458,20 +460,6 @@ const std::string &Settings::get(const std::string &name) const
458460
}
459461

460462

461-
const std::string &Settings::getDefault(const std::string &name) const
462-
{
463-
const SettingsEntry *entry;
464-
if (auto parent = getParent())
465-
entry = &parent->getEntry(name);
466-
else
467-
entry = &getEntry(name); // Bottom of the chain
468-
469-
if (entry->is_group)
470-
throw SettingNotFoundException("Setting [" + name + "] is a group.");
471-
return entry->value;
472-
}
473-
474-
475463
bool Settings::getBool(const std::string &name) const
476464
{
477465
return is_yes(get(name));

Diff for: ‎src/settings.h

-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,6 @@ class Settings {
132132

133133
Settings *getGroup(const std::string &name) const;
134134
const std::string &get(const std::string &name) const;
135-
const std::string &getDefault(const std::string &name) const;
136135
bool getBool(const std::string &name) const;
137136
u16 getU16(const std::string &name) const;
138137
s16 getS16(const std::string &name) const;

Diff for: ‎src/unittest/test_settings.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ void TestSettings::testAllSettings()
229229
// Confirm settings
230230
Settings s2("[dummy_eof_end_tag]");
231231
std::istringstream is(config_text_after, std::ios_base::binary);
232-
s2.parseConfigLines(is);
232+
UASSERT(s2.parseConfigLines(is) == true);
233233

234234
compare_settings("(main)", &s, &s2);
235235
}

0 commit comments

Comments
 (0)
Please sign in to comment.