handle_keyrelease - 1Fr3aK2/Cub3d GitHub Wiki

📝 handle_keyrelease

Handles the release of a key by updating the player's movement or rotation state flags in the main game data structure. When a key is released, the corresponding bit in the keys bitmask is cleared, stopping the associated movement or rotation action.

⚙️ Parameters

Parameter Type Description
key int The keycode of the key that was released.
data t_data* Pointer to the main game data structure containing player and key state information.

🔁 Returns

Return value Description
int Always returns 0 (used for compatibility with the event handler).

📖 Description

The handle_keyrelease function checks which key was released and clears the corresponding bit in the data->keys bitmask using the helper function set_bit. This ensures that the game stops performing the action associated with that key.

Supported key releases:

  • XK_w → clears the FORWARD flag.
  • XK_s → clears the BACKWARDS flag.
  • XK_a → clears the LEFT_S flag.
  • XK_d → clears the RIGHT_S flag.
  • XK_Left → clears the TURN_L flag.
  • XK_Right → clears the TURN_R flag.

Notes:

  • Each movement or rotation flag corresponds to a bit in the keys bitmask.
  • Clearing the bit ensures the player stops moving or turning in that direction.
  • The function does not perform movement directly; it only updates the state used by the game loop.

💡 Example Usage

t_data data;
// Assume the keys bitmask is already initialized and a key event is triggered

handle_keyrelease(XK_w, &data); // Stops the player from moving forward
handle_keyrelease(XK_Left, &data); // Stops the player from turning left