From fb98fd19ba9e27eb3a169a7599ce41fb0877b7a6 Mon Sep 17 00:00:00 2001 From: Rudy Bousset Date: Thu, 13 Feb 2020 19:22:15 +0100 Subject: Better code structure --- Makefile | 6 ++-- inc/cub3d.h | 12 +++++--- inc/cub3d_structs.h | 20 ++++---------- src/ft_draw_map.c | 46 +++++++++++++++++++++++++++++++ src/ft_draw_scene.c | 33 ++++++++++++++++++++++ src/ft_draw_square.c | 37 +++++++++++++++++++++++++ src/ft_drawmap.c | 77 ---------------------------------------------------- src/ft_drawsquare.c | 37 ------------------------- src/ft_extra_keys.c | 18 ++++++++++++ src/ft_init_lists.c | 1 + src/ft_key_events.c | 5 +++- src/main.c | 2 +- 12 files changed, 157 insertions(+), 137 deletions(-) create mode 100644 src/ft_draw_map.c create mode 100644 src/ft_draw_scene.c create mode 100644 src/ft_draw_square.c delete mode 100644 src/ft_drawmap.c delete mode 100644 src/ft_drawsquare.c create mode 100644 src/ft_extra_keys.c diff --git a/Makefile b/Makefile index ffe8446..70a75e6 100644 --- a/Makefile +++ b/Makefile @@ -38,8 +38,10 @@ SRCS_NAME += ft_check_map_line.c SRCS_NAME += ft_free_words.c SRCS_NAME += ft_map_error.c SRCS_NAME += ft_init_winlx.c -SRCS_NAME += ft_drawsquare.c -SRCS_NAME += ft_drawmap.c +SRCS_NAME += ft_draw_square.c +SRCS_NAME += ft_draw_map.c +SRCS_NAME += ft_draw_scene.c +SRCS_NAME += ft_extra_keys.c SRCS_NAME += ft_draw_verline.c SRCS_NAME += ft_print_list.c SRCS_NAME += ft_rgb_to_hex.c diff --git a/inc/cub3d.h b/inc/cub3d.h index f0dbf61..81692e4 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -39,6 +39,9 @@ # ifndef FT_ESC_KEY # define FT_ESC_KEY 53 # endif +# ifndef FT_F1_KEY +# define FT_F1_KEY 122 +# endif # ifndef FT_SCR_SIZE # define FT_SCR_SIZE "1920x1080" # endif @@ -59,7 +62,7 @@ void ft_hooks_and_loops(t_win *wl, t_cub *cl); int ft_key_event(int keycode, t_cub *clist); int ft_click_close(int keycode, t_cub *clist); int ft_exit(uint8_t exit_code, t_cub *clist); -void ft_drawsquare(int a, int b, int rgb, t_cub *clist); +void ft_draw_square(int a, int b, int rgb, t_cub *clist); void ft_parse_map(const char *map_path, t_cub *clist); int8_t ft_select_get(char **words, t_cub *clist); int8_t ft_get_screen_size(t_win *wlist); @@ -79,10 +82,9 @@ int ft_check_missing(t_cub *clist); int8_t ft_check_map_line(char *line, uint8_t l, t_cub *clist); size_t ft_get_line_len(char *line); int ft_missing_error(const char *err, t_cub *clist); -uint8_t ft_free_words(char **words); -int ft_map_error(t_cub *clist); +uint8_t ft_free_words(char **words); int ft_map_error(t_cub *clist); int ft_init_winlx(t_cub *clist); -void ft_drawmap(t_cub *clist); +void ft_draw_scene(t_cub *clist); void ft_print_list(t_cub *clist); uint32_t ft_rgb_to_hex(t_rgb rgb); t_ray ft_init_s_ray(void); @@ -90,5 +92,7 @@ void ft_detect(t_cub *cl); int8_t ft_draw_verline(t_cub *cl, int32_t x, int32_t y1, int32_t y2, int32_t color); void ft_castray(t_cub *cl); +void ft_draw_map(char **map, t_cub *clist); +int8_t ft_f1_key(t_cub *clist); # endif diff --git a/inc/cub3d_structs.h b/inc/cub3d_structs.h index 0c3cb7c..f40a438 100644 --- a/inc/cub3d_structs.h +++ b/inc/cub3d_structs.h @@ -48,21 +48,6 @@ typedef struct s_player float plane_y; } t_player; -/* typedef struct s_ray */ -/* { */ -/* double x_ray_position; */ -/* double y_ray_position; */ -/* double x_ray_direction; */ -/* double y_ray_direction; */ -/* double x_side_distance; */ -/* double y_side_distance; */ -/* double x_delta_distance; */ -/* double y_delta_distance; */ -/* int hitX; */ -/* int hitY; */ -/* int hits[150]; */ -/* } t_ray; */ - typedef struct s_ray { uint16_t line_h; @@ -83,6 +68,10 @@ typedef struct s_ray uint8_t hit; } t_ray; +/* typedef struct s_map */ +/* { */ +/* } t_map; */ + typedef struct s_cub { char *no_tex_path; @@ -100,6 +89,7 @@ typedef struct s_cub size_t map_start; uint8_t isspawn; uint8_t scale; + uint8_t minimap; struct s_win *wlist; struct s_player *plist; struct s_ray rlist; diff --git a/src/ft_draw_map.c b/src/ft_draw_map.c new file mode 100644 index 0000000..e32b4e9 --- /dev/null +++ b/src/ft_draw_map.c @@ -0,0 +1,46 @@ +#include +#include + +static void + ft_draw_player(t_player *plist, t_cub *clist) +{ + const float x = plist->pos_x; + const float y = plist->pos_y - 1; + const uint16_t scale = clist->scale; + + ft_draw_square( + scale + (x * (scale)), + scale + (y * (scale)), + 0x009843fa, + clist); +} + +void + ft_draw_map(char **map, t_cub *clist) +{ + const uint8_t scale = clist->scale; + size_t x; + size_t y; + + x = 0; + y = 0; + while (map[y]) + { + while (map[y][x]) + { + if (map[y][x] == '1') + ft_draw_square(scale + (x * (scale)), + scale + (y * (scale)), 0x00aa99aa, clist); + else if (map[y][x] == '2') + ft_draw_square(scale + (x * (scale)), + scale + (y * (scale)), 0x0033ccff, clist); + else + ft_draw_square(scale + (x * (scale)), scale + (y * (scale)), + ft_rgb_to_hex(clist->f_rgb), clist); + x++; + } + x = 0; + y++; + } + ft_draw_player(clist->plist, clist); +} diff --git a/src/ft_draw_scene.c b/src/ft_draw_scene.c new file mode 100644 index 0000000..57cecd4 --- /dev/null +++ b/src/ft_draw_scene.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_drawmap.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2020/02/02 17:19:18 by rbousset #+# ## ## #+# */ +/* Updated: 2020/02/02 17:19:19 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include +#include +#include +#include + +void + ft_draw_scene(t_cub *clist) +{ + mlx_clear_window(clist->wlist->wlx, clist->wlist->winptr); + clist->img.img = mlx_new_image(clist->wlist->wlx, + clist->wlist->x_size, clist->wlist->y_size); + clist->img.ptr = mlx_get_data_addr(clist->img.img, &clist->img.bpp, + &clist->img.sizeline, &clist->img.endian); + ft_castray(clist); + if (clist->minimap) + ft_draw_map(clist->map, clist); + mlx_put_image_to_window(clist->wlist->wlx, + clist->wlist->winptr, clist->img.img, 0, 0); + mlx_destroy_image(clist->wlist->wlx, clist->img.img); +} diff --git a/src/ft_draw_square.c b/src/ft_draw_square.c new file mode 100644 index 0000000..72cf904 --- /dev/null +++ b/src/ft_draw_square.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* LE - / */ +/* / */ +/* ft_drawsquare.c .:: .:/ . .:: */ +/* +:+:+ +: +: +:+:+ */ +/* By: rbousset +:+ +: +: +:+ */ +/* #+# #+ #+ #+# */ +/* Created: 2020/02/02 17:19:22 by rbousset #+# ## ## #+# */ +/* Updated: 2020/02/02 17:19:22 by rbousset ### #+. /#+ ###.fr */ +/* / */ +/* / */ +/* ************************************************************************** */ + +#include +#include +#include + +void + ft_draw_square(int a, int b, int rgb, t_cub *clist) +{ + const uint16_t scale = clist->scale; + int x; + int y; + + x = a; + y = b; + while (x > a - scale) + { + while (y > b - scale) + { + *(int*)(clist->img.ptr + (x * 4 + (y * clist->img.sizeline))) = rgb; + y--; + } + y = b; + x--; + } +} diff --git a/src/ft_drawmap.c b/src/ft_drawmap.c deleted file mode 100644 index b8b4e2c..0000000 --- a/src/ft_drawmap.c +++ /dev/null @@ -1,77 +0,0 @@ -/* ************************************************************************** */ -/* LE - / */ -/* / */ -/* ft_drawmap.c .:: .:/ . .:: */ -/* +:+:+ +: +: +:+:+ */ -/* By: rbousset +:+ +: +: +:+ */ -/* #+# #+ #+ #+# */ -/* Created: 2020/02/02 17:19:18 by rbousset #+# ## ## #+# */ -/* Updated: 2020/02/02 17:19:19 by rbousset ### #+. /#+ ###.fr */ -/* / */ -/* / */ -/* ************************************************************************** */ - -#include -#include -#include -#include - -static void - ft_draw_core_map(char **map, t_cub *clist) -{ - const uint8_t scale = clist->scale; - size_t x; - size_t y; - - x = 0; - y = 0; - while (map[y]) - { - while (map[y][x]) - { - if (map[y][x] == '1') - ft_drawsquare(scale + (x * (scale)), - scale + (y * (scale)), 0x00aa99aa, clist); - else if (map[y][x] == '2') - ft_drawsquare(scale + (x * (scale)), - scale + (y * (scale)), 0x0033ccff, clist); - else - ft_drawsquare(scale + (x * (scale)), - scale + (y * (scale)), - ft_rgb_to_hex(clist->f_rgb), clist); - x++; - } - x = 0; - y++; - } -} - -static void - ft_draw_player(t_player *plist, t_cub *clist) -{ - const float x = plist->pos_x; - const float y = plist->pos_y; - const uint16_t scale = clist->scale; - - ft_drawsquare( - scale + (x * (scale + 1)), - scale + (y * (scale + 1)), - 0x009843fa, - clist); -} - -void - ft_drawmap(t_cub *clist) -{ - mlx_clear_window(clist->wlist->wlx, clist->wlist->winptr); - clist->img.img = mlx_new_image(clist->wlist->wlx, - clist->wlist->x_size, clist->wlist->y_size); - clist->img.ptr = mlx_get_data_addr(clist->img.img, &clist->img.bpp, - &clist->img.sizeline, &clist->img.endian); - ft_castray(clist); - ft_draw_core_map(clist->map, clist); - ft_draw_player(clist->plist, clist); - mlx_put_image_to_window(clist->wlist->wlx, - clist->wlist->winptr, clist->img.img, 0, 0); - mlx_destroy_image(clist->wlist->wlx, clist->img.img); -} diff --git a/src/ft_drawsquare.c b/src/ft_drawsquare.c deleted file mode 100644 index dd7cce7..0000000 --- a/src/ft_drawsquare.c +++ /dev/null @@ -1,37 +0,0 @@ -/* ************************************************************************** */ -/* LE - / */ -/* / */ -/* ft_drawsquare.c .:: .:/ . .:: */ -/* +:+:+ +: +: +:+:+ */ -/* By: rbousset +:+ +: +: +:+ */ -/* #+# #+ #+ #+# */ -/* Created: 2020/02/02 17:19:22 by rbousset #+# ## ## #+# */ -/* Updated: 2020/02/02 17:19:22 by rbousset ### #+. /#+ ###.fr */ -/* / */ -/* / */ -/* ************************************************************************** */ - -#include -#include -#include - -void - ft_drawsquare(int a, int b, int rgb, t_cub *clist) -{ - const uint16_t scale = clist->scale; - int x; - int y; - - x = a; - y = b; - while (x > a - scale) - { - while (y > b - scale) - { - *(int*)(clist->img.ptr + (x * 4 + (y * clist->img.sizeline))) = rgb; - y--; - } - y = b; - x--; - } -} diff --git a/src/ft_extra_keys.c b/src/ft_extra_keys.c new file mode 100644 index 0000000..5bbb4b4 --- /dev/null +++ b/src/ft_extra_keys.c @@ -0,0 +1,18 @@ +#include +#include + +int8_t + ft_f1_key(t_cub *clist) +{ + if (clist->minimap == 0) + { + clist->minimap = 1; + ft_draw_scene(clist); + } + else if (clist->minimap == 1) + { + clist->minimap = 0; + ft_draw_scene(clist); + } + return (0); +} diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c index 8bb0d24..d56f40c 100644 --- a/src/ft_init_lists.c +++ b/src/ft_init_lists.c @@ -89,6 +89,7 @@ static t_cub clist->y_step = 0; clist->line_chk = 0; clist->map_start = 0; + clist->minimap = 0; clist->isspawn = 0; clist->f_rgb = ft_init_rgb(); clist->c_rgb = ft_init_rgb(); diff --git a/src/ft_key_events.c b/src/ft_key_events.c index 2112d2e..40f211a 100644 --- a/src/ft_key_events.c +++ b/src/ft_key_events.c @@ -73,6 +73,7 @@ int (tmp_code == FT_S_KEY) ? (keycode = 2) : 0; (tmp_code == FT_D_KEY) ? (keycode = 3) : 0; (tmp_code == 3) ? (keycode = INT16_MAX) : 0; + (tmp_code == 4) ? (keycode = INT16_MAX) : 0; pl = clist->plist; if (keycode <= 3) { @@ -81,10 +82,12 @@ int (pl->pos_x < 0.4) ? (pl->pos_x = 0.4) : 0; (pl->pos_y > clist->map_h - 1.4) ? (pl->pos_y = clist->map_h - 1.4) : 0; (pl->pos_x > clist->map_w - 1.4) ? (pl->pos_x = clist->map_w - 1.4) : 0; - ft_drawmap(clist); + ft_draw_scene(clist); return (0); } else if (keycode == FT_ESC_KEY) return (ft_exit(0, (clist))); + else if (keycode == FT_F1_KEY) + return (ft_f1_key(clist)); return (0); } diff --git a/src/main.c b/src/main.c index 39b5f7c..8dfac59 100644 --- a/src/main.c +++ b/src/main.c @@ -32,7 +32,7 @@ int ft_parse_map(argv[1], clist); if (ft_init_winlx(clist) < 0) return (ft_exit(3, clist)); - ft_drawmap(clist); + ft_draw_scene(clist); ft_hooks_and_loops(clist->wlist, clist); return (0); } -- cgit v1.2.3