Optimised Text Localisation

Added an unordered map to act as a cache for text that has already been localised
This commit is contained in:
RGreenlees 2023-09-13 16:11:05 +01:00 committed by pierow
parent eb91c70a75
commit 664a578775
4 changed files with 60 additions and 38 deletions

View file

@ -22,6 +22,8 @@
#include "stdlib.h"
#include "math.h"
#include <unordered_map>
#include "hud.h"
#include "cl_util.h"
#include "common/cl_entity.h"
@ -38,6 +40,9 @@ extern playermove_t *pmove;
extern float HUD_GetFOV( void );
vec3_t vec3_origin( 0, 0, 0 );
extern vec3_t v_angles;
std::unordered_map<const char*, std::string> LocalizationMap;
//double sqrt(double x);
//
//float Length(const float *v)
@ -269,46 +274,63 @@ AVHHSPRITE LoadSprite(const char *pszName)
return SPR_Load(sz);
}
void FlushLocalization()
{
LocalizationMap.clear();
}
bool LocalizeString(const char* inMessage, string& outputString)
{
#define kMaxLocalizedStringLength 1024
// Don't localize empty strings
if (!strcmp(inMessage, ""))
{
return false;
}
bool theSuccess = false;
char theInputString[kMaxLocalizedStringLength];
char theOutputString[kMaxLocalizedStringLength];
// Don't localize empty strings
if(strcmp(inMessage, ""))
if(*inMessage != '#')
{
if(*inMessage != '#')
{
sprintf(theInputString, "#%s", inMessage);
}
else
{
sprintf(theInputString, "%s", inMessage);
}
if((CHudTextMessage::LocaliseTextString(theInputString, theOutputString, kMaxLocalizedStringLength) != NULL))
{
outputString = theOutputString;
sprintf(theInputString, "#%s", inMessage);
}
else
{
sprintf(theInputString, "%s", inMessage);
}
if(theOutputString[0] != '#')
{
theSuccess = true;
}
else
{
string theTempString = theOutputString;
theTempString = theTempString.substr(1, theTempString.length());
outputString = theTempString;
}
std::unordered_map<const char*, std::string>::const_iterator FoundLocalization = LocalizationMap.find(theInputString);
if (FoundLocalization != LocalizationMap.end())
{
outputString = FoundLocalization->second;
return true;
}
if((CHudTextMessage::LocaliseTextString(theInputString, theOutputString, kMaxLocalizedStringLength) != NULL))
{
LocalizationMap[inMessage] = theOutputString;
outputString = theOutputString;
if(theOutputString[0] != '#')
{
theSuccess = true;
}
else
{
outputString = string("err: ") + theInputString;
string theTempString = theOutputString;
theTempString = theTempString.substr(1, theTempString.length());
outputString = theTempString;
}
}
else
{
outputString = string("err: ") + theInputString;
}
return theSuccess;
}

View file

@ -6825,16 +6825,6 @@ void AvHHud::UpdateResources(float inTimePassed)
}
}
}
string theResourceText;
char theResourceBuffer[64];
if(this->GetInTopDownMode() && this->mCommanderResourceLabel)
{
LocalizeString(kMarineResourcePrefix, theResourceText);
sprintf(theResourceBuffer, "%s %d", theResourceText.c_str(), this->mVisualResources);
this->mCommanderResourceLabel->setText(64, theResourceBuffer);
}
// Update visual resource indicators, expiring old ones
const float kNumericalInfoEffectLifetime = 1.1f;

View file

@ -3618,6 +3618,16 @@ void AvHHud::RenderCommanderUI()
AvHSpriteDrawTiles(mTopDownBottomSprite, 4, 1, 0, ScreenHeight()-theHeight, ScreenWidth(), ScreenHeight(), 0, 0, 1, 1);
}
if (this->mCommanderResourceLabel)
{
string theResourceText;
char theResourceBuffer[64];
LocalizeString(kMarineResourcePrefix, theResourceText);
sprintf(theResourceBuffer, "%s %d", theResourceText.c_str(), this->mVisualResources);
this->mCommanderResourceLabel->setText(64, theResourceBuffer);
}
// Draw scaled sprites for all ActionButtons
this->DrawActionButtons();

View file

@ -444,9 +444,9 @@ vec3_t AvHSHUGetRealLocation(const vec3_t& inLocation, const vec3_t& inMinBox, c
if((outLocation.x == outLocation.y) && (outLocation.y == outLocation.z) && (outLocation.z == 0.0f))
{
outLocation.x = (inMinBox.x + inMaxBox.x)/2.0f;
outLocation.y = (inMinBox.y + inMaxBox.y)/2.0f;
outLocation.z = (inMinBox.z + inMaxBox.z)/2.0f;
outLocation.x = (inMinBox.x + inMaxBox.x) * 0.5f;
outLocation.y = (inMinBox.y + inMaxBox.y) * 0.5f;
outLocation.z = (inMinBox.z + inMaxBox.z) * 0.5f;
}
return outLocation;
}