change: fluid movements
This commit is contained in:
parent
20e92ef401
commit
3ab5f9cb1d
7 changed files with 173 additions and 26 deletions
24
algo.c
24
algo.c
|
@ -6,11 +6,12 @@
|
||||||
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
|
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
|
||||||
/* Updated: 2024/10/17 12:10:07 by mcolonna ### ########.fr */
|
/* Updated: 2024/10/17 15:00:28 by mcolonna ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "algo.h"
|
#include "algo.h"
|
||||||
|
#include "input.h"
|
||||||
|
|
||||||
void *g_mlx = NULL;
|
void *g_mlx = NULL;
|
||||||
void *g_win = NULL;
|
void *g_win = NULL;
|
||||||
|
@ -54,9 +55,28 @@ void load_textures(t_tex *tex)
|
||||||
&width, &height);
|
&width, &height);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void loop(void)
|
||||||
|
{
|
||||||
|
move();
|
||||||
|
if (g_input_actions.quit)
|
||||||
|
{
|
||||||
|
mlx_destroy_window(g_mlx, g_win);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
draw_screen();
|
||||||
|
}
|
||||||
|
|
||||||
|
static int loop_hook(void *param)
|
||||||
|
{
|
||||||
|
(void)param;
|
||||||
|
timedloop(loop);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
g_mlx = mlx_init();
|
g_mlx = mlx_init();
|
||||||
|
input_init();
|
||||||
if (argc != 2)
|
if (argc != 2)
|
||||||
{
|
{
|
||||||
printf("Syntax: %s <map.cub>\n", argv[0]);
|
printf("Syntax: %s <map.cub>\n", argv[0]);
|
||||||
|
@ -66,6 +86,8 @@ int main(int argc, char *argv[])
|
||||||
return (1);
|
return (1);
|
||||||
g_win = mlx_new_window(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT, "cub3d");
|
g_win = mlx_new_window(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT, "cub3d");
|
||||||
mlx_hook(g_win, KeyPress, KeyPressMask, keypress, NULL);
|
mlx_hook(g_win, KeyPress, KeyPressMask, keypress, NULL);
|
||||||
|
mlx_hook(g_win, KeyRelease, KeyReleaseMask, keyrelease, NULL);
|
||||||
|
mlx_loop_hook(g_mlx, loop_hook, NULL);
|
||||||
draw_screen();
|
draw_screen();
|
||||||
mlx_loop(g_mlx);
|
mlx_loop(g_mlx);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
10
algo.h
10
algo.h
|
@ -6,7 +6,7 @@
|
||||||
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
|
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
|
||||||
/* Updated: 2024/10/17 14:19:12 by mcolonna ### ########.fr */
|
/* Updated: 2024/10/17 15:07:14 by mcolonna ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -30,9 +30,9 @@
|
||||||
# define MAP_HEIGHT 24 // cases (TODO test)
|
# define MAP_HEIGHT 24 // cases (TODO test)
|
||||||
# define TEX_WIDTH 64
|
# define TEX_WIDTH 64
|
||||||
# define TEX_HEIGHT 64
|
# define TEX_HEIGHT 64
|
||||||
# define MOVE_SPEED 0.1 // cases
|
# define MOVE_SPEED 0.05 // cases
|
||||||
# define PI 3.1415926535
|
# define PI 3.1415926535
|
||||||
# define ROT_SPEED_DIVIDE_PI 16 // pi/n rad
|
# define ROT_SPEED_DIVIDE_PI 64 // pi/n rad
|
||||||
# define COLOR_WALL 0xFF0000 // 0xRRGGBB
|
# define COLOR_WALL 0xFF0000 // 0xRRGGBB
|
||||||
# define COLOR_CEILING 0x29f8ff // 0xRRGGBB
|
# define COLOR_CEILING 0x29f8ff // 0xRRGGBB
|
||||||
# define COLOR_FLOOR 0xFF985C // 0xRRGGBB
|
# define COLOR_FLOOR 0xFF985C // 0xRRGGBB
|
||||||
|
@ -63,10 +63,10 @@ void write_err(const char *str, ...);
|
||||||
|
|
||||||
void vector_from_rotation(t_point_double *vec, double angle, double norm);
|
void vector_from_rotation(t_point_double *vec, double angle, double norm);
|
||||||
|
|
||||||
int keypress(int keycode);
|
|
||||||
|
|
||||||
int render(u_int32_t *img_data);
|
int render(u_int32_t *img_data);
|
||||||
|
|
||||||
void draw_screen(void);
|
void draw_screen(void);
|
||||||
|
|
||||||
|
void move(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
52
input.c
Normal file
52
input.c
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* input.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/17 14:43:36 by mcolonna #+# #+# */
|
||||||
|
/* Updated: 2024/10/17 15:00:54 by mcolonna ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "input.h"
|
||||||
|
#include <X11/keysym.h>
|
||||||
|
#include "algo.h"
|
||||||
|
|
||||||
|
t_input_actions g_input_actions;
|
||||||
|
|
||||||
|
static void set_action(int keycode, bool value)
|
||||||
|
{
|
||||||
|
if (keycode == XK_Up || keycode == XK_z || keycode == XK_w)
|
||||||
|
g_input_actions.up = value;
|
||||||
|
if (keycode == XK_Down || keycode == XK_s)
|
||||||
|
g_input_actions.down = value;
|
||||||
|
if (keycode == XK_Right || keycode == XK_d)
|
||||||
|
g_input_actions.right = value;
|
||||||
|
if (keycode == XK_Left || keycode == XK_q || keycode == XK_a)
|
||||||
|
g_input_actions.left = value;
|
||||||
|
if (keycode == XK_Escape)
|
||||||
|
g_input_actions.quit = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
int keypress(int keycode)
|
||||||
|
{
|
||||||
|
set_action(keycode, true);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int keyrelease(int keycode)
|
||||||
|
{
|
||||||
|
set_action(keycode, false);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void input_init(void)
|
||||||
|
{
|
||||||
|
g_input_actions.down = false;
|
||||||
|
g_input_actions.up = false;
|
||||||
|
g_input_actions.left = false;
|
||||||
|
g_input_actions.right = false;
|
||||||
|
g_input_actions.quit = false;
|
||||||
|
}
|
35
input.h
Normal file
35
input.h
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* input.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/17 14:43:09 by mcolonna #+# #+# */
|
||||||
|
/* Updated: 2024/10/17 14:59:41 by mcolonna ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef INPUT_H
|
||||||
|
# define INPUT_H
|
||||||
|
|
||||||
|
# include <stdbool.h>
|
||||||
|
|
||||||
|
/// @brief Modified by the input of the user. A value becomes true
|
||||||
|
/// when the user presses the corresponding key.
|
||||||
|
typedef struct s_input_actions
|
||||||
|
{
|
||||||
|
bool left;
|
||||||
|
bool right;
|
||||||
|
bool up;
|
||||||
|
bool down;
|
||||||
|
bool quit;
|
||||||
|
} t_input_actions;
|
||||||
|
|
||||||
|
extern t_input_actions g_input_actions;
|
||||||
|
|
||||||
|
int keypress(int keycode);
|
||||||
|
int keyrelease(int keycode);
|
||||||
|
void input_init(void);
|
||||||
|
|
||||||
|
#endif
|
27
move.c
27
move.c
|
@ -6,11 +6,12 @@
|
||||||
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */
|
/* Created: 2024/10/15 12:47:34 by mcolonna #+# #+# */
|
||||||
/* Updated: 2024/10/17 14:20:10 by mcolonna ### ########.fr */
|
/* Updated: 2024/10/17 15:06:43 by mcolonna ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "algo.h"
|
#include "algo.h"
|
||||||
|
#include "input.h"
|
||||||
|
|
||||||
static void push_from_wall(t_point_int c)
|
static void push_from_wall(t_point_int c)
|
||||||
{
|
{
|
||||||
|
@ -47,7 +48,7 @@ static void push_from_walls(void)
|
||||||
push_from_wall(c);
|
push_from_wall(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int move(int factor)
|
static int move_forward(int factor)
|
||||||
{
|
{
|
||||||
t_point_double dir;
|
t_point_double dir;
|
||||||
|
|
||||||
|
@ -55,7 +56,6 @@ static int move(int factor)
|
||||||
g_map.player.pos.x += dir.x * MOVE_SPEED * factor;
|
g_map.player.pos.x += dir.x * MOVE_SPEED * factor;
|
||||||
g_map.player.pos.y += dir.y * MOVE_SPEED * factor;
|
g_map.player.pos.y += dir.y * MOVE_SPEED * factor;
|
||||||
push_from_walls();
|
push_from_walls();
|
||||||
printf("%f %f\n", g_map.player.pos.x, g_map.player.pos.y);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,21 +67,14 @@ static int rotate(int factor)
|
||||||
|
|
||||||
// TODO leaks when quit?
|
// TODO leaks when quit?
|
||||||
|
|
||||||
int keypress(int keycode)
|
void move(void)
|
||||||
{
|
{
|
||||||
if (keycode == XK_Up || keycode == XK_z || keycode == XK_w)
|
if (g_input_actions.up && !g_input_actions.down)
|
||||||
move(+1);
|
move_forward(+1);
|
||||||
if (keycode == XK_Down || keycode == XK_s)
|
if (g_input_actions.down && !g_input_actions.up)
|
||||||
move(-1);
|
move_forward(-1);
|
||||||
if (keycode == XK_Right || keycode == XK_d)
|
if (g_input_actions.right && !g_input_actions.left)
|
||||||
rotate(+1);
|
rotate(+1);
|
||||||
if (keycode == XK_Left || keycode == XK_q || keycode == XK_a)
|
if (g_input_actions.left && !g_input_actions.right)
|
||||||
rotate(-1);
|
rotate(-1);
|
||||||
if (keycode == XK_Escape)
|
|
||||||
{
|
|
||||||
mlx_destroy_window(g_mlx, g_win);
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
draw_screen();
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
42
utils.c
42
utils.c
|
@ -3,10 +3,10 @@
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* utils.c :+: :+: :+: */
|
/* utils.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: mcolonna <mcolonna@student.42perpignan.fr +#+ +:+ +#+ */
|
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */
|
/* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */
|
||||||
/* Updated: 2024/10/16 18:01:25 by mcolonna ### ########.fr */
|
/* Updated: 2024/10/17 15:05:15 by mcolonna ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -31,3 +31,41 @@ 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++;
|
||||||
|
}
|
||||||
|
|
9
utils.h
9
utils.h
|
@ -6,7 +6,7 @@
|
||||||
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/10/15 16:56:47 by mcolonna #+# #+# */
|
/* Created: 2024/10/15 16:56:47 by mcolonna #+# #+# */
|
||||||
/* Updated: 2024/10/15 17:21:10 by mcolonna ### ########.fr */
|
/* Updated: 2024/10/17 14:41:09 by mcolonna ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -41,4 +41,11 @@ t_color color_from_rgb(int red, int green, int blue);
|
||||||
|
|
||||||
void write_err(const char *str, ...);
|
void write_err(const char *str, ...);
|
||||||
|
|
||||||
|
/// @brief Call the function in loop.
|
||||||
|
/// If not enough time passed since the last call, returns without doing
|
||||||
|
/// anything.
|
||||||
|
///
|
||||||
|
/// @param f Function to call in loop.
|
||||||
|
void timedloop(void (*f)(void));
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Reference in a new issue