sjg Posted January 23, 2017 Share Posted January 23, 2017 I'm hoping to get some non-ASCII characters to the screen, but DrawText doesn't like std::wstring. Is there a way around this already, or should I nut out a way to use bitmapped characters? Quote Link to comment Share on other sites More sharing options...
sjg Posted January 23, 2017 Author Share Posted January 23, 2017 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. 1 Quote Link to comment Share on other sites More sharing options...
aiaf Posted January 23, 2017 Share Posted January 23, 2017 Yes please, im interested. Quote I made this with Leadwerks/UAK: Structura | Stacky Desktop Edition Website: Binary Station Link to comment Share on other sites More sharing options...
sjg Posted January 24, 2017 Author Share Posted January 24, 2017 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: 1 Quote Link to comment Share on other sites More sharing options...
aiaf Posted January 24, 2017 Share Posted January 24, 2017 Thanks, this is good, one improvement i can see is using a single texture with all the not ascii characters and extract the letters by offset. 1 Quote I made this with Leadwerks/UAK: Structura | Stacky Desktop Edition Website: Binary Station Link to comment Share on other sites More sharing options...
sjg Posted January 24, 2017 Author Share Posted January 24, 2017 That's not a bad idea The only reason I did them individually was so I could easily add/remove them as I went along. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.