🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Bug in Lesson 14 - text length calculation

Started by
0 comments, last by Kazade 16 years, 11 months ago
Hello all, I think there is a bug in lesson 14 in the section that calculates the text length of a string. I discovered it because accented characters such as é, à, etc. are not displayed in the example program. In fact, all characters with ASCII code above 128 are not displayed. The reason is a mismatch between signed char and unsigned char Code of lesson 14: char text[256]; --> char data type defaults to signed char in VC ++ (From MSDN: char : –128 to 127) for (unsigned int loop=0;loop<(strlen(text));loop++) // Loop To Find Text Length { length+=gmf[text[loop]].gmfCellIncX; // Increase Length By Each Characters Width } --> for all ASCII codes above 128, text[loop] is a value below 0 --> gmf[text[loop]].gmfCellIncX gives undesired results For example : character "é" (ASCII code 233) --> text[loop] will have -23 as value because the char data type is in fact signed char. Solution: cast the char to an unsigned char unsigned char ch; for (unsigned int loop=0;loop<(strlen(text));loop++) // Loop To Find Text Length { ch = (unsigned char) text[loop]; length+=gmf[ch].gmfCellIncX; // Increase Length By Each Characters Width } --> Now ch will have the correct value 233, and the "é" will be nicely displayed Kind regards, Patrick Pasteels
Advertisement
Hi,

Thanks for that information. I'll make the change as soon as our FTP access to the site is back.

Luke.
Member of the NeHe team.

This topic is closed to new replies.

Advertisement