removed g_ray and g_tex
This commit is contained in:
parent
ff7ba8760d
commit
50c4e56303
4 changed files with 77 additions and 78 deletions
44
algo.c
44
algo.c
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* algo.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/01 16:24:58 by grobledo #+# #+# */
|
||||
/* Updated: 2024/10/17 16:59:16 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/10/22 15:48:47 by grobledo ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -17,19 +17,35 @@ void *g_mlx = NULL;
|
|||
void *g_win = NULL;
|
||||
t_map g_map;
|
||||
t_player g_player;
|
||||
t_tex g_tex;
|
||||
t_ray g_ray;
|
||||
// t_tex g_tex;
|
||||
|
||||
// TODO manage image format error better
|
||||
|
||||
void draw_screen(void)
|
||||
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->tex_width, &tex->tex_height);
|
||||
tex->textures[3]= mlx_xpm_file_to_image(g_mlx, "textures/west.xpm",
|
||||
&tex->tex_width, &tex->tex_height);
|
||||
}
|
||||
|
||||
void draw_screen()
|
||||
{
|
||||
int bpp;
|
||||
int size_line;
|
||||
int endian;
|
||||
void *img_ptr;
|
||||
u_int32_t *img_data;
|
||||
t_tex tex;
|
||||
|
||||
load_textures(&tex);
|
||||
img_ptr = mlx_new_image(g_mlx, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
img_data = (u_int32_t *)
|
||||
mlx_get_data_addr(img_ptr, &bpp, &size_line, &endian);
|
||||
|
@ -38,26 +54,11 @@ void draw_screen(void)
|
|||
printf("image format error\n");
|
||||
exit(1);
|
||||
}
|
||||
render(img_data);
|
||||
render(img_data, &tex);
|
||||
mlx_put_image_to_window(g_mlx, g_win, img_ptr, 0, 0);
|
||||
mlx_destroy_image(g_mlx, img_ptr);
|
||||
}
|
||||
|
||||
void load_textures()
|
||||
{
|
||||
g_tex.tex_width = 64;
|
||||
g_tex.tex_height = 64;
|
||||
|
||||
g_tex.textures[0] = mlx_xpm_file_to_image(g_mlx, "textures/north.xpm",
|
||||
&g_tex.tex_width, &g_tex.tex_height);
|
||||
g_tex.textures[1] = mlx_xpm_file_to_image(g_mlx, "textures/south.xpm",
|
||||
&g_tex.tex_width, &g_tex.tex_height);
|
||||
g_tex.textures[2]= mlx_xpm_file_to_image(g_mlx, "textures/east.xpm",
|
||||
&g_tex.tex_width, &g_tex.tex_height);
|
||||
g_tex.textures[3]= mlx_xpm_file_to_image(g_mlx, "textures/west.xpm",
|
||||
&g_tex.tex_width, &g_tex.tex_height);
|
||||
}
|
||||
|
||||
static void loop(void)
|
||||
{
|
||||
move();
|
||||
|
@ -80,7 +81,6 @@ int main(int argc, char *argv[])
|
|||
{
|
||||
g_mlx = mlx_init();
|
||||
input_init();
|
||||
load_textures();
|
||||
if (argc != 2)
|
||||
{
|
||||
printf("Syntax: %s <map.cub>\n", argv[0]);
|
||||
|
|
9
algo.h
9
algo.h
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* algo.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <mcolonna@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/09/30 15:45:59 by grobledo #+# #+# */
|
||||
/* Updated: 2024/10/17 16:34:32 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/10/22 15:48:21 by grobledo ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -62,8 +62,7 @@ extern void *g_mlx;
|
|||
extern void *g_win;
|
||||
extern t_player g_player;
|
||||
extern t_map g_map;
|
||||
extern t_tex g_tex;
|
||||
extern t_ray g_ray;
|
||||
// extern t_tex g_tex;
|
||||
|
||||
/// @brief Write an error message on stderr.
|
||||
///
|
||||
|
@ -72,7 +71,7 @@ void write_err(const char *str, ...);
|
|||
|
||||
void vector_from_rotation(t_point_double *vec, double angle, double norm);
|
||||
|
||||
int render(u_int32_t *img_data);
|
||||
int render(u_int32_t *img_data, t_tex *tex);
|
||||
|
||||
void draw_screen(void);
|
||||
|
||||
|
|
100
render.c
100
render.c
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* render.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: greg <greg@student.42.fr> +#+ +:+ +#+ */
|
||||
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/14 14:55:05 by greg #+# #+# */
|
||||
/* Updated: 2024/10/18 02:02:32 by greg ### ########.fr */
|
||||
/* Updated: 2024/10/22 15:50:27 by grobledo ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -14,7 +14,8 @@
|
|||
#include <stdio.h>
|
||||
|
||||
static void calculate_perpwalldist3(int x,
|
||||
t_point_double *deltadist)
|
||||
t_point_double *deltadist,
|
||||
t_ray *ray)
|
||||
{
|
||||
const double ray_direction = 2 * x / (double)SCREEN_WIDTH - 1;
|
||||
t_point_double plane;
|
||||
|
@ -22,27 +23,27 @@ static void calculate_perpwalldist3(int x,
|
|||
|
||||
vector_from_rotation(&dir, g_map.player.rot, 1);
|
||||
vector_from_rotation(&plane, g_map.player.rot + PI / 2, 1);
|
||||
g_ray.dir.x = dir.x + plane.x * ray_direction;
|
||||
g_ray.dir.y = dir.y + plane.y * ray_direction;
|
||||
ray->dir.x = dir.x + plane.x * ray_direction;
|
||||
ray->dir.y = dir.y + plane.y * ray_direction;
|
||||
deltadist->x = 1e30;
|
||||
if (g_ray.dir.x != 0)
|
||||
deltadist->x = fabs(1 / g_ray.dir.x);
|
||||
if (ray->dir.x != 0)
|
||||
deltadist->x = fabs(1 / ray->dir.x);
|
||||
deltadist->y = 1e30;
|
||||
if (g_ray.dir.y != 0)
|
||||
deltadist->y = fabs(1 / g_ray.dir.y);
|
||||
if (ray->dir.y != 0)
|
||||
deltadist->y = fabs(1 / ray->dir.y);
|
||||
}
|
||||
|
||||
static void calculate_perpwalldist2(int x,
|
||||
t_point_double *sidedist,
|
||||
t_point_double *deltadist,
|
||||
t_point_int *step)
|
||||
t_point_int *step,
|
||||
t_ray *ray)
|
||||
{
|
||||
const int map_x = (int)g_map.player.pos.x;
|
||||
const int map_y = (int)g_map.player.pos.y;
|
||||
// t_point_double raydir;
|
||||
|
||||
calculate_perpwalldist3(x, deltadist);
|
||||
if (g_ray.dir.x < 0)
|
||||
calculate_perpwalldist3(x, deltadist, ray);
|
||||
if (ray->dir.x < 0)
|
||||
{
|
||||
step->x = -1;
|
||||
sidedist->x = (g_map.player.pos.x - map_x) * deltadist->x;
|
||||
|
@ -52,7 +53,7 @@ static void calculate_perpwalldist2(int x,
|
|||
step->x = 1;
|
||||
sidedist->x = (map_x + 1.0 - g_map.player.pos.x) * deltadist->x;
|
||||
}
|
||||
if (g_ray.dir.y < 0)
|
||||
if (ray->dir.y < 0)
|
||||
{
|
||||
step->y = -1;
|
||||
sidedist->y = (g_map.player.pos.y - map_y) * deltadist->y;
|
||||
|
@ -69,60 +70,59 @@ static void calculate_perpwalldist2(int x,
|
|||
///
|
||||
/// @param x Ray direction x between 0 and 1.
|
||||
/// @return Result.
|
||||
static double calculate_perpwalldist(int x)
|
||||
static double calculate_perpwalldist(int x, t_ray *ray)
|
||||
{
|
||||
t_point_int map_pos;
|
||||
// bool side;
|
||||
t_point_double sidedist;
|
||||
t_point_double deltadist;
|
||||
t_point_int step;
|
||||
|
||||
map_pos.x = (int)g_map.player.pos.x;
|
||||
map_pos.y = (int)g_map.player.pos.y;
|
||||
calculate_perpwalldist2(x, &sidedist, &deltadist, &step);
|
||||
calculate_perpwalldist2(x, &sidedist, &deltadist, &step, ray);
|
||||
while (map_get_case(&g_map, map_pos.x, map_pos.y)->wall == EMPTY)
|
||||
{
|
||||
if (sidedist.x < sidedist.y)
|
||||
{
|
||||
sidedist.x += deltadist.x;
|
||||
map_pos.x += step.x;
|
||||
g_ray.side = false;
|
||||
ray->side = false;
|
||||
continue ;
|
||||
}
|
||||
sidedist.y += deltadist.y;
|
||||
map_pos.y += step.y;
|
||||
g_ray.side = true;
|
||||
ray->side = true;
|
||||
}
|
||||
if (!g_ray.side)
|
||||
if (!ray->side)
|
||||
return (sidedist.x - deltadist.x);
|
||||
return (sidedist.y - deltadist.y);
|
||||
}
|
||||
|
||||
static void ft_init_texture(const double perpwalldist)
|
||||
static void ft_init_texture(const double perpwalldist, t_ray *ray, t_tex *tex)
|
||||
{
|
||||
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_map.player.pos.y + perpwalldist \
|
||||
* g_ray.dir.y;
|
||||
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
|
||||
g_ray.wallx = g_map.player.pos.x + perpwalldist \
|
||||
* g_ray.dir.x;
|
||||
g_ray.wallx -= floor((g_ray.wallx));
|
||||
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)
|
||||
static void draw_vertical_line(int x, u_int32_t *img_data, t_ray *ray, t_tex *tex)
|
||||
{
|
||||
const double perpwalldist = calculate_perpwalldist(x);
|
||||
const double perpwalldist = calculate_perpwalldist(x, ray);
|
||||
const int line_height = (int)(SCREEN_HEIGHT / perpwalldist);
|
||||
int draw_start;
|
||||
int draw_end;
|
||||
|
@ -134,7 +134,7 @@ static void draw_vertical_line(int x, u_int32_t *img_data)
|
|||
int endian;
|
||||
u_int32_t color;
|
||||
|
||||
ft_init_texture(perpwalldist);
|
||||
ft_init_texture(perpwalldist, ray, tex);
|
||||
draw_start = -line_height / 2 + SCREEN_HEIGHT / 2;
|
||||
if (draw_start < 0)
|
||||
draw_start = 0;
|
||||
|
@ -148,14 +148,14 @@ static void draw_vertical_line(int x, u_int32_t *img_data)
|
|||
img_data[y * SCREEN_WIDTH + x] = COLOR_CEILING;
|
||||
else if (y < draw_end)
|
||||
{
|
||||
tex_y = ((y - (-line_height / 2 + SCREEN_HEIGHT / 2)) * g_tex.tex_height) / line_height;
|
||||
tex_x = (int)(g_ray.wallx * (double)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_x * g_tex.tex_width + tex_y];
|
||||
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;
|
||||
}
|
||||
else
|
||||
|
@ -164,14 +164,14 @@ static void draw_vertical_line(int x, u_int32_t *img_data)
|
|||
}
|
||||
}
|
||||
|
||||
int render(u_int32_t *img_data)
|
||||
int render(u_int32_t *img_data, t_tex *tex)
|
||||
{
|
||||
int x;
|
||||
|
||||
int x;
|
||||
t_ray ray;
|
||||
x = 0;
|
||||
while (x < SCREEN_WIDTH)
|
||||
{
|
||||
draw_vertical_line(x, img_data);
|
||||
draw_vertical_line(x, img_data, &ray, tex);
|
||||
x++;
|
||||
}
|
||||
return (0);
|
||||
|
|
|
@ -6,7 +6,7 @@ SO SOUTH!!!!!!1
|
|||
WE weeeee
|
||||
111111111
|
||||
100000001
|
||||
100000001
|
||||
100010001
|
||||
100000001
|
||||
1000N0001
|
||||
100000001
|
||||
|
|
Loading…
Add table
Reference in a new issue