-
April 7th, 2010, 03:13 PM
#1
StretchRect making an image from string
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.
I've come across this:
int srcY = ( ( ( *c - 'A' ) / 6 ) ) * letterHeight;
int srcX = ( ( ( *c - 'A' ) %7 ) * letterWidth );
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.
Thanks Malb
-
April 8th, 2010, 08:18 AM
#2
Re: StretchRect making an image from string
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.
-
April 8th, 2010, 08:19 AM
#3
Re: StretchRect making an image from string
'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.
-
April 8th, 2010, 12:27 PM
#4
Re: StretchRect making an image from string
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.
Thanks both for the explanation I understand now
-
April 8th, 2010, 12:29 PM
#5
Re: StretchRect making an image from string
Actually that was my error!!
It is 7 across and 4 down!! So % 7 is correct and / 6 is wrong yes?
-
April 8th, 2010, 12:31 PM
#6
Re: StretchRect making an image from string
Yes! After changing / 6 to / 7 it works perfectly although "Hello World" Somehow worked fine when it was / 6. Strange book this.
Thanks again
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|