Conversation
- cleanup function that cleans all the data when the user presses ESC key or click the red cross on top of the windows - prepare stub functions for player movements, for now, while waiting raycasting part, they log their action into the console
it's very basic, I'll see what I can do better later
There was a problem hiding this comment.
Pull request overview
This PR implements event handling functionality for the cub3D game, enabling user interaction through keyboard input and window management. The implementation includes event routing, player movement/rotation stubs, cleanup logic, and a welcome ASCII art banner.
Key changes:
- Event handling system with keyboard input routing (WASD for movement, arrow keys for rotation, ESC to exit)
- Window close button handler and resource cleanup function
- Stub implementations for player actions (forward, backward, strafe, rotate)
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/events/events_handler.c | Implements keypress and window close event handlers that route inputs to appropriate action functions |
| src/events/cleanup_exit.c | Handles cleanup of MLX resources and game data before program exit |
| src/events/player_actions_move.c | Provides stub implementations for player movement actions (forward, backward, strafe) |
| src/events/player_actions_rotate.c | Provides stub implementations for camera rotation actions (left, right) |
| src/main.c | Integrates event handlers with MLX loop and adds ASCII art welcome message |
| src/utils/ascii_art.c | Displays colorized cub3D logo and usage instructions at startup |
| includes/cub3d.h | Adds function declarations for event handling and utility functions |
| Makefile | Includes new source files in build configuration and fixes ASCII art formatting |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/events/events_handler.c
Outdated
| game = (t_game *)param; | ||
| if (keycode == XK_Escape) | ||
| cleanup_exit(game); | ||
| if (keycode == XK_w || keycode == XK_W) |
There was a problem hiding this comment.
Missing else statement before the next condition check. After cleanup_exit(game) is called, the function will exit the program, but for code clarity and to prevent potential issues if cleanup_exit is modified in the future, line 19 should use else if instead of just if. This ensures the subsequent checks are only evaluated when Escape is not pressed.
| if (keycode == XK_w || keycode == XK_W) | |
| else if (keycode == XK_w || keycode == XK_W) |
There was a problem hiding this comment.
yes my bad, it was working do I did not payed attention
| game = (t_game *)param; | ||
| cleanup_exit(game); | ||
| return (EXIT_SUCCESS); | ||
|
|
There was a problem hiding this comment.
[nitpick] Extra blank line at the end of the file. This should be removed to maintain consistency with the rest of the codebase.
There was a problem hiding this comment.
ok, and I did a norm pass as well
| int handle_keypress(int keycode, void *param) | ||
| { | ||
| t_game *game; | ||
|
|
||
| game = (t_game *)param; | ||
| if (keycode == XK_Escape) | ||
| cleanup_exit(game); | ||
| if (keycode == XK_w || keycode == XK_W) | ||
| move_forward(game); | ||
| else if (keycode == XK_s || keycode == XK_S) | ||
| move_backward(game); | ||
| else if (keycode == XK_a || keycode == XK_A) | ||
| strafe_left(game); | ||
| else if (keycode == XK_d || keycode == XK_D) | ||
| strafe_right(game); | ||
| else if (keycode == XK_Left) | ||
| rotate_left(game); | ||
| else if (keycode == XK_Right) | ||
| rotate_right(game); | ||
| return (EXIT_SUCCESS); | ||
| } |
There was a problem hiding this comment.
The event handling functions (handle_keypress, handle_close) lack test coverage. Given that the repository has unit tests for other functionality (see tests/unit/), these critical event handlers should also have corresponding tests to verify keycode routing, cleanup behavior, and proper handling of different input scenarios.
There was a problem hiding this comment.
the test is the program
| 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); | ||
| } |
There was a problem hiding this comment.
The cleanup_exit function lacks test coverage. Given that the repository has unit tests for other functionality (see tests/unit/), this critical cleanup function should have tests to verify proper resource deallocation and exit behavior under different scenarios (e.g., when some resources are NULL, when all resources are allocated).
There was a problem hiding this comment.
the test is the program
src/main.c
Outdated
| return (EXIT_FAILURE); | ||
| print_ascii_art_hello(); | ||
| print_map_grid(&game.map); | ||
| free_map(&game.map); |
There was a problem hiding this comment.
The map is freed here, but then mlx_loop() is started on line 24. The game loop will likely need access to the map data for rendering. This will cause a use-after-free bug when the game tries to access game.map during the render loop. The free_map() call should be removed from here since cleanup_exit() already handles freeing the map when the game exits.
| free_map(&game.map); |
There was a problem hiding this comment.
there is a check in the cleanup function to prevent double free but yeah, let's get rid of the extra ligne in the main, we will need it for the norm anyway
- missing else word - extra line to free in main - norm
- now you don't have to spam the keys to advance, rotate, you keep the keys pressed and it will stop when you release the keys
new structure for key_bindings explained
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 11 out of 12 changed files in this pull request and generated 18 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| free_map(&game.map); | ||
| setup_hooks(&game); | ||
| mlx_loop_hook(game.mlx, game_loop, &game); | ||
| mlx_loop(game.mlx); |
There was a problem hiding this comment.
The game loop is started but there is no cleanup of resources when the loop exits normally. The mlx_loop function can return, and when it does, allocated resources (map grid, MLX resources) should be freed. Consider adding cleanup_exit(&game); or free_map(&game.map); after line 23 to prevent memory leaks if the loop terminates normally.
| mlx_loop(game.mlx); | |
| mlx_loop(game.mlx); | |
| cleanup_exit(&game); |
There was a problem hiding this comment.
oops my bad, a victim of the refactor and the helper function
There was a problem hiding this comment.
I am a bit unsure if mlx_loop does actually return
| t_player player; // player data (position, direction, camera plane) | ||
| t_map map; // map data (grid, size, textures paths, colors) | ||
| t_keys keys; // tracks which keys are currently pressed | ||
| // === Mouse === |
There was a problem hiding this comment.
[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.
| // === Mouse === | |
| // === Mouse === |
|
|
||
| (void)game; | ||
| frame_count++; | ||
| if (frame_count % 60 == 0) |
There was a problem hiding this comment.
[nitpick] Magic number: The value 60 appears to be used as a frame count divisor for debug logging throttling. Consider defining this as a named constant (e.g., DEBUG_LOG_FRAME_INTERVAL or DEBUG_PRINT_EVERY_N_FRAMES) to improve code clarity and maintainability.
There was a problem hiding this comment.
it's explained in the notion card + it's only a test, nothing to see here
|
|
||
| (void)game; | ||
| frame_count++; | ||
| if (frame_count % 60 == 0) |
There was a problem hiding this comment.
[nitpick] Magic number: The value 60 appears to be used as a frame count divisor for debug logging throttling. Consider defining this as a named constant (e.g., DEBUG_LOG_FRAME_INTERVAL or DEBUG_PRINT_EVERY_N_FRAMES) to improve code clarity and maintainability.
There was a problem hiding this comment.
it's explained in the notion card + it's only a test, nothing to see here
|
|
||
| (void)game; | ||
| frame_count++; | ||
| if (frame_count % 60 == 0) |
There was a problem hiding this comment.
[nitpick] Magic number: The value 60 appears to be used as a frame count divisor for debug logging throttling. Consider defining this as a named constant (e.g., DEBUG_LOG_FRAME_INTERVAL or DEBUG_PRINT_EVERY_N_FRAMES) to improve code clarity and maintainability.
There was a problem hiding this comment.
it's explained in the notion card + it's only a test, nothing to see here
| else if (delta_x < 0) | ||
| { | ||
| frame_count_left++; | ||
| if (frame_count_left % 60 == 0) |
There was a problem hiding this comment.
[nitpick] Magic number: The value 60 appears to be used as a frame count divisor for debug logging throttling. Consider defining this as a named constant (e.g., DEBUG_LOG_FRAME_INTERVAL or DEBUG_PRINT_EVERY_N_FRAMES) to improve code clarity and maintainability.
There was a problem hiding this comment.
it's explained in the notion card + it's only a test, nothing to see here
| /** | ||
| * @brief Rotates camera to the right | ||
| * | ||
| * @param game Pointer to game structure | ||
| */ |
There was a problem hiding this comment.
[nitpick] Documentation formatting: The closing part of the docstring has inconsistent indentation. It should be aligned with the opening /** for consistency with the other docstrings in this file.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
| /** | ||
| * @brief Moves player forward in current direction | ||
| * | ||
| * @param game Pointer to game structure | ||
| */ |
There was a problem hiding this comment.
[nitpick] Documentation formatting: The closing part of the docstring has inconsistent indentation. It should be aligned with the opening /** for consistency with the other docstrings in this file.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
| /** | ||
| * @brief Moves player backward (opposite of current direction) | ||
| * | ||
| * @param game Pointer to game structure | ||
| */ |
There was a problem hiding this comment.
[nitpick] Documentation formatting: The closing part of the docstring has inconsistent indentation. It should be aligned with the opening /** for consistency with the other docstrings in this file.
There was a problem hiding this comment.
@copilot open a new pull request to apply changes based on this feedback
| /** | ||
| * @brief Strafes player to the left (perpendicular to direction) | ||
| * | ||
| * @param game Pointer to game structure | ||
| */ |
There was a problem hiding this comment.
[nitpick] Documentation formatting: The closing part of the docstring has inconsistent indentation. It should be aligned with the opening /** for consistency with the other docstrings in this file.
- remove const on a struct (no need) - add doxygen com
https://www.notion.so/feature-events-2c2456d6aba380fe9d83e0edc1b6a0d4