Advertisement

Packing 2 dwords in a long??

Started by May 15, 2001 09:00 AM
3 comments, last by KlePt0 23 years, 8 months ago
I've been trying to get an accurate conversion for the past few days but they all come out rather off. The numbers themselves would easily fit into a word but the come oput rather distorted.
    
            WORD lTemp = SomeStruct.dwordX_Value;
            WORD hTemp = SomeStruct.dwordY_Value;
            LPARAM lParam = MAKELPARAM(lTemp, hTemp);
    
I'm trying to get an ACT-Labs Gun System to accurately emulate a mouse for backwards-compatability in our games. Thanks Robert Warden Lasershot Shooting Simulations Edited by - KlePt0 on May 15, 2001 10:24:54 AM
Bobby Ward - COM Guru in training
Maybe I''m stating the obvious...

a DWORD is 32 bits. a LONG is 32 bits. No way you can pack two dwords into a long without chopping off the upper 16 bits of each.

The implicit cast from DWORD to WORD that you show is doing this.

Maybe something is going wrong there.

Try doing it explicitly

DWORD dwTemp = (SomeStruct.dwordX_Value & 0x0000FFFF);
dwTemp |= ((SomeStruct.dwordY_Value << 16) & 0xFFFF0000)

lParam = (LPARAM)dwTemp;

DSutherland
Advertisement
I''ve never heard of a MAKELPARAM function/macro. Maybe you might want to try MAKELONG?
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
All the posts so far are good, but I''m gonna post the strait C way to pack to oversized 32 values (that both are really 16 bits) into a single 32 bit value. This is for those who don''t have the windows macros and such.

  // say we have unsigned long origA and unsigned long origB// but both are only using the low 16 bits for their value// and you want to create a 32 value with origA in the High// Order bitsunsigned long packedValue = (origA << 16) + (origB & 0xFFFF);  


It''s as easy as that. There are 2 important notes. 1) you probably want your packed type to be unsigned. 2) the left shift operator will always bring in Zeros, so origA wont polute origB''s space, but the mask operation is necessary to insure origB doesn''t accidentally step on origA''s space (in case origB has junk in its high bits).

good luck
Oh, forgot to mention, the add operation could be a bitwise OR instead, but on most current platforms add is equal or occasionally faster ... crazy huh.

This topic is closed to new replies.

Advertisement