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: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ # # By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/11/28 14:30:42 by bgoulard ### ########.fr # # Updated: 2024/12/01 18:57:47 by rparodi ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -44,24 +44,31 @@ MLXFLAGS += -L/opt/X11/lib
LDFLAGS += $(MLXFLAGS) LDFLAGS += $(MLXFLAGS)
SRC =\ SRC =\
parsing/arguments.c \ mlx_layer/mlx_init.c \
mlx_layer/looks.c \
mlx_layer/mlx_load_texture.c \
mlx_layer/mooves.c \
mlx_layer/hooks.c \
raycast/frame_update.c \
raycast/frame_update2.c \
raycast/rc_utils.c \
raycast/utils_math.c \
raycast/get_cl.c \
sources/options_impl.c \
sources/main.c \
sources/error.c \
sources/cleanups.c \
sources/options.c \
sources/rgb_to_color.c \
parsing/load_textures.c \
parsing/load_bgs.c \
parsing/traverse_map.c \
parsing/load_file.c \
parsing/utils.c \
parsing/map.c \ parsing/map.c \
parsing/blank_for_raph.c \ parsing/blank_for_raph.c \
sources/rgb_to_color.c \ parsing/arguments.c \
sources/error.c \ parsing/load_tiles.c
sources/options.c \
sources/options_impl.c \
sources/cleanups.c \
sources/main.c \
mlx_layer/mlx_load_texture.c \
mlx_layer/mlx_init.c \
mlx_layer/mooves.c \
mlx_layer/looks.c \
mlx_layer/hooks.c \
raycast/utils_math.c \
raycast/rc_utils.c \
raycast/get_cl.c \
raycast/frame_update.c \
# Objects # Objects
OBJDIRNAME = ./build OBJDIRNAME = ./build

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/30 16:30:26 by rparodi #+# #+# */ /* Created: 2024/10/30 16:30:26 by rparodi #+# #+# */
/* Updated: 2024/11/29 16:41:34 by bgoulard ### ########.fr */ /* Updated: 2024/12/01 17:59:19 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/28 13:53:54 by bgoulard #+# #+# */ /* Created: 2024/11/28 13:53:54 by bgoulard #+# #+# */
/* Updated: 2024/12/01 17:33:48 by rparodi ### ########.fr */ /* Updated: 2024/12/01 18:01:48 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -74,10 +74,16 @@ typedef enum e_tile
WALL = 1, WALL = 1,
} t_tile_type; } t_tile_type;
typedef struct s_tile { /*
bool tile_visited; // parsing * @param tile_visited parsing
unsigned int other; // disponible * @param other disponible
unsigned int tile_type; // 16 tile types possible * @param tile_type 16 tiles types possible
*/
typedef struct s_tile
{
bool tile_visited;
unsigned int other;
unsigned int tile_type;
} t_tile; } t_tile;
typedef struct s_map typedef struct s_map

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/28 14:12:25 by bgoulard #+# #+# */ /* Created: 2024/11/28 14:12:25 by bgoulard #+# #+# */
/* Updated: 2024/12/01 17:27:45 by rparodi ### ########.fr */ /* Updated: 2024/12/01 18:46:32 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,11 +15,21 @@
#include "mlx_functions.h" #include "mlx_functions.h"
#include "ft_char.h"
#include <X11/keysym.h> #include <X11/keysym.h>
#include <stdio.h> #include <stdio.h>
int key_hook(int keycode, t_info *data) int key_hook(int keycode, t_info *data)
{ {
if (ft_isalpha(keycode))
printf("Event detected: %d\t(%c)\n", keycode, keycode);
else if (keycode == 65361)
printf("Event detected: %d\t(Left Arrow)\n", keycode);
else if (keycode == 65363)
printf("Event detected: %d\t(Right Arrow)\n", keycode);
else if (keycode == 65307)
printf("Event detected: %d\t(Echap)\n", keycode);
else
printf("Event detected: %d\n", keycode); printf("Event detected: %d\n", keycode);
if (keycode == XK_Escape) if (keycode == XK_Escape)
mlx_loop_end(data->mlx_ptr); mlx_loop_end(data->mlx_ptr);
@ -37,4 +47,3 @@ int key_hook(int keycode, t_info *data)
look_right(data); look_right(data);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/11 19:53:42 by rparodi #+# #+# */ /* Created: 2024/11/11 19:53:42 by rparodi #+# #+# */
/* Updated: 2024/12/01 17:33:00 by rparodi ### ########.fr */ /* Updated: 2024/12/01 18:02:27 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,6 +23,7 @@
int c3_frame_update(void *inf_ptr); int c3_frame_update(void *inf_ptr);
int key_hook(int keycode, t_info *data); int key_hook(int keycode, t_info *data);
/* move player w keys and call to redraw screen */ /* move player w keys and call to redraw screen */
int c3_keyhook(int keycode, t_info *info) int c3_keyhook(int keycode, t_info *info)
{ {
@ -38,7 +39,8 @@ int c3_redcross(t_info *info)
t_win_list *c3_init_mlx_window(t_info *info) t_win_list *c3_init_mlx_window(t_info *info)
{ {
mlx_get_screen_size(info->mlx_ptr, &info->screen_size.x, &info->screen_size.y); mlx_get_screen_size(info->mlx_ptr, \
&info->screen_size.x, &info->screen_size.y);
info->screen_size.x *= WIN_COEF; info->screen_size.x *= WIN_COEF;
info->screen_size.y *= WIN_COEF; info->screen_size.y *= WIN_COEF;
ft_clamp(info->screen_size.x, 0, 1920); ft_clamp(info->screen_size.x, 0, 1920);
@ -57,7 +59,8 @@ int init_mlx_env(t_info *info)
if (!info->win_ptr) if (!info->win_ptr)
return (ERROR_MLX); return (ERROR_MLX);
mlx_hook(info->win_ptr, KeyPress, KeyPressMask, key_hook, info); mlx_hook(info->win_ptr, KeyPress, KeyPressMask, key_hook, info);
mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, c3_redcross, info); mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, \
c3_redcross, info);
mlx_loop_hook(info->mlx_ptr, (int (*)())shelves_launch, info); mlx_loop_hook(info->mlx_ptr, (int (*)())shelves_launch, info);
return (NO_ERROR); return (NO_ERROR);
} }

View file

@ -6,12 +6,13 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/28 14:10:44 by bgoulard #+# #+# */ /* Created: 2024/11/28 14:10:44 by bgoulard #+# #+# */
/* Updated: 2024/12/01 17:15:27 by bgoulard ### ########.fr */ /* Updated: 2024/12/01 18:36:26 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d.h" #include "cub3d.h"
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "cub3d_parsing.h"
void update_pos_i(t_player *player) void update_pos_i(t_player *player)
{ {
@ -24,12 +25,14 @@ void move_straight(t_info *data)
t_tile *tile_x; t_tile *tile_x;
t_tile *tile_y; t_tile *tile_y;
tile_x = c3_get_cell(data->map.map, data->map.size, tile_x = c3_get_cell(data->map.map, data->map.size, \
(t_ipoint){(int)(data->player.pos.x + data->player.dir.x * MOVE_SPEED), (int)data->player.pos.y}); (t_ipoint){(int)(data->player.pos.x + data->player.dir.x * MOVE_SPEED), \
(int)data->player.pos.y});
if (tile_x->tile_type == EMPTY) if (tile_x->tile_type == EMPTY)
data->player.pos.x += data->player.dir.x * MOVE_SPEED; data->player.pos.x += data->player.dir.x * MOVE_SPEED;
tile_y = c3_get_cell(data->map.map, data->map.size, tile_y = c3_get_cell(data->map.map, data->map.size, \
(t_ipoint){(int)data->player.pos.x, (int)(data->player.pos.y + data->player.dir.y * MOVE_SPEED)}); (t_ipoint){(int)data->player.pos.x, (int)(data->player.pos.y \
+ data->player.dir.y * MOVE_SPEED)});
if (tile_y->tile_type == EMPTY) if (tile_y->tile_type == EMPTY)
data->player.pos.y += data->player.dir.y * MOVE_SPEED; data->player.pos.y += data->player.dir.y * MOVE_SPEED;
update_pos_i(&data->player); update_pos_i(&data->player);
@ -40,12 +43,14 @@ void move_backward(t_info *data)
t_tile *tile_x; t_tile *tile_x;
t_tile *tile_y; t_tile *tile_y;
tile_x = c3_get_cell(data->map.map, data->map.size, tile_x = c3_get_cell(data->map.map, data->map.size, \
(t_ipoint){(int)(data->player.pos.x - data->player.dir.x * MOVE_SPEED), (int)data->player.pos.y}); (t_ipoint){(int)(data->player.pos.x - data->player.dir.x * MOVE_SPEED), \
(int)data->player.pos.y});
if (tile_x->tile_type == EMPTY) if (tile_x->tile_type == EMPTY)
data->player.pos.x -= data->player.dir.x * MOVE_SPEED; data->player.pos.x -= data->player.dir.x * MOVE_SPEED;
tile_y = c3_get_cell(data->map.map, data->map.size, tile_y = c3_get_cell(data->map.map, data->map.size, \
(t_ipoint){(int)data->player.pos.x, (int)(data->player.pos.y - data->player.dir.y * MOVE_SPEED)}); (t_ipoint){(int)data->player.pos.x, (int)(data->player.pos.y \
- data->player.dir.y * MOVE_SPEED)});
if (tile_y->tile_type == EMPTY) if (tile_y->tile_type == EMPTY)
data->player.pos.y -= data->player.dir.y * MOVE_SPEED; data->player.pos.y -= data->player.dir.y * MOVE_SPEED;
update_pos_i(&data->player); update_pos_i(&data->player);
@ -59,12 +64,14 @@ void move_left(t_info *data)
pplayer.x = -data->player.dir.y; pplayer.x = -data->player.dir.y;
pplayer.y = data->player.dir.x; pplayer.y = data->player.dir.x;
tile_x = c3_get_cell(data->map.map, data->map.size, tile_x = c3_get_cell(data->map.map, data->map.size, \
(t_ipoint){(int)(data->player.pos.x + pplayer.x * MOVE_SPEED), (int)data->player.pos.y}); (t_ipoint){(int)(data->player.pos.x + pplayer.x * MOVE_SPEED), \
(int)data->player.pos.y});
if (tile_x->tile_type == EMPTY) if (tile_x->tile_type == EMPTY)
data->player.pos.x += pplayer.x * MOVE_SPEED; data->player.pos.x += pplayer.x * MOVE_SPEED;
tile_y = c3_get_cell(data->map.map, data->map.size, tile_y = c3_get_cell(data->map.map, data->map.size, \
(t_ipoint){(int)data->player.pos.x, (int)(data->player.pos.y + pplayer.y * MOVE_SPEED)}); (t_ipoint){(int)data->player.pos.x, (int)(data->player.pos.y + \
pplayer.y * MOVE_SPEED)});
if (tile_y->tile_type == EMPTY) if (tile_y->tile_type == EMPTY)
data->player.pos.y += pplayer.y * MOVE_SPEED; data->player.pos.y += pplayer.y * MOVE_SPEED;
update_pos_i(&data->player); update_pos_i(&data->player);
@ -78,12 +85,14 @@ void move_right(t_info *data)
pplayer.x = -data->player.dir.y; pplayer.x = -data->player.dir.y;
pplayer.y = data->player.dir.x; pplayer.y = data->player.dir.x;
tile_x = c3_get_cell(data->map.map, data->map.size, tile_x = c3_get_cell(data->map.map, data->map.size, \
(t_ipoint){(int)(data->player.pos.x - pplayer.x * MOVE_SPEED), (int)data->player.pos.y}); (t_ipoint){(int)(data->player.pos.x - pplayer.x * MOVE_SPEED), \
(int)data->player.pos.y});
if (tile_x->tile_type == EMPTY) if (tile_x->tile_type == EMPTY)
data->player.pos.x -= pplayer.x * MOVE_SPEED; data->player.pos.x -= pplayer.x * MOVE_SPEED;
tile_y = c3_get_cell(data->map.map, data->map.size, tile_y = c3_get_cell(data->map.map, data->map.size, \
(t_ipoint){(int)data->player.pos.x, (int)(data->player.pos.y - pplayer.y * MOVE_SPEED)}); (t_ipoint){(int)data->player.pos.x, (int)(data->player.pos.y - pplayer.y \
* MOVE_SPEED)});
if (tile_y->tile_type == EMPTY) if (tile_y->tile_type == EMPTY)
data->player.pos.y -= pplayer.y * MOVE_SPEED; data->player.pos.y -= pplayer.y * MOVE_SPEED;
update_pos_i(&data->player); update_pos_i(&data->player);

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/13 06:44:42 by bgoulard #+# #+# */ /* Created: 2024/11/13 06:44:42 by bgoulard #+# #+# */
/* Updated: 2024/11/29 16:07:01 by bgoulard ### ########.fr */ /* Updated: 2024/12/01 18:51:20 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,12 +38,11 @@ static t_tile char_to_tile(char c)
/// @note The blank does not fill in the textures. /// @note The blank does not fill in the textures.
void blank(t_info *info) void blank(t_info *info)
{ {
int i = 0; int i;
i = 0;
info->map.size.x = 5; info->map.size.x = 5;
info->map.size.y = 6; info->map.size.y = 6;
// info->map.player_pos.x = 2.5;
// info->map.player_pos.y = 2.5;
info->map.fraw = malloc(sizeof(char *) * 6); info->map.fraw = malloc(sizeof(char *) * 6);
info->map.fraw[0] = ft_strdup("11111"); info->map.fraw[0] = ft_strdup("11111");
info->map.fraw[1] = ft_strdup("10001"); info->map.fraw[1] = ft_strdup("10001");
@ -51,11 +50,11 @@ void blank(t_info *info)
info->map.fraw[3] = ft_strdup("10001"); info->map.fraw[3] = ft_strdup("10001");
info->map.fraw[4] = ft_strdup("10111"); info->map.fraw[4] = ft_strdup("10111");
info->map.fraw[5] = ft_strdup("11111"); info->map.fraw[5] = ft_strdup("11111");
info->map.map = ft_calloc(sizeof(t_tile), (info->map.size.y * info->map.size.x)); info->map.map = ft_calloc(sizeof(t_tile), \
(info->map.size.y * info->map.size.x));
while (i < info->map.size.y * info->map.size.x) while (i < info->map.size.y * info->map.size.x)
{ {
info->map.map[i] = char_to_tile ( info->map.map[i] = char_to_tile (\
info->map.fraw[i / info->map.size.x][i % info->map.size.x]); info->map.fraw[i / info->map.size.x][i % info->map.size.x]);
i++; i++;
} }

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 06:02:54 by bgoulard #+# #+# */ /* 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 <math.h>
#include <unistd.h> #include <unistd.h>
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) 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)); const int line_height = (int)(data->screen_size.y / perpWallDist) \
int drawStart; * cos(deg2rad(FOV / 2));
int drawEnd; int draw_start;
int draw_end;
int color; int color;
drawStart = -lineHeight / 2 + data->screen_size.y / 2; draw_start = -line_height / 2 + data->screen_size.y / 2;
drawEnd = lineHeight / 2 + data->screen_size.y / 2; draw_end = line_height / 2 + data->screen_size.y / 2;
// normalize drawStart and drawEnd draw_start = ft_clamp(draw_start, 0, data->screen_size.y - 1);
drawStart = ft_clamp(drawStart, 0, data->screen_size.y - 1); draw_end = ft_clamp(draw_end, 0, data->screen_size.y - 1);
drawEnd = ft_clamp(drawEnd, 0, data->screen_size.y - 1);
color = get_cl(side, step); color = get_cl(side, step);
while (drawStart < drawEnd) while (draw_start < draw_end)
my_mlx_pixel_put(data, x, drawStart++, color); 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]; int *side;
t_info *data = (t_info *)_data[0]; t_info *data;
while (true) { side = (int *)_data[1];
if (sideDist->x < sideDist->y) { data = (t_info *)_data[0];
while (true)
{
if (sideDist->x < sideDist->y)
{
sideDist->x += deltaDist.x; sideDist->x += deltaDist.x;
pos_i->x += step.x; pos_i->x += step.x;
*side = 0; *side = 0;
} else { }
else
{
sideDist->y += deltaDist.y; sideDist->y += deltaDist.y;
pos_i->y += step.y; pos_i->y += step.y;
*side = 1; *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) t_dpoint side_dist;
step->x = -1; t_dpoint delta_dist;
else double perp_wall_dist;
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_ipoint step; t_ipoint step;
int side; int side;
deltaDist = (t_dpoint){fabs(1 / rayDir.x), fabs(1 / rayDir.y)}; delta_dist = (t_dpoint){fabs(1 / ray_dir.x), fabs(1 / ray_dir.y)};
set_step(&step, rayDir); set_step(&step, ray_dir);
set_side_dist(&sideDist, (t_dpoint[]){rayDir, data->player.pos, deltaDist}, pos_i); set_side_dist(&side_dist, (t_dpoint[]){ray_dir, data->player.pos, \
search_hit(&sideDist, deltaDist, &pos_i, step, (void *[]){data, &side}); delta_dist}, pos_i);
search_hit(&side_dist, delta_dist, &pos_i, step, (void *[]){data, &side});
if (side == 0) if (side == 0)
perpWallDist = (pos_i.x - data->player.pos.x + (double)(1 - step.x) / 2) perp_wall_dist = (pos_i.x - data->player.pos.x + \
/ rayDir.x; (double)(1 - step.x) / 2) / ray_dir.x;
else else
perpWallDist = (pos_i.y - data->player.pos.y + (double)(1 - step.y) / 2) perp_wall_dist = (pos_i.y - data->player.pos.y + \
/ rayDir.y; (double)(1 - step.y) / 2) / ray_dir.y;
draw_(side, perpWallDist, step, x, data); draw_(side, perp_wall_dist, step, x, data);
} }
int render_frame(t_info *data) int render_frame(t_info *data)
{ {
double camera_x; double camera_x;
double coef; double coef;
int x;
x = 0;
coef = 2 * tan(deg2rad(FOV) / 2) / (double)data->screen_size.x; 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 \
for(int x = 0; x < data->screen_size.x; x++) * (data->camera.bpp / 8));
while (x < data->screen_size.x)
{ {
camera_x = x * coef - 1; camera_x = x * coef - 1;
column_handler(data->player.pos_i, column_handler(data->player.pos_i, (t_dpoint){\
(t_dpoint){ data->player.dir.x + data->player.plane.x * camera_x, \
data->player.dir.x + data->player.plane.x * camera_x, data->player.dir.y + data->player.plane.y * camera_x}, \
data->player.dir.y + data->player.plane.y * camera_x},
data, 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 (0);
} }
// Return 42 is irrelevant check mlx_loop to see that mlx doesn't care for the // Return 42 is irrelevant check mlx_loop to see that mlx doesn't care for the
// return of the function... // return of the function...
// (Why 'int (*)(void*)' when you dont use the int) // (Why 'int (*)(void*)' when you dont use the int)
@ -145,6 +134,5 @@ int c3_frame_update(void *inf_ptr)
info = inf_ptr; info = inf_ptr;
mlx_clear_window(info->mlx_ptr, info->win_ptr); mlx_clear_window(info->mlx_ptr, info->win_ptr);
// ft_putendl_fd("update called\n", STDOUT_FILENO);
return (EXIT_SUCCESS); 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> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 12:24:35 by rparodi #+# #+# */ /* 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> #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 * This function calculates the distance of a ray cast in a specified angle
* until it either hits a wall or reaches the maximum range. * until it either hits a wall or reaches the maximum range.
* *
* @param info A pointer to the `t_info` structure containing the map
* @param info A pointer to the `t_info` structure containing the map and other relevant data. * and other relevant data.
* @param angle The angle of the ray being cast, in radians. * @param angle The angle of the ray being cast, in radians.
* *
* @return The distance from the starting position to the first wall hit. * @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 rc_launch(t_info *info, double angle)
{ {
double distance = 0; double distance;
t_dpoint direction = { cos(angle), sin(angle) }; t_dpoint direction;
t_dpoint rayon = { 0, 0 }; t_dpoint rayon;
distance = 0;
direction.x = cos(angle);
direction.y = sin(angle);
rayon.x = 0;
rayon.y = 0;
while (distance < MAX_RANGE) while (distance < MAX_RANGE)
{ {
rayon.x = info->player.pos.x + direction.x * distance; rayon.x = info->player.pos.x + direction.x * distance;
@ -47,50 +53,6 @@ double rc_launch(t_info *info, double angle)
return (MAX_RANGE); 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); render_frame(info);

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/28 14:09:12 by bgoulard #+# #+# */ /* 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) void rotate_plane(t_dpoint *plane, double angle)
{ {
double old_plane_x; double old_plane_x;
// double old_plane_y;
old_plane_x = (*plane).x; old_plane_x = (*plane).x;
// old_plane_y = (*plane).y;
plane->x = plane->x * cos(angle) - plane->y * sin(angle); plane->x = plane->x * cos(angle) - plane->y * sin(angle);
plane->y = old_plane_x * sin(angle) + plane->y * cos(angle); plane->y = old_plane_x * sin(angle) + plane->y * cos(angle);
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */ /* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */
/* Updated: 2024/12/01 17:27:34 by bgoulard ### ########.fr */ /* Updated: 2024/12/01 18:53:12 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -50,10 +50,14 @@ void dump_info(t_info *info)
i = 0; i = 0;
printf("t_info:\n"); printf("t_info:\n");
printf("\tplayer:\n"); printf("\tplayer:\n");
printf("\t\tpos_i:\t(x: %d, y:%d)\n", info->player.pos_i.x, info->player.pos_i.y); printf("\t\tpos_i:\t(x: %d, y:%d)\n", \
printf("\t\tpos:\t(x:%lf, y:%lf)\n", info->player.pos.x, info->player.pos.y); info->player.pos_i.x, info->player.pos_i.y);
printf("\t\tdir:\t(x:%lf, y:%lf)\n", info->player.dir.x, info->player.dir.y); printf("\t\tpos:\t(x:%lf, y:%lf)\n", \
printf("\t\tplane:\t(x:%lf, y:%lf)\n", info->player.plane.x, info->player.plane.y); info->player.pos.x, info->player.pos.y);
printf("\t\tdir:\t(x:%lf, y:%lf)\n", \
info->player.dir.x, info->player.dir.y);
printf("\t\tplane:\t(x:%lf, y:%lf)\n", \
info->player.plane.x, info->player.plane.y);
printf("\tcli_ctx:\n"); printf("\tcli_ctx:\n");
printf("\t\tfile:\t%s\n", info->cli_ctx.file); printf("\t\tfile:\t%s\n", info->cli_ctx.file);
printf("\t\tdebug:\t%s\n", bool_str[info->cli_ctx.debug]); printf("\t\tdebug:\t%s\n", bool_str[info->cli_ctx.debug]);