Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [Allegro 4.9.22] Drawing extended ASCII characters with the TTF addon

This thread is locked; no one can reply to it. rss feed Print
[Allegro 4.9.22] Drawing extended ASCII characters with the TTF addon
kenmasters1976
Member #8,794
July 2007

I'm writing a simple program to write characters 0-255 on the screen with the TTF addon. It is the first time I use the TTF addon so I'm a bit confused.

If I hardcode a string with extended ASCII characters as in:

al_draw_text(font, al_map_rgb(0, 0, 0), 0, 0, 0, "ÁÉÍÓÚÜ");

it works fine. But if try to initialize a char array and then draw the resulting string as in:

for (i = 0; i < 256; i++)
  text[i] = i + 1;
al_draw_text(font, al_map_rgb(0, 0, 0), 0, 0, 0, text);

it only draws characters up to 127.

So, what's the trick here?.

kazzmir
Member #1,786
December 2001
avatar

Did you declare text as unsigned char[]?

SiegeLord
Member #7,827
October 2006
avatar

Allegro uses UTF-8 as its text representation, not ASCII. The first 127 characters are the same in UTF-8 and ASCII, but anything above 127 is different. In fact, a byte the value of which is > 127 signifies many different things in UTF-8, none of which are single-byte characters. See this for more info.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

kenmasters1976
Member #8,794
July 2007

OK. Thanks SiegeLord.

SiegeLord
Member #7,827
October 2006
avatar

That said, this works:

  for(int ii = 0; ii < 256; ii++)
  {
    char str[5];
    int num_bytes = al_utf8_encode(str, ii);
    str[num_bytes] = '\0';
    al_draw_text(font, al_map_rgb(255,255,255), (ii % 32) * 15, (ii / 32) * 25 + 10, 0, str);
  }

Supposedly that set of characters corresponds to a particular extended ASCII set of characters.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

kenmasters1976
Member #8,794
July 2007

Oh, great!. I was just looking for a way to do this.

Thanks again.

Go to: