Avancement Cube3D, init, manage_keys, raycasting
This commit is contained in:
15
game/game.c
15
game/game.c
@ -6,22 +6,23 @@
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/26 12:59:53 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/26 15:44:55 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/27 14:52:17 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../cube3D.h"
|
||||
#include "game.h"
|
||||
|
||||
int start_game(t_map *map)
|
||||
{
|
||||
t_game *game;
|
||||
|
||||
(void)map;
|
||||
game->mlx = mlx_init(1920, 1080, "jan lili", true)
|
||||
if (mlx)
|
||||
game = init(map);
|
||||
if (!game)
|
||||
return (1);
|
||||
if (!game->mlx)
|
||||
return (1);
|
||||
game->window = mlx_new_image(game->mlx, 128, 128);
|
||||
if (game->image)
|
||||
if (!game->window)
|
||||
{
|
||||
mlx_close_window(game->mlx);
|
||||
return(1);
|
||||
@ -32,7 +33,7 @@ int start_game(t_map *map)
|
||||
return(1);
|
||||
}
|
||||
|
||||
mlx_loop_hook(game->mlx, manage, game)
|
||||
mlx_key_hook(game->mlx, manage, game);
|
||||
mlx_loop(game->mlx);
|
||||
mlx_terminate(game->mlx);
|
||||
|
||||
|
19
game/game.h
19
game/game.h
@ -6,13 +6,15 @@
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/26 15:38:47 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/04/27 14:52:04 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef GAME_H
|
||||
# define GAME_H
|
||||
|
||||
# include "../cube3D.h"
|
||||
|
||||
typedef struct s_player
|
||||
{
|
||||
double pos_x;
|
||||
@ -22,8 +24,21 @@ typedef struct s_player
|
||||
typedef struct s_game
|
||||
{
|
||||
mlx_t *mlx;
|
||||
mlx_image_t *windows;
|
||||
mlx_image_t *window;
|
||||
t_ply *ply;
|
||||
t_map *map;
|
||||
} t_game;
|
||||
|
||||
/* INIT */
|
||||
t_game *init(t_map *map);
|
||||
|
||||
/* MANAGE */
|
||||
void manage(mlx_key_data_t keydata, void *param);
|
||||
|
||||
/* KEYS */
|
||||
int manage_keys(mlx_key_data_t keydata, t_game *game);
|
||||
|
||||
/* RAYCASTING */
|
||||
int raycasting(t_game *game);
|
||||
|
||||
#endif
|
||||
|
54
game/init.c
Normal file
54
game/init.c
Normal file
@ -0,0 +1,54 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* init.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/27 14:50:22 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/27 15:08:25 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "game.h"
|
||||
|
||||
int check_free(t_game *game)
|
||||
{
|
||||
if (!game->mlx || !game->ply)
|
||||
{
|
||||
if (game->mlx)
|
||||
free(game->mlx);
|
||||
if (game->ply)
|
||||
free(game->ply);
|
||||
free(game);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
t_ply *init_ply(t_map *map)
|
||||
{
|
||||
t_ply *ply;
|
||||
|
||||
ply = malloc(sizeof(t_ply));
|
||||
if (!ply)
|
||||
return (NULL);
|
||||
ply->pos_x = map->ply_x;
|
||||
ply->pos_y = map->ply_y;
|
||||
return (ply);
|
||||
}
|
||||
|
||||
t_game *init(t_map *map)
|
||||
{
|
||||
t_game *game;
|
||||
|
||||
game = malloc(sizeof(game));
|
||||
if (!game)
|
||||
return (NULL);
|
||||
game->mlx = mlx_init(1920, 1080, "jan lili", true);
|
||||
game->map = map;
|
||||
game->ply = init_ply(map);
|
||||
if (check_free(game))
|
||||
return (NULL);
|
||||
return (game);
|
||||
}
|
22
game/manage.c
Normal file
22
game/manage.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* manage.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/27 12:52:14 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/27 14:31:50 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "game.h"
|
||||
|
||||
void manage(mlx_key_data_t keydata, void *param)
|
||||
{
|
||||
t_game *game;
|
||||
|
||||
game = param;
|
||||
if (manage_keys(keydata, game))
|
||||
raycasting(game);
|
||||
}
|
39
game/manage_keys.c
Normal file
39
game/manage_keys.c
Normal file
@ -0,0 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* manage_keys.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/27 14:14:51 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/27 14:35:39 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "game.h"
|
||||
|
||||
int movement(double *to_add, double add)
|
||||
{
|
||||
*to_add += add;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int manage_keys(mlx_key_data_t keys, t_game *game)
|
||||
{
|
||||
int is_moving;
|
||||
|
||||
is_moving = 0;
|
||||
if (keys.key == MLX_KEY_ESCAPE)
|
||||
mlx_close_window(game->mlx);
|
||||
else if (keys.key == MLX_KEY_W)
|
||||
is_moving = movement(&game->ply->pos_y, -0.1);
|
||||
else if (keys.key == MLX_KEY_S)
|
||||
is_moving = movement(&game->ply->pos_y, 0.1);
|
||||
else if (keys.key == MLX_KEY_D)
|
||||
is_moving = movement(&game->ply->pos_x, 0.1);
|
||||
else if (keys.key == MLX_KEY_A)
|
||||
is_moving = movement(&game->ply->pos_x, -0.1);
|
||||
if (is_moving)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
20
game/raycasting.c
Normal file
20
game/raycasting.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* raycasting.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/04/27 14:30:29 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/04/27 14:36:06 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "game.h"
|
||||
|
||||
int raycasting(t_game *game)
|
||||
{
|
||||
(void)game;
|
||||
write(1, "raycasting...\n", 14);
|
||||
return (0);
|
||||
}
|
Reference in New Issue
Block a user