Jump to content

Internationalisation


sjg
 Share

Recommended Posts

Once again, sorted it out right after posting. I could probably write a book on doing that already.

 

Ended up writing a thing that takes wchars and displays textures. Can talk about this more if anyone's curious.

  • Upvote 1
Link to comment
Share on other sites

Alright, so first I created a bunch of hand-drawn textures, which were 256x256 PNGs with a transparent background. I made one for each letter I want to use. I then put them in a subdir of my project and opened the Leadwerks Editor, which automatically converted them to tex. I then had to edit each one to use DXT5 so I could get the alpha channel working properly.

 

 

Then I created a singleton "GlyphManager" class, which has a std::map<std::wstring, Leadwerks::Texture*> (to hold the textures and reference them) and a std::vector<std::wstring> (the buffer of text to display).

 

Then I wrote a "start" function which loads all the glyphs into the map individually, like so:

glyphs[L"A"] = Leadwerks::Texture::Load(path + subdir + "a.tex");
glyphs[L"B"] = Leadwerks::Texture::Load(path + subdir + "b.tex");

 

The L before the double quotes tells the compiler that it's a wide (multibyte) char. This means I could also load in non-ASCII characters, such as:

glyphs[L"Г"] = Leadwerks::Texture::Load(path + subdir + "g.tex");

glyphs[L"Д"] = Leadwerks::Texture::Load(path + subdir + "d.tex");

 

(Leadwerks Editor doesn't recognise non-ASCII characters in filenames so I divided the images into subdirectories such as "eng-cap" and "rus-cap"

 

 

Right, so the textures are in place, so what do we do with them?

void GlyphManager::displayText(Leadwerks::Context* context) {
 float startx = 1;
 float starty = 1;
 float xoffset = startx;
 float yoffset = starty;
 float size = context->window->GetWidth() / 40;
 context->SetBlendMode(Leadwerks::Blend::Alpha);
 for (auto str : textToDisplay) {
   for (auto ch : str) {

     std::wstring tempWStr;
     tempWStr.push_back(ch);
     if (glyphs[tempWStr]) {
       context->DrawImage(glyphs[tempWStr], xoffset * size, yoffset * size, size, size);
     }
     ++xoffset;
   }
   xoffset = startx;
   ++yoffset;
 }

textToDisplay.clear();

}

 

glyphs is my std::map<std::wstring, Leadwerks::Texture*>, and textToDisplay is my std::vector<std::wstring>. It iterates over each line in the vector, and in each line, it gets each char and puts the appropriate letter to the screen in the appropriate place. I did it quickly and nastily; you'll see I iterate over a wstring getting a wchar each time, but then I have to put the single wchar into a wstring and work with that because I wasn't sure how to get the wchar to work as the key in my hashmap. I'm sure there's a better way of doing it. Anyway, that's the gist of it all.

 

So, as it's a singleton method I can call it from anywhere I've included the header. I've been calling it in an update function for my game state every frame, because as you'll notice it clears the buffer at the end of that function.

 

So the result of calling this code every frame:

GlyphManager::getInstance()->addTextToDisplay(L" ");
GlyphManager::getInstance()->addTextToDisplay(L" ");
GlyphManager::getInstance()->addTextToDisplay(L" ");
GlyphManager::getInstance()->addTextToDisplay(L"TESTING BITMAP FONT...");
GlyphManager::getInstance()->addTextToDisplay(L" ");
GlyphManager::getInstance()->addTextToDisplay(L"THE QUICK BROWN FOX");
GlyphManager::getInstance()->addTextToDisplay(L"JUMPS OVER THE LAZY DOG");
GlyphManager::getInstance()->addTextToDisplay(L" ");
GlyphManager::getInstance()->addTextToDisplay(L"РАЗЪЯРЕННЫЙ ЧТЕЦ ЭГОИСТИЧНО БЬЁТ");
GlyphManager::getInstance()->addTextToDisplay(L"ПЯТЬЮ ЖЕРДЯМИ ШУСТРОГО ФЕХТОВАЛЬЩИКА");

 

is this:

fPz9ACS.png

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...