Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » drawing weird characters

This thread is locked; no one can reply to it. rss feed Print
drawing weird characters
shadyvillian
Member #12,426
December 2010

How would I draw something like this Æ? do you have to use al_draw_ustr? do strings even support that character? I'm pretty sure my font supports it since its arial.

ALLEGRO_USTR* NameText = al_ustr_new(CardList[CsTopListItem+i].Name.c_str());

al_draw_ustr(Font, al_map_rgb(255, 255, 255), name_x, y, 0, NameText);

I tried this but it didn't work. I'm guessing there's more too it.

Software Engineer by day, hacker by night.

kazzmir
Member #1,786
December 2001
avatar

What does CardList[CsTopListItem+i].Name contain? Are you 100% sure its a utf-8 encoded character for Æ?

shadyvillian
Member #12,426
December 2010

I load the data for that string from a text file and a couple of the names include the character Æ. There's around 1000 different names that can be drawn and about 5 have that character. When I used that code it drew all the other stuff fine still but the name with Æ didn't draw at all. I know the string contains the right character because when you click on the name it loads an image and the name has to match with the images name or it will fail to load. The image loads ok so its just something wrong with the drawing I think.

Software Engineer by day, hacker by night.

Matthew Leverton
Supreme Loser
January 1999
avatar

UTF-8 is an encoding. In it the character Æ is two bytes. Allegro expects UTF-8 encoding.

Try passing your strings through this function:

char *utf8_encode_str(char *dest, const unsigned char *src, size_t dest_size)
{
  char *c = dest;
  unsigned int i, len = strlen(src);

  for (i = 0; i < len && c < dest + dest_size - 5; ++i)
  {
    size_t char_size = al_utf8_encode(c, src[i]);
    c += char_size;
  }
  *c = 0;

  return dest;
}

e.g.:

const char utf8_str[400];
// ...
al_draw_text(Font, al_map_rgb(255, 255, 255), name_x, y, 0,
  utf8_encode_str(utf8_str, CardList[CsTopListItem+i].Name.c_str(), 400));

shadyvillian
Member #12,426
December 2010

I think I figured it out. The text file that it is reading the string from I had to save it as UTF-8 encoding. But I might try that too.

EDIT: your way is better saving the file as UTF-8 brings up a lot more problems. I had to tweak it so it would compile:

#SelectExpand
1char *utf8_encode_str(char *dest, const char *src, size_t dest_size) 2{ 3 char *c = dest; 4 unsigned int i, len = strlen(src); 5 6 for (i = 0; i < len && c < dest + dest_size - 5; ++i) 7 { 8 size_t char_size = al_utf8_encode(c, src[i]); 9 c += char_size; 10 } 11 12 *c = 0; 13 14 return dest; 15} 16 17char utf8_str[400]; 18 19al_draw_text(Font, al_map_rgb(255, 255, 255), name_x, y, 0, 20utf8_encode_str(utf8_str, CardList[CsTopListItem+i].Name.c_str(), 400));

Now its just cutting off Æ when it draws it.

Software Engineer by day, hacker by night.

Go to: