25.09.2010, 18:57
Example usage of bitwise XOR and bitwise AND:
This will give you a variable (s_iChangedKeys) with only the keys that were just pressed down.
You can, for example, do:
This will give you a variable (s_iChangedKeys) with only the keys that were just pressed down.
You can, for example, do:
pawn Code:
if ( s_iChangedKeys & KEY_FIRE )
{
// ...
}
pawn Code:
new
g_iPreviousKeys[ MAX_PLAYERS ] // This variable will hold the previous key state for each player
;
public OnPlayerUpdate( playerid )
{
// I use static in OnPlayerUpdate because the varaible won't have to get re-initialized every call; therefore, less work for the CPU.
static
s_iKeys,
s_iUnused, // blah blah
s_iChangedKeys
;
GetPlayerKeys( playerid, s_iKeys, s_iUnused, s_iUnused );
s_iChangedKeys = ( s_iKeys ^ g_iPreviousKeys[ playerid ] );
// First, filter out the bits that were 1 in both variables; leaving only the bits that has changed.
s_iChangedKeys = s_iChangedKeys & s_iKeys;
// Second, keep only the bits that are 1 in both variables.
// Now, s_iChangedKeys contain only bits that was off in g_iPreviousKeys and on in s_iKeys.
// You can also save a line by doing: s_iChangedKeys = ( s_iKeys ^ g_iPreviousKeys[ playerid ] ) & s_iKeys;
if ( s_iChangedKeys & KEY_FIRE )
{
SendClientMessage( playerid, -1, "you just pressed fire" );
}
g_iPreviousKeys[ playerid ] = s_iKeys;
return 1;
}