style: normed all the repo (except the debug function and the too_many_args

This commit is contained in:
Raphael 2024-12-01 18:59:17 +01:00
parent 57c7893501
commit 954c5f76d6
12 changed files with 244 additions and 207 deletions

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 06:02:54 by bgoulard #+# #+# */
/* Updated: 2024/12/01 17:09:40 by bgoulard ### ########.fr */
/* Updated: 2024/12/01 18:57:25 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,34 +20,45 @@
#include <math.h>
#include <unistd.h>
void draw_(int side, double perpWallDist, t_ipoint step, int x, t_info *data)
void set_step(t_ipoint *step, t_dpoint raydir);
void set_side_dist(t_dpoint *side_dist, t_dpoint *tb, t_ipoint pos_i);
/// line 33: normalize draw_start and draw_end
void draw_(int side, double perpWallDist, t_ipoint step, int x, t_info *data)
{
const int lineHeight = (int)(data->screen_size.y / perpWallDist) * cos(deg2rad(FOV/ 2));
int drawStart;
int drawEnd;
const int line_height = (int)(data->screen_size.y / perpWallDist) \
* cos(deg2rad(FOV / 2));
int draw_start;
int draw_end;
int color;
drawStart = -lineHeight / 2 + data->screen_size.y / 2;
drawEnd = lineHeight / 2 + data->screen_size.y / 2;
// normalize drawStart and drawEnd
drawStart = ft_clamp(drawStart, 0, data->screen_size.y - 1);
drawEnd = ft_clamp(drawEnd, 0, data->screen_size.y - 1);
draw_start = -line_height / 2 + data->screen_size.y / 2;
draw_end = line_height / 2 + data->screen_size.y / 2;
draw_start = ft_clamp(draw_start, 0, data->screen_size.y - 1);
draw_end = ft_clamp(draw_end, 0, data->screen_size.y - 1);
color = get_cl(side, step);
while (drawStart < drawEnd)
my_mlx_pixel_put(data, x, drawStart++, color);
while (draw_start < draw_end)
my_mlx_pixel_put(data, x, draw_start++, color);
}
void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *pos_i, 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];
int *side;
t_info *data;
while (true) {
if (sideDist->x < sideDist->y) {
side = (int *)_data[1];
data = (t_info *)_data[0];
while (true)
{
if (sideDist->x < sideDist->y)
{
sideDist->x += deltaDist.x;
pos_i->x += step.x;
*side = 0;
} else {
}
else
{
sideDist->y += deltaDist.y;
pos_i->y += step.y;
*side = 1;
@ -58,74 +69,52 @@ void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *pos_i, t_ipoin
}
}
static void set_step(t_ipoint *step, t_dpoint raydir)
void column_handler(t_ipoint pos_i, t_dpoint ray_dir, t_info *data, int x)
{
if (raydir.x < 0)
step->x = -1;
else
step->x = 1;
if (raydir.y < 0)
step->y = -1;
else
step->y = 1;
}
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 - pos_i.x) * deltaDist.x;
else
sideDist->x = (pos_i.x + 1.0 - pos.x) * deltaDist.x;
if (rayDir.y < 0)
sideDist->y = (pos.y - pos_i.y) * deltaDist.y;
else
sideDist->y = (pos_i.y + 1.0 - pos.y) * deltaDist.y;
}
void column_handler(t_ipoint pos_i, t_dpoint rayDir, t_info *data, int x)
{
t_dpoint sideDist;
t_dpoint deltaDist;
double perpWallDist;
t_dpoint side_dist;
t_dpoint delta_dist;
double perp_wall_dist;
t_ipoint step;
int side;
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}, pos_i);
search_hit(&sideDist, deltaDist, &pos_i, step, (void *[]){data, &side});
delta_dist = (t_dpoint){fabs(1 / ray_dir.x), fabs(1 / ray_dir.y)};
set_step(&step, ray_dir);
set_side_dist(&side_dist, (t_dpoint[]){ray_dir, data->player.pos, \
delta_dist}, pos_i);
search_hit(&side_dist, delta_dist, &pos_i, step, (void *[]){data, &side});
if (side == 0)
perpWallDist = (pos_i.x - data->player.pos.x + (double)(1 - step.x) / 2)
/ rayDir.x;
perp_wall_dist = (pos_i.x - data->player.pos.x + \
(double)(1 - step.x) / 2) / ray_dir.x;
else
perpWallDist = (pos_i.y - data->player.pos.y + (double)(1 - step.y) / 2)
/ rayDir.y;
draw_(side, perpWallDist, step, x, data);
perp_wall_dist = (pos_i.y - data->player.pos.y + \
(double)(1 - step.y) / 2) / ray_dir.y;
draw_(side, perp_wall_dist, step, x, data);
}
int render_frame(t_info *data)
{
double camera_x;
double coef;
double camera_x;
double coef;
int x;
x = 0;
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));
for(int x = 0; x < data->screen_size.x; x++)
ft_bzero(data->camera.img_addr, data->screen_size.x * data->screen_size.y \
* (data->camera.bpp / 8));
while (x < data->screen_size.x)
{
camera_x = x * coef - 1;
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},
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);
x++;
}
mlx_put_image_to_window(data->mlx_ptr, data->win_ptr, data->camera.screen_buff, 0, 0);
mlx_put_image_to_window(data->mlx_ptr, data->win_ptr, \
data->camera.screen_buff, 0, 0);
return (0);
}
// Return 42 is irrelevant check mlx_loop to see that mlx doesn't care for the
// return of the function...
// (Why 'int (*)(void*)' when you dont use the int)
@ -139,12 +128,11 @@ int render_frame(t_info *data)
// struct.
// As a pure artefact of using mlx this function will likely be mooved to
// mlx_layer in the final repo.
int c3_frame_update(void *inf_ptr)
int c3_frame_update(void *inf_ptr)
{
t_info *info;
t_info *info;
info = inf_ptr;
mlx_clear_window(info->mlx_ptr, info->win_ptr);
// ft_putendl_fd("update called\n", STDOUT_FILENO);
return (EXIT_SUCCESS);
}

52
raycast/frame_update2.c Normal file
View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* frame_update2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 18:55:11 by rparodi #+# #+# */
/* Updated: 2024/12/01 18:57:21 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
#include "cub3d_struct.h"
#include "mlx_functions.h"
#include "ft_string.h"
#include "ft_math.h"
#include <math.h>
#include <unistd.h>
void set_step(t_ipoint *step, t_dpoint raydir)
{
if (raydir.x < 0)
step->x = -1;
else
step->x = 1;
if (raydir.y < 0)
step->y = -1;
else
step->y = 1;
}
void set_side_dist(t_dpoint *side_dist, t_dpoint *tb, t_ipoint pos_i)
{
t_dpoint ray_dir;
t_dpoint pos;
t_dpoint delta_dist;
ray_dir = tb[0];
pos = tb[1];
delta_dist = tb[2];
if (ray_dir.x < 0)
side_dist->x = (pos.x - pos_i.x) * delta_dist.x;
else
side_dist->x = (pos_i.x + 1.0 - pos.x) * delta_dist.x;
if (ray_dir.y < 0)
side_dist->y = (pos.y - pos_i.y) * delta_dist.y;
else
side_dist->y = (pos_i.y + 1.0 - pos.y) * delta_dist.y;
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 12:24:35 by rparodi #+# #+# */
/* Updated: 2024/11/28 15:15:15 by bgoulard ### ########.fr */
/* Updated: 2024/12/01 18:39:11 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,13 +17,14 @@
#include <stdlib.h>
/**
* @brief Launches a ray for raycasting to determine the distance to the first wall.
* @brief Launches a ray for raycasting to determine the distance to the first
* wall.
*
* This function calculates the distance of a ray cast in a specified angle
* until it either hits a wall or reaches the maximum range.
*
* @param info A pointer to the `t_info` structure containing the map and other relevant data.
* @param info A pointer to the `t_info` structure containing the map
* and other relevant data.
* @param angle The angle of the ray being cast, in radians.
*
* @return The distance from the starting position to the first wall hit.
@ -32,10 +33,15 @@
*/
double rc_launch(t_info *info, double angle)
{
double distance = 0;
t_dpoint direction = { cos(angle), sin(angle) };
t_dpoint rayon = { 0, 0 };
double distance;
t_dpoint direction;
t_dpoint rayon;
distance = 0;
direction.x = cos(angle);
direction.y = sin(angle);
rayon.x = 0;
rayon.y = 0;
while (distance < MAX_RANGE)
{
rayon.x = info->player.pos.x + direction.x * distance;
@ -47,51 +53,7 @@ double rc_launch(t_info *info, double angle)
return (MAX_RANGE);
}
/**
* @brief Launches algorithm over a specified width with angle adjustments.
*
* @param info The structure of the game (for the view)
* @param spacing The spacing between rays.
* @param focal The focal length affecting the angle adjustment.
* @param width The total width (number of columns) to iterate over.
*
* @note The function assumes `rc_launch` is defined elsewhere to handle the angle.
*/
void draw_line(t_info *info, int x, int start, int end, unsigned int color)
{
while (start <= end)
{
mlx_pixel_put(info->mlx_ptr, info->win_ptr, x, start, color);
start++;
}
}
/*
{
info->player.pos = (t_dpoint){ 4.5, 4.5 }; // Starting in the middle of the map
info->player.view = 0; int x;
double angle;
double distance;
double projection_plane = info->map.size.x / (2.0 * tan(M_PI / 6.0));
for (x = 0; x < info->map.size.x; x++)
{
angle = info->player.view + atan((x - info->map.size.x / 2.0) / projection_plane);
distance = rc_launch(info, angle);
int line_height = (int)(TILE_SIZE / distance * projection_plane);
int start = (info->map.size.y / 2) - (line_height / 2);
int end = (info->map.size.y / 2) + (line_height / 2);
if (start < 0) start = 0;
if (end >= info->map.size.y) end = info->map.size.y - 1;
draw_line(info, x, start, end, 0xFFFFFF);
}
}*/
int shelves_launch(t_info *info)
int shelves_launch(t_info *info)
{
render_frame(info);
return (EXIT_SUCCESS);

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/28 14:09:12 by bgoulard #+# #+# */
/* Updated: 2024/11/29 15:33:19 by bgoulard ### ########.fr */
/* Updated: 2024/12/01 18:39:24 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,10 +30,8 @@ double deg2rad(int deg)
void rotate_plane(t_dpoint *plane, double angle)
{
double old_plane_x;
// double old_plane_y;
old_plane_x = (*plane).x;
// old_plane_y = (*plane).y;
plane->x = plane->x * cos(angle) - plane->y * sin(angle);
plane->y = old_plane_x * sin(angle) + plane->y * cos(angle);
}