Hello! I'm trying to learn DirectX from a book called Beginning DirectX 9. I've come to a part where I'm learning to load images and put them to the screen, all is going well although there are many errors in the book I had to correct myself.
The book shows how to load an image of the letters A through Z and by looping through a character string it is able to find the letter on the image and cut them out and paste them after each other.
Where *c is the current character in the string and letterHeight & width are the height and width of the letters.
this is somehow finding the correct X and Y coordinate of the letter on the image, I'm just curious as to how this works. Could someone explain to me what that is doing? e.g. *c - 'A', I have no idea what that does and the only thing the book says about it is:
"Each time through the loop, you are working with only one letter. For example, the first
time through the code, you’re handling only the H from the word “HELLO”. The code then
computes the source rectangle by getting the top-left X and Y coordinates for this letter."
and also %7
More info:
The bitmap of the letters is 336x192, width and height of the letters are 48, there are 6 letters across the top of the image and 4 going down with two empty squares at the end.
You have to think in ASCII codes. The ASCII code of A is 65. So what *c - 'A' does is to substract 65 from the character *c. This effectively gives the index of *c into the alphabet. /6 and %7 are to calculate row and columns and those values depend on the number of columns there are in your image.
'A' is an Ascii character. Its value is 0x41 in hexadecimal or 65 in decimal.
*c is a pointer to a character in an area of character. It could be 'F', whose value is 0x46 or 70. It could be 'H', whose value is 72.
*c - 'A', could be, in my example, 70 - 65 = 5, or 72 - 65 = 7.
( *c - 'A' ) / 6 is an integer division by 6. It could be 5 / 6 = 0, or 7 / 6 = 1.
So for the first 6 characters of the alphabet, the result will be 0, then for the next six characters, it will be 1, etc.
( *c - 'A' ) %7 is the rest of the division by 7. It could be 5 % 7 = 5, or 7 % 7 = 0. I think there is an error here, because it should be modulo 6 instead of modulo 7.
ahhhh yes, that makes more sense now, this book is riddled with little errors like that. The % 7 confused me the most as you said it seems to be an error.
Bookmarks