Merge remote-tracking branch 'origin/_mylan2' into dev

This commit is contained in:
mcolonna 2024-10-17 16:39:57 +02:00
commit d7ff717f35
6 changed files with 150 additions and 44 deletions

View file

@ -2,7 +2,7 @@ NAME = cub3d
CPP = gcc $(FLAGS) CPP = gcc $(FLAGS)
LIBRARIES_DIR = Libft Minilibx LIBRARIES_DIR = Libft Minilibx
LIBRARIES_A = Libft/libft.a Minilibx/libmlx.a LIBRARIES_A = Libft/libft.a Minilibx/libmlx.a
CPP_1 = $(CPP) -Wall -Wextra -Werror -I Libft -I Minilibx $(FLAGS_1) CPP_1 = $(CPP) -Wall -Wextra -Werror -I Libft -I Minilibx $(FLAGS_1) -g
CPP_2 = $(CPP) $(FLAGS_2) CPP_2 = $(CPP) $(FLAGS_2)
SHARED = -lX11 -lXext -lm SHARED = -lX11 -lXext -lm
C_FILES = $(wildcard **.c) C_FILES = $(wildcard **.c)

27
algo.c
View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */ /* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */ /* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
/* Updated: 2024/10/17 15:00:28 by mcolonna ### ########.fr */ /* Updated: 2024/10/17 16:35:28 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,8 @@ void *g_mlx = NULL;
void *g_win = NULL; void *g_win = NULL;
t_map g_map; t_map g_map;
t_player g_player; t_player g_player;
t_tex g_tex;
t_ray g_ray;
// TODO manage image format error better // TODO manage image format error better
@ -40,19 +42,19 @@ void draw_screen(void)
mlx_put_image_to_window(g_mlx, g_win, img_ptr, 0, 0); mlx_put_image_to_window(g_mlx, g_win, img_ptr, 0, 0);
} }
void load_textures(t_tex *tex) void load_textures()
{ {
int width; g_tex.tex_width = 64;
int height; g_tex.tex_height = 64;
tex->tex_north = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm", g_tex.textures[0] = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm",
&width, &height); &g_tex.tex_width, &g_tex.tex_height);
tex->tex_south = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm", g_tex.textures[1] = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm",
&width, &height); &g_tex.tex_width, &g_tex.tex_height);
tex->tex_east = mlx_xpm_file_to_image(g_mlx, "textures/east.xpm", g_tex.textures[2]= mlx_xpm_file_to_image(g_mlx, "textures/east.xpm",
&width, &height); &g_tex.tex_width, &g_tex.tex_height);
tex->tex_west = mlx_xpm_file_to_image(g_mlx, "textures/west.xpm", g_tex.textures[3]= mlx_xpm_file_to_image(g_mlx, "textures/west.xpm",
&width, &height); &g_tex.tex_width, &g_tex.tex_height);
} }
static void loop(void) static void loop(void)
@ -77,6 +79,7 @@ int main(int argc, char *argv[])
{ {
g_mlx = mlx_init(); g_mlx = mlx_init();
input_init(); input_init();
load_textures();
if (argc != 2) if (argc != 2)
{ {
printf("Syntax: %s <map.cub>\n", argv[0]); printf("Syntax: %s <map.cub>\n", argv[0]);

31
algo.h
View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */ /* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */ /* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
/* Updated: 2024/10/17 15:07:14 by mcolonna ### ########.fr */ /* Updated: 2024/10/17 16:34:32 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -41,20 +41,29 @@
# define FOV 0.66 // ? TODO unit # define FOV 0.66 // ? TODO unit
# define HITBOX 0.25 // cases. should be more than MOVE_SPEED # define HITBOX 0.25 // cases. should be more than MOVE_SPEED
typedef struct s_tex
{
void *textures[4];
void *current_tex;
int tex_height;
int tex_width;
int tex_dir;
} t_tex;
typedef struct s_ray
{
// pos player on map (cases)
t_point_double dir;
bool side;
double wallx;
} t_ray;
extern void *g_mlx; extern void *g_mlx;
extern void *g_win; extern void *g_win;
extern t_player g_player; extern t_player g_player;
extern t_map g_map; extern t_map g_map;
extern t_tex g_tex;
typedef struct s_tex extern t_ray g_ray;
{
void *tex_north;
void *tex_south;
void *tex_east;
void *tex_west;
void *current_tex;
} t_tex;
/// @brief Write an error message on stderr. /// @brief Write an error message on stderr.
/// ///

View file

@ -3,17 +3,18 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* render.c :+: :+: :+: */ /* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */ /* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 14:55:05 by greg #+# #+# */ /* Created: 2024/10/14 14:55:05 by greg #+# #+# */
/* Updated: 2024/10/15 16:55:02 by mcolonna ### ########.fr */ /* Updated: 2024/10/17 16:28:20 by grobledo ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "algo.h" #include "algo.h"
#include <stdio.h>
static void calculate_perpwalldist3(int x, static void calculate_perpwalldist3(int x,
t_point_double *raydir, t_point_double *deltadist) t_point_double *deltadist)
{ {
const double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1; const double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1;
t_point_double plane; t_point_double plane;
@ -21,14 +22,14 @@ static void calculate_perpwalldist3(int x,
vector_from_rotation(&dir, g_map.player.rot, 1); vector_from_rotation(&dir, g_map.player.rot, 1);
vector_from_rotation(&plane, g_map.player.rot + PI / 2, 1); vector_from_rotation(&plane, g_map.player.rot + PI / 2, 1);
raydir->x = dir.x + plane.x * ray_direction; g_ray.dir.x = dir.x + plane.x * ray_direction;
raydir->y = dir.y + plane.y * ray_direction; g_ray.dir.y = dir.y + plane.y * ray_direction;
deltadist->x = 1e30; deltadist->x = 1e30;
if (raydir->x != 0) if (g_ray.dir.x != 0)
deltadist->x = fabs(1 / raydir->x); deltadist->x = fabs(1 / g_ray.dir.x);
deltadist->y = 1e30; deltadist->y = 1e30;
if (raydir->y != 0) if (g_ray.dir.y != 0)
deltadist->y = fabs(1 / raydir->y); deltadist->y = fabs(1 / g_ray.dir.y);
} }
static void calculate_perpwalldist2(int x, static void calculate_perpwalldist2(int x,
@ -38,10 +39,10 @@ static void calculate_perpwalldist2(int x,
{ {
const int map_x = (int)g_map.player.pos.x; const int map_x = (int)g_map.player.pos.x;
const int map_y = (int)g_map.player.pos.y; const int map_y = (int)g_map.player.pos.y;
t_point_double raydir; // t_point_double raydir;
calculate_perpwalldist3(x, &raydir, deltadist); calculate_perpwalldist3(x, deltadist);
if (raydir.x < 0) if (g_ray.dir.x < 0)
{ {
step->x = -1; step->x = -1;
sidedist->x = (g_map.player.pos.x - map_x) * deltadist->x; sidedist->x = (g_map.player.pos.x - map_x) * deltadist->x;
@ -51,7 +52,7 @@ static void calculate_perpwalldist2(int x,
step->x = 1; step->x = 1;
sidedist->x = (map_x + 1.0 - g_map.player.pos.x) * deltadist->x; sidedist->x = (map_x + 1.0 - g_map.player.pos.x) * deltadist->x;
} }
if (raydir.y < 0) if (g_ray.dir.y < 0)
{ {
step->y = -1; step->y = -1;
sidedist->y = (g_map.player.pos.y - map_y) * deltadist->y; sidedist->y = (g_map.player.pos.y - map_y) * deltadist->y;
@ -71,7 +72,7 @@ static void calculate_perpwalldist2(int x,
static double calculate_perpwalldist(int x) static double calculate_perpwalldist(int x)
{ {
t_point_int map_pos; t_point_int map_pos;
bool side; // bool side;
t_point_double sidedist; t_point_double sidedist;
t_point_double deltadist; t_point_double deltadist;
t_point_int step; t_point_int step;
@ -85,18 +86,37 @@ static double calculate_perpwalldist(int x)
{ {
sidedist.x += deltadist.x; sidedist.x += deltadist.x;
map_pos.x += step.x; map_pos.x += step.x;
side = false; g_ray.side = false;
continue ; continue ;
} }
sidedist.y += deltadist.y; sidedist.y += deltadist.y;
map_pos.y += step.y; map_pos.y += step.y;
side = true; g_ray.side = true;
} }
if (!side) if (!g_ray.side)
return (sidedist.x - deltadist.x); return (sidedist.x - deltadist.x);
return (sidedist.y - deltadist.y); return (sidedist.y - deltadist.y);
} }
static void ft_init_texture(const double perpwalldist)
{
if (g_ray.side == 0 && g_ray.dir.x < 0)
g_tex.tex_dir = 0;
if (g_ray.side == 0 && g_ray.dir.x >= 0)
g_tex.tex_dir = 1;
if (g_ray.side == 1 && g_ray.dir.y < 0)
g_tex.tex_dir = 2;
if (g_ray.side == 1 && g_ray.dir.y >= 0)
g_tex.tex_dir = 3;
if (g_ray.side == 0)
g_ray.wallx = g_player.pos.y + perpwalldist \
* g_ray.dir.y;
else
g_ray.wallx = g_player.pos.x + perpwalldist \
* g_ray.dir.x;
g_ray.wallx -= floor((g_ray.wallx));
}
/// @brief Draw a vertical line according to the ray direction x. /// @brief Draw a vertical line according to the ray direction x.
/// ///
/// @param x Ray direction x between 0 and 1. /// @param x Ray direction x between 0 and 1.
@ -107,7 +127,14 @@ static void draw_vertical_line(int x, u_int32_t *img_data)
int draw_start; int draw_start;
int draw_end; int draw_end;
int y; int y;
int tex_y;
int tex_x;
int bpp;
int size_line;
int endian;
u_int32_t color;
ft_init_texture(perpwalldist);
draw_start = -line_height / 2 + SCREEN_HEIGHT / 2; draw_start = -line_height / 2 + SCREEN_HEIGHT / 2;
if (draw_start < 0) if (draw_start < 0)
draw_start = 0; draw_start = 0;
@ -120,7 +147,18 @@ static void draw_vertical_line(int x, u_int32_t *img_data)
if (y < draw_start) if (y < draw_start)
img_data[y * SCREEN_WIDTH + x] = COLOR_CEILING; img_data[y * SCREEN_WIDTH + x] = COLOR_CEILING;
else if (y < draw_end) else if (y < draw_end)
img_data[y * SCREEN_WIDTH + x] = COLOR_WALL; {
tex_y = ((y - draw_start) * g_tex.tex_height) / line_height;
tex_x = (int)(g_ray.wallx * g_tex.tex_width);
if (g_ray.side == 0 && g_ray.dir.x > 0)
tex_x = g_tex.tex_width - tex_x - 1;
if (g_ray.side == 1 && g_ray.dir.y < 0)
tex_x = g_tex.tex_height - tex_x - 1;
u_int32_t *texture_data = (u_int32_t *)mlx_get_data_addr(g_tex.textures[g_tex.tex_dir], &bpp, &size_line, &endian);
color = texture_data[tex_y * g_tex.tex_width + tex_x];
img_data[y * SCREEN_WIDTH + x] = color;
// img_data[y * SCREEN_WIDTH + x] = COLOR_WALL;
}
else else
img_data[y * SCREEN_WIDTH + x] = COLOR_FLOOR; img_data[y * SCREEN_WIDTH + x] = COLOR_FLOOR;
y++; y++;
@ -138,4 +176,4 @@ int render(u_int32_t *img_data)
x++; x++;
} }
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */ /* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */ /* Created: 2024/10/15 12:38:40 by mcolonna #+# #+# */
/* Updated: 2024/10/17 15:05:15 by mcolonna ### ########.fr */ /* Updated: 2024/10/17 16:36:14 by mcolonna ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

56
utils2.c Normal file
View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 01:57:03 by greg #+# #+# */
/* Updated: 2024/10/17 14:58:33 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
// #include "algo.h"
// void ft_init_texture(const double perpwalldist)
// {
// if (g_ray.side == 0 && g_ray.dir.x < 0)
// g_tex.tex_dir = 0;
// if (g_ray.side == 0 && g_ray.dir.x >= 0)
// g_tex.tex_dir = 1;
// if (g_ray.side == 1 && g_ray.dir.y < 0)
// g_tex.tex_dir = 2;
// if (g_ray.side == 1 && g_ray.dir.y >= 0)
// g_tex.tex_dir = 3;
// if (g_ray.side == 0)
// g_ray.wallx = g_player.pos.y + perpwalldist
// * g_ray.dir.y;
// else
// g_ray.wallx = g_player.pos.x + perpwalldist
// * g_ray.dir.x;
// g_ray.wallx -= floor((g_ray.wallx));
// }
// void *tex_ptr = (int *)mlx_get_data_addr(texture[0].img, &bpp,
// g_tex, &endian);
// tex_y = ((y - draw_start) * texture_height) / line_height;
// tex_x = (int)(wall_x * (double)texture_width);
// if ((!side && raydir.x > 0) || (side && raydir.y < 0))
// {
// tex_x = texture_width - tex_x - 1;
// }
// u_int32_t color = textures[direction][tex_y * texture_width + tex_x];
// else if (y < draw_end)
// {
// tex_y = ((y - draw_start) * g_tex.tex_height) / line_height;
// tex_x = (int)(g_ray.wallx * g_tex.tex_width);
// u_int32_t *texture_data = (u_int32_t *)g_tex.textures[g_tex.tex_dir];
// u_int32_t color = texture_data[tex_y * g_tex.tex_width + tex_x];
// img_data[y * SCREEN_WIDTH + x] = color;
// }