press_x - 1Fr3aK2/Cub3d GitHub Wiki

📝 press_x

Handles the event of pressing the close button (X) on the game window, triggering a clean exit from the program.

⚙️ Parameters

Parameter Type Description
data t_data * Pointer to the main data structure containing all game-related information and resources.

🔁 Returns

Return value Description
int Always returns 0 after calling exit_error, although the program terminates before the return occurs.

📖 Description

The press_x function is called when the user attempts to close the game window (usually by clicking the "X" button).
It serves as an event handler that ensures the program terminates properly by calling the exit_error function.

Internally, it performs the following steps:

  1. Calls exit_error(data, "GAME OVER!"); to print a message and free all allocated resources.
  2. Terminates the program gracefully.
  3. Returns 0 as a formality, even though the program exits before this return is reached.

This function is commonly registered as a callback in the event system of the graphics library (e.g., MiniLibX) to handle window-close events safely.

💡 Example Usage

// Register the window close event handler
mlx_hook(data.win, 17, 0, press_x, &data);

// When the user closes the window, this function will be triggered:
int press_x(t_data *data)
{
    exit_error(data, "GAME OVER!\n");
    return (0);
}