Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Drawing UTF8 text

Credits go to Arthur Kalliokoski, Edgar Reynaldo, jmasterx, and Thomas Fjellstrom for helping out!
This thread is locked; no one can reply to it. rss feed Print
Drawing UTF8 text
ReVolt
Member #15,283
August 2013

Hi, I have problem with al_draw_text, I can draw it perfectly, but I want to draw UTF8 characters such as ěščřžýáíé. Is it possible to draw these characters in Allegro 5?

Tahnk you

Arthur Kalliokoski
Second in Command
February 2005
avatar

I've had the problem of the text editor used on the source files not saving as UTF-8, double check it with a hex editor or something.

They all watch too much MSNBC... they get ideas.

ReVolt
Member #15,283
August 2013

I'm using visual studio 2010 express, i think it saves everythig in utf8 or not?

Arthur Kalliokoski
Second in Command
February 2005
avatar

I don't know, I use mingw with the SciTech editor on windows.

They all watch too much MSNBC... they get ideas.

ReVolt
Member #15,283
August 2013

too bad :(.... i've searched internet anf found this:
stackoverflow.com/questions/279673/save-all-files-in-visual-studio-project-as-utf-8

but didn't helped

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

ReVolt
Member #15,283
August 2013

i can't because strings are generated in run time

Thomas Fjellstrom
Member #476
June 2000
avatar

How are they generated at run time? Are any parts of them embedded in the source? Allegro itself is pure UTF8, so if you pass it UTF8 text, it'll draw it. If any of the text is in the source file, your editor could be messing with it.

If what you're doing is concatenating bits of text at run time, take those bits of text from a file.

Or if you want to, I think you can use escape codes in your literal strings, ie: "\xCDFEBA01" to encode the utf8 directly into a literal string, and get around the fact that MSVC is broken. But its a royal pain to have to hand encode the utf8 into hex escape codes.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

ReVolt
Member #15,283
August 2013

it's history of changes

Thomas Fjellstrom
Member #476
June 2000
avatar

Meaning what exactly?

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

ReVolt
Member #15,283
August 2013

i'm programming board game and it shows hystiry of moves and some words are still the same, do you think that it would be good to load those words from file like suggested other guys?
i would like to avoid that way

Thomas Fjellstrom
Member #476
June 2000
avatar

Unfortunately it doesn't look like you can avoid that, if you want to include non ASCII characters.

One bonus to loading from a file, is it'll be easier to translate the game into other languages.

All you really need to do is load the file into an array of strings at program startup, and use that array.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

jmasterx
Member #11,410
October 2009

Here is the simple language manager for my game:

#SelectExpand
1LanguageManager.hpp 2 3#ifndef CGE_LANGUAGE_MANAGER_HPP 4#define CGE_LANGUAGE_MANAGER_HPP 5#include <stdlib.h> 6#include <string> 7#include <map> 8namespace cge 9{ 10 class LanguageManager 11 { 12 std::map<std::string,std::string> m_strings; 13 std::string m_textNotFound; 14 bool _loadTexts(const std::string& path); 15 public: 16 void mapElement(const std::string& elementName, const std::string& text); 17 const std::string& getElement(const std::string& elementName); 18 LanguageManager(const std::string& path); 19 virtual ~LanguageManager(void); 20 }; 21} 22 23#endif 24 25LanguageManager.cpp 26 27#include "Game/Engine/LanguageManager.hpp" 28#include "Game/Utility/File.hpp" 29namespace cge 30{ 31 LanguageManager::~LanguageManager(void) 32 { 33 } 34 35 const std::string& LanguageManager::getElement( const std::string& elementName ) 36 { 37 if(m_strings.find(elementName) == m_strings.end()) 38 { 39 return m_textNotFound; 40 } 41 else 42 { 43 return m_strings[elementName]; 44 } 45 } 46 47 void LanguageManager::mapElement( const std::string& elementName, const std::string& text ) 48 { 49 m_strings[elementName] = text; 50 } 51 52 bool LanguageManager::_loadTexts( const std::string& path ) 53 { 54 //open the file 55 File ifs( path ); 56 57 //ensure it is open 58 if(!ifs.isOpen()) 59 { 60 return false; 61 } 62 std::string line; 63 64 //read each line 65 while( ifs.readLine(line) ) 66 { 67 68 if(line.length() == 0) 69 { 70 continue; 71 } 72 else if(line.length() >= 1 && line[0] == '#') 73 { 74 continue; 75 } 76 77 int lineAt = 0; 78 std::string elemName; 79 80 //extract until space 81 while(lineAt < (int)line.length()) 82 { 83 if(line[lineAt] != ' ') 84 { 85 elemName += line[lineAt]; 86 lineAt++; 87 } 88 else 89 { 90 lineAt += 3; //' = ' 91 break; 92 } 93 } 94 95 std::string elemVal; 96 //extract until end 97 while(lineAt < (int)line.length()) 98 { 99 //filter newline 100 if(line[lineAt] == '\\' && lineAt + 1 < 101 (int)line.length() && line[lineAt + 1] == 'n') 102 { 103 elemVal += '\n'; 104 lineAt += 2; 105 } 106 else 107 { 108 elemVal += line[lineAt]; 109 lineAt++; 110 } 111 } 112 113 mapElement(elemName, elemVal); 114 } 115 116 return true; 117 } 118 119 LanguageManager::LanguageManager( const std::string& path ) 120 : m_textNotFound("Error: Text not found.") 121 { 122 _loadTexts(path); 123 } 124 125} 126 127 128File.hpp 129#ifndef CGE_FILE_HPP 130#define CGE_FILE_HPP 131#include <allegro5/allegro.h> 132#include <allegro5/allegro5.h> 133#include <stdlib.h> 134#include <string> 135namespace cge 136{ 137 class File 138 { 139 ALLEGRO_FILE* m_file; 140 public: 141 File(void); 142 bool close(); 143 bool isOpen() const; 144 bool readLine(std::string& buff); 145 bool readAll(std::string& buff); 146 bool open(const std::string& fileName, const std::string& openMode); 147 File(const std::string& fileName, const std::string& openMode = "r"); 148 bool eof() const; 149 ~File(void); 150 }; 151}; 152 153#endif 154 155File.cpp 156 157#include "Game/Utility/File.hpp" 158namespace cge 159{ 160 File::File(void) 161 : m_file(NULL) 162 { 163 } 164 165 File::File( const std::string& fileName, const std::string& openMode /*= "r"*/ ) 166 : m_file(NULL) 167 { 168 open(fileName,openMode); 169 } 170 171 File::~File(void) 172 { 173 } 174 175 bool File::isOpen() const 176 { 177 return m_file != NULL; 178 } 179 180 bool File::open( const std::string& fileName, const std::string& openMode ) 181 { 182 if(isOpen()) 183 { 184 close(); 185 } 186 187 m_file = al_fopen(fileName.c_str(),openMode.c_str()); 188 189 return m_file != NULL; 190 } 191 192 bool File::close() 193 { 194 if(isOpen()) 195 { 196 al_fclose(m_file); 197 m_file = NULL; 198 return true; 199 } 200 return false; 201 } 202 203 bool File::eof() const 204 { 205 if(!isOpen()) 206 { 207 return true; 208 } 209 else 210 { 211 return al_feof(m_file); 212 } 213 } 214 215 bool File::readLine( std::string& buff ) 216 { 217 buff = ""; 218 if(eof() || !isOpen()) 219 { 220 return false; 221 } 222 223 224 int c = 0; 225 while((c = al_fgetc(m_file)) != EOF && c != '\n') 226 { 227 if(c > 13) 228 buff += (char)c; 229 } 230 231 return true; 232 } 233 234 bool File::readAll( std::string& buff ) 235 { 236 buff = ""; 237 if(eof() || !isOpen()) 238 { 239 return false; 240 } 241 242 243 int c = 0; 244 while((c = al_fgetc(m_file)) != EOF) 245 { 246 if(c > 13 || c == '\n') 247 buff += (char)c; 248 } 249 250 return true; 251 } 252 253} 254 255 256Sample File: 257nav.back = 258nav.sound = 259pass.across = Pass\n2\nAcross 260chat.send = Send 261chat.mute = Mute... 262chat.censor = Censor 263chat.emoticon = 264 265 266Sample usage: 267 268 //init language manager 269 m_languageManager = new LanguageManager("res/language/english.lang"); 270 271//set button text 272m_filterButton->setText(language->getElement("filter.button"));

It looks like a.cc's code blocks don't support unicode so
nav.back = ←
nav.sound = ♪
chat.emoticon = ☺
look empty.

ReVolt
Member #15,283
August 2013

thank you for your answers, i will use the file

Aikei_c
Member #14,871
January 2013
avatar

You can save the file in UTF8 in Visual Studio by going to File->Advanced Save Options... and selecting Encoding Unicode (UTF-8 without signature) - Codepage 65001.
That works for me.

Go to: