This commit is contained in:
Etienne Rey-bethbeder 2023-05-02 15:39:16 +02:00
parent 5c14d5be1f
commit da452769c2
6 changed files with 83 additions and 61 deletions

Binary file not shown.

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:59:53 by erey-bet #+# #+# */ /* Created: 2023/04/26 12:59:53 by erey-bet #+# #+# */
/* Updated: 2023/04/27 14:52:17 by erey-bet ### ########.fr */ /* Updated: 2023/04/28 16:23:57 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,28 +14,25 @@
int start_game(t_map *map) int start_game(t_map *map)
{ {
t_game *game; t_game game;
game = init(map); if (init(map, &game))
if (!game)
return (1); return (1);
if (!game->mlx) game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT);
return (1); if (!game.window)
game->window = mlx_new_image(game->mlx, 128, 128);
if (!game->window)
{ {
mlx_close_window(game->mlx); mlx_close_window(game.mlx);
return(1); return(1);
} }
if (mlx_image_to_window(game->mlx, game->window, 0, 0) == -1) if (mlx_image_to_window(game.mlx, game.window, 0, 0) == -1)
{ {
mlx_close_window(game->mlx); mlx_close_window(game.mlx);
return(1); return(1);
} }
mlx_key_hook(game->mlx, manage, game); mlx_key_hook(game.mlx, manage, &game);
mlx_loop(game->mlx); mlx_loop(game.mlx);
mlx_terminate(game->mlx); mlx_terminate(game.mlx);
return (0); return (0);
} }

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */ /* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */
/* Updated: 2023/04/27 14:52:04 by erey-bet ### ########.fr */ /* Updated: 2023/05/02 15:32:43 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,19 +14,34 @@
# define GAME_H # define GAME_H
# include "../cube3D.h" # include "../cube3D.h"
# include <math.h>
# define WIDTH 1920
# define HEIGHT 1080
typedef struct s_player typedef struct s_player
{ {
double pos_x; double pos_x;
double pos_y; double pos_y;
double dir_x;
double dir_y;
double pla_x;
double pla_y;
} t_ply; } t_ply;
typedef struct s_raycast
{
double x;
double y;
} t_ray;
typedef struct s_game typedef struct s_game
{ {
mlx_t *mlx; mlx_t *mlx;
mlx_image_t *window; mlx_image_t *window;
t_ply *ply; t_ply *ply;
t_map *map; t_map *map;
t_ray *ray;
} t_game; } t_game;
/* INIT */ /* INIT */

View File

@ -6,49 +6,32 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:50:22 by erey-bet #+# #+# */ /* Created: 2023/04/27 14:50:22 by erey-bet #+# #+# */
/* Updated: 2023/04/27 15:08:25 by erey-bet ### ########.fr */ /* Updated: 2023/05/02 15:18:05 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game.h"
int check_free(t_game *game) void init_ply(t_map *map, t_ply *ply)
{ {
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_x = map->ply_x;
ply->pos_y = map->ply_y; ply->pos_y = map->ply_y;
return (ply);
} }
t_game *init(t_map *map) void init_ray(t_ray *ray)
{ {
t_game *game; ray->x = 0;
ray->y = 0;
game = malloc(sizeof(game)); }
if (!game)
return (NULL); void init(t_map *map, t_game &game)
game->mlx = mlx_init(1920, 1080, "jan lili", true); {
game->map = map; t_ply ply;
game->ply = init_ply(map); t_ray ray;
if (check_free(game))
return (NULL); game.mlx = mlx_init(WIDTH, HEIGHT, "jan lili", true);
return (game); if (!game.mlx)
return (NULL);
game.map = map;
init_ply(map, &ply);
} }

View File

@ -6,15 +6,22 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:14:51 by erey-bet #+# #+# */ /* Created: 2023/04/27 14:14:51 by erey-bet #+# #+# */
/* Updated: 2023/04/27 14:35:39 by erey-bet ### ########.fr */ /* Updated: 2023/04/28 12:50:21 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game.h"
int movement(double *to_add, double add) int movement(t_ply *ply, int key)
{ {
*to_add += add; else if (key == MLX_KEY_W)
ply->pos_y -= 0.1;
else if (key == MLX_KEY_S)
ply->pos_y += 0.1;
else if (key == MLX_KEY_D)
ply->pos_x += 0.1;
else if (key == MLX_KEY_A)
ply->pos_x -= 0.1;
return (1); return (1);
} }
@ -25,14 +32,15 @@ int manage_keys(mlx_key_data_t keys, t_game *game)
is_moving = 0; is_moving = 0;
if (keys.key == MLX_KEY_ESCAPE) if (keys.key == MLX_KEY_ESCAPE)
mlx_close_window(game->mlx); mlx_close_window(game->mlx);
else if (keys.key == MLX_KEY_W) else if (keys.key == MLX_KEY_W
is_moving = movement(&game->ply->pos_y, -0.1); || keys.key == MLX_KEY_S
else if (keys.key == MLX_KEY_S) || keys.key == MLX_KEY_D
is_moving = movement(&game->ply->pos_y, 0.1); || keys.key == MLX_KEY_A)
else if (keys.key == MLX_KEY_D) is_moving = movement(game->ply, keys.key);
is_moving = movement(&game->ply->pos_x, 0.1); /*else if (key.key == MLX_KEY_RIGHT)
else if (keys.key == MLX_KEY_A) game->ply->direc += 0.1;
is_moving = movement(&game->ply->pos_x, -0.1); else if (key.key == MLX_KEY_LEFT)
game->ply->direc -= 0.1;*/
if (is_moving) if (is_moving)
return (1); return (1);
return (0); return (0);

View File

@ -6,15 +6,34 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:30:29 by erey-bet #+# #+# */ /* Created: 2023/04/27 14:30:29 by erey-bet #+# #+# */
/* Updated: 2023/04/27 14:36:06 by erey-bet ### ########.fr */ /* Updated: 2023/05/02 13:12:15 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "game.h" #include "game.h"
int ray(t_game *game)
{
t_ply *p;
t_ray *ray;
double camera;
int i;
i = 0;
p = game->ply;
ray = game->ray;
while (i <= WIDTH)
{
camera = 2 * i / (double)WIDTH - 1; //x-coordinate in camera space
ray->x = p->dir_x + p->pla_x * camera;
ray->y = p->dir_y + p->pla_y * camera;
}
}
int raycasting(t_game *game) int raycasting(t_game *game)
{ {
(void)game; (void)game;
ray(game)
write(1, "raycasting...\n", 14); write(1, "raycasting...\n", 14);
return (0); return (0);
} }