Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/c-cpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
- name: make
run: make
- name: cub3d
run: ./cub3d maps/valid/mandatory.cub
run: ./cub3D maps/valid/mandatory.cub
23 changes: 16 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
NAME = cub3d
MAKEFLAGS += --silent
NAME = cub3D
SRCDIR = src
OBJDIR = .obj
INCDIR = inc
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 \
\
Expand Down Expand Up @@ -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)"
Expand All @@ -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:
Expand All @@ -100,13 +104,18 @@ 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}"

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
16 changes: 14 additions & 2 deletions inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
#ifndef MAIN_H
# define MAIN_H

# include <math.h>
# include <sys/time.h>
# include <X11/keysym.h>
# include <X11/X.h>

# include "libft/libft.h"
# include "minilibx/mlx.h"
# include "struct.h"
Expand All @@ -22,9 +27,16 @@
# include "mlx_win.h"
# include "parsing.h"
# include "player.h"
# include <math.h>
# include "textures.h"
# include "mini_map.h"
# include "raycast.h"
# include <sys/time.h>

# ifndef DEBUG
# define DEBUG 0
# endif

# ifndef BONUS
# define BONUS 1
# endif

#endif
4 changes: 2 additions & 2 deletions inc/minilibx/mlx.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: rpinheir <rpinheir@student.42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion inc/mlx_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
/* By: ch <chguerre@42lausanne.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);
Expand All @@ -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
3 changes: 2 additions & 1 deletion inc/struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: chguerre <chguerre@student.42lausanne.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/11/12 17:23:35 by ch #+# #+# */
/* Updated: 2026/11/17 20:14:58 by chguerre ### ########.fr */
/* Updated: 2026/11/24 15:15:15 by rpinheir ### ########.ch */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -83,6 +83,7 @@ typedef struct s_game
int c_color[3];
int f_color[3];
int map_fd;
int mouse_x;
t_player player;
t_img screen;
t_img w_north;
Expand Down
70 changes: 70 additions & 0 deletions src/core/mouse_bonus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mouse_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rpinheir <rpinheir@student.42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
6 changes: 5 additions & 1 deletion src/core/win.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ch <chguerre@42lausanne.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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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);
Expand Down
32 changes: 16 additions & 16 deletions src/engine/player/move_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,51 +12,51 @@

#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);
}

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);
}
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
5 changes: 2 additions & 3 deletions src/render/render_canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}