add: better timedloop (uses FPS) for bonus
This commit is contained in:
parent
08db89f6dc
commit
c8b3a42d13
7 changed files with 94 additions and 48 deletions
15
Makefile
15
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_1 = $(CPP) -Wall -Wextra -Werror -I Libft -I Minilibx -I include $(FLAGS) $(FLAGS_1)
|
||||||
CPP_2 = $(CPP) $(FLAGS) $(FLAGS_2)
|
CPP_2 = $(CPP) $(FLAGS) $(FLAGS_2)
|
||||||
C_FILES = $(wildcard src/*.c)
|
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 = $(patsubst %.c,%.o,$(C_FILES))
|
||||||
|
O_FILES_REAL = $(patsubst %.c,%.o,$(C_FILES_REAL))
|
||||||
|
|
||||||
ECHO = echoo(){ \
|
ECHO = echoo(){ \
|
||||||
if [ -t 1 ]; then \
|
if [ -t 1 ]; then \
|
||||||
|
@ -18,6 +24,9 @@ ECHO = echoo(){ \
|
||||||
|
|
||||||
all : $(NAME)
|
all : $(NAME)
|
||||||
|
|
||||||
|
bonus :
|
||||||
|
$(MAKE) all BONUS=yes FLAGS_1="$(FLAGS_1) -DBONUS"
|
||||||
|
|
||||||
debug :
|
debug :
|
||||||
$(MAKE) all FLAGS_1="$(FLAGS_1) -g"
|
$(MAKE) all FLAGS_1="$(FLAGS_1) -g"
|
||||||
|
|
||||||
|
@ -44,10 +53,12 @@ fclean : clean
|
||||||
|
|
||||||
re : clean all
|
re : clean all
|
||||||
|
|
||||||
$(NAME) : $(O_FILES) $(LIBRARIES_DIR)
|
rebonus : clean bonus
|
||||||
|
|
||||||
|
$(NAME) : $(O_FILES_REAL) $(LIBRARIES_DIR)
|
||||||
@$(ECHO)
|
@$(ECHO)
|
||||||
@$(ECHO) "\e[30;47;1m $(NAME): linking... \e[0m"
|
@$(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)
|
||||||
@$(ECHO) "\t\t \e[0;92m\(^o^)/ \e[0;102;30;1m $(NAME) made! \e[0;92m \(^o^)/\e[0m"
|
@$(ECHO) "\t\t \e[0;92m\(^o^)/ \e[0;102;30;1m $(NAME) made! \e[0;92m \(^o^)/\e[0m"
|
||||||
@$(ECHO)
|
@$(ECHO)
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/31 17:00:59 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 PI 3.1415926535 // it's just pi
|
||||||
|
|
||||||
# define MOVE_SPEED 0.05 // Player oves by N cases by tick
|
# define FPS 30 // Number of frames per second (bonus only)
|
||||||
# define ROT_SPEED_DIVIDE_PI 64 // Player turns by pi/N rad by tick
|
|
||||||
# define MOUSE_ROTATION_SPEED 0.001 // rad/px
|
# 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.
|
# 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_WIDTH 640 // Width of the window
|
||||||
# define SCREEN_HEIGHT 480 // Height of the window
|
# define SCREEN_HEIGHT 480 // Height of the window
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/31 16:22:09 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 <stdlib.h>
|
# include <stdlib.h>
|
||||||
# include <string.h>
|
# include <string.h>
|
||||||
# include <sys/types.h>
|
# include <sys/types.h>
|
||||||
|
# include <time.h>
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <X11/keysym.h>
|
# include <X11/keysym.h>
|
||||||
# include <X11/X.h>
|
# include <X11/X.h>
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/31 16:57:40 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;
|
g_return_value = 1;
|
||||||
else
|
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);
|
input_init(g_mlx, g_win);
|
||||||
mlx_loop_hook(g_mlx, loop_hook, NULL);
|
mlx_loop_hook(g_mlx, loop_hook, NULL);
|
||||||
draw_screen();
|
draw_screen();
|
||||||
|
|
40
src/utils.c
40
src/utils.c
|
@ -6,7 +6,7 @@
|
||||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/15 12:38:40 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 *);
|
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++;
|
|
||||||
}
|
|
||||||
|
|
42
src/utils_timedloop_bonus.c
Normal file
42
src/utils_timedloop_bonus.c
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils_timedloop_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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;
|
||||||
|
}
|
22
src/utils_timedloop_nobonus.c
Normal file
22
src/utils_timedloop_nobonus.c
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils_timedloop_nobonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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++;
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue