CEGUI tutorial: Cyrillic Russian fonts

By , last updated May 1, 2017

We were planning on going to Steam Greenlight with our game “Burnt Islands” and decided to have more than just “English” language in the game. As we’ve built our own engine it was not so difficult to implement several languages. The solution (and the problem) was CEGUI. It has support for different languages and UTF fonts, but to figure out how to make it work was not so easy.

russian dropdown cegui

We have now two languages in “Burnt Islands” – English and Russian. Here we will show you how to make it work with Cyrillic (Russian) fonts.

Build CEGUI

The first thing you need to do in order to be able to use UNICODE feature in CEGUI is to make sure CEGUI is actually using CEGUI::String as it’s internal string class. Normally this is the default value, but it’s worthwhile to make sure this CMake setting is set to 1.

-DCEGUI_STRING_CLASS:STRING="1"

To make sure you made it right run the “Font demo” that comes with CEGUI and check that you see Russian letters:
cegui russian

Set up CEGUI font

Next thing you need to do is to set CEGUI up with the right font. In CEGUI documentation it says that “DejaVuSans” font can be used to show Cyrillic texts. Look at CEGUI example “FontDemo” for more information. Here is what we did:

CEGUI::Font& font(CEGUI::FontManager::getSingleton().createFromFile( "DejaVuSans-10.font" ));
CEGUI::System::getSingleton().getDefaultGUIContext().setDefaultFont( &font);

Now, some places in the game we needed a smaller font, so here is what we did then:


CEGUI::Font& textFont = CEGUI::FontManager::getSingleton().createFreeTypeFont("DejaVuSerif-10", 12.f, true, "DejaVuSans.ttf", CEGUI::Font::getDefaultResourceGroup());

CEGUI::DefaultWindow* window = createStaticText(false);
window->setFont(&textFont);

Set the text

The most tricky part was to set the text in a right format.

Create dropdown combobox

To make things easy in the beginning we decided to make a simple combobox with available languages.

CEGUI::Combobox* m_LanguageDropdown = createCombobox();
m_LanguageDropdown->setPosition(CEGUI::UVector2(cegui_reldim(0.70f), cegui_reldim( 0.10f )));
m_LanguageDropdown->setSize( relsize(0.20, 0.15) );

Add languages

We added two languages: English and Russian. First English is the easiest part:

//english
CEGUI::ListboxTextItem* englishLanguage = new CEGUI::ListboxTextItem("English", 0);
englishLanguage->setSelectionBrushImage("Vanilla-Images/GenericBrush");
m_LanguageDropdown->addItem(englishLanguage);

Now, the Russian language is added with several text conversions:
1. Create the text as a std::wstring
2. Convert std::wstring to CEGUI::String

//russian
std::wstring ruswide = L"Русский";
CEGUI::Win32StringTranscoder stc;
CEGUI::String ruslang = stc.stringFromStdWString(ruswide);
CEGUI::ListboxTextItem* russianLanguage = new CEGUI::ListboxTextItem(ruslang, 1);
russianLanguage->setSelectionBrushImage("Vanilla-Images/GenericBrush");
m_LanguageDropdown->addItem(russianLanguage);

Note that each ListboxTextItem has its own index. It’s needed in order to choose the right one after reloading the game.

Choose current language

You need to set two properties on ListBoxTextItem in order to choose it right: setSelected() and setText():

LanguageType language = GameOptions()->getLanguage();

switch (language)
{
case RUSSIAN:
	{
		m_LanguageDropdown->getListboxItemFromIndex(0)->setSelected(false);
		m_LanguageDropdown->getListboxItemFromIndex(1)->setSelected(true);
		m_LanguageDropdown->getEditbox()->setText(m_LanguageDropdown->getListboxItemFromIndex(1)->getText());
		break;
	}
default:
	m_LanguageDropdown->getListboxItemFromIndex(0)->setSelected(true);
        m_LanguageDropdown->getEditbox()->setText(m_LanguageDropdown->getListboxItemFromIndex(0)->getText());
	m_LanguageDropdown->getListboxItemFromIndex(1)->setSelected(false);
	break;
}

Subscribe to combobox event

Now you want the language to change as you choose a different one. This can be done by subscribing to a combobox event getSelectedItem():

auto setLanguageClicked = [&] (const CEGUI::EventArgs & args) -> bool
{
	if(m_LanguageDropdown->getSelectedItem()->getID() == 0)
	{
		GameOptions()->setLanguage(ENGLISH);
	} else {
	        GameOptions()->setLanguage(RUSSIAN);
	}
        return true;
};

m_LanguageDropdown->subscribeEvent( m_LanguageDropdown->EventListSelectionAccepted, setLanguageClicked );

Result

Russian menu form “Burnt Islands”:
cegui startmenu burnt islands russian

English menu from “Burnt Islands”:
cegui start menu burnt islands english

Senior Software Engineer developing all kinds of stuff.

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*