This commit is contained in:
Gregory Robledo 2024-10-29 13:52:11 +01:00
parent 50c4e56303
commit 8e650871e7
5 changed files with 73 additions and 101 deletions

10
algo.c
View file

@ -6,12 +6,13 @@
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
/* Updated: 2024/10/22 15:48:47 by grobledo ### ########.fr */
/* Updated: 2024/10/24 14:37:04 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
#include "input.h"
#include <stdio.h>
void *g_mlx = NULL;
void *g_win = NULL;
@ -25,18 +26,17 @@ static void load_textures(t_tex *tex)
{
tex->tex_width = 64;
tex->tex_height = 64;
tex->textures[0] = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm",
&tex->tex_width, &tex->tex_height);
tex->textures[1] = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm",
&tex->tex_width, &tex->tex_height);
tex->textures[2]= mlx_xpm_file_to_image(g_mlx, "textures/east.xpm",
tex->textures[2] = mlx_xpm_file_to_image(g_mlx, "textures/east.xpm",
&tex->tex_width, &tex->tex_height);
tex->textures[3]= mlx_xpm_file_to_image(g_mlx, "textures/west.xpm",
tex->textures[3] = mlx_xpm_file_to_image(g_mlx, "textures/west.xpm",
&tex->tex_width, &tex->tex_height);
}
void draw_screen()
void draw_screen(void)
{
int bpp;
int size_line;

11
algo.h
View file

@ -6,7 +6,7 @@
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
/* Updated: 2024/10/22 15:48:21 by grobledo ### ########.fr */
/* Updated: 2024/10/24 14:27:27 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
@ -48,6 +48,11 @@ typedef struct s_tex
int tex_height;
int tex_width;
int tex_dir;
int tex_x;
int tex_y;
int bpp;
int size_line;
int endian;
} t_tex;
typedef struct s_ray
@ -77,4 +82,8 @@ void draw_screen(void);
void move(void);
void ft_init_texture(const double perpwalldist, t_ray *ray, t_tex *tex);
void draw_wall(t_tex *tex, t_ray *ray, const int line_height, int x,
int y, u_int32_t *img_data);
#endif

View file

@ -6,7 +6,7 @@
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/14 14:55:05 by greg #+# #+# */
/* Updated: 2024/10/22 15:50:27 by grobledo ### ########.fr */
/* Updated: 2024/10/24 14:27:52 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
@ -98,41 +98,17 @@ static double calculate_perpwalldist(int x, t_ray *ray)
return (sidedist.y - deltadist.y);
}
static void ft_init_texture(const double perpwalldist, t_ray *ray, t_tex *tex)
{
if (ray->side == 0 && ray->dir.x < 0)
tex->tex_dir = 0;
if (ray->side == 0 && ray->dir.x >= 0)
tex->tex_dir = 1;
if (ray->side == 1 && ray->dir.y < 0)
tex->tex_dir = 2;
if (ray->side == 1 && ray->dir.y >= 0)
tex->tex_dir = 3;
if (ray->side == 0)
ray->wallx = g_map.player.pos.y + perpwalldist \
* ray->dir.y;
else
ray->wallx = g_map.player.pos.x + perpwalldist \
* ray->dir.x;
ray->wallx -= floor((ray->wallx));
}
/// @brief Draw a vertical line according to the ray direction x.
///
/// @param x Ray direction x between 0 and 1.
static void draw_vertical_line(int x, u_int32_t *img_data, t_ray *ray, t_tex *tex)
static void draw_vertical_line(int x, u_int32_t *img_data,
t_ray *ray, t_tex *tex)
{
const double perpwalldist = calculate_perpwalldist(x, ray);
const int line_height = (int)(SCREEN_HEIGHT / perpwalldist);
int draw_start;
int draw_end;
int y;
int tex_y;
int tex_x;
int bpp;
int size_line;
int endian;
u_int32_t color;
ft_init_texture(perpwalldist, ray, tex);
draw_start = -line_height / 2 + SCREEN_HEIGHT / 2;
@ -142,25 +118,16 @@ static void draw_vertical_line(int x, u_int32_t *img_data, t_ray *ray, t_tex *te
if (draw_end >= SCREEN_HEIGHT)
draw_end = SCREEN_HEIGHT - 1;
y = 0;
while (y < SCREEN_HEIGHT)
while (y++ < SCREEN_HEIGHT)
{
if (y < draw_start)
img_data[y * SCREEN_WIDTH + x] = COLOR_CEILING;
else if (y < draw_end)
{
tex_y = ((y - (-line_height / 2 + SCREEN_HEIGHT / 2)) * tex->tex_height) / line_height;
tex_x = (int)(ray->wallx * (double)tex->tex_width);
if (ray->side == 0 && ray->dir.x > 0)
tex_x = tex->tex_width - tex_x - 1;
if (ray->side == 1 && ray->dir.y < 0)
tex_x = tex->tex_height - tex_x - 1;
u_int32_t *texture_data = (u_int32_t *)mlx_get_data_addr(tex->textures[tex->tex_dir], &bpp, &size_line, &endian);
color = texture_data[tex_x * tex->tex_width + tex_y];
img_data[y * SCREEN_WIDTH + x] = color;
draw_wall(tex, ray, line_height, x, y, img_data);
}
else
img_data[y * SCREEN_WIDTH + x] = COLOR_FLOOR;
y++;
}
}
@ -168,6 +135,7 @@ int render(u_int32_t *img_data, t_tex *tex)
{
int x;
t_ray ray;
x = 0;
while (x < SCREEN_WIDTH)
{

51
render2.c Normal file
View file

@ -0,0 +1,51 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/24 14:26:22 by grobledo #+# #+# */
/* Updated: 2024/10/24 14:27:37 by grobledo ### ########.fr */
/* */
/* ************************************************************************** */
#include "algo.h"
void ft_init_texture(const double perpwalldist, t_ray *ray, t_tex *tex)
{
if (ray->side == 0 && ray->dir.x < 0)
tex->tex_dir = 0;
if (ray->side == 0 && ray->dir.x >= 0)
tex->tex_dir = 1;
if (ray->side == 1 && ray->dir.y < 0)
tex->tex_dir = 2;
if (ray->side == 1 && ray->dir.y >= 0)
tex->tex_dir = 3;
if (ray->side == 0)
ray->wallx = g_map.player.pos.y + perpwalldist \
* ray->dir.y;
else
ray->wallx = g_map.player.pos.x + perpwalldist \
* ray->dir.x;
ray->wallx -= floor((ray->wallx));
}
void draw_wall(t_tex *tex, t_ray *ray, const int line_height, int x,
int y, u_int32_t *img_data)
{
u_int32_t color;
u_int32_t *texture_data;
tex->tex_y = ((y - (-line_height / 2 + SCREEN_HEIGHT / 2)) \
* tex->tex_height) / line_height;
tex->tex_x = (int)(ray->wallx * (double)tex->tex_width);
if (ray->side == 0 && ray->dir.x > 0)
tex->tex_x = tex->tex_width - tex->tex_x - 1;
if (ray->side == 1 && ray->dir.y < 0)
tex->tex_x = tex->tex_height - tex->tex_x - 1;
texture_data = (u_int32_t *)mlx_get_data_addr(tex->textures[tex->tex_dir],
&tex->bpp, &tex->size_line, &tex->endian);
color = texture_data[tex->tex_x * tex->tex_width + tex->tex_y];
img_data[y * SCREEN_WIDTH + x] = color;
}

View file

@ -1,56 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 01:57:03 by greg #+# #+# */
/* Updated: 2024/10/17 17:39:18 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;
// }