merge of parsing and raycast finished (not normed)

This commit is contained in:
B.Goulard 2024-11-29 17:12:04 +01:00
parent 00dc0e9e67
commit aa496d4f5d
11 changed files with 106 additions and 84 deletions

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 06:02:54 by bgoulard #+# #+# */
/* Updated: 2024/11/28 15:18:47 by bgoulard ### ########.fr */
/* Updated: 2024/11/29 17:01:14 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,6 @@
#include "ft_math.h"
#include <math.h>
#include <stdio.h>
#include <unistd.h>
void draw_(int side, double perpWallDist, t_ipoint step, int x, t_info *data)
@ -38,7 +37,7 @@ void draw_(int side, double perpWallDist, t_ipoint step, int x, t_info *data)
my_mlx_pixel_put(data, x, drawStart++, color);
}
void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *map, t_ipoint step, void *_data[2])
void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *pos_i, t_ipoint step, void *_data[2])
{
int *side = (int *)_data[1];
t_info *data = (t_info *)_data[0];
@ -46,21 +45,25 @@ void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *map, t_ipoint
while (true) {
if (sideDist->x < sideDist->y) {
sideDist->x += deltaDist.x;
map->x += step.x;
pos_i->x += step.x;
*side = 0;
} else {
sideDist->y += deltaDist.y;
map->y += step.y;
pos_i->y += step.y;
*side = 1;
}
printf("map.x: %d, map.y: %d\n", map->x, map->y);
if ((*c3_get_cell(data->map.map, data->map.size, *map))
if (data->map.size.x < 0 || data->map.size.y < 0 ||
data->map.size.x > 100 || data->map.size.y > 100)
exit (EXIT_FAILURE);
if ((*c3_get_cell(data->map.map, data->map.size, *pos_i))
.tile_type != EMPTY)
return ;
}
}
void static set_step(t_ipoint *step, t_dpoint raydir)
static void set_step(t_ipoint *step, t_dpoint raydir)
{
if (raydir.x < 0)
step->x = -1;
@ -72,23 +75,23 @@ void static set_step(t_ipoint *step, t_dpoint raydir)
step->y = 1;
}
void static set_side_dist(t_dpoint *sideDist, t_dpoint *tb, t_ipoint map)
static void set_side_dist(t_dpoint *sideDist, t_dpoint *tb, t_ipoint pos_i)
{
t_dpoint rayDir = tb[0];
t_dpoint pos = tb[1];
t_dpoint deltaDist = tb[2];
if (rayDir.x < 0)
sideDist->x = (pos.x - map.x) * deltaDist.x;
sideDist->x = (pos.x - pos_i.x) * deltaDist.x;
else
sideDist->x = (map.x + 1.0 - pos.x) * deltaDist.x;
sideDist->x = (pos_i.x + 1.0 - pos.x) * deltaDist.x;
if (rayDir.y < 0)
sideDist->y = (pos.y - map.y) * deltaDist.y;
sideDist->y = (pos.y - pos_i.y) * deltaDist.y;
else
sideDist->y = (map.y + 1.0 - pos.y) * deltaDist.y;
sideDist->y = (pos_i.y + 1.0 - pos.y) * deltaDist.y;
}
void column_handler(t_ipoint map, t_dpoint rayDir, t_info *data, int x)
void column_handler(t_ipoint pos_i, t_dpoint rayDir, t_info *data, int x)
{
t_dpoint sideDist;
t_dpoint deltaDist;
@ -98,13 +101,13 @@ void column_handler(t_ipoint map, t_dpoint rayDir, t_info *data, int x)
deltaDist = (t_dpoint){fabs(1 / rayDir.x), fabs(1 / rayDir.y)};
set_step(&step, rayDir);
set_side_dist(&sideDist, (t_dpoint[]){rayDir, data->player.pos, deltaDist}, map);
search_hit(&sideDist, deltaDist, &map, step, (void *[]){data, &side});
set_side_dist(&sideDist, (t_dpoint[]){rayDir, data->player.pos, deltaDist}, pos_i);
search_hit(&sideDist, deltaDist, &pos_i, step, (void *[]){data, &side});
if (side == 0)
perpWallDist = (map.x - data->player.pos.x + (double)(1 - step.x) / 2)
perpWallDist = (pos_i.x - data->player.pos.x + (double)(1 - step.x) / 2)
/ rayDir.x;
else
perpWallDist = (map.y - data->player.pos.y + (double)(1 - step.y) / 2)
perpWallDist = (pos_i.y - data->player.pos.y + (double)(1 - step.y) / 2)
/ rayDir.y;
draw_(side, perpWallDist, step, x, data);
}
@ -115,12 +118,14 @@ int render_frame(t_info *data)
double coef;
coef = 2 * tan(deg2rad(FOV) / 2) / (double)data->screen_size.x;
ft_bzero(data->camera.img_addr, data->screen_size.x * data->screen_size.y * data->camera.bpp / 8);
ft_bzero(data->camera.img_addr, data->screen_size.x * data->screen_size.y * (data->camera.bpp / 8));
for(int x = 0; x < data->screen_size.x; x++)
{
camera_x = x * coef - 1;
column_handler((t_ipoint){(int)data->player.pos.x, (int)data->player.pos.y},
(t_dpoint){data->player.dir.x + data->player.plane.x * camera_x, data->player.dir.y + data->player.plane.y * camera_x},
column_handler(data->player.pos_i,
(t_dpoint){
data->player.dir.x + data->player.plane.x * camera_x,
data->player.dir.y + data->player.plane.y * camera_x},
data, x);
}
mlx_put_image_to_window(data->mlx_ptr, data->win_ptr, data->camera.screen_buff, 0, 0);