Skip to content

Commit 3ddec3a

Browse files
authored
refactor(language): Cleanup GlobalLanguage code and add function GlobalLanguage::getResolutionFontSizeScale() (TheSuperHackers#2338)
1 parent 862422d commit 3ddec3a

2 files changed

Lines changed: 51 additions & 37 deletions

File tree

Core/GameEngine/Include/GameClient/GlobalLanguage.h

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,28 @@ class GlobalLanguage : public SubsystemInterface
7777
ResolutionFontSizeMethod_Default = ResolutionFontSizeMethod_ClassicNoCeiling,
7878
};
7979

80+
typedef std::list<AsciiString> StringList; // Used for our font file names that we want to load
81+
8082
public:
8183

8284
GlobalLanguage();
8385
virtual ~GlobalLanguage();
8486

8587
void init();
8688
void reset();
87-
void update() { }
89+
void update() {}
90+
91+
Real getResolutionFontSizeAdjustment() const;
92+
Int adjustFontSize(Int theFontSize); // Adjusts font size for resolution. jba.
93+
94+
void parseCustomDefinition();
95+
96+
// Get current resolution font size scale for the given method and scaler, based on the base game resolution of 800 x 600.
97+
// Defaults to a scaler of 1 for a uniform resolution scale.
98+
static Real getResolutionFontSizeScale(ResolutionFontSizeMethod method, Real scaler = 1.0f);
99+
100+
static void parseFontFileName(INI *ini, void *instance, void *store, const void *userData);
101+
static void parseFontDesc(INI *ini, void *instance, void *store, const void *userData);
88102

89103
AsciiString m_unicodeFontName;
90104
AsciiString m_unicodeFontFileName;
@@ -111,19 +125,9 @@ class GlobalLanguage : public SubsystemInterface
111125
Real m_resolutionFontSizeAdjustment;
112126
Real m_userResolutionFontSizeAdjustment;
113127
ResolutionFontSizeMethod m_resolutionFontSizeMethod;
114-
115-
float getResolutionFontSizeAdjustment() const;
116-
Int adjustFontSize(Int theFontSize); // Adjusts font size for resolution. jba.
117-
118-
void parseCustomDefinition();
119-
120-
typedef std::list<AsciiString> StringList; // Used for our font file names that we want to load
121-
typedef StringList::iterator StringListIt;
122-
123-
StringList m_localFonts; // List of the font filenames that are in our local directory
124-
static void parseFontFileName( INI *ini, void *instance, void *store, const void* userData );
125-
static void parseFontDesc(INI *ini, void *instance, void *store, const void* userData);
128+
StringList m_localFonts; // List of the font filenames that are in our local directory
126129
};
130+
127131
//-----------------------------------------------------------------------------
128132
// INLINING ///////////////////////////////////////////////////////////////////
129133
//-----------------------------------------------------------------------------

Core/GameEngine/Source/GameClient/GlobalLanguage.cpp

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
//-----------------------------------------------------------------------------
6565
// DEFINES ////////////////////////////////////////////////////////////////////
6666
//-----------------------------------------------------------------------------
67-
GlobalLanguage *TheGlobalLanguageData = nullptr; ///< The global language singleton
67+
GlobalLanguage *TheGlobalLanguageData = nullptr;
6868

6969
static const LookupListRec ResolutionFontSizeMethodNames[] =
7070
{
@@ -116,7 +116,7 @@ void INI::parseLanguageDefinition( INI *ini )
116116
DEBUG_ASSERTCRASH(TheGlobalLanguageData, ("INI::parseLanguageDefinition - TheGlobalLanguage Data is not around, please create it before trying to parse the ini file."));
117117
return;
118118
}
119-
// parse the ini weapon definition
119+
120120
ini->initFromINI( TheGlobalLanguageData, TheGlobalLanguageDataFieldParseTable );
121121
}
122122

@@ -136,7 +136,7 @@ GlobalLanguage::GlobalLanguage()
136136

137137
GlobalLanguage::~GlobalLanguage()
138138
{
139-
StringListIt it = m_localFonts.begin();
139+
StringList::iterator it = m_localFonts.begin();
140140
while( it != m_localFonts.end())
141141
{
142142
AsciiString font = *it;
@@ -146,7 +146,7 @@ GlobalLanguage::~GlobalLanguage()
146146
}
147147
}
148148

149-
void GlobalLanguage::init( void )
149+
void GlobalLanguage::init()
150150
{
151151
{
152152
AsciiString fname;
@@ -156,7 +156,7 @@ void GlobalLanguage::init( void )
156156
ini.loadFileDirectory( fname, INI_LOAD_OVERWRITE, nullptr );
157157
}
158158

159-
StringListIt it = m_localFonts.begin();
159+
StringList::iterator it = m_localFonts.begin();
160160
while( it != m_localFonts.end())
161161
{
162162
AsciiString font = *it;
@@ -174,49 +174,48 @@ void GlobalLanguage::init( void )
174174
// override values with user preferences
175175
OptionPreferences optionPref;
176176
m_userResolutionFontSizeAdjustment = optionPref.getResolutionFontAdjustment();
177-
178177
}
179-
void GlobalLanguage::reset( void ) {}
180178

179+
void GlobalLanguage::reset()
180+
{
181+
}
181182

182-
void GlobalLanguage::parseFontDesc(INI *ini, void *instance, void *store, const void* userData)
183+
void GlobalLanguage::parseFontDesc(INI *ini, void *instance, void *store, const void *userData)
183184
{
184185
FontDesc *fontDesc = (FontDesc *)store;
185186
fontDesc->name = ini->getNextQuotedAsciiString();
186187
fontDesc->size = ini->scanInt(ini->getNextToken());
187188
fontDesc->bold = ini->scanBool(ini->getNextToken());
188189
}
189190

190-
void GlobalLanguage::parseFontFileName( INI *ini, void * instance, void *store, const void* userData )
191+
void GlobalLanguage::parseFontFileName(INI *ini, void *instance, void *store, const void *userData)
191192
{
192-
GlobalLanguage *monkey = (GlobalLanguage *)instance;
193+
GlobalLanguage *globalLanguage = static_cast<GlobalLanguage *>(instance);
193194
AsciiString asciiString = ini->getNextAsciiString();
194-
monkey->m_localFonts.push_front(asciiString);
195+
globalLanguage->m_localFonts.push_front(asciiString);
195196
}
196197

197-
float GlobalLanguage::getResolutionFontSizeAdjustment( void ) const
198+
Real GlobalLanguage::getResolutionFontSizeAdjustment() const
198199
{
199200
if (m_userResolutionFontSizeAdjustment >= 0.0f)
200201
return m_userResolutionFontSizeAdjustment;
201202
else
202203
return m_resolutionFontSizeAdjustment;
203204
}
204205

205-
Int GlobalLanguage::adjustFontSize(Int theFontSize)
206+
Real GlobalLanguage::getResolutionFontSizeScale(ResolutionFontSizeMethod method, Real scaler)
206207
{
207-
// TheSuperHackers @todo This function is called very often.
208-
// Therefore cache the adjustFactor on resolution change to not recompute it on every call.
209208
Real adjustFactor;
210209

211-
switch (m_resolutionFontSizeMethod)
210+
switch (method)
212211
{
213212
default:
214213
case ResolutionFontSizeMethod_Classic:
215214
{
216215
// TheSuperHackers @info The original font scaling for this game.
217216
// Useful for not breaking legacy Addons and Mods. Scales poorly with large resolutions.
218217
adjustFactor = TheDisplay->getWidth() / (Real)DEFAULT_DISPLAY_WIDTH;
219-
adjustFactor = 1.0f + (adjustFactor - 1.0f) * getResolutionFontSizeAdjustment();
218+
adjustFactor = 1.0f + (adjustFactor - 1.0f) * scaler;
220219
if (adjustFactor > 2.0f)
221220
adjustFactor = 2.0f;
222221
break;
@@ -226,7 +225,7 @@ Int GlobalLanguage::adjustFontSize(Int theFontSize)
226225
// TheSuperHackers @feature The original font scaling, but without ceiling.
227226
// Useful for not changing the original look of the game. Scales alright with large resolutions.
228227
adjustFactor = TheDisplay->getWidth() / (Real)DEFAULT_DISPLAY_WIDTH;
229-
adjustFactor = 1.0f + (adjustFactor - 1.0f) * getResolutionFontSizeAdjustment();
228+
adjustFactor = 1.0f + (adjustFactor - 1.0f) * scaler;
230229
break;
231230
}
232231
case ResolutionFontSizeMethod_Strict:
@@ -236,7 +235,7 @@ Int GlobalLanguage::adjustFontSize(Int theFontSize)
236235
const Real wScale = TheDisplay->getWidth() / (Real)DEFAULT_DISPLAY_WIDTH;
237236
const Real hScale = TheDisplay->getHeight() / (Real)DEFAULT_DISPLAY_HEIGHT;
238237
adjustFactor = min(wScale, hScale);
239-
adjustFactor = 1.0f + (adjustFactor - 1.0f) * getResolutionFontSizeAdjustment();
238+
adjustFactor = 1.0f + (adjustFactor - 1.0f) * scaler;
240239
break;
241240
}
242241
case ResolutionFontSizeMethod_Balanced:
@@ -265,14 +264,25 @@ Int GlobalLanguage::adjustFontSize(Int theFontSize)
265264
hScale = h / (Real)DEFAULT_DISPLAY_HEIGHT;
266265
}
267266
adjustFactor = (wScale + hScale) * 0.5f;
268-
adjustFactor = 1.0f + (adjustFactor - 1.0f) * getResolutionFontSizeAdjustment();
267+
adjustFactor = 1.0f + (adjustFactor - 1.0f) * scaler;
269268
break;
270269
}
271270
}
272271

273272
if (adjustFactor < 1.0f)
274273
adjustFactor = 1.0f;
275-
Int pointSize = REAL_TO_INT_FLOOR(theFontSize*adjustFactor);
274+
275+
return adjustFactor;
276+
}
277+
278+
Int GlobalLanguage::adjustFontSize(Int theFontSize)
279+
{
280+
// TheSuperHackers @todo This function is called very often.
281+
// Therefore cache the adjustFactor on resolution change to not recompute it on every call.
282+
const Real resolutionScaler = getResolutionFontSizeAdjustment();
283+
const Real adjustFactor = getResolutionFontSizeScale(m_resolutionFontSizeMethod, resolutionScaler);
284+
const Int pointSize = REAL_TO_INT_FLOOR(theFontSize * adjustFactor);
285+
276286
return pointSize;
277287
}
278288

@@ -288,9 +298,9 @@ void GlobalLanguage::parseCustomDefinition()
288298

289299
FontDesc::FontDesc(void)
290300
{
291-
name = "Arial Unicode MS"; ///<name of font
292-
size = 12; ///<point size
293-
bold = FALSE; ///<is bold?
301+
name = "Arial Unicode MS";
302+
size = 12;
303+
bold = FALSE;
294304
}
295305
//-----------------------------------------------------------------------------
296306
// PRIVATE FUNCTIONS //////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)