Public Group

#### Archived

This topic is now archived and is closed to further replies.

# Two Questions...

This topic is 5982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I have two questions, number one, how do I output an integer onto the screen. Im using the text out function but that only takes text and I tried casting my int but it doesnt work. Is there a function that outputs an int or is there an easier way. Second, what''s happening in this macro _RGB16BIT555(r, g, b) ((b%32) + ((g%32) << 5) + ((r%32) << 10)) It''s supposed to make a DWORD and it does but I dont know how I know the bit shifts but what I dont get is what all the brackets are for and what does the % sign do. Also, if you could tell me how it comes up with the DWORD id be mucho grateful. Anyways, I have alot of questions, hoping to get some anwsered. Thx in advance

##### Share on other sites
this is one way of doing it, there are better ways to do it, this is old, I''m using sprintf (you must include stdio.h):

int a = 10;char temp[255];sprintf(temp, "%d", a);

Now you can use textOut to print temp.

-----------------------------
"problems have solutions
a lifetime of fucking things up fixed in one determined flash"
- The Downward Spiral, NIN

##### Share on other sites
quote:
Original post by perfectly_dark
_RGB16BIT555(r, g, b) ((b%32) + ((g%32) << 5) + ((r%32) << 10))

The "%" character is the modulus operator which returns the _remainder_ of a division operation, thus b%c will return the remainder of b divided by c. Like you said, the "<<" characters are the shift operations. The "(" characters are to establish "order of operation". Since the << operator has a higher precedence than the % operator, if we didn''t have the parenthesis the shift would be processed before the modulus, which is not what we want.

The shift operations in this function will move the resulting values of the modulus operations into 5 bit positions of the double word. Hence, the 555 in the name of the macro (5 bits for each color.) This macro effectively converts three integer (32 bit?) representations of color intensity into three 5 bit representations of color intensity and packs them together into one 16 bit field.

It converts large integer values into a smaller integer values while preserving the aspect ratio of the original intensity value versus the maximum value the integer may have assumed. Your color variance is diminished by this conversion. Instead of several million color combinations with the original color values you are getting only 32,768 values after the conversions.

Since 5 bits (binary) can only represent 32 values, the greatest value we can get from x%32 is 31 before we start to loose data. Anything greater than 31 (because we start counting at zero) will not fit into a 5 bit binary number. This means that the highest integer intensity that you can pass in for any of the colors is 992 (decimal) and the macro will interpret that as an intensity of 31, which just fits into the 5 bit field.

This gives you 16 bit color for your game.

I hope that wasn''t too confusing.

##### Share on other sites
quote:
Original post by perfectly_dark
I dont get is what all the brackets are for and what does the % sign do.

The % sign is very important when dealing with integers. Like most people said, it gives the remainder of a division. But this doesn''t give adequate credit to just how powerful the % operator is.

The result of NUMBER%N is going to be in the range of (0...NUMBER - 1), assuming both numbers are positive. This is VERY useful when dealing with fixed ranges such as arrays. In the code you cited, the author only wanted r,g, and b values in the range of 0...31, so he modded them by 32 (note that NUMBER%N = NUMBER if 0 >= NUMBER < N).

As I said, the mod operator is very useful when dealing with arrays as well. Sometimes, you don''t have the luxury of using a 2D array (for example, maybe you want the array size to be allocated at runtime). Therefor, you have to fake it. If you have:

int arr[100];

but want to treat it as a 10x10 array, you have to use some tricks. Array position N is really in row N/10, column N%10 (so position 53 is in row 5 and column 3, but of course these are really the 6th and 4th row and column but that is already taken into account because arrays start at 0).

Hope this helps.

PS: How do I format code in a message?

##### Share on other sites
OK, thanks for all your help. I think I get it now

##### Share on other sites
To format text in posts, use CODE or SOURCE tags, with their respective /''s, enclosed in square brackets. The code will make

void main()
{
}
with [ code ]:
void main(){}

and with [ source ]:
  void main(){}

• 48
• 12
• 10
• 10
• 9
• ### Forum Statistics

• Total Topics
631378
• Total Posts
2999665
×