diff --git a/Makefile b/Makefile index df469cc..78f9595 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/11/11 20:02:50 by rparodi ### ########.fr # +# Updated: 2024/11/12 06:12:02 by bgoulard ### ########.fr # # # # **************************************************************************** # @@ -42,14 +42,16 @@ MLXFLAGS = -L$(MLX_DIR) -lmlx -L/opt/X11/lib -lX11 -lXext -lXrender -lXrandr -lX LDFLAGS += $(MLXFLAGS) SRC =\ - raycast/mlx_init.c \ - parsing/arguments.c \ - sources/cleanups.c \ - sources/error.c \ - sources/main.c \ - sources/options.c \ - sources/options_impl.c \ - sources/rgb_to_color.c + raycast/frame_update.c \ + mlx_layer/mlx_init.c \ + parsing/arguments.c \ + sources/main.c \ + sources/cleanups.c \ + sources/options.c \ + sources/rgb_to_color.c \ + sources/error.c \ + sources/options_impl.c + # Objects OBJDIRNAME = ./build diff --git a/raycast/mlx_init.c b/mlx_layer/mlx_init.c similarity index 60% rename from raycast/mlx_init.c rename to mlx_layer/mlx_init.c index 0a5ee03..1600019 100644 --- a/raycast/mlx_init.c +++ b/mlx_layer/mlx_init.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/11 19:53:42 by rparodi #+# #+# */ -/* Updated: 2024/11/11 21:27:12 by rparodi ### ########.fr */ +/* Updated: 2024/11/12 06:19:04 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,23 +14,26 @@ #include "cub3d_struct.h" #include "mlx_functions.h" #include "mlx_structs.h" + #include +#include -int _keyhook(int keycode, t_info *info) +int c3_frame_update(void *inf_ptr); + +int c3_keyhook(int keycode, t_info *info) { - (void)info; - if (keycode == 53 || keycode == 65307) - exit(EXIT_SUCCESS); // return (cleanup_info(info), EXIT_SUCCESS); /// Remove for segfault - return (0); + if (keycode == XK_Escape || keycode == 65307) + return (mlx_loop_end(info->mlx_ptr), EXIT_SUCCESS); + /* move player w keys and call to redraw screen */ + return (EXIT_SUCCESS); } -int _redcross(t_info *info) +int c3_redcross(t_info *info) { - (void) info; // return (cleanup_info(info), EXIT_SUCCESS); /// Remove for segfault - exit(EXIT_SUCCESS); + return (mlx_loop_end(info->mlx_ptr), EXIT_SUCCESS); } -t_win_list *_init_mlx_window(t_info *info) +t_win_list *c3_init_mlx_window(t_info *info) { int x; int y; @@ -38,7 +41,7 @@ t_win_list *_init_mlx_window(t_info *info) x = 0; y = 0; mlx_get_screen_size(info->mlx_ptr, &x, &y); - return (mlx_new_window(info->mlx_ptr, x, y, "Miaou")); + return (mlx_new_window(info->mlx_ptr, x, y, "C3D")); } int init_mlx_env(t_info *info) @@ -46,11 +49,11 @@ int init_mlx_env(t_info *info) info->mlx_ptr = mlx_init(); if (!info->mlx_ptr) return (MLX_ERROR); - info->win_ptr = _init_mlx_window(info); + info->win_ptr = c3_init_mlx_window(info); if (!info->win_ptr) return (MLX_ERROR); - mlx_hook(info->win_ptr, 2, 1L << 0, _keyhook, info); - mlx_hook(info->win_ptr, 17, 1L << 17, _redcross, info); - mlx_loop(info->mlx_ptr); + mlx_hook(info->win_ptr, KeyPress, KeyPressMask, c3_keyhook, info); + mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, c3_redcross, info); + mlx_loop_hook(info->mlx_ptr, c3_frame_update, info); return (NO_ERROR); } diff --git a/raycast/frame_update.c b/raycast/frame_update.c new file mode 100644 index 0000000..d0f9541 --- /dev/null +++ b/raycast/frame_update.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* frame_update.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/12 06:02:54 by bgoulard #+# #+# */ +/* Updated: 2024/11/12 06:21:09 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d_struct.h" + +#include "mlx_functions.h" +#include "ft_string.h" + +#include + +// Return 42 is irrelevant check mlx_loop to see that mlx doesn't care for the +// return of the function... +// (Why 'int (*)(void*)' when you dont use the int) +// +// This function is called each time the mlx loops over the mlx pointer in +// mlx_loop. here we do calc on time since last frame and player position +// to know if we need to re-draw the screen. if yes call raph function for +// calc. +// The need to redraw can also be expressed in the diferent key_pressed +// functions, I would recomend to make a bool field for that in the info +// struct. +// As a pure artefact of using mlx this function will likely be mooved to +// mlx_layer in the final repo. +int c3_frame_update(void *inf_ptr) +{ + t_info *info; + + info = inf_ptr; + mlx_clear_window(info->mlx_ptr, info->win_ptr); + ft_putendl_fd("update called\n", STDOUT_FILENO); + return (EXIT_SUCCESS); +} diff --git a/sources/cleanups.c b/sources/cleanups.c index 72a1847..fbf9708 100644 --- a/sources/cleanups.c +++ b/sources/cleanups.c @@ -6,12 +6,13 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 01:11:01 by bgoulard #+# #+# */ -/* Updated: 2024/11/11 21:30:19 by bgoulard ### ########.fr */ +/* Updated: 2024/11/12 05:49:23 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d_struct.h" #include "ft_string.h" +#include "mlx_functions.h" static void cleanup_cli(t_cli *cli_ctx) { @@ -21,12 +22,16 @@ static void cleanup_cli(t_cli *cli_ctx) static void cleanup_map(t_map *map) { - (void)map; + if (map->fd) + (close(map->fd), map->fd = 0); } static void cleanup_mlx(t_info *info) { - (void)info; + mlx_destroy_window(info->mlx_ptr, info->win_ptr); + mlx_destroy_display(info->mlx_ptr); + if (info->mlx_ptr) + ft_free((void **)&info->mlx_ptr); } void cleanup_info(t_info *info) diff --git a/sources/main.c b/sources/main.c index 2ef363f..c08801d 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,20 +6,21 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */ -/* Updated: 2024/11/11 21:34:07 by bgoulard ### ########.fr */ +/* Updated: 2024/11/12 06:20:46 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" #include "cub3d_struct.h" -#include "mlx_functions.h" -#include "mlx_structs.h" #include "ft_string.h" +#include "mlx_functions.h" #include #include #include +// not normed but we'll take care of this as a niceties at the last +// possible moment :) void dump_info(t_info *info) { const char *bool_str[2] = { "True", "False"}; @@ -52,13 +53,16 @@ void check_err(t_info *info) void run_cub3d(t_info *info) { - // code here + // todo: here // - parse map // - validity check - // - mlx inits - // - game loop init_mlx_env(info); - // - mlx cleanup + mlx_loop(info->mlx_ptr); +// - game loop : already loops over the mlx_ptr +// -> get events if key pressed move player + run math to re-draw screen +// - mlx cleanup : already called in parent function deprecated to call here +// -> previous 'segfault' were due to someone calling cleaup instead of +// mlx_loop_end } int main_cub3d(char *file_arg, t_info *info) @@ -73,7 +77,7 @@ int main_cub3d(char *file_arg, t_info *info) if (info->cli_ctx.debug) (dump_info(info), printf("file_arg: %s\n", file_arg)); run_cub3d(info); - return (cleanup_info(info), EXIT_SUCCESS); + return (cleanup_info(info), info->last_error); } int main(int argc, char *argv[])