gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

OffTopic > Unicode with C++

#162449 - jake2431 - Wed Sep 03, 2008 5:01 am

Hey, I want to write a program that uses the block and smiley face characters from unicode as my graphics (█ and ☻). I have googled a few articles on this, but I am still confused about how it was done. Can anyone show me some example code that will display these characters (I am using C::B)? I want to read a maze made of the block characters from a unicode file.

Or could someone at least point me in the right direction, a article that explains how to do this well, perhaps?

#162457 - Kyoufu Kawa - Wed Sep 03, 2008 10:10 am

AFAIK, Unicode is just an encoding system that assigns characters to certain byte sequences. If you save your maze as UTF-16 you can read it as a simple unsigned short (u16) array and go from there. I think the block and smiley characters are in the Basic Multilingual Plane so you should be good.

What the characters actually look like depends on the font artist, really. And don't forget the byte order mark.

Example data:
Code:
{
  0x2588, 0x2588, 0x2588, 0x2588, 0x2588,
  0x2588, 0x0020, 0x0020, 0x0020, 0x2588,
  0x2588, 0x0020, 0x263B, 0x0020, 0x2588,
  0x2588, 0x0020, 0x0020, 0x0020, 0x2588,
  0x2588, 0x2588, 0x2588, 0x2588, 0x2588,
};

Should be a five by five "room" with █ all around and a ☻ in the middle, surrounded by spaces.

#162460 - sgeos - Wed Sep 03, 2008 2:10 pm

Well... first you need a unicode font...

-Brendan

#162465 - jake2431 - Wed Sep 03, 2008 5:02 pm

Oh, I just realized that I left out a huge point. I am just making it a console app. Just want it to display what it would using the command line. I have done something similar to what I want to do in my C++ class using Xs and such for the graphics. I am just clearing the screen and redrawing. It is very flickery, but is fine for what I want to do.

#162466 - sgeos - Wed Sep 03, 2008 5:36 pm

Is there a unicode font built in? If not, you need to provide one.

#162474 - Kyoufu Kawa - Wed Sep 03, 2008 8:04 pm

I think most console windows support █ and ☻ by default. Let me look up the appropriate codes... one was in the upper range and one was... I think 0x01...

#162477 - sgeos - Wed Sep 03, 2008 8:51 pm

Kyoufu Kawa wrote:
I think most console windows support █ and ☻ by default. Let me look up the appropriate codes... one was in the upper range and one was... I think 0x01...

It does not matter what most windows do. What does yours do? Try using printf and see what happens.

Now, if you only want to read a unicode file, that seems simple enough...
I'd probably put the file through a regex to squash the multibyte characters to single byte characters, but you can read a multibyte file just as easily.

#162479 - jake2431 - Wed Sep 03, 2008 9:08 pm

My console supports those characters, I just don't know how to display them.

#162480 - sgeos - Wed Sep 03, 2008 9:12 pm

jake2431 wrote:
My console supports those characters, I just don't know how to display them.

What happened when you tried to printf() them?

#162482 - Kyoufu Kawa - Wed Sep 03, 2008 10:04 pm

sgeos wrote:
It does not matter what most windows do. What does yours do? Try using printf and see what happens.
Hold on. I'll check as soon as I know what character code █ was. Last time I seriously used that was back in my Q(uick)Basic days... maaaaan... Be back later.

I'm back!
Code:
printf("Smiley: \x2\nWall block: \xDB\n");
Quick look-up in VBDOS' help file and an even quicker test in a VC6 console project.

Guess I was mistaken about the smiley being character #1... that's ☺.

#162486 - jake2431 - Wed Sep 03, 2008 10:35 pm

Sorry, I wasn't where I could test it. Works great! Thanks guys. I didn't know the character code for those characters, so it wasn't working last time I tried. The charts I have are only for ascii.

#162490 - jake2431 - Wed Sep 03, 2008 10:59 pm

Where do I get those character codes? The VBDOS help file, I mean. Is that something to do with visual basic?

#162515 - Kyoufu Kawa - Thu Sep 04, 2008 7:33 am

Indeed it is -- Visual Basic 1.0 for DOS. Please don't ask why I have that lying around ^^;

#162533 - sgeos - Thu Sep 04, 2008 4:07 pm

You can print an extended ASCII table with something like this:
Code:
char i;
for (i = 0x20; i != 0; i++)
{
   printf("0x%02X : %c", i, i);
}
Untested.

#162544 - Kyoufu Kawa - Thu Sep 04, 2008 7:44 pm

Seems about right though.

#162549 - sgeos - Thu Sep 04, 2008 8:14 pm

Kyoufu Kawa wrote:
Seems about right though.

I don't doubt my ability to make simple mistakes with devastating results even in simple untested code. Printing a unicode table is similar, but would require information on how a given unicode encoding works.

#162558 - Kyoufu Kawa - Thu Sep 04, 2008 9:55 pm

Well, for UTF16 it's piss easy if you limit yourself to the Basic Multilingual Plane I guess. Just make sure to skip over the delegates or something.

#162560 - sgeos - Thu Sep 04, 2008 11:19 pm

That sounds about right. You could also fprintf() it to a file if you don't trust the console.

#162565 - jake2431 - Fri Sep 05, 2008 2:33 am

Okay, thanks guys. Now I have to figure out how to pull those characters from a text file. I am not at my computer right now. I want to be able to store it as those characters and pull them that way, instead of storing characte codes.

#162579 - Kyoufu Kawa - Fri Sep 05, 2008 12:40 pm

Open the file as binary (despite being text), reading one character unit at a time and mangling the newlines yourself?