This commit is contained in:
Camille Chauvet 2023-05-05 11:51:11 +00:00
commit addd78ecf2
15 changed files with 235 additions and 136 deletions

View File

@ -1,6 +1,6 @@
SRCS_GAME := main.c game/game.c game/init.c game/manage.c \
game/manage_keys.c game/raycasting.c
SRCS_MAP := parsing.c parsing_header.c parsing_header2.c parsing_meta.c parsing_body.c
game/manage_keys.c game/raycasting.c game/dda.c
SRCS_MAP := #parsing.c parsing_header.c parsing_header2.c parsing_meta.c parsing_body.c
SRCS_MAP := $(addprefix map/, $(SRCS_MAP))
SRCS := ${SRCS_MAP} \
@ -9,7 +9,7 @@ SRCS := ${SRCS_MAP} \
OBJS := ${SRCS:.c=.o}
CC := clang
LIBS := libftx/libftx.a MLX42/build/libmlx42.a -ldl -lglfw -lm
CFLAGS := -g -Wall -Wextra -Werror
CFLAGS := -g -Wall -Wextra -Werror -Wno-conversion -Ofast
NAME := cub3D
all: ${NAME}

View File

@ -6,13 +6,14 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:34:04 by erey-bet #+# #+# */
/* Updated: 2023/05/05 11:15:42 by cchauvet ### ########.fr */
/* Updated: 2023/05/05 11:37:41 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef CUBE3D_H
# define CUBE3D_H
# include "map/parsing.h"
# include "map/map.h"
# include "libftx/libftx.h"
# include <sys/types.h>
@ -25,7 +26,6 @@
/*INIT*/
t_map *map_parsing(char *path);
int start_game(t_map *map);
int start_game(t_map map);
#endif

Binary file not shown.

View File

@ -6,48 +6,53 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/02 15:49:00 by erey-bet #+# #+# */
/* Updated: 2023/05/02 16:58:31 by erey-bet ### ########.fr */
/* Updated: 2023/05/04 14:37:21 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
double ft_abs(double nb)
{
if (nb < 0)
return (-nb);
return (nb);
}
void init_dda(t_game *game, t_dda *dda)
{
t_ray *ray
t_ray *ray;
t_ply *ply;
ray = game->ray;
dda->map_x = (int)game->ply->pos_x;
dda->map_y = (int)game->ply->pos_y;
ray = &game->ray;
ply = &game->ply;
dda->map_x = (int)ply->pos_x;
dda->map_y = (int)ply->pos_y;
if (ray->dir_x == 0)
dda->delta_dist_x = 1e30;
else
dda->delta_dist_x = abs(1 / ray->dir_x);
dda->delta_dist_x = ft_abs(1 / ray->dir_x);
if (ray->dir_y == 0)
dda->delta_dist_y = 1e30;
else
dda->delta_dist_y = abs(1 / ray->dir_y);
dda->hit = 0;
dda->delta_dist_y = ft_abs(1 / ray->dir_y);
dda->step_x = 1;
dda->step_y = 1;
if (ray->dir_x < 0)
{
dda->step_x = -1;
dda->side_dist_x = (game->ply->pos_x - dda->map_x) * dda->delta_dist_x;
dda->side_dist_x = (ply->pos_x - dda->map_x) * dda->delta_dist_x;
}
else
{
dda->step_x = 1;
dda->side_dist_x = (dda->map_x + 1.0 - game->ply->pos_y) * dda->delta_dist_x;
}
dda->side_dist_x = (dda->map_x + 1.0 - ply->pos_x) * dda->delta_dist_x;
if (ray->dir_y < 0)
{
dda->step_y = -1;
dda->side_dist_y = (game->ply->pos_y - dda->map_y) * dda->delta_dist_y;
dda->side_dist_y = (ply->pos_y - dda->map_y) * dda->delta_dist_y;
}
else
{
dda->step_y = 1;
dda->side_dist_y = (dda->map_y + 1.0 - game->ply->pos_y) * dda->delta_dist_y;
}
dda->side_dist_y = (dda->map_y + 1.0 - ply->pos_y) * dda->delta_dist_y;
dda->hit = 0;
}
void loop_dda(t_game *game, t_dda *dda)
@ -66,56 +71,89 @@ void loop_dda(t_game *game, t_dda *dda)
dda->map_y += dda->step_y;
dda->side = 1;
}
if (game->map->map[dda->map_x][dda->map_y] > 0)
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->perp_wall_dist = 1e30;
return ;
}
if (game->map.map[dda->map_y][dda->map_x] == '1')
dda->hit = 1;
}
if(dda->side == 0)
dda->perp_wall_dist = dda->side_dist_x - dda->delta_dist_x;
else
dda->perp_wall_dist = dda->side_dist_y - dda->delta_dist_y;
}
void ray_dda(t_game *game, t_dda *dda)
void ray_dda(t_dda *dda)
{
if(dda->side == 0)
dda->perp_wall_dist = (dda->side_dist_x - dda->delta_dist_x);
else
dda->perp_wall_dist = (dda->side_dist_y - dda->delta_dist_y);
if (dda->perp_wall_dist == 0)
dda->perp_wall_dist = 1e30;
dda->line_height = (int)(HEIGHT / dda->perp_wall_dist);
dda->draw_start = -dda->line_height / 2 + HEIGHT / 2;
dda->draw_start = -(dda->line_height) / 2 + HEIGHT / 2;
if(dda->draw_start < 0)
dda->draw_start = 0;
dda->draw_end = dda->line_height / 2 + HEIGHT / 2;
if(draw_end >= HEIGHT)
draw_end = HEIGHT - 1;
if(dda->draw_end >= HEIGHT)
dda->draw_end = HEIGHT - 1;
}
int create_trgb(int t, int r, int g, int b)
unsigned int get_rgba(int r, int g, int b, int a)
{
return (t << 24 | r << 16 | g << 8 | b);
return (r << 24 | g << 16 | b << 8 | a);
}
#include <time.h>
#include <stdlib.h>
void draw_dda(t_game *game, t_dda *dda)
void draw_line(t_game *game, t_dda *dda, unsigned int color, int x)
{
int color;
int y;
unsigned int white;
srand(time(NULL));
int r = rand() % 4;
if (r == 0)
color = create_trgv(255, 150, 10, 200);
else if (r == 1)
color = create_trgv(255, 10, 150, 200);
else if (r == 2)
color = create_trgv(255, 200, 150, 10);
else if (r == 3)
color = create_trgv(255, 10, 200, 150);
mlx_put_pixel(game->mlx->window, abs(x), y,
white = 0xFFFFFFFF;
y = 0;
while (y < HEIGHT)
{
if (y < dda->draw_start)
{
mlx_put_pixel(game->window, x, y, white);
}
else if (y >= dda->draw_start && y <= dda->draw_end)
{
mlx_put_pixel(game->window, x, y, color);
}
else if (y > dda->draw_end)
{
mlx_put_pixel(game->window, x, y, white);
}
y++;
}
}
void dda(t_game *game, t_dda *dda)
void draw_dda(t_game *game, t_dda *dda, int x)
{
init_dda(game, dda);
loop_dda(game, dda);
ray_dda(game, dda);
draw_dda(game, dda);
unsigned int color;
if (dda->step_x < 0 && dda->side == 0) // NORTH
color = get_rgba(0, 0, 200, 255);
else if (dda->step_x > 0 && dda->side == 0) // SOUTH
color = get_rgba(200, 0, 0, 255);
else if (dda->step_y < 0 && dda->side == 1) // EAST
color = get_rgba(0, 200, 0, 255);
else if (dda->step_y > 0 && dda->side == 1) // WEST
color = get_rgba(50, 100, 50, 255);
else
color = get_rgba(0, 0, 0, 255);
draw_line(game, dda, color, x);
}
void dda(t_game *game, int x)
{
t_dda dda;
init_dda(game, &dda);
loop_dda(game, &dda);
ray_dda(&dda);
draw_dda(game, &dda, x);
}

View File

@ -6,13 +6,13 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:59:53 by erey-bet #+# #+# */
/* Updated: 2023/05/03 12:47:12 by erey-bet ### ########.fr */
/* Updated: 2023/05/03 14:50:16 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
int start_game(t_map *map)
int start_game(t_map map)
{
t_game game;
@ -22,15 +22,16 @@ int start_game(t_map *map)
game.window = mlx_new_image(game.mlx, WIDTH, HEIGHT);
if (!game.window)
{
mlx_close_window(game.mlx);
mlx_terminate(game.mlx);
return(1);
}
if (mlx_image_to_window(game.mlx, game.window, 0, 0) == -1)
{
mlx_close_window(game.mlx);
mlx_terminate(game.mlx);
return(1);
}
raycasting(&game);
mlx_key_hook(game.mlx, manage, &game);
mlx_loop(game.mlx);
mlx_terminate(game.mlx);

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/03 12:42:31 by erey-bet ### ########.fr */
/* Updated: 2023/05/04 13:27:54 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,9 +15,38 @@
# include "../cube3D.h"
# 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
# define ROTATIONSPEED 60
typedef struct s_player
{
@ -57,13 +86,13 @@ typedef struct s_game
{
mlx_t *mlx;
mlx_image_t *window;
t_ply *ply;
t_map *map;
t_ray *ray;
t_ply ply;
t_map map;
t_ray ray;
} t_game;
/* INIT */
void init(t_map *map, t_game *game);
void init(t_map map, t_game *game);
/* MANAGE */
void manage(mlx_key_data_t keydata, void *param);
@ -74,4 +103,6 @@ int manage_keys(mlx_key_data_t keydata, t_game *game);
/* RAYCASTING */
int raycasting(t_game *game);
/* DDA ALGO */
void dda(t_game *game, int x);
#endif

View File

@ -6,16 +6,20 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:50:22 by erey-bet #+# #+# */
/* Updated: 2023/05/03 12:44:19 by erey-bet ### ########.fr */
/* Updated: 2023/05/04 14:34:47 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
void init_ply(t_map *map, t_ply *ply)
void init_ply(t_map map, t_ply *ply)
{
ply->pos_x = map->ply_x;
ply->pos_y = map->ply_y;
ply->pos_x = map.ply_x;
ply->pos_y = map.ply_y;
ply->dir_x = -1;
ply->dir_y = 0;
ply->pla_x = 0.66;
ply->pla_y = 0.66;
}
void init_ray(t_ray *ray)
@ -24,17 +28,12 @@ void init_ray(t_ray *ray)
ray->dir_y = 0;
}
void init(t_map *map, t_game *game)
void init(t_map map, t_game *game)
{
t_ply ply;
t_ray ray;
game->mlx = mlx_init(WIDTH, HEIGHT, "jan lili", true);
if (!game->mlx)
return ;
game->map = map;
init_ply(map, &ply);
game->ply = &ply;
init_ray(&ray);
game->ray = &ray;
init_ply(map, &game->ply);
init_ray(&game->ray);
}

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2023/05/03 14:53:14 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,22 +6,61 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:14:51 by erey-bet #+# #+# */
/* Updated: 2023/05/03 12:44:41 by erey-bet ### ########.fr */
/* Updated: 2023/05/04 14:16:06 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "game.h"
int movement(t_ply *ply, int key)
void rotate(t_ply *ply, bool right)
{
double old_dir_x;
double old_dir_y;
double old_plane_x;
double old_plane_y;
double speed;
old_dir_x = ply->dir_x;
old_dir_y = ply->dir_y;
old_plane_x = ply->pla_x;
old_plane_y = ply->pla_y;
speed = ROTATIONSPEED * (M_1_PI / 180.0);
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);
ply->pla_x = old_plane_x * cos(speed) - old_plane_y * sin(speed);
ply->pla_y = old_plane_x * sin(speed) + old_plane_y * cos(speed);
}
void movement(t_game *game, t_ply *ply, double x, double y)
{
(void)game;
//if(game->map.map[(int)(ply->pos_x + x)][(int)ply->pos_y] == '0')
ply->pos_x += x;
//if(game->map.map[(int)ply->pos_x][(int)(ply->pos_y + y)] == '0')
ply->pos_y += y;
}
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);
if (key == MLX_KEY_W)
ply->pos_y -= 0.1;
movement(game, ply, (ply->dir_x * MOVESPEED), (ply->dir_y * MOVESPEED));
else if (key == MLX_KEY_S)
ply->pos_y += 0.1;
movement(game, ply, -(ply->dir_x * MOVESPEED), -(ply->dir_y * MOVESPEED));
else if (key == MLX_KEY_D)
ply->pos_x += 0.1;
movement(game, ply, -(ply->dir_x * MOVESPEED), (ply->dir_y * MOVESPEED));
else if (key == MLX_KEY_A)
ply->pos_x -= 0.1;
movement(game, ply, (ply->dir_x * MOVESPEED), -(ply->dir_y * MOVESPEED));
else if (key == MLX_KEY_L)
rotate(ply, true);
else if (key == MLX_KEY_J)
rotate(ply, false);
return (1);
}
@ -30,17 +69,16 @@ int manage_keys(mlx_key_data_t keys, t_game *game)
int is_moving;
is_moving = 0;
if (keys.key == MLX_KEY_ESCAPE)
if (keys.key == MLX_KEY_Q)
mlx_close_window(game->mlx);
else if (keys.key == MLX_KEY_W
|| keys.key == MLX_KEY_S
|| keys.key == MLX_KEY_D
|| keys.key == MLX_KEY_A)
is_moving = movement(game->ply, keys.key);
/*else if (key.key == MLX_KEY_RIGHT)
game->ply->direc += 0.1;
else if (key.key == MLX_KEY_LEFT)
game->ply->direc -= 0.1;*/
|| keys.key == MLX_KEY_A
|| keys.key == MLX_KEY_ESCAPE
|| keys.key == MLX_KEY_L
|| keys.key == MLX_KEY_J )
is_moving = manage_movement(game, keys.key);
if (is_moving)
return (1);
return (0);

View File

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/27 14:30:29 by erey-bet #+# #+# */
/* Updated: 2023/05/03 12:45:15 by erey-bet ### ########.fr */
/* Updated: 2023/05/05 12:57:44 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,16 +17,19 @@ int ray(t_game *game)
t_ply *p;
t_ray *ray;
double camera;
int i;
int x;
i = 0;
p = game->ply;
ray = game->ray;
while (i <= WIDTH)
x = 0;
p = &game->ply;
ray = &game->ray;
while (x < WIDTH)
{
camera = 2 * i / (double)WIDTH - 1; //x-coordinate in camera space
camera = 2 * x / ((double)(WIDTH - 1));
ray->dir_x = p->dir_x + p->pla_x * camera;
ray->dir_y = p->dir_y + p->pla_y * camera;
dda(game, x);
x++;
}
return(0);
}
@ -34,6 +37,5 @@ int ray(t_game *game)
int raycasting(t_game *game)
{
ray(game);
write(1, "raycasting...\n", 14);
return (0);
}

5
main.c
View File

@ -6,12 +6,11 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/26 12:44:55 by erey-bet #+# #+# */
/* Updated: 2023/05/05 11:20:37 by cchauvet ### ########.fr */
/* Updated: 2023/05/05 11:47:28 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "cube3D.h"
#include "libftx/libftx.h"
int main(int argc, char **argv)
{
@ -25,7 +24,7 @@ int main(int argc, char **argv)
map = map_parsing(argv[1]);
if (!map)
return (2);
if (start_game(map))
if (start_game(*map))
{
return (3);
}

View File

@ -10,6 +10,8 @@ typedef struct s_map
void *img[4];
long long color_bot;
long long color_top;
size_t size_x;
size_t size_y;
double ply_x;
double ply_y;
char direction;

BIN
map/parsing.o Normal file

Binary file not shown.

View File

@ -1,7 +1,5 @@
#include "map.h"
#include "parsing_private.h"
#include <stddef.h>
#include <stdlib.h>
char **get_body(const char **file_content, size_t header_size)
{
@ -75,38 +73,11 @@ static int map_is_in_one_part(const char **body)
return (0);
}
static char **map_cpy(const char **body)
{
size_t i;
char **copy;
copy = malloc((ft_tablen((const void **) body) + 1) * sizeof(char *));
if (copy == NULL)
return (NULL);
i = 0;
while (body[i] != NULL)
{
copy[i] = ft_strdup(body[i]);
if (copy[i] == NULL)
{
ft_cancel((void **) copy, i);
return (NULL);
}
i++;
}
copy[i] = NULL;
return (copy);
}
static int map_surround(const char **body)
{
char **copy;
size_t y;
size_t x;
copy = map_cpy(body);
if (copy == NULL)
return (1);
y = 0;
while (body[y] != NULL)
{
@ -127,7 +98,6 @@ static int map_surround(const char **body)
}
y++;
}
ft_freer_tab_ultimate(1, copy);
return (0);
}
@ -154,6 +124,23 @@ static int body_contain_normal_char(const char **body)
return (1);
}
void get_size(const char **body, t_map *map)
{
size_t y;
size_t x;
map->size_x = 0;
y = 0;
while (body[y] != NULL)
{
x = ft_strlen(body[y]);
if (x > map->size_x)
map->size_x = x;
y++;
}
map->size_y = y;
}
int body_is_valid(const char **body, t_map *map)
{
int error;
@ -185,5 +172,6 @@ int body_is_valid(const char **body, t_map *map)
}
if (body_contain_normal_char(body) == 0)
return (0);
get_size(body, map);
return (1);
}

1
pos Normal file
View File

@ -0,0 +1 @@
Binary file (standard input) matches