-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/events #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/events #6
Changes from all commits
10373cd
ba051aa
3ed4d1c
0037c72
0fb5093
3707c9f
89f49a6
6408707
c0157a4
b3f338b
7e48b6d
3421e77
9c8ca36
953f75e
225b246
7bb1d01
d7739cf
93d2320
dda6de3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| #include "cub3d.h" | ||
|
|
||
| /** | ||
| * @brief Cleans up all game resources and exits program | ||
| * | ||
| * Destroys MLX resources (image, window, display) and frees game data. | ||
| * Called when user presses ESC or clicks window close button. | ||
| * | ||
| * @param game Pointer to game structure | ||
| */ | ||
|
Comment on lines
+3
to
+10
|
||
| void cleanup_exit(t_game *game) | ||
| { | ||
| if (!game) | ||
| exit(EXIT_FAILURE); | ||
| if (game->img) | ||
| mlx_destroy_image(game->mlx, game->img); | ||
| if (game->win) | ||
| mlx_destroy_window(game->mlx, game->win); | ||
| if (game->mlx) | ||
| { | ||
| mlx_destroy_display(game->mlx); | ||
| free(game->mlx); | ||
| } | ||
| if (game->map.grid) | ||
| free_map(&game->map); | ||
| exit(EXIT_SUCCESS); | ||
| } | ||
|
Comment on lines
+11
to
+27
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| #include "cub3d.h" | ||
|
|
||
| /** | ||
| * @brief Returns an array of key bindings mapping keycodes to actions | ||
| * | ||
| * This function returns a static array that maps X11 keycodes to their | ||
| * corresponding action functions and key state flags. | ||
| * | ||
| * @param game Pointer to game structure to access key flags | ||
| * @return Pointer to a static array of key bindings (NULL-terminated) | ||
| */ | ||
| t_key_binding *get_key_bindings(t_game *game) | ||
| { | ||
| static t_key_binding bindings[7]; | ||
|
|
||
| bindings[0] = (t_key_binding){XK_w, move_forward, &game->keys.w_pressed}; | ||
| bindings[1] = (t_key_binding){XK_s, move_backward, &game->keys.s_pressed}; | ||
| bindings[2] = (t_key_binding){XK_a, strafe_left, &game->keys.a_pressed}; | ||
| bindings[3] = (t_key_binding){XK_d, strafe_right, &game->keys.d_pressed}; | ||
| bindings[4] = (t_key_binding){XK_Left, rotate_left, | ||
| &game->keys.left_arrow_pressed}; | ||
| bindings[5] = (t_key_binding){XK_Right, rotate_right, | ||
| &game->keys.right_arrow_pressed}; | ||
| bindings[6] = (t_key_binding){0, NULL, NULL}; | ||
| return (bindings); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Handles key press events using function pointers | ||
| * | ||
| * Iterates through key bindings and sets the corresponding flag to true | ||
| * when a registered key is pressed. | ||
| * | ||
| * @param keycode X11 keycode of the pressed key | ||
| * @param param Pointer to game structure (void* from MLX, must cast) | ||
| * @return EXIT_SUCCESS | ||
| */ | ||
| int handle_keypress(int keycode, void *param) | ||
| { | ||
| t_game *game; | ||
| const t_key_binding *bindings; | ||
| int i; | ||
|
|
||
| game = (t_game *)param; | ||
| if (keycode == XK_Escape) | ||
| cleanup_exit(game); | ||
| bindings = get_key_bindings(game); | ||
| i = 0; | ||
| while (bindings[i].action) | ||
| { | ||
| if (bindings[i].keycode == keycode) | ||
| { | ||
| *bindings[i].flag_ptr = true; | ||
| return (EXIT_SUCCESS); | ||
| } | ||
| i++; | ||
| } | ||
| return (EXIT_SUCCESS); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Handles key release events using function pointers | ||
| * | ||
| * Iterates through key bindings and sets the corresponding flag to false | ||
| * when a registered key is released. | ||
| * | ||
| * @param keycode X11 keycode of the released key | ||
| * @param param Pointer to game structure (void* from MLX, must cast) | ||
| * @return EXIT_SUCCESS | ||
| */ | ||
| int handle_keyrelease(int keycode, void *param) | ||
| { | ||
| t_game *game; | ||
| t_key_binding *bindings; | ||
| int i; | ||
|
|
||
| game = (t_game *)param; | ||
| bindings = get_key_bindings(game); | ||
| i = 0; | ||
| while (bindings[i].action) | ||
| { | ||
| if (bindings[i].keycode == keycode) | ||
| { | ||
| *bindings[i].flag_ptr = false; | ||
| return (EXIT_SUCCESS); | ||
| } | ||
| i++; | ||
| } | ||
| return (EXIT_SUCCESS); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Handles window close button (red cross) event | ||
| * | ||
| * @param param Pointer to game structure (void* from MLX, must cast) | ||
| * @return 0/EXIT_SUCCESS (required by MLX) | ||
| */ | ||
| int handle_close(void *param) | ||
| { | ||
| t_game *game; | ||
|
|
||
| game = (t_game *)param; | ||
| cleanup_exit(game); | ||
| return (EXIT_SUCCESS); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Main game loop - called every frame by MLX (~60 FPS) | ||
| * | ||
| * Checks which keys are pressed and calls their corresponding action | ||
| * functions using the key bindings system. | ||
| * | ||
| * @param param Pointer to game structure (void* from MLX, must cast) | ||
| * @return EXIT_SUCCESS | ||
| */ | ||
| int game_loop(void *param) | ||
| { | ||
| t_game *game; | ||
| t_key_binding *bindings; | ||
| int i; | ||
|
|
||
| game = (t_game *)param; | ||
| bindings = get_key_bindings(game); | ||
| i = 0; | ||
| while (bindings[i].action) | ||
| { | ||
| if (*bindings[i].flag_ptr) | ||
| bindings[i].action(game); | ||
| i++; | ||
| } | ||
| return (EXIT_SUCCESS); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| #include "cub3d.h" | ||
|
|
||
| /** | ||
| * @brief Registers all event hooks for the game window. | ||
| * | ||
| * Sets up keyboard press/release events, window close event, | ||
| * and mouse movement handling. These functions allow the game | ||
| * to react to player inputs during runtime. | ||
| * | ||
| * @param game Pointer to the game structure containing MLX window. | ||
| */ | ||
| void setup_hooks(t_game *game) | ||
| { | ||
| mlx_hook(game->win, 2, 1L << 0, handle_keypress, game); | ||
| mlx_hook(game->win, 3, 1L << 1, handle_keyrelease, game); | ||
| mlx_hook(game->win, 17, 0, handle_close, game); | ||
| mlx_hook(game->win, 6, 1L << 6, handle_mouse_move, game); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Inconsistent indentation: the "// === Mouse ===" comment is indented with a tab character while other similar section comments (like "// === MLX ===" and "// === Game state ===") appear to start at the same indentation level. This should be aligned consistently with the other section comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-_-