movement is SOO SMOOTH now
This commit is contained in:
parent
5bceacaba8
commit
0ee3b0ae0d
31
inc/cub3d.h
31
inc/cub3d.h
@ -6,26 +6,13 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
|
/* Created: 2025/04/15 12:22:29 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/04/17 19:20:17 by qmennen ### ########.fr */
|
/* Updated: 2025/04/17 19:51:30 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef CUB3D_H
|
#ifndef CUB3D_H
|
||||||
# define CUB3D_H
|
# define CUB3D_H
|
||||||
|
|
||||||
# include <MLX42.h>
|
|
||||||
# include <libft.h>
|
|
||||||
# include <allowed.h>
|
|
||||||
# include "types.h"
|
|
||||||
# include "errors.h"
|
|
||||||
# include "map.h"
|
|
||||||
# include "game.h"
|
|
||||||
# include "screen.h"
|
|
||||||
# include "keyhandle.h"
|
|
||||||
# include "line.h"
|
|
||||||
# include "render.h"
|
|
||||||
# include "player.h"
|
|
||||||
|
|
||||||
# define FAILURE 0
|
# define FAILURE 0
|
||||||
# define SUCCESS 1
|
# define SUCCESS 1
|
||||||
|
|
||||||
@ -43,6 +30,20 @@
|
|||||||
# define CYAN "\033[0;36m"
|
# define CYAN "\033[0;36m"
|
||||||
# define WHITE "\033[0;37m"
|
# define WHITE "\033[0;37m"
|
||||||
|
|
||||||
t_tile **get_temp_map();
|
# define NUM_KEYS 256
|
||||||
|
|
||||||
|
# include <MLX42.h>
|
||||||
|
# include <libft.h>
|
||||||
|
# include <allowed.h>
|
||||||
|
# include "types.h"
|
||||||
|
# include "errors.h"
|
||||||
|
# include "map.h"
|
||||||
|
# include "game.h"
|
||||||
|
# include "screen.h"
|
||||||
|
# include "keyboard.h"
|
||||||
|
# include "hooks.h"
|
||||||
|
# include "line.h"
|
||||||
|
# include "render.h"
|
||||||
|
# include "player.h"
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -1,17 +1,17 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* keyhandle.h :+: :+: :+: */
|
/* hooks.h :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/15 17:51:44 by qmennen #+# #+# */
|
/* Created: 2025/04/15 17:51:44 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/04/15 17:56:00 by qmennen ### ########.fr */
|
/* Updated: 2025/04/17 19:51:48 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef KEYHANDLE_H
|
#ifndef HOOKS_H
|
||||||
# define KEYHANDLE_H
|
# define HOOKS_H
|
||||||
|
|
||||||
# include "cub3d.h"
|
# include "cub3d.h"
|
||||||
|
|
||||||
23
inc/keyboard.h
Normal file
23
inc/keyboard.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* keyboard.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/04/17 19:29:36 by qmennen #+# #+# */
|
||||||
|
/* Updated: 2025/04/17 19:39:51 by qmennen ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef KEYBOARD_H
|
||||||
|
# define KEYBOARD_H
|
||||||
|
|
||||||
|
# include "cub3d.h"
|
||||||
|
|
||||||
|
int keyboard_create(t_game **game);
|
||||||
|
void keyboard_update(t_game *game);
|
||||||
|
int get_key(t_game *game, int k);
|
||||||
|
int get_key_down(t_game *game, int k);
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/15 18:53:27 by qmennen #+# #+# */
|
/* Created: 2025/04/15 18:53:27 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/04/15 19:08:01 by qmennen ### ########.fr */
|
/* Updated: 2025/04/17 19:49:36 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,7 +16,7 @@
|
|||||||
# include "cub3d.h"
|
# include "cub3d.h"
|
||||||
|
|
||||||
int player_create(t_game **game);
|
int player_create(t_game **game);
|
||||||
void player_move(keys_t key, t_player *player);
|
|
||||||
void player_render(t_screen *screen, t_player *player);
|
void player_render(t_screen *screen, t_player *player);
|
||||||
|
void player_update(t_game *game);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
|
/* Created: 2025/04/15 15:52:44 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/04/15 15:53:29 by qmennen ### ########.fr */
|
/* Updated: 2025/04/17 19:38:44 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -44,6 +44,12 @@ typedef struct s_map
|
|||||||
t_tile **grid;
|
t_tile **grid;
|
||||||
} t_map;
|
} t_map;
|
||||||
|
|
||||||
|
typedef struct s_keyboard
|
||||||
|
{
|
||||||
|
int keys[NUM_KEYS];
|
||||||
|
int last_keys[NUM_KEYS];
|
||||||
|
} t_keyboard;
|
||||||
|
|
||||||
typedef struct s_screen
|
typedef struct s_screen
|
||||||
{
|
{
|
||||||
mlx_t *mlx;
|
mlx_t *mlx;
|
||||||
@ -57,6 +63,7 @@ typedef struct s_game
|
|||||||
t_map *map;
|
t_map *map;
|
||||||
t_player *player;
|
t_player *player;
|
||||||
t_screen *screen;
|
t_screen *screen;
|
||||||
|
t_keyboard *keyboard;
|
||||||
} t_game;
|
} t_game;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
|
/* Created: 2025/04/15 15:46:08 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/04/17 19:24:14 by qmennen ### ########.fr */
|
/* Updated: 2025/04/17 19:47:19 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -34,8 +34,10 @@ void game_loop(void *param)
|
|||||||
|
|
||||||
game = (t_game *)param;
|
game = (t_game *)param;
|
||||||
render_clear(game->screen);
|
render_clear(game->screen);
|
||||||
|
player_update(game);
|
||||||
render_entities(game);
|
render_entities(game);
|
||||||
render_map(game->screen, game->map);
|
render_map(game->screen, game->map);
|
||||||
|
keyboard_update(game); // Goes last
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_terminate(t_game *game)
|
void game_terminate(t_game *game)
|
||||||
@ -51,6 +53,8 @@ void game_terminate(t_game *game)
|
|||||||
free(game->player);
|
free(game->player);
|
||||||
if (game->map)
|
if (game->map)
|
||||||
map_free(game->map);
|
map_free(game->map);
|
||||||
|
if (game->keyboard)
|
||||||
|
free(game->keyboard);
|
||||||
free(game);
|
free(game);
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,25 +1,24 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* keyhandle.c :+: :+: :+: */
|
/* hooks.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/15 17:51:26 by qmennen #+# #+# */
|
/* Created: 2025/04/15 17:51:26 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/04/15 17:56:24 by qmennen ### ########.fr */
|
/* Updated: 2025/04/17 19:52:14 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "keyhandle.h"
|
#include "hooks.h"
|
||||||
|
|
||||||
void keyhandle(mlx_key_data_t keydata, void *param)
|
void keyhandle(mlx_key_data_t keydata, void *param)
|
||||||
{
|
{
|
||||||
t_game *game;
|
t_game *game;
|
||||||
|
|
||||||
game = (t_game *)param;
|
game = (t_game *)param;
|
||||||
if (keydata.key == MLX_KEY_W || keydata.key == MLX_KEY_S
|
if (keydata.key == MLX_KEY_ESCAPE)
|
||||||
|| keydata.key == MLX_KEY_LEFT || keydata.key == MLX_KEY_RIGHT)
|
|
||||||
player_move(keydata.key, game->player);
|
|
||||||
else if (keydata.key == MLX_KEY_ESCAPE)
|
|
||||||
game_terminate(game);
|
game_terminate(game);
|
||||||
|
else
|
||||||
|
game->keyboard->keys[keydata.key] = 1;
|
||||||
}
|
}
|
||||||
50
src/keyboard.c
Normal file
50
src/keyboard.c
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* keyboard.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/04/17 19:29:29 by qmennen #+# #+# */
|
||||||
|
/* Updated: 2025/04/17 19:45:46 by qmennen ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "keyboard.h"
|
||||||
|
|
||||||
|
int keyboard_create(t_game **game)
|
||||||
|
{
|
||||||
|
t_keyboard *keyboard;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
keyboard = malloc(sizeof(t_keyboard));
|
||||||
|
if (!keyboard)
|
||||||
|
return (FAILURE);
|
||||||
|
(*game)->keyboard = keyboard;
|
||||||
|
i = -1;
|
||||||
|
while ((i++) < NUM_KEYS)
|
||||||
|
(*game)->keyboard->keys[i] = 0;
|
||||||
|
return (SUCCESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
void keyboard_update(t_game *game)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while (i < NUM_KEYS)
|
||||||
|
{
|
||||||
|
game->keyboard->last_keys[i] = get_key(game, i);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_key(t_game *game, int k)
|
||||||
|
{
|
||||||
|
return mlx_is_key_down(game->screen->mlx, k);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_key_down(t_game *game, int k)
|
||||||
|
{
|
||||||
|
return (get_key(game, k) && !game->keyboard->last_keys[k]);
|
||||||
|
}
|
||||||
@ -6,7 +6,7 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */
|
/* Created: 2025/04/15 16:01:29 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/04/17 19:20:05 by qmennen ### ########.fr */
|
/* Updated: 2025/04/17 19:45:13 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -20,6 +20,8 @@ static int init_game(t_game **game)
|
|||||||
return (FAILURE);
|
return (FAILURE);
|
||||||
if (!map_create(game))
|
if (!map_create(game))
|
||||||
return (FAILURE);
|
return (FAILURE);
|
||||||
|
if (!keyboard_create(game))
|
||||||
|
return (FAILURE);
|
||||||
screen_center((*game)->screen);
|
screen_center((*game)->screen);
|
||||||
mlx_key_hook((*game)->screen->mlx, keyhandle, *game);
|
mlx_key_hook((*game)->screen->mlx, keyhandle, *game);
|
||||||
mlx_loop_hook((*game)->screen->mlx, game_loop, *game);
|
mlx_loop_hook((*game)->screen->mlx, game_loop, *game);
|
||||||
|
|||||||
31
src/player.c
31
src/player.c
@ -6,7 +6,7 @@
|
|||||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
|
/* Created: 2025/04/15 18:53:19 by qmennen #+# #+# */
|
||||||
/* Updated: 2025/04/17 19:20:56 by qmennen ### ########.fr */
|
/* Updated: 2025/04/17 19:50:48 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -30,22 +30,27 @@ int player_create(t_game **game)
|
|||||||
return (SUCCESS);
|
return (SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
void player_move(keys_t key, t_player *player)
|
static void move(t_player *player, int dir)
|
||||||
{
|
{
|
||||||
if (key == MLX_KEY_W)
|
player->pos.x += dir * (cos(player->angle) * player->speed);
|
||||||
{
|
player->pos.y += dir * (sin(player->angle) * player->speed);
|
||||||
player->pos.x += cos(player->angle) * player->speed;
|
|
||||||
player->pos.y += sin(player->angle) * player->speed;
|
|
||||||
}
|
}
|
||||||
else if (key == MLX_KEY_S)
|
|
||||||
|
static void rotate(t_player *player, int dir)
|
||||||
{
|
{
|
||||||
player->pos.x -= cos(player->angle) * player->speed;
|
player->angle += dir * .1f;
|
||||||
player->pos.y -= sin(player->angle) * player->speed;
|
|
||||||
}
|
}
|
||||||
else if (key == MLX_KEY_LEFT)
|
|
||||||
player->angle -= .1f;
|
void player_update(t_game *game)
|
||||||
else if (key == MLX_KEY_RIGHT)
|
{
|
||||||
player->angle += .1f;
|
if (get_key(game, MLX_KEY_W))
|
||||||
|
move(game->player, 1);
|
||||||
|
else if (get_key(game, MLX_KEY_S))
|
||||||
|
move(game->player, -1);
|
||||||
|
if (get_key(game, MLX_KEY_LEFT))
|
||||||
|
rotate(game->player, -1);
|
||||||
|
else if (get_key(game, MLX_KEY_RIGHT))
|
||||||
|
rotate(game->player, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void player_render(t_screen *screen, t_player *player)
|
void player_render(t_screen *screen, t_player *player)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user