diff --git a/Makefile b/Makefile index f63a460..d5ad61d 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,13 @@ SHARED = -lX11 -lXext -lm CPP_1 = $(CPP) -Wall -Wextra -Werror -I Libft -I Minilibx -I include $(FLAGS) $(FLAGS_1) CPP_2 = $(CPP) $(FLAGS) $(FLAGS_2) C_FILES = $(wildcard src/*.c) +ifeq ($(BONUS), yes) + C_FILES_REAL = $(filter-out $(wildcard src/*_nobonus.c),$(C_FILES)) +else + C_FILES_REAL = $(filter-out $(wildcard src/*_bonus.c),$(C_FILES)) +endif O_FILES = $(patsubst %.c,%.o,$(C_FILES)) +O_FILES_REAL = $(patsubst %.c,%.o,$(C_FILES_REAL)) ECHO = echoo(){ \ if [ -t 1 ]; then \ @@ -18,6 +24,9 @@ ECHO = echoo(){ \ all : $(NAME) +bonus : + $(MAKE) all BONUS=yes FLAGS_1="$(FLAGS_1) -DBONUS" + debug : $(MAKE) all FLAGS_1="$(FLAGS_1) -g" @@ -44,10 +53,12 @@ fclean : clean re : clean all -$(NAME) : $(O_FILES) $(LIBRARIES_DIR) +rebonus : clean bonus + +$(NAME) : $(O_FILES_REAL) $(LIBRARIES_DIR) @$(ECHO) @$(ECHO) "\e[30;47;1m $(NAME): linking... \e[0m" - $(CPP_2) $(O_FILES) $(LIBRARIES_A) $(SHARED) -o $@ + $(CPP_2) $(O_FILES_REAL) $(LIBRARIES_A) $(SHARED) -o $@ @$(ECHO) @$(ECHO) "\t\t \e[0;92m\(^o^)/ \e[0;102;30;1m $(NAME) made! \e[0;92m \(^o^)/\e[0m" @$(ECHO) diff --git a/include/const.h b/include/const.h index 7e77f8b..85da448 100644 --- a/include/const.h +++ b/include/const.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 17:00:59 by mcolonna #+# #+# */ -/* Updated: 2024/11/19 13:54:46 by mcolonna ### ########.fr */ +/* Updated: 2024/11/19 15:29:31 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,11 +17,18 @@ # define PI 3.1415926535 // it's just pi -# define MOVE_SPEED 0.05 // Player oves by N cases by tick -# define ROT_SPEED_DIVIDE_PI 64 // Player turns by pi/N rad by tick -# define MOUSE_ROTATION_SPEED 0.001 // rad/px +# define FPS 30 // Number of frames per second (bonus only) + +# define MOVE_SPEED 0.1 // Player oves by N cases by tick +# define ROT_SPEED_DIVIDE_PI 48 // Player turns by pi/N rad by tick +# define MOUSE_ROTATION_SPEED 0.001 // Rotation speed from mouse: rad/px # define HITBOX 0.25 // Hitbox of N cases around player. +# ifdef BONUS +# define WINDOW_NAME "cub3d (bonus)" +# else +# define WINDOW_NAME "cub3d (mandatory)" +# endif # define SCREEN_WIDTH 640 // Width of the window # define SCREEN_HEIGHT 480 // Height of the window diff --git a/include/include.h b/include/include.h index 5e903b3..60a38f5 100644 --- a/include/include.h +++ b/include/include.h @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 16:22:09 by mcolonna #+# #+# */ -/* Updated: 2024/11/12 17:41:11 by mcolonna ### ########.fr */ +/* Updated: 2024/11/19 15:08:27 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ # include # include # include +# include # include # include # include diff --git a/src/main.c b/src/main.c index 69b1b19..287ea7d 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 16:57:40 by mcolonna #+# #+# */ -/* Updated: 2024/11/18 13:29:26 by mcolonna ### ########.fr */ +/* Updated: 2024/11/19 15:35:50 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,7 +68,8 @@ int main(int argc, char *argv[]) g_return_value = 1; else { - g_win = mlx_new_window(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT, "cub3d"); + g_win = mlx_new_window(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT, + WINDOW_NAME); input_init(g_mlx, g_win); mlx_loop_hook(g_mlx, loop_hook, NULL); draw_screen(); diff --git a/src/utils.c b/src/utils.c index d9b6787..688531a 100644 --- a/src/utils.c +++ b/src/utils.c @@ -6,7 +6,7 @@ /* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */ -/* Updated: 2024/11/07 00:05:39 by mcolonna ### ########.fr */ +/* Updated: 2024/11/19 15:07:05 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,41 +30,3 @@ void write_err(const char *str, ...) str = va_arg(args, const char *); } } - -/* FOR BONUS -static long get_nanos(void) -{ - struct timespec ts; - - timespec_get(&ts, TIME_UTC); - return ((long)ts.tv_sec * 1000000000L + ts.tv_nsec); -} - -void timedloop(void (*f)(void)) -{ - static long last_time = 0; - const long new_time = get_nanos(); - static bool checked = false; - - if (new_time - last_time >= 1000000000L / FPS) - { - if (checked) - last_time += 1000000000L / FPS; - else - last_time = new_time; - checked = false; - f(); - } - else - checked = true; -} -*/ - -void timedloop(void (*f)(void)) -{ - static int ticks; - - if (ticks % 100 == 0) - f(); - ticks++; -} diff --git a/src/utils_timedloop_bonus.c b/src/utils_timedloop_bonus.c new file mode 100644 index 0000000..9358daa --- /dev/null +++ b/src/utils_timedloop_bonus.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils_timedloop_bonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/19 15:05:10 by mcolonna #+# #+# */ +/* Updated: 2024/11/19 15:13:02 by mcolonna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "utils.h" + +#include "const.h" + +static long get_nanos(void) +{ + struct timespec ts; + + timespec_get(&ts, TIME_UTC); + return ((long)ts.tv_sec * 1000000000L + ts.tv_nsec); +} + +void timedloop(void (*f)(void)) +{ + static long last_time = 0; + const long new_time = get_nanos(); + static bool checked = false; + + if (new_time - last_time >= 1000000000L / FPS) + { + if (checked) + last_time += 1000000000L / FPS; + else + last_time = new_time; + checked = false; + f(); + } + else + checked = true; +} diff --git a/src/utils_timedloop_nobonus.c b/src/utils_timedloop_nobonus.c new file mode 100644 index 0000000..26d6c1e --- /dev/null +++ b/src/utils_timedloop_nobonus.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils_timedloop_nobonus.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/19 15:05:10 by mcolonna #+# #+# */ +/* Updated: 2024/11/19 15:36:17 by mcolonna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "utils.h" + +void timedloop(void (*f)(void)) +{ + static int ticks; + + if (ticks % 100 == 0) + f(); + ticks++; +}