Game Development Reference
In-Depth Information
edit and load. In this case, the data format is XML, read easily by the TinyXML SDK,
available freely under the zlib license. Here
'
s an example of what this might look like:
<?xml version=“1.0” encoding=“UTF-8”?>
<strings>
<string value=
“
Alert
”
id=
“
IDS_ALERT
”
/>
<string value=
“
Question
”
id=
“
IDS_QUESTION
”
/>
<string value=
“
Initializing
”
id=
“
IDS_INITIALIZING
”
/>
<string value=
“
Ok
”
id=
“
IDS_OK
”
/>
<string value=
“
Yes
”
id=
“
IDS_YES
”
hotkey=
“
Y
”
/>
</strings>
One note: the identifier should be representative of what the string stands for and
named to group strings together into categories. For example, if you had a string
“
You are out of hard drive space,
”
you could define that as I
DS_INITCHECK_
LOW_DISK_SPACE
.
Reading this file is a piece of cake. First, an STL map is declared that will map a
string key to the actual string resource:
std::map<std::wstring,std::wstring> m_textResource;
Then two methods are defined
the first to load the strings from the XML file from
the resource cache and the next to access the string given the key value:
—
bool GameCodeApp::LoadStrings(std::string language)
{
std::string languageFile =
“
Strings\\
”
;
languageFile += language;
languageFile +=
“
.xml
”
;
TiXmlElement* pRoot =
XmlResourceLoader::LoadAndReturnRootXmlElement(languageFile.c_str());
if (!pRoot)
{
GCC_ERROR(
“
Strings are missing.
”
);
return false;
}
// Loop through each child element and load the component
for (TiXmlElement* pElem = pRoot->FirstChildElement(); pElem; pElem =
pElem->NextSiblingElement())
{
const char *pKey=pElem->Attribute(
“
id
”
);
const char *pText=pElem->Attribute(
“
value
”
);
if (pKey && pText)
{