diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml index a03401a..4244282 100644 --- a/.github/workflows/c-cpp.yml +++ b/.github/workflows/c-cpp.yml @@ -35,4 +35,4 @@ jobs: - name: make run: make - name: cub3d - run: ./cub3d maps/valid/mandatory.cub + run: ./cub3D maps/valid/mandatory.cub diff --git a/Makefile b/Makefile index f3ba90c..f6eea4e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ -NAME = cub3d +MAKEFLAGS += --silent +NAME = cub3D SRCDIR = src OBJDIR = .obj INCDIR = inc @@ -6,12 +7,15 @@ MINILIB_DIR = $(INCDIR)/minilibx MINILIB = $(MINILIB_DIR)/libmlx.a LIBFT_DIR = $(INCDIR)/libft LIBFT = $(LIBFT_DIR)/libft.a +DEBUG = 0 +BONUS = 0 CC = cc -CFLAGS = -Wall -Wextra -Werror -I$(INCDIR) -I$(LIBFT_DIR) -g3 -O0 -Wshadow -Wstrict-prototypes -Wpointer-arith +CFLAGS = -Wall -Wextra -Werror -I$(INCDIR) -I$(LIBFT_DIR) -g3 -O0 -Wshadow -Wpointer-arith ASAN_FLAGS = -fsanitize=address,undefined -fno-omit-frame-pointer SRC =main.c \ core/utils.c \ core/win.c \ + core/mouse_bonus.c \ \ debug/main.c \ \ @@ -70,7 +74,7 @@ all: $(NAME) compile_flags.txt @printf " $(C)✅ [$(NAME)] $(B)Build complete$(X)\n" $(NAME): $(OBJ) $(LIBFT) $(MINILIB) - @$(CC) $(CFLAGS) -L$(LIBFT_DIR) $(OBJ) $(LIBFT) -L$(MINILIB_DIR) $(MINILIB) -lmlx -lXext -lX11 -lm -o $(NAME) + @$(CC) $(CFLAGS) -DDEBUG=$(DEBUG) -DBONUS=$(BONUS) -L$(LIBFT_DIR) $(OBJ) $(LIBFT) -L$(MINILIB_DIR) $(MINILIB) -lmlx -lXext -lX11 -lm -o $(NAME) $(MINILIB): @printf "$(Y) 📚 [MiniLibX] building... $(RESET)" @@ -81,7 +85,7 @@ $(LIBFT): $(OBJDIR)/%.o: $(SRCDIR)/%.c @mkdir -p $(@D) - @$(CC) $(CFLAGS) -c $< -o $@ + @$(CC) $(CFLAGS) -DDEBUG=$(DEBUG) -DBONUS=$(BONUS) -c $< -o $@ @$(call progress,$(words $(SRC))) clean: @@ -100,8 +104,13 @@ re: fclean all asan: CFLAGS += $(VAL_FLAGS) $(ASAN_FLAGS) asan: re -run: re - ./$(NAME) +bonus: + make re BONUS=1 + +debug: + make re DEBUG=1 +bonus-dbg: + make re BONUS=1 DEBUG=1 norm: @norminette src inc/*.h inc/libft | grep "Error" || echo "${G}No errors found :3${X}" @@ -109,4 +118,4 @@ norm: compile_flags.txt: Makefile @printf '%s\n' $(filter -I% -D% -std=%,$(CPPFLAGS) $(CFLAGS)) > $@ -.PHONY: all clean fclean re debug asan +.PHONY: all clean fclean re debug asan bonus bonus-dbg diff --git a/inc/main.h b/inc/main.h index a39344e..e8ca33b 100644 --- a/inc/main.h +++ b/inc/main.h @@ -13,6 +13,11 @@ #ifndef MAIN_H # define MAIN_H +# include +# include +# include +# include + # include "libft/libft.h" # include "minilibx/mlx.h" # include "struct.h" @@ -22,9 +27,16 @@ # include "mlx_win.h" # include "parsing.h" # include "player.h" -# include # include "textures.h" # include "mini_map.h" # include "raycast.h" -# include + +# ifndef DEBUG +# define DEBUG 0 +# endif + +# ifndef BONUS +# define BONUS 1 +# endif + #endif diff --git a/inc/minilibx/mlx.h b/inc/minilibx/mlx.h index 21d9050..a13d348 100644 --- a/inc/minilibx/mlx.h +++ b/inc/minilibx/mlx.h @@ -6,7 +6,7 @@ /* By: rpinheir +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/11/04 12:28:03 by rpinheir #+# #+# */ -/* Updated: 2026/11/04 12:30:09 by rpinheir ### ########.ch */ +/* Updated: 2026/11/24 14:22:28 by rpinheir ### ########.ch */ /* */ /* ************************************************************************** */ @@ -103,7 +103,7 @@ int mlx_destroy_display(void *mlx_ptr); */ int mlx_hook(void *win_ptr, int x_event, int x_mask, - int (*funct)(void), void *param); + int (*funct)(), void *param); int mlx_do_key_autorepeatoff(void *mlx_ptr); int mlx_do_key_autorepeaton(void *mlx_ptr); diff --git a/inc/mlx_win.h b/inc/mlx_win.h index 08274a2..343394e 100644 --- a/inc/mlx_win.h +++ b/inc/mlx_win.h @@ -6,17 +6,19 @@ /* By: ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/11/12 15:00:51 by ch #+# #+# */ -/* Updated: 2026/11/16 20:08:58 by rpinheir ### ########.ch */ +/* Updated: 2026/11/24 16:19:43 by rpinheir ### ########.ch */ /* */ /* ************************************************************************** */ #ifndef MLX_WIN_H # define MLX_WIN_H +# include "struct.h" # define HEIGHT 1080 # define WIDTH 1920 # define MINI_OFFSET_X 0 # define MINI_OFFSET_Y 0 +# define DIST_EDGE_WRAP 20 void draw_background(t_game *game); int fill_path_img(t_img *img, char *path); @@ -29,4 +31,5 @@ void clear_paths(t_game *game); int safe_exit(t_game *game, int code); void generate_minimap(t_game *game); double get_time_in_seconds(void); +int mouse_handler(int x, int y, void *param); #endif // H_WIN diff --git a/inc/struct.h b/inc/struct.h index d19f074..569f39a 100644 --- a/inc/struct.h +++ b/inc/struct.h @@ -6,7 +6,7 @@ /* By: chguerre +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2026/11/24 13:58:37 by rpinheir #+# #+# */ +/* Updated: 2026/11/24 16:20:48 by rpinheir ### ########.ch */ +/* */ +/* ************************************************************************** */ + +#include "main.h" + +static int wrap_mouse_location(int x, int y, t_game *game) +{ + if (x > WIDTH - DIST_EDGE_WRAP) + { + x = DIST_EDGE_WRAP; + mlx_mouse_move(game->mlx_ptr, game->win_ptr, x, y); + game->mouse_x = x ; + return (1); + } + if (x < DIST_EDGE_WRAP) + { + x = WIDTH - DIST_EDGE_WRAP; + mlx_mouse_move(game->mlx_ptr, game->win_ptr, x, y); + game->mouse_x = x; + return (1); + } + return (0);\ +} + +static void setup_rotation(double cos_rot, double sin_rot, t_game *game) +{ + double old_dir_x; + double old_plane_x; + + old_dir_x = game->player.dir_x; + game->player.dir_x = old_dir_x * cos_rot - game->player.dir_y * sin_rot; + game->player.dir_y = old_dir_x * sin_rot + game->player.dir_y * cos_rot; + old_plane_x = game->player.plane_x; + game->player.plane_x = old_plane_x * cos_rot + - game->player.plane_y * sin_rot; + game->player.plane_y = old_plane_x * sin_rot + + game->player.plane_y * cos_rot; +} + +int mouse_handler(int x, int y, void *param) +{ + double cos_rot; + double sin_rot; + double angle; + t_game *game; + + game = (t_game *)param; + if (wrap_mouse_location(x, y, game) != 0) + return (0); + if (x == game->mouse_x) + return (0); + angle = (x - game->mouse_x) * 0.002; + cos_rot = cos(angle); + sin_rot = sin(angle); + game->mouse_x = x; + setup_rotation(cos_rot, sin_rot, game); + if (DEBUG) + printf("dir: %f %f plane: %f %f\n", game->player.dir_x, + game->player.dir_y, game->player.plane_x, game->player.plane_y); + return (0); +} diff --git a/src/core/win.c b/src/core/win.c index f62af6a..d0a2e62 100644 --- a/src/core/win.c +++ b/src/core/win.c @@ -6,7 +6,7 @@ /* By: ch +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2026/11/12 17:26:06 by ch #+# #+# */ -/* Updated: 2026/11/12 17:26:06 by ch ### ########.ch */ +/* Updated: 2026/11/24 16:28:54 by rpinheir ### ########.ch */ /* */ /* ************************************************************************** */ @@ -59,6 +59,10 @@ int run_win_mlx(t_game *game) init_graphics(game); mlx_hook(game->win_ptr, 2, 1L << 0, (void *)handle_keypressed, game); mlx_hook(game->win_ptr, 3, 1L << 1, (void *)handle_keyrelease, game); + mlx_mouse_hide(game->mlx_ptr, game->win_ptr); + if (BONUS) + mlx_hook(game->win_ptr, MotionNotify, PointerMotionMask, + (void *)mouse_handler, game); mlx_hook(game->win_ptr, 17, 9, (void *)close_window, game); mlx_loop_hook(game->mlx_ptr, (void *)game_loop, game); mlx_loop(game->mlx_ptr); diff --git a/src/engine/player/move_player.c b/src/engine/player/move_player.c index d213073..8bce75c 100644 --- a/src/engine/player/move_player.c +++ b/src/engine/player/move_player.c @@ -12,38 +12,38 @@ #include "main.h" -int move_up(t_player *p, char **map, double speed, double pad) +int move_up(t_player *p, char **map, double speed) { - if (map[(int)p->pos_y][(int)(p->pos_x + p->dir_x * (speed + pad))] != '1') + if (map[(int)p->pos_y][(int)(p->pos_x + p->dir_x * speed)] != '1') p->pos_x += p->dir_x * speed; - if (map[(int)(p->pos_y + p->dir_y * (speed + pad))][(int)p->pos_x] != '1') + if (map[(int)(p->pos_y + p->dir_y * speed)][(int)p->pos_x] != '1') p->pos_y += p->dir_y * speed; return (0); } -int move_down(t_player *p, char **map, double speed, double pad) +int move_down(t_player *p, char **map, double speed) { - if (map[(int)p->pos_y][(int)(p->pos_x - p->dir_x * (speed + pad))] != '1') + if (map[(int)p->pos_y][(int)(p->pos_x - p->dir_x * speed)] != '1') p->pos_x -= p->dir_x * speed; - if (map[(int)(p->pos_y - p->dir_y * (speed + pad))][(int)p->pos_x] != '1') + if (map[(int)(p->pos_y - p->dir_y * speed)][(int)p->pos_x] != '1') p->pos_y -= p->dir_y * speed; return (0); } -int move_rigth(t_player *p, char **map, double speed, double pad) +int move_rigth(t_player *p, char **map, double speed) { - if (map[(int)p->pos_y][(int)(p->pos_x + p->plane_x * (speed + pad))] != '1') + if (map[(int)p->pos_y][(int)(p->pos_x + p->plane_x * speed)] != '1') p->pos_x += p->plane_x * speed; - if (map[(int)(p->pos_y + p->plane_y * (speed + pad))][(int)p->pos_x] != '1') + if (map[(int)(p->pos_y + p->plane_y * speed)][(int)p->pos_x] != '1') p->pos_y += p->plane_y * speed; return (0); } -int move_left(t_player *p, char **map, double speed, double pad) +int move_left(t_player *p, char **map, double speed) { - if (map[(int)p->pos_y][(int)(p->pos_x - p->plane_x * (speed + pad))] != '1') + if (map[(int)p->pos_y][(int)(p->pos_x - p->plane_x * speed)] != '1') p->pos_x -= p->plane_x * speed; - if (map[(int)(p->pos_y - p->plane_y * speed - pad)][(int)p->pos_x] != '1') + if (map[(int)(p->pos_y - p->plane_y * speed)][(int)p->pos_x] != '1') p->pos_y -= p->plane_y * speed; return (0); } @@ -51,12 +51,12 @@ int move_left(t_player *p, char **map, double speed, double pad) int move_player(t_player *p, char **map, t_game *game) { if (p->key_w) - move_up(p, map, p->move_speed * game->delta_time, 0.30); + move_up(p, map, p->move_speed * game->delta_time); if (p->key_s) - move_down(p, map, p->move_speed * game->delta_time, 0.30); + move_down(p, map, p->move_speed * game->delta_time); if (p->key_d) - move_rigth(p, map, p->move_speed * game->delta_time, 0.30); + move_rigth(p, map, p->move_speed * game->delta_time); if (p->key_a) - move_left(p, map, p->move_speed * game->delta_time, 0.30); + move_left(p, map, p->move_speed * game->delta_time); return (0); } diff --git a/src/main.c b/src/main.c index e8414b6..3d200be 100644 --- a/src/main.c +++ b/src/main.c @@ -29,7 +29,8 @@ int main(int ac, char **av) if (error) return (safe_exit(&game, error)); load_player_dt(&game); - print_data(&game); + if (DEBUG) + print_data(&game); run_win_mlx(&game); return (safe_exit(&game, 0)); } diff --git a/src/render/render_canvas.c b/src/render/render_canvas.c index c2854e4..46e2f24 100644 --- a/src/render/render_canvas.c +++ b/src/render/render_canvas.c @@ -33,12 +33,11 @@ void draw_square(t_game *game, int x, int y, int size) return ; } -// -50 to hide textures - void create_canvas(t_game *game) { ft_bzero(game->screen.addr, HEIGHT * game->screen.size_line); draw_background(game); cast_rays(game); - generate_minimap(game); + if (BONUS) + generate_minimap(game); }