From fd4fb3c93f4a274f407a1abd884e9bc610346813 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Thu, 7 Nov 2024 19:08:00 +0100 Subject: [PATCH] some norminette and some cleaning --- include/input.h | 14 +------------- include/temp.h | 6 +++--- include/utils.h | 15 +++++++-------- src/input.c | 23 +++++------------------ src/input_utils.c | 28 ++++++++++++++++++++++++++++ src/input_utils.h | 20 ++++++++++++++++++++ src/main.c | 12 ++++++------ src/map1.c | 5 +++-- src/map_utils.h | 4 ++-- src/map_utils1.c | 39 +++++++++++++++++++-------------------- src/map_utils2.c | 11 ++++++----- src/utils2.c | 4 ++-- 12 files changed, 102 insertions(+), 79 deletions(-) create mode 100644 src/input_utils.c create mode 100644 src/input_utils.h diff --git a/include/input.h b/include/input.h index c0e2c55..76da466 100644 --- a/include/input.h +++ b/include/input.h @@ -6,7 +6,7 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:43:09 by mcolonna #+# #+# */ -/* Updated: 2024/11/07 17:13:28 by mc ### ########.fr */ +/* Updated: 2024/11/07 19:03:25 by mc ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,18 +30,6 @@ typedef struct s_input_actions /// when the user presses the corresponding key. extern t_input_actions g_input_actions; -/// @brief To call when a specific key is newly pressed. -/// -/// @param keycode Key pressed. -/// @return Unused. -int hook_keypress(int keycode); - -/// @brief To call when a specific key is released. -/// -/// @param keycode Key released. -/// @return Unused. -int hook_keyrelease(int keycode); - /// @brief To handle the input of a window. /// /// @param mlx_ptr mlx connection identifier diff --git a/include/temp.h b/include/temp.h index d447a27..73f1248 100644 --- a/include/temp.h +++ b/include/temp.h @@ -6,7 +6,7 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 17:04:08 by mc #+# #+# */ -/* Updated: 2024/10/31 17:28:09 by mc ### ########.fr */ +/* Updated: 2024/11/07 19:07:25 by mc ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,8 +40,8 @@ typedef struct s_ray { // pos player on map (cases) t_point_double dir; - bool side; - double wallx; + bool side; + double wallx; } t_ray; #endif diff --git a/include/utils.h b/include/utils.h index 597362d..c8f82ae 100644 --- a/include/utils.h +++ b/include/utils.h @@ -6,7 +6,7 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 16:56:47 by mcolonna #+# #+# */ -/* Updated: 2024/11/01 20:55:00 by mc ### ########.fr */ +/* Updated: 2024/11/07 19:07:03 by mc ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,21 +15,19 @@ # include "include.h" -typedef uint32_t u_int32_t; - /// @brief Represents a point of double type. typedef struct s_point_double { double x; double y; -} t_point_double; +} t_point_double; /// @brief Represents a point of int type. typedef struct s_point_int { int x; int y; -} t_point_int; +} t_point_int; // TODO Must the transparency be 0 or 255? /// @brief Represents an TRGB color in 0xTTRRGGBB format. @@ -60,7 +58,7 @@ void timedloop(void (*f)(void)); /// /// @param img_ptr mlx image to use. /// @return Address to the data, or NULL if error. -u_int32_t *get_data_addr(void *img_ptr); +uint32_t *get_data_addr(void *img_ptr); /// @brief Create a vector according to the angle with a specific norm. /// (0 rad returns (0;-1) / pi/2 rad returns (1;0)) @@ -68,7 +66,8 @@ u_int32_t *get_data_addr(void *img_ptr); /// @param vec This will be set to the result. /// @param angle Angle to use in radians. /// @param norm Norm the vector must have. -void vector_from_rotation(t_point_double *vec, double angle, double norm); +void vector_from_rotation(t_point_double *vec, double angle, + double norm); /// @brief Size of buffer used in read_all_text(). # define READ_ALL_TEXT_BUFFER_SIZE 1000 @@ -81,6 +80,6 @@ void vector_from_rotation(t_point_double *vec, double angle, double norm); /// /// @param fd File descriptor of the file. /// @return Alloc'd string of the file content. NULL if error. -char *read_all_text(int fd); +char *read_all_text(int fd); #endif diff --git a/src/input.c b/src/input.c index ac940fe..bebde7b 100644 --- a/src/input.c +++ b/src/input.c @@ -6,11 +6,13 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:43:36 by mcolonna #+# #+# */ -/* Updated: 2024/11/07 18:37:52 by mc ### ########.fr */ +/* Updated: 2024/11/07 19:03:15 by mc ### ########.fr */ /* */ /* ************************************************************************** */ #include "input.h" +#include "input_utils.h" + #include "global.h" #include "const.h" @@ -37,33 +39,18 @@ static void set_action(int keycode, bool value) set_quit(NULL); } -int hook_keypress(int keycode) +static int hook_keypress(int keycode) { set_action(keycode, true); return (0); } -int hook_keyrelease(int keycode) +static int hook_keyrelease(int keycode) { set_action(keycode, false); return (0); } -static int hook_mousemove(int x, int y) -{ - static const t_point_int window_middle = { - SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2}; - static bool first_call = true; - const int dx = x - window_middle.x; - - if (!first_call && dx) - g_input_actions.rotate += dx; - first_call = false; - if (x != window_middle.x || y != window_middle.y) - mlx_mouse_move(g_mlx, g_win, window_middle.x, window_middle.y); - return (0); -} - // TODO hide cursor without leak? void input_init(void *mlx_ptr, void *win_ptr) { diff --git a/src/input_utils.c b/src/input_utils.c new file mode 100644 index 0000000..1e8e415 --- /dev/null +++ b/src/input_utils.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* input_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mc +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/07 19:02:04 by mc #+# #+# */ +/* Updated: 2024/11/07 19:02:15 by mc ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "input_utils.h" + +static int hook_mousemove(int x, int y) +{ + static const t_point_int window_middle = { + SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2}; + static bool first_call = true; + const int dx = x - window_middle.x; + + if (!first_call && dx) + g_input_actions.rotate += dx; + first_call = false; + if (x != window_middle.x || y != window_middle.y) + mlx_mouse_move(g_mlx, g_win, window_middle.x, window_middle.y); + return (0); +} diff --git a/src/input_utils.h b/src/input_utils.h new file mode 100644 index 0000000..0006b00 --- /dev/null +++ b/src/input_utils.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* input_utils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mc +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/07 19:02:26 by mc #+# #+# */ +/* Updated: 2024/11/07 19:03:02 by mc ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef INPUT_UTILS_H +# define INPUT_UTILS_H + +# include "input.h" + +static int hook_mousemove(int x, int y); + +#endif diff --git a/src/main.c b/src/main.c index dc27d99..ebc393e 100644 --- a/src/main.c +++ b/src/main.c @@ -6,17 +6,17 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 16:57:40 by mc #+# #+# */ -/* Updated: 2024/11/07 17:12:44 by mc ### ########.fr */ +/* Updated: 2024/11/07 18:44:01 by mc ### ########.fr */ /* */ /* ************************************************************************** */ #include "main.h" -# include "render.h" -# include "move.h" -# include "input.h" -# include "const.h" -# include "global.h" +#include "render.h" +#include "move.h" +#include "input.h" +#include "const.h" +#include "global.h" static int g_return_value = 0; diff --git a/src/map1.c b/src/map1.c index 97a6d4a..ce75afc 100644 --- a/src/map1.c +++ b/src/map1.c @@ -6,7 +6,7 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/01 17:12:58 by mcolonna #+# #+# */ -/* Updated: 2024/11/07 15:49:39 by mc ### ########.fr */ +/* Updated: 2024/11/07 18:58:34 by mc ### ########.fr */ /* */ /* ************************************************************************** */ @@ -81,7 +81,8 @@ void map_destroy(t_map *map) d = 0; while (d < 4) { - mlx_destroy_image(g_mlx, map->textures[d].image); + if (map->textures[d].image) + mlx_destroy_image(g_mlx, map->textures[d].image); free((void *)map->texture_srcs[d]); d++; } diff --git a/src/map_utils.h b/src/map_utils.h index db5f72b..15a3713 100644 --- a/src/map_utils.h +++ b/src/map_utils.h @@ -6,7 +6,7 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/03 15:05:13 by mc #+# #+# */ -/* Updated: 2024/11/06 19:34:01 by mc ### ########.fr */ +/* Updated: 2024/11/07 19:04:19 by mc ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,6 +74,6 @@ bool read_map_description(t_map *map, t_stream *stream); /// @param map Map to use. void map_init_objects(t_map *map); -bool load_textures(t_map *map); +bool load_textures(t_map *map); #endif diff --git a/src/map_utils1.c b/src/map_utils1.c index 74abb76..728d2c5 100644 --- a/src/map_utils1.c +++ b/src/map_utils1.c @@ -6,7 +6,7 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/03 15:02:09 by mc #+# #+# */ -/* Updated: 2024/11/06 19:33:31 by mc ### ########.fr */ +/* Updated: 2024/11/07 18:59:07 by mc ### ########.fr */ /* */ /* ************************************************************************** */ @@ -74,41 +74,40 @@ bool read_string_parameter(const char *name, const char **dest, static void read_map2(t_map *dest, bool *err, bool *rdf) { - ft_memset(dest, '\0', sizeof(dest)); + ft_memset(dest, '\0', sizeof(t_map)); dest->color_ceiling = 0xFF000000; dest->color_floor = 0xFF000000; *err = false; *rdf = false; } -bool read_map(t_map *dest, t_stream *s) +bool read_map(t_map *d, t_stream *s) { - bool err; - bool rdf; + bool err[2]; int old_stream_i; - read_map2(dest, &err, &rdf); - while (!rdf && !err && s->str[s->i]) + read_map2(d, &err[0], &err[1]); + while (!err[1] && !err[0] && s->str[s->i]) { old_stream_i = s->i; - if ((read_expected_string("\n", s, &err), err) - && !read_string_parameter("NO", &dest->texture_srcs[NORTH], s, &rdf) - && !read_string_parameter("SO", &dest->texture_srcs[SOUTH], s, &rdf) - && !read_string_parameter("WE", &dest->texture_srcs[WEST], s, &rdf) - && !read_string_parameter("EA", &dest->texture_srcs[EAST], s, &rdf) - && !read_color_parameter("F", &dest->color_floor, s, &rdf) - && !read_color_parameter("C", &dest->color_ceiling, s, &rdf)) + if ((read_expected_string("\n", s, &err[0]), err[0]) + && !read_string_parameter("NO", &d->texture_srcs[NORTH], s, &err[1]) + && !read_string_parameter("SO", &d->texture_srcs[SOUTH], s, &err[1]) + && !read_string_parameter("WE", &d->texture_srcs[WEST], s, &err[1]) + && !read_string_parameter("EA", &d->texture_srcs[EAST], s, &err[1]) + && !read_color_parameter("F", &d->color_floor, s, &err[1]) + && !read_color_parameter("C", &d->color_ceiling, s, &err[1])) { s->i = old_stream_i; - err = !read_map_description(dest, s); + err[0] = !read_map_description(d, s); break ; } - err = false; + err[0] = false; } - if (!rdf && !dest->cases) - err = (write_err("Map description missing\n", NULL), true); - (rdf || err) && (map_destroy(dest), false); - return (!err && !rdf && dest->cases); + if (!err[1] && !d->cases) + err[0] = (write_err("Map description missing\n", NULL), true); + (err[1] || err[0]) && (map_destroy(d), false); + return (!err[0] && !err[1] && d->cases); } void map_init_objects(t_map *map) diff --git a/src/map_utils2.c b/src/map_utils2.c index ecb7e8d..3f55963 100644 --- a/src/map_utils2.c +++ b/src/map_utils2.c @@ -6,7 +6,7 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/04 15:12:08 by mcolonna #+# #+# */ -/* Updated: 2024/11/07 00:08:13 by mc ### ########.fr */ +/* Updated: 2024/11/07 19:05:06 by mc ### ########.fr */ /* */ /* ************************************************************************** */ @@ -103,15 +103,16 @@ bool read_map_description(t_map *map, t_stream *stream) return (true); } -bool load_textures(t_map *map) +bool load_textures(t_map *map) { t_direction d; d = 0; - while (d < 4) { + while (d < 4) + { map->textures[d].image = mlx_xpm_file_to_image(g_mlx, - (char *)(map->texture_srcs[d]), - &map->textures[d].width, &map->textures[d].height); + (char *)(map->texture_srcs[d]), + &map->textures[d].width, &map->textures[d].height); d++; } return (true); diff --git a/src/utils2.c b/src/utils2.c index 60a922b..bf686a5 100644 --- a/src/utils2.c +++ b/src/utils2.c @@ -6,7 +6,7 @@ /* By: mc +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/29 14:02:37 by mcolonna #+# #+# */ -/* Updated: 2024/10/31 17:25:34 by mc ### ########.fr */ +/* Updated: 2024/11/07 18:44:16 by mc ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ u_int32_t *get_data_addr(void *img_ptr) if (bpp != 32 || endian != 0) { write_err("Wrong bpp or wrong endian when using mlx_get_data_addr().\n", - NULL); + NULL); return (NULL); } return (r);