Cube3D Fonctionnelle, perpective, tout ça tout ça, il manque plus que les textures et la norme

This commit is contained in:
Etienne Rey-bethbeder 2023-05-05 17:06:47 +02:00
parent fbb9864ef4
commit 147c4e94c0
9 changed files with 42 additions and 43 deletions

BIN
assets/blue.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 640 B

BIN
assets/green.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 631 B

BIN
assets/purple.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 642 B

BIN
assets/red.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 631 B

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/02 15:49:00 by erey-bet #+# #+# */
/* Updated: 2023/05/04 14:37:21 by erey-bet ### ########.fr */
/* Updated: 2023/05/05 15:54:46 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -72,7 +72,8 @@ void loop_dda(t_game *game, t_dda *dda)
dda->side = 1;
}
if (dda->map_x < 0 || dda->map_y < 0
|| dda->map_x >= (int)game->map.size_x || dda->map_y >= (int)game->map.size_y)
|| dda->map_y >= (int)game->map.size_y
|| dda->map_x >= (int)ft_strlen(game->map.map[dda->map_y]))
{
dda->perp_wall_dist = 1e30;
return ;
@ -107,15 +108,13 @@ unsigned int get_rgba(int r, int g, int b, int a)
void draw_line(t_game *game, t_dda *dda, unsigned int color, int x)
{
int y;
unsigned int white;
white = 0xFFFFFFFF;
y = 0;
while (y < HEIGHT)
{
if (y < dda->draw_start)
{
mlx_put_pixel(game->window, x, y, white);
mlx_put_pixel(game->window, x, y, game->map.color_bot);
}
else if (y >= dda->draw_start && y <= dda->draw_end)
{
@ -123,7 +122,7 @@ void draw_line(t_game *game, t_dda *dda, unsigned int color, int x)
}
else if (y > dda->draw_end)
{
mlx_put_pixel(game->window, x, y, white);
mlx_put_pixel(game->window, x, y, game->map.color_top);
}
y++;
}

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 15:29:34 by erey-bet #+# #+# */
/* Updated: 2023/05/04 13:27:54 by erey-bet ### ########.fr */
/* Updated: 2023/05/05 16:54:19 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,32 +17,6 @@
# include <math.h>
# include <stdlib.h>
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
# include <time.h>
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
/* FAUT REMOVE */
# define WIDTH 1920
# define HEIGHT 1080
# define MOVESPEED 0.10
@ -86,6 +60,7 @@ typedef struct s_game
{
mlx_t *mlx;
mlx_image_t *window;
mlx_image_t textures[4];
t_ply ply;
t_map map;
t_ray ray;

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:50:22 by erey-bet #+# #+# */
/* Updated: 2023/05/04 14:34:47 by erey-bet ### ########.fr */
/* Updated: 2023/05/05 17:05:49 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,10 +16,18 @@ void init_ply(t_map map, t_ply *ply)
{
ply->pos_x = map.ply_x;
ply->pos_y = map.ply_y;
ply->dir_x = -1;
ply->dir_x = 0;
ply->dir_y = 0;
ply->pla_x = 0.66;
ply->pla_y = 0.66;
if (map.direction == 'N')
ply->dir_y = -1;
if (map.direction == 'S')
ply->dir_y = 1;
if (map.direction == 'E')
ply->dir_x = 1;
if (map.direction == 'W')
ply->dir_x = -1;
ply->pla_x = -(ply->dir_y) * 0.66;
ply->pla_y = (ply->dir_x) * 0.66;
}
void init_ray(t_ray *ray)
@ -28,6 +36,23 @@ void init_ray(t_ray *ray)
ray->dir_y = 0;
}
int init_textures(t_map map, mlx_image_t **textures)
{
(*textures)[0] = mlx_load_png(map.img_path[0]);
if (!textures[0])
return (1);
(*textures)[1] = mlx_load_png(map.img_path[1]);
if (!textures[1])
return (1);
(*textures)[2] = mlx_load_png(map.img_path[2]);
if (!textures[2])
return (1);
(*textures)[3] = mlx_load_png(map.img_path[3]);
if (!textures[3])
return (1);
return (0);
}
void init(t_map map, t_game *game)
{
game->mlx = mlx_init(WIDTH, HEIGHT, "jan lili", true);
@ -36,4 +61,5 @@ void init(t_map map, t_game *game)
game->map = map;
init_ply(map, &game->ply);
init_ray(&game->ray);
init_textures(map, &game->textures);
}

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:14:51 by erey-bet #+# #+# */
/* Updated: 2023/05/04 14:16:06 by erey-bet ### ########.fr */
/* Updated: 2023/05/05 15:00:40 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,7 +25,7 @@ void rotate(t_ply *ply, bool right)
old_plane_x = ply->pla_x;
old_plane_y = ply->pla_y;
speed = ROTATIONSPEED * (M_1_PI / 180.0);
if (right)
if (!right)
speed = -speed;
ply->dir_x = old_dir_x * cos(speed) - old_dir_y * sin(speed);
ply->dir_y = old_dir_x * sin(speed) + old_dir_y * cos(speed);
@ -48,15 +48,15 @@ int manage_movement(t_game *game, int key)
t_ply *ply;
ply = &game->ply;
printf("dir_x: %f, dir_y: %f\n", ply->dir_x, ply->dir_y);
//printf("dir_x: %f, dir_y: %f\n", ply->dir_x, ply->dir_y);
if (key == MLX_KEY_W)
movement(game, ply, (ply->dir_x * MOVESPEED), (ply->dir_y * MOVESPEED));
else if (key == MLX_KEY_S)
movement(game, ply, -(ply->dir_x * MOVESPEED), -(ply->dir_y * MOVESPEED));
else if (key == MLX_KEY_D)
movement(game, ply, -(ply->dir_x * MOVESPEED), (ply->dir_y * MOVESPEED));
movement(game, ply, (ply->pla_x * MOVESPEED), (ply->pla_y * MOVESPEED));
else if (key == MLX_KEY_A)
movement(game, ply, (ply->dir_x * MOVESPEED), -(ply->dir_y * MOVESPEED));
movement(game, ply, -(ply->pla_x * MOVESPEED), -(ply->pla_y * MOVESPEED));
else if (key == MLX_KEY_L)
rotate(ply, true);
else if (key == MLX_KEY_J)

View File

@ -7,7 +7,6 @@
typedef struct s_map
{
char **map;
void *img[4];
void *img_path[4];
long long color_bot;
long long color_top;