Advertisement

hi, am trying to use the | and the >> << to combine my UCHAR variables...,

Started by April 27, 2002 11:22 AM
5 comments, last by mickey 22 years, 6 months ago
into a single UINT for the color, but there are floating point values included their and my compiler complains that it cannot contain float, what will i do with this then? pls also state the reason why floats or double will make the compiler complain, ie, color = ( ((float)r * 0.99) | ((float)g * 0.99) | ((float)b * 0.99) ); thanks, [edited by - mickey on April 27, 2002 12:23:14 PM]
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
The | & etc are bitwise operators and cannot be used on FLOAT''s, use this:

color = ( (DWORD)((float)r * 0.99) | (DWORD)((float)g * 0.99) | (DWORD)((float)b * 0.99) );
--------<a href="http://www.icarusindie.com/rpc>Reverse Pop Culture
Advertisement
quote: Original post by mickey
into a single UINT for the color, but there are floating point values included their and my compiler complains that it cannot contain float, what will i do with this then?

Cast to an integral type. Then combine.

quote:
pls also state the reason why floats or double will make the compiler complain

You can''t perform binary shift logic on floating-point types; they''re not stored the same way.

Here''s corrected code:
unsigned char color = (((unsigned char)(r * 0.99) << 16) |                       ((unsigned char)(g * 0.99) << 8) |                       (unsigned char)(b * 0.99) ); 



[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
you cant cast to uchar, or the shift will fail (i have made this mistake before) it needs to be int (or uint). furthermore i am assuming this is a fade algorithm, try learning fixed point math (in fact there is a thread somewhere on the forum that i recently explained some simple fixed point math in relation to software fading and alpha blending).

check http://gamedev.net/community/forums/post.asp?method=reply&topic_id=92103

you will be enlightend. also search the forum for more info.
quote: Original post by a person
you cant cast to uchar, or the shift will fail (i have made this mistake before) it needs to be int (or uint).

Um, no.
#include <iostream>int main( void ){  using namespace std;  unsigned char c = 0;  c = (unsigned char) 12 << 4 | (unsigned char) 9 << 2 | (unsigned char) 10;  cout << (int) c << endl;  return 0;} 

Program output: 238

You just have to make sure to constrain the values to what an unsigned char can handle, and cast to integer before display via cout.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!
Are you trying to convert several rgb bytes (0-255) into a single 32-bit colour for D3D/OpenGL?

If so, don''t multiply the rgb values by anything, just shift them and put them in a UINT32 or similar.

If using D3D, there''s a macro for doing this (check the macros section of the D3D Graphics reference pages in the SDK).

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Advertisement
hey guys thanks!

a person: this is the same person you''re helping out on that link,
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,

This topic is closed to new replies.

Advertisement