• Advertisement

Archived

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

macro question

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I was reading widnows game programming for dummies and its a great book!!! But i do have one small problem. In the source code there are a few macros used. I dont understand what they are. Could anyone explain to me how they work or where to learn about them?? Also is there any way to get around using them??? PLEEAAAASSEEE HELP!!!! ----------------------------- I''m almost finished my 3D Engine for the TI-83 calculator

Share this post


Link to post
Share on other sites
Advertisement
aright well heres a few ive been wondering aobut... i have a small idea of what they are but im not sure.

this is tested with a value (vk_code) to see what key is pressed:

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

ok thats all supposed to be on one line...

Aright so can someone tell me what that does and why it does it... and whats all this "0x8000" stuff and the "? 1 : 0". This is confusing me out of my brains.

if someone can explain that for me then ill ask about the other one thats also eating me brains alive

-----------------------------
I''m almost finished my 3D Engine for the TI-83 calculator

Share this post


Link to post
Share on other sites
Basicly, macros are pieces of code that are referenced like functions( most of the time ). Ex:

  
#define ISNULL(X) ( ( X == NULL ) ? true : false )


basicly, this is simply an example but it could replace a NULL "if" test.

  
void main( void )
{
int b = NULL;

if ( ISNULL( b ) )
{
// do code if var is NULL here

}
}


now, when the compiler is called, the preprocessor turns the code to this:

  
void main( void )
{
int b = NULL;

if ( ( ( X == NULL ) ? true : false ) )
{
// do code if var is NULL

}
else
{
// do code if not NULL

}
}


Now, a macro is simply some code that is refered to by a single name. Just like a function. They save alot of typing time. When you get the hang of them. Anyhow, my examples aren''t extremely clear but hey, it''s Midnight and I''ve been up for 72 hours straight coding so I''m a bit messed up. I still hope this helps you out.



Cyberdrek
Headhunter Soft
DLC Multimedia

Share this post


Link to post
Share on other sites
quote:
Original post by ninja770

aright well heres a few ive been wondering aobut... i have a small idea of what they are but im not sure.

this is tested with a value (vk_code) to see what key is pressed:

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

ok thats all supposed to be on one line...

Aright so can someone tell me what that does and why it does it... and whats all this "0x8000" stuff and the "? 1 : 0". This is confusing me out of my brains.

if someone can explain that for me then ill ask about the other one thats also eating me brains alive

-----------------------------
I''m almost finished my 3D Engine for the TI-83 calculator


Ok, now, with your example, it''s easier to explain, this checks to see if a key is pressed.. Normally, if you don''t want to use macros, you would have to type this in your test:

  
if ( ( ( GetAsyncKeyState(VK_ENTER) & 0x8000 ) ? 1 : 0 ) )


to see if the enter key was pressed. Now, the 0x8000 is actually the bitmask to convert the scan code into an ascii caracter( unless I''m wrong ). second, the 1 is true, 0 is false.

Ok, I guess you''re confused about the ?: operator. Well, here''s what it is:

the ?: operator is just like an if statement.

  
( b == NULL ) ? 1 : 0


this would return true if b == NULL and false otherwise. Get the drift? Hope you do.




Cyberdrek
Headhunter Soft
DLC Multimedia

Share this post


Link to post
Share on other sites
Thanx alot this does help me out... but some of those are different techniques then im used to. oh well ill just have ta deal with it till i finish reading the book. Well if anyone else can add to that please do so.. there are a few things im still "ify" on. Ive been looking for a while and i cant find any information on macro programming.

-----------------------------
I''m almost finished my 3D Engine for the TI-83 calculator

Share this post


Link to post
Share on other sites
wait... so in that statement i can avoid using the "?" operator totally if i jsut use "=" and "!=" am i right?

-----------------------------
I''m almost finished my 3D Engine for the TI-83 calculator

Share this post


Link to post
Share on other sites
Actually the "& 0x8000" checks to see if the highest order bit is 1, 0x8000 is hexadecimal (16 bit in this case), and the & operator compares bits using boolean AND logic, if all bits that are 1 on the second argument are also 1 on the first argument then it returns true, if not it returns false. In this case it checks the return value of GetAsyncKeyState(), and if the highest order bit (number 16) is 1 then it returns true saying that the key is pressed, otherwise it returns false. I hope that this explanation helps, I know what I''m talking about in my head but I''m not too good at explaining things, and if you''re still confused about the 0x8000 thing then I''ll try to explain it better at a later time.

Share this post


Link to post
Share on other sites
Actually, ninja, the first macro you posted is excessive, all you need is


#define KEYDOWN(key) (GetAsyncKeyState(key)&0x8000)


the ?: is completed useless since the & operator returns a bool anyway.

Share this post


Link to post
Share on other sites
quote:
Original post by BraveZeus

Actually the "& 0x8000" checks to see if the highest order bit is 1, 0x8000 is hexadecimal (16 bit in this case), and the & operator compares bits using boolean AND logic, if all bits that are 1 on the second argument are also 1 on the first argument then it returns true, if not it returns false. In this case it checks the return value of GetAsyncKeyState(), and if the highest order bit (number 16) is 1 then it returns true saying that the key is pressed, otherwise it returns false. I hope that this explanation helps, I know what I''m talking about in my head but I''m not too good at explaining things, and if you''re still confused about the 0x8000 thing then I''ll try to explain it better at a later time.


Thanks for the correction dude, I stand corrected...

for my part, I did understand it correctly, I had a course on binary math back in college so I do get.




Cyberdrek
Headhunter Soft
DLC Multimedia

Share this post


Link to post
Share on other sites
aright i think i get it now. But one things still a little confusing... if anyone is familiar with the GetAsyncKeyState, could you tell me what it returns? if it returns a bool i dont see why the "& 0x8000" is needed.

-----------------------------
I''m almost finished my 3D Engine for the TI-83 calculator

Share this post


Link to post
Share on other sites
The GetAsyncKeyState function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to GetAsyncKeyState.

SHORT GetAsyncKeyState( int vKey
// virtual-key code

);




Parameters


vKey

Specifies one of 256 possible virtual-key codes.

Windows NT: You can use left- and right-distinguishing constants to specify certain keys. See the Remarks section for further information.

Windows 95: Windows 95 does not support the left- and right-distinguishing constants available on Windows NT.

Return Values


If the function succeeds, the return value specifies whether the key was pressed since the last call to GetAsyncKeyState, and whether the key is currently up or down. If the most significant bit is set, the key is down, and if the least significant bit is set, the key was pressed after the previous call to GetAsyncKeyState. The return value is zero if a window in another thread or process currently has the keyboard focus.

Windows 95: Windows 95 does not support the left- and right-distinguishing constants. If you call GetAsyncKeyState on the Windows 95 platform with these constants, the return value is zero.

Share this post


Link to post
Share on other sites

  • Advertisement