From 3e1e619497fb5f1051f00e1e93d87d979ff81105 Mon Sep 17 00:00:00 2001 From: adjoly Date: Tue, 24 Sep 2024 18:32:34 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=F0=9F=8F=97=EF=B8=8F=E3=80=8D=20wip:?= =?UTF-8?q?=20Map=20is=20printing=20yayyy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MacroLibX | 2 +- Makefile | 14 ++--- asdf | 8 +++ includes/game.h | 58 ++++++++++++++++--- src/cub3d.c | 19 ++++-- src/raycasting/get_player_image.c | 49 ++++++++++++++++ .../draw_square.c} | 33 +++++------ src/raycasting/key_hook.c | 5 +- src/raycasting/print_map.c | 36 ++++++++++++ 9 files changed, 184 insertions(+), 40 deletions(-) create mode 100644 asdf create mode 100644 src/raycasting/get_player_image.c rename src/raycasting/{player_print.c => graphic_utils/draw_square.c} (54%) create mode 100644 src/raycasting/print_map.c diff --git a/MacroLibX b/MacroLibX index 5a09ebb..3dd68f9 160000 --- a/MacroLibX +++ b/MacroLibX @@ -1 +1 @@ -Subproject commit 5a09ebb179f8f20afa8405a6d1e29845f0952c46 +Subproject commit 3dd68f994248bfa6e6c634ca9c838dfe48b53520 diff --git a/Makefile b/Makefile index b6a727a..f6715d7 100644 --- a/Makefile +++ b/Makefile @@ -20,15 +20,15 @@ SRCS = $(shell find src -name *.c) OBJS = $(addprefix $(OBJSDIR), $(SRCS:.c=.o)) -FLAGS = -Wall -Werror -Wextra -g -lm -lSDL2 +FLAGS = -Wall -Werror -Wextra -g -lm -lSDL2 -MMD -MP LIB = libft/libft.a \ MacroLibX/libmlx.so $(NAME): $(OBJS) - @make -sj $(nproc) -C $(LIBFT_DIR) + @make -sj -C $(LIBFT_DIR) @echo "โœ… Libft compiled" - @make -sj $(nproc) -C $(MACRO_DIR) > /dev/null + @make -sj -C $(MACRO_DIR) @echo "โœ… MacroLibX compiled" @$(CC) $(FLAGS) $(OBJS) $(LIB) -o $(NAME) @echo "โœ… Compiled" @@ -40,14 +40,14 @@ $(OBJSDIR)%.o: %.c all: $(NAME) clean: - @make -s -C $(LIBFT_DIR) clean - @make -s -C $(MACRO_DIR) clean > /dev/null + @make -sC $(LIBFT_DIR) clean + @make -sC $(MACRO_DIR) clean > /dev/null @rm -f $(OBJS) fclean: clean - @make -s -C $(LIBFT_DIR) fclean + @make -sC $(LIBFT_DIR) fclean @echo "๐Ÿงน Libft Cleaned" - @make -s -C $(MACRO_DIR) fclean > /dev/null + @make -sC $(MACRO_DIR) fclean > /dev/null @echo "๐Ÿงน MacroLibX Cleaned" @rm -f $(NAME) @rm -Rf $(OBJSDIR) diff --git a/asdf b/asdf new file mode 100644 index 0000000..e7bebc5 --- /dev/null +++ b/asdf @@ -0,0 +1,8 @@ +# 1 "" +# 1 "" 1 +# 1 "" 3 +# 384 "" 3 +# 1 "" 1 +# 1 "" 2 +# 1 "" 2 + diff --git a/includes/game.h b/includes/game.h index 7d7b86e..38245d1 100644 --- a/includes/game.h +++ b/includes/game.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/11 16:38:40 by adjoly #+# #+# */ -/* Updated: 2024/09/17 10:47:26 by adjoly ### ########.fr */ +/* Updated: 2024/09/24 16:50:56 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,15 +15,24 @@ # include "parsing.h" # include +# include +# include +# include + +typedef struct s_player +{ + t_coord coords; + double direction; +} t_player; typedef struct s_cub { - void *mlx; - void *win; - void *sprites; - void *img; - char **map; - t_coord p_coord; + void *mlx; + void *win; + void *sprites; + void *img; + char **map; + t_player player; } t_cub; # define ESCAPE_KEY 41 @@ -34,6 +43,9 @@ typedef struct s_cub # define WHITE 0xFFFFFFFF # define WINDOW_Y 900 # define WINDOW_X 1600 +# define PLAYER_ROT_SPEED (2 * M_PI) / 8 +# define PLAYER_SPEED 3 +# define MAP_CHUNK_SIZE 64 /** * @brief This function is used to handle keypress @@ -52,6 +64,36 @@ int key_hook(int key, void *param); * thing * */ -void *get_player_image(t_cub *cub, uint8_t key_pressed); +void get_player_image(t_cub *cub, uint8_t key_pressed); +/** + * @brief This function is here to change the direction of the player + * by setting the t_player->direction and check overflow to be + * sure it stays between 0 and 2pi because it is expressed in + * radians + * + * @param speed The speed a which the player rotate + * @param clockwise The direction which the player rotate + * @param player A pointer to a t_player struct + * + */ +void change_direction(double speed, bool clockwise, t_player *player); + +/** + * @brief Function used to draw a square + * + * @param cub The adress of a t_cub struct + * @param coord The coordinate of the printed square + * @param size The size of the printed square + * @param color The color of the printed square + * + */ +void draw_square(t_cub *cub, t_coord coord, uint16_t size, int color); + +/** + * @brief Function used to draw the map + * + * @param cub The address of the t_cub struct + */ +void print_map(t_cub *cub); #endif diff --git a/src/cub3d.c b/src/cub3d.c index 8da2f09..bdd9239 100644 --- a/src/cub3d.c +++ b/src/cub3d.c @@ -6,7 +6,7 @@ /* By: madumerg +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/04 16:58:27 by madumerg #+# #+# */ -/* Updated: 2024/09/17 10:36:01 by adjoly ### ########.fr */ +/* Updated: 2024/09/24 18:31:05 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,13 +18,22 @@ int main(int ac, char **av) { t_cub cub; + cub.map = (char *[]){ + "1111111\0", + "1000101\0", + "1000101\0", + "1000001\0", + "1111111\0", + NULL + }; (void)ac; (void)av; cub.mlx = mlx_init(); - cub.win = mlx_new_window(cub.mlx, 400, 400, "WTF"); - cub.p_coord.x = 100; - cub.p_coord.y = 100; - cub.img = get_player_image(&cub, 0); + cub.win = mlx_new_window(cub.mlx, 600, 600, "WTF"); + cub.player.coords.x = 100; + cub.player.coords.y = 100; + cub.img = mlx_new_image(cub.mlx, 600, 600); + get_player_image(&cub, 0); mlx_put_image_to_window(cub.mlx, cub.win, cub.img, 0, 0); mlx_on_event(cub.mlx, cub.win, MLX_KEYDOWN, key_hook, &cub); mlx_loop(cub.mlx); diff --git a/src/raycasting/get_player_image.c b/src/raycasting/get_player_image.c new file mode 100644 index 0000000..0cc6fc4 --- /dev/null +++ b/src/raycasting/get_player_image.c @@ -0,0 +1,49 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_player_image.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/13 11:05:02 by adjoly #+# #+# */ +/* Updated: 2024/09/24 14:40:23 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "game.h" +#include "mlx.h" +#include "stdio.h" + +void change_direction(double speed, bool clockwise, t_player *player) +{ + if (clockwise) + player->direction -= speed; + else + player->direction += speed; + if (player->direction >= 2 * M_PI) + player->direction -= 2 * M_PI; + else if (player->direction < 0) + player->direction += 2 * M_PI; + printf("%f\n", player->direction); +} + +void get_player_image(t_cub *cub, uint8_t key_pressed) +{ + if (key_pressed == W_KEY) + { + cub->player.coords.x += PLAYER_SPEED * cos(cub->player.direction); + cub->player.coords.y += PLAYER_SPEED * sin(cub->player.direction); + } + else if (key_pressed == S_KEY) + { + cub->player.coords.x -= PLAYER_SPEED * cos(cub->player.direction); + cub->player.coords.y -= PLAYER_SPEED * sin(cub->player.direction); + } + else if (key_pressed == D_KEY) + change_direction(PLAYER_ROT_SPEED, false, &cub->player); + else if (key_pressed == A_KEY) + change_direction(PLAYER_ROT_SPEED, true, &cub->player); + print_map(cub); + draw_square(cub, (t_coord){cub->player.coords.x - 2, cub->player.coords.y - 2}, \ + 5, WHITE); +} diff --git a/src/raycasting/player_print.c b/src/raycasting/graphic_utils/draw_square.c similarity index 54% rename from src/raycasting/player_print.c rename to src/raycasting/graphic_utils/draw_square.c index 388b7aa..b847ad0 100644 --- a/src/raycasting/player_print.c +++ b/src/raycasting/graphic_utils/draw_square.c @@ -1,32 +1,31 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* player_print.c :+: :+: :+: */ +/* draw_square.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/09/13 11:05:02 by adjoly #+# #+# */ -/* Updated: 2024/09/17 10:30:44 by adjoly ### ########.fr */ +/* Created: 2024/09/17 11:09:52 by adjoly #+# #+# */ +/* Updated: 2024/09/23 14:05:54 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ #include "game.h" -#include #include "mlx.h" -void *get_player_image(t_cub *cub, uint8_t key_pressed) +void draw_square(t_cub *cub, t_coord coord, uint16_t size, int color) { - void *img; + t_coord start; - img = mlx_new_image(cub->mlx, WINDOW_X, WINDOW_Y); - if (key_pressed == W_KEY) - cub->p_coord.y--; - else if (key_pressed == S_KEY) - cub->p_coord.y++; - else if (key_pressed == D_KEY) - cub->p_coord.x++; - else if (key_pressed == A_KEY) - cub->p_coord.x--; - mlx_set_image_pixel(cub->mlx, img, cub->p_coord.x, cub->p_coord.y, WHITE); - return (img); + start = coord; + while (coord.x - start.x <= size) + { + coord.y = start.y; + while (coord.y - start.y <= size) + { + mlx_set_image_pixel(cub->mlx, cub->img, coord.x, coord.y, color); + coord.y++; + } + coord.x++; + } } diff --git a/src/raycasting/key_hook.c b/src/raycasting/key_hook.c index e0a6976..f49e924 100644 --- a/src/raycasting/key_hook.c +++ b/src/raycasting/key_hook.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/11 16:37:56 by adjoly #+# #+# */ -/* Updated: 2024/09/17 10:33:35 by adjoly ### ########.fr */ +/* Updated: 2024/09/23 14:41:31 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,8 @@ int key_hook(int key, void *param) { mlx_clear_window(cub->mlx, cub->win); mlx_destroy_image(cub->mlx, cub->img); - cub->img = get_player_image(cub, key); + cub->img = mlx_new_image(cub->mlx, 600, 600); + get_player_image(cub, key); mlx_put_image_to_window(cub->mlx, cub->win, cub->img, 0, 0); } return (0); diff --git a/src/raycasting/print_map.c b/src/raycasting/print_map.c new file mode 100644 index 0000000..9c8e56a --- /dev/null +++ b/src/raycasting/print_map.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/23 14:09:17 by adjoly #+# #+# */ +/* Updated: 2024/09/23 14:41:51 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "game.h" +#include "mlx.h" +#include + +void print_map(t_cub *cub) +{ + size_t i; + size_t j; + + i = 0; + while (cub->map[i]) + { + j = 0; + while (cub->map[i][j]) + { + if (cub->map[i][j] == '1') + { + draw_square(cub, (t_coord){j * MAP_CHUNK_SIZE, i * MAP_CHUNK_SIZE}, MAP_CHUNK_SIZE, WHITE); + } + j++; + } + i++; + } +}