Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Something is terribly wrong with al_ustr_new()

Credits go to Arthur Kalliokoski for helping out!
This thread is locked; no one can reply to it. rss feed Print
Something is terribly wrong with al_ustr_new()
Aikei_c
Member #14,871
January 2013
avatar

I now work on a project which loads allegro unicode strings from lua script. It goes like this: first a function in lua creates strings, then it passes an array of these strings to c++ code which places them into a vector for later use.
Here is my simple lua code which creates strings, pretty straight-forward:

#SelectExpand
1function CreateStrings(lang) 2 lang = lang or "english" 3 local strings = { } 4 if (lang == "english") then 5 strings = 6 { 7 "Select Class", 8 "Edit Class" 9 } 10 end 11 12 if (lang == "russian") then 13 strings = 14 { 15 "Выбор класса", 16 "Редактирование класса" 17 } 18 end 19 return strings 20end 21 22function GetStrings() 23 local strings = CreateStrings("russian") 24 return strings 25end

As you can see, it creates an array of strings either in Russian or in English. Then it gets passed to the c++ code here:

#SelectExpand
2void CPlGameView::LoadStringsFromLua() 3{ 4 ALLEGRO_USTR* str; 5 LuaFunction<LuaObject> GetStrings = LuaManager.GetGlobalState()->GetGlobal("GetStrings"); 6 LuaObject luaStrings = GetStrings(); 7 for (int i = 0; i < STRID_LAST_ID; i++) 8 { 9 const char* ch; 10 ch = luaStrings[i+1].GetString(); 11 strings.push_back(al_ustr_new(ch)); 12 } 13}

These strings should be window titles. However, when I later try to display the text with al_draw_ustr:

#SelectExpand
3al_draw_ustr(font,PALETTE[BACKGROUND_COLOR],pos.x+2,pos.y,0,title);

I get empty window titles when choosing russian. Everything displays fine if I choose English. Look attachements.
You may think that this is the lua problem, however, I tried to substitute the variable ch for literal cyrillic string:

#SelectExpand
4strings.push_back(al_ustr_new("Выбор класса"))

- the situation is the same. No text. On the other hand, everything is again fine with literal string in english is used.
I went even further and debugged the variables.
So, when the code gets c string from lua everything is fine, which is no wonder (you can look it in attachements). But what is more astonishing is that when I look at contents of ALLEGERO_USTR the text displayed in the debegger is again right. Even right before displaying the window title. However the russian text is still not displayed.

So, maybe al_draw_ustr is to blame? No. I wrote code loading ALLEGRO_USTR right from a file without using al_ustr_new():

#SelectExpand
5bool CPlGameView::LoadStrings(const char* lang) 6{ 7 ALLEGRO_USTR* str; 8 ALLEGRO_FILE* langfile; 9 if (strcmp(lang,"russian") == 0) 10 { 11 langfile = al_fopen("rus.l","r"); 12 } 13 else 14 if (strcmp(lang,"english") == 0) 15 langfile = al_fopen("eng.l","r"); 16 else 17 { 18 WRITE_VIEWLOG("error: wrong language file name!"); 19 return false; 20 } 21 22 str = al_fget_ustr(langfile); 23 while (!al_feof(langfile)) 24 { 25 str = al_fget_ustr(langfile); 26 al_ustr_rtrim_ws(str); 27 strings.push_back(str); 28 } 29 al_fclose(langfile); 30 return true; 31}

which loads russian text. Then this text gets displayed with al_draw_ustr() just fine. So, there is either a bug here with loading multibyte utf-8 characters with al_ustr_new(), or I am doing something wrong, which you probably can explain to me. Thank you for you patience if you read to the end :)

Arthur Kalliokoski
Second in Command
February 2005
avatar

Usually these types of problems are caused by your source code editor not saving files as proper UTF8.

“Throughout history, poverty is the normal condition of man. Advances which permit this norm to be exceeded — here and there, now and then — are the work of an extremely small minority, frequently despised, often condemned, and almost always opposed by all right-thinking people. Whenever this tiny minority is kept from creating, or (as sometimes happens) is driven out of a society, the people then slip back into abject poverty. This is known as "bad luck.”

― Robert A. Heinlein

Aikei_c
Member #14,871
January 2013
avatar

You know, I just checked it and it seems you were right. Thsnks.

Go to: