v1.0 - normed texture system, multiple keypress, fixes on parsing, removed trailing maps

This commit is contained in:
B.Goulard 2024-12-16 14:56:22 +01:00
parent e581c72b02
commit 3f43074d05
35 changed files with 474 additions and 438 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/12/02 00:17:35 by rparodi ### ########.fr # # Updated: 2024/12/16 14:50:54 by bgoulard ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -44,16 +44,19 @@ MLXFLAGS += -L/opt/X11/lib
LDFLAGS += $(MLXFLAGS) LDFLAGS += $(MLXFLAGS)
SRC =\ SRC =\
ft_addons/ft_inrange.c \
mlx_layer/mlx_init.c \ mlx_layer/mlx_init.c \
mlx_layer/looks.c \ mlx_layer/looks.c \
mlx_layer/mlx_load_texture.c \
mlx_layer/mooves.c \ mlx_layer/mooves.c \
mlx_layer/hooks.c \ mlx_layer/hooks.c \
mlx_layer/keys_event.c \
raycast/frame_update.c \ raycast/frame_update.c \
raycast/frame_update2.c \ raycast/frame_update2.c \
raycast/rc_utils.c \ raycast/rc_utils.c \
raycast/utils_math.c \ raycast/utils_math.c \
raycast/get_cl.c \ raycast/get_cl.c \
raycast/draw_call.c \
raycast/get_texture.c \
sources/options_impl.c \ sources/options_impl.c \
sources/main.c \ sources/main.c \
sources/error.c \ sources/error.c \
@ -127,7 +130,6 @@ $(OBJDIRNAME)/%.o: %.c
# Header # Header
header: header:
@clear
@printf '\n\n' @printf '\n\n'
@printf '$(GOLD) ******* ****** ******* $(END)\n' @printf '$(GOLD) ******* ****** ******* $(END)\n'
@printf '$(GOLD) ****** *** ******* $(END)\n' @printf '$(GOLD) ****** *** ******* $(END)\n'

24
ft_addons/ft_inrange.c Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_inrange.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/16 06:04:32 by bgoulard #+# #+# */
/* Updated: 2024/12/16 09:01:36 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
// dest ft_math/ft_inrange.c
#include <stdbool.h>
bool ft_inrange_ex(int value, int min, int max)
{
return (value > min && value < max);
}
bool ft_inrange(int value, int min, int max)
{
return (value >= min && value <= max);
}

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/12/05 16:45:30 by rparodi ### ########.fr */ /* Updated: 2024/12/16 14:48:17 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,9 +22,15 @@
# include <stdbool.h> # include <stdbool.h>
// raycasting
void draw_bg(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);
t_texture *get_texture(int side, t_ipoint step, t_info *data);
void draw_(int *ti, double *td, t_ipoint step, t_info *data);
// utils // utils
void draw_floor(t_info *data); void draw_floor(t_info *data);
void dump_info(t_info *info);
int render_frame(t_info *data); int render_frame(t_info *data);
void my_mlx_pixel_put(t_info *data, int x, int y, int color); void my_mlx_pixel_put(t_info *data, int x, int y, int color);
double deg2rad(int deg); double deg2rad(int deg);
@ -38,6 +44,9 @@ void move_backward(t_info *data);
void move_left(t_info *data); void move_left(t_info *data);
void move_right(t_info *data); void move_right(t_info *data);
int keyrelease_feature(int keycode, t_info *data);
int keypress_feature(int keycode, t_info *data);
int displacement_hook(t_info *data);
int get_cl(int side, t_ipoint step); int get_cl(int side, t_ipoint step);
t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos); t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos);

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 18:01:48 by rparodi ### ########.fr */ /* Updated: 2024/12/16 14:15:43 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,7 @@
# define FILE_EXTENSION ".cub" # define FILE_EXTENSION ".cub"
# define FILE_EXTENSION_LEN 4 # define FILE_EXTENSION_LEN 4
# define BG_CLG 0 # define BG_SKY 0
# define BG_FLR 1 # define BG_FLR 1
# define TILE_SIZE 64 # define TILE_SIZE 64
# define WIN_COEF .5 # define WIN_COEF .5
@ -26,8 +26,8 @@
// defines that should be mooved to a config option / file // defines that should be mooved to a config option / file
# define FOV 70 # define FOV 70
# define ROT_SPEED 0.1 # define ROT_SPEED 0.004
# define MOVE_SPEED 0.5 # define MOVE_SPEED 0.008
// -- graphic utils // -- graphic utils
@ -132,12 +132,25 @@ typedef enum e_error
ERROR_MISSING_FILE, ERROR_MISSING_FILE,
ERROR_MALLOC, ERROR_MALLOC,
ERROR_PARSE, ERROR_PARSE,
ERROR_MAP_OPEN,
ERROR_PARSE_BG_COLOR_FORMAT,
ERROR_PARSE_ALREADY_SET,
ERROR_CLI, ERROR_CLI,
ERROR_MLX, ERROR_MLX,
ERROR_TEXTURE_FORMAT, ERROR_TEXTURE_FORMAT,
ERROR_IMPLEM, ERROR_IMPLEM,
} t_error; } t_error;
typedef struct s_kb
{
bool forward;
bool backward;
bool left;
bool right;
bool l_left;
bool l_right;
} t_keyboard;
typedef struct s_pixel_buffer typedef struct s_pixel_buffer
{ {
char *img_addr; char *img_addr;
@ -153,11 +166,13 @@ typedef struct s_info
{ {
t_error last_error; t_error last_error;
int errno_state; int errno_state;
t_keyboard kb;
t_xvar *mlx_ptr; t_xvar *mlx_ptr;
t_win_list *win_ptr; t_win_list *win_ptr;
t_ipoint screen_size; t_ipoint screen_size;
t_pixel_buffer camera; t_pixel_buffer camera;
bool redraw;
t_map map; t_map map;
t_player player; t_player player;

32
includes/ft_addons.h Normal file
View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_addons.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/16 06:09:01 by bgoulard #+# #+# */
/* Updated: 2024/12/16 09:36:19 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_ADDONS_H
# include <stdbool.h>
// ft_addons
/// @brief Says if a value is in a range excluding the bounds
/// @param value The value to check
/// @param min The lower bound
/// @param max The upper bound
/// @return true if the value is in the range, false otherwise
bool ft_inrange_ex(int value, int min, int max);
/// @brief Says if a value is in a range including the bounds
/// @param value The value to check
/// @param min The lower bound
/// @param max The upper bound
/// @return true if the value is in the range, false otherwise
bool ft_inrange(int value, int min, int max);
#endif

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:25:27 by bgoulard #+# #+# */ /* Created: 2023/11/07 23:25:27 by bgoulard #+# #+# */
/* Updated: 2024/08/21 21:42:42 by bgoulard ### ########.fr */ /* Updated: 2024/12/16 06:02:56 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -322,7 +322,7 @@ size_t ft_strclen(char *str, char c);
/// @param str String to search from /// @param str String to search from
/// @param c Char to search /// @param c Char to search
/// @return the number of occurance of char c in string str /// @return the number of occurance of char c in string str
size_t ft_strcnb(char *str, char c); size_t ft_strcnb(const char *str, char c);
/// @brief Calculate the length of the starting segment of str that contain /// @brief Calculate the length of the starting segment of str that contain
/// char from the accept string /// char from the accept string

View file

@ -6,13 +6,13 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/28 10:58:46 by bgoulard #+# #+# */ /* Created: 2023/12/28 10:58:46 by bgoulard #+# #+# */
/* Updated: 2024/05/18 20:04:31 by bgoulard ### ########.fr */ /* Updated: 2024/12/16 06:02:31 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stddef.h> #include <stddef.h>
size_t ft_strcnb(char *str, char c) size_t ft_strcnb(const char *str, char c)
{ {
size_t i; size_t i;
size_t nb; size_t nb;

View file

@ -1,23 +0,0 @@
SO ./textures/png_ugly.png
NO ./textures/png_ugly.png
WE ./textures/png_pretty.png
EA ./textures/png_pretty.png
F 255,255,255
C 255,,255
111
1N1
111

View file

@ -1,23 +0,0 @@
SO ./textures/png_ugly.png
NO ./textures/png_ugly.png
WE ./textures/png_pretty.png
EA ./textures/png_pretty.png
F 255,255,
C 255,255,255
111
1N1
111

View file

@ -1,24 +0,0 @@
SO ./textures/png_ugly.png
NO ./textures/png_ugly.png
WE ./textures/png_pretty.png
EA ./textures/png_pretty.png
F 255,255,255
C 255,255,255
111
1N11111
1000xy1
1111111

View file

@ -1,23 +0,0 @@
SO ./textures/png_ugly.png
NO ./textures/png_ugly.png
WE ./textures/png_pretty.png
EA ./textures/png_pretty.png
F 255,255,255
C 255,255,255
1111
00N1
1111

View file

@ -1,23 +0,0 @@
SO ./textures/png_ugly.png
NO = ./textures/png_ugly.png
WE ./textures/png_pretty.png
EA ./textures/png_pretty.png
F 255,255,255
C 255,255,255
111
1N1
111

20
maps/bad_open_map.cub Normal file
View file

@ -0,0 +1,20 @@
SO ./textures/b.xpm
NO ./textures/wasteland_32.xpm
WE ./textures/sandy_32.xpm
EA ./textures/cobblestone_32.xpm
F 0,255,0
C 0,123,255
11111111111111111111
10000011000001010101
10000011000000000001
10111111111111000101
1011 1000000
10111111 11000100
10000011111110000000
100000000000000N0000
11111111111111111111

View file

@ -0,0 +1,20 @@
SO ./textures/b.xpm
NO ./textures/wasteland_32.xpm
WE ./textures/sandy_32.xpm
EA ./textures/cobblestone_32.xpm
F 0,255,0
C 0,123,255
11111111111111111111
10000011000001010101
10000011000000000001
10111111101111000101
1011 1000000
10111111 11000100
10000011111110000000
100000000000000N0000
11111111111111111111

View file

@ -1,23 +0,0 @@
SO ./textures/png_ugly.png
NO ./textures/png_ugly.png
WE ./textures/png_pretty.png
EA ./textures/png_pretty.png
F 255,255,255
C 255,255,255
111
1N1
111

View file

@ -1,10 +1,10 @@
SO ./textures/a.xpm SO ./textures/b.xpm
NO ./textures/a.xpm NO ./textures/wasteland_32.xpm
WE ./textures/a.xpm WE ./textures/sandy_32.xpm
EA ./textures/a.xpm EA ./textures/cobblestone_32.xpm
F 255,255,255 F 0,255,0
C 255,255,255 C 0,255,255
@ -19,7 +19,7 @@ C 255,255,255
11111111111111111111111 11111111111111111111111
100000000N0000000000001 100000000S0000000000001
10000000000000000000001 10000000000000000000001
10000000000000000000001 10000000000000000000001
11111011101100111110011 11111011101100111110011

20
maps/valid_02.cub Normal file
View file

@ -0,0 +1,20 @@
SO ./textures/b.xpm
NO ./textures/wasteland_32.xpm
WE ./textures/sandy_32.xpm
EA ./textures/cobblestone_32.xpm
F 0,255,0
C 0,123,255
11111111111111111111
10000011000001010101
10000011000000000001
10111111111111000101
1011 1000001
10111111 11000101
10000011111110000001
100000000000000N0001
11111111111111111111

View file

@ -6,44 +6,38 @@
/* 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 18:46:32 by rparodi ### ########.fr */ /* Updated: 2024/12/16 14:16:17 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d.h" #include "cub3d.h"
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "mlx_functions.h"
#include "ft_char.h"
#include <X11/keysym.h> #include <X11/keysym.h>
#include <stdio.h> #include <sys/types.h>
int key_hook(int keycode, t_info *data) static void update_pos_i(t_player *player)
{ {
if (ft_isalpha(keycode)) player->pos_i.x = (int)player->pos.x;
printf("Event detected: %d\t(%c)\n", keycode, keycode); player->pos_i.y = (int)player->pos.y;
else if (keycode == 65361) }
printf("Event detected: %d\t(Left Arrow)\n", keycode);
else if (keycode == 65363) int displacement_hook(t_info *data)
printf("Event detected: %d\t(Right Arrow)\n", keycode); {
else if (keycode == 65307) data->redraw = true;
printf("Event detected: %d\t(Echap)\n", keycode); update_pos_i(&data->player);
else if (data->kb.forward && !data->kb.backward)
printf("Event detected: %d\n", keycode);
if (keycode == XK_Escape)
mlx_loop_end(data->mlx_ptr);
if (keycode == XK_w)
move_straight(data); move_straight(data);
if (keycode == XK_s) if (data->kb.backward && !data->kb.forward)
move_backward(data); move_backward(data);
if (keycode == XK_a) if (data->kb.left && !data->kb.right)
move_left(data); move_left(data);
if (keycode == XK_d) if (data->kb.right && !data->kb.left)
move_right(data); move_right(data);
if (keycode == XK_Left) if (data->kb.l_left && !data->kb.l_right)
look_left(data); look_left(data);
if (keycode == XK_Right) if (data->kb.l_right && !data->kb.l_left)
look_right(data); look_right(data);
update_pos_i(&data->player);
return (0); return (0);
} }

63
mlx_layer/keys_event.c Normal file
View file

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* keys_event.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/16 14:40:02 by bgoulard #+# #+# */
/* Updated: 2024/12/16 14:41:40 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d_struct.h"
#include "mlx_functions.h"
#include <stdbool.h>
#include <X11/keysym.h>
void set_kb(t_keyboard *kb, int keycode)
{
if (keycode == XK_w)
kb->forward = true;
if (keycode == XK_s)
kb->backward = true;
if (keycode == XK_a)
kb->left = true;
if (keycode == XK_d)
kb->right = true;
if (keycode == XK_Right)
kb->l_right = true;
if (keycode == XK_Left)
kb->l_left = true;
}
void unset_kb(t_keyboard *kb, int keycode)
{
if (keycode == XK_w)
kb->forward = false;
if (keycode == XK_s)
kb->backward = false;
if (keycode == XK_a)
kb->left = false;
if (keycode == XK_d)
kb->right = false;
if (keycode == XK_Left)
kb->l_left = false;
if (keycode == XK_Right)
kb->l_right = false;
}
int keypress_feature(int keycode, t_info *data)
{
if (keycode == XK_Escape)
mlx_loop_end(data->mlx_ptr);
set_kb(&data->kb, keycode);
return (0);
}
int keyrelease_feature(int keycode, t_info *data)
{
unset_kb(&data->kb, keycode);
return (0);
}

View file

@ -6,25 +6,22 @@
/* 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/05 16:55:30 by rparodi ### ########.fr */ /* Updated: 2024/12/16 14:40:55 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d.h" #include "cub3d.h"
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "mlx_functions.h" #include "mlx_functions.h"
#include "mlx_structs.h" #include "mlx_structs.h"
#include "ft_string.h"
#include "ft_math.h" #include "ft_math.h"
#include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <X11/keysym.h> #include <X11/keysym.h>
int c3_frame_update(void *inf_ptr);
int key_hook(int keycode, t_info *data);
/* 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)
{ {
if (keycode == XK_Escape || keycode == 65307) if (keycode == XK_Escape || keycode == 65307)
@ -41,8 +38,8 @@ t_win_list *c3_init_mlx_window(t_info *info)
{ {
mlx_get_screen_size(info->mlx_ptr, \ mlx_get_screen_size(info->mlx_ptr, \
&info->screen_size.x, &info->screen_size.y); &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);
ft_clamp(info->screen_size.y, 0, 1080); ft_clamp(info->screen_size.y, 0, 1080);
return (\ return (\
@ -58,7 +55,9 @@ int init_mlx_env(t_info *info)
info->win_ptr = c3_init_mlx_window(info); info->win_ptr = c3_init_mlx_window(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, keypress_feature, info);
mlx_hook(info->win_ptr, KeyRelease, KeyReleaseMask, keyrelease_feature, \
info);
mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, \ mlx_hook(info->win_ptr, DestroyNotify, StructureNotifyMask, \
c3_redcross, info); c3_redcross, info);
mlx_loop_hook(info->mlx_ptr, (int (*)())shelves_launch, info); mlx_loop_hook(info->mlx_ptr, (int (*)())shelves_launch, info);

View file

@ -1,45 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mlx_load_texture.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/20 12:25:45 by rparodi #+# #+# */
/* Updated: 2024/12/05 17:01:12 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
#include "mlx_functions.h"
t_img *mlx_load_image(t_info *info, char *path)
{
t_img *image;
int size;
size = TILES_SIZE;
image = mlx_xpm_file_to_image(info->mlx_ptr, path, &size, &size);
if (!image)
return (NULL);
return (image);
}
bool mlx_load_all_textures(t_info *info)
{
size_t i;
i = 0;
info->map.texture[0] = mlx_load_image(info, "../textures/wasteland_32.xpm");
info->map.texture[1] = mlx_load_image(info, "../textures/b.xpm");
info->map.texture[2] = mlx_load_image(info, "../textures/sandy_32.xpm");
info->map.texture[3] = mlx_load_image(info, "../textures/cobblestone_32.xpm");
while (i < 4)
{
if (!info->map.texture[i])
return (false);
i++;
}
return (true);
}

View file

@ -6,7 +6,7 @@
/* 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 18:36:26 by rparodi ### ########.fr */ /* Updated: 2024/12/16 08:36:17 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,12 +14,6 @@
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "cub3d_parsing.h" #include "cub3d_parsing.h"
void update_pos_i(t_player *player)
{
player->pos_i.x = (int)player->pos.x;
player->pos_i.y = (int)player->pos.y;
}
void move_straight(t_info *data) void move_straight(t_info *data)
{ {
t_tile *tile_x; t_tile *tile_x;
@ -35,7 +29,6 @@ void move_straight(t_info *data)
+ data->player.dir.y * MOVE_SPEED)}); + 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);
} }
void move_backward(t_info *data) void move_backward(t_info *data)
@ -53,7 +46,6 @@ void move_backward(t_info *data)
- data->player.dir.y * MOVE_SPEED)}); - 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);
} }
void move_left(t_info *data) void move_left(t_info *data)
@ -74,7 +66,6 @@ void move_left(t_info *data)
pplayer.y * MOVE_SPEED)}); 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);
} }
void move_right(t_info *data) void move_right(t_info *data)
@ -95,5 +86,4 @@ void move_right(t_info *data)
* MOVE_SPEED)}); * 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);
} }

View file

@ -6,63 +6,66 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 17:47:37 by bgoulard #+# #+# */ /* Created: 2024/12/01 17:47:37 by bgoulard #+# #+# */
/* Updated: 2024/12/01 17:52:28 by bgoulard ### ########.fr */ /* Updated: 2024/12/16 09:36:28 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_addons.h"
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "cub3d_parsing.h" #include "cub3d_parsing.h"
#include "ft_string.h" #include "ft_string.h"
#include <stdbool.h>
#include <stdio.h>
// col [ 3 ] is alpha channel (not used in the project)
bool color_from_str(const char *str, t_color *color) bool color_from_str(const char *str, t_color *color)
{ {
int col[4]; int col[4];
char **split; const char *arr[3];
split = ft_split(str, ','); if (ft_strcnb(str, ',') != 2)
if (split == NULL)
return (false); return (false);
col[0] = ft_atoi(split[0]); arr[0] = ft_strtok((char *)str, ",");
col[1] = ft_atoi(split[1]); arr[1] = ft_strtok(NULL, ",");
col[2] = ft_atoi(split[2]); arr[2] = ft_strtok(NULL, ",");
if (split[3]) if (!arr[0] || !arr[1] || !arr[2] || !ft_str_isnum(arr[0]) || \
col[3] = ft_atoi(split[3]); !ft_str_isnum(arr[1]) || !ft_str_isnum(arr[2]))
else return (false);
col[3] = 255; col[0] = ft_atoi(arr[0]);
if (col[0] < 0 || col[0] > 255 || col[1] < 0 || col[1] > 255 || col[2] < 0 col[1] = ft_atoi(arr[1]);
|| col[2] > 255 || col[3] < 0 || col[3] > 255) col[2] = ft_atoi(arr[2]);
col[3] = 255;
if (ft_inrange(col[0], 0, 255) == false || \
ft_inrange(col[1], 0, 255) == false || \
ft_inrange(col[2], 0, 255) == false)
return (false); return (false);
*color = (t_color){.r = col[0], .g = col[1], .b = col[2], .a = col[3]}; *color = (t_color){.r = col[0], .g = col[1], .b = col[2], .a = col[3]};
ft_free_2d((void **)split);
return (true); return (true);
} }
bool load_bg(t_info *info, const char *line, const char **id_str) bool load_bg(t_info *info, const char *line, const char **id_str)
{ {
t_color color; t_color color;
size_t i; size_t id_idx;
i = 0; id_idx = 0;
ft_bzero(&color, sizeof(t_color)); ft_bzero(&color, sizeof(t_color));
while (id_str[i]) while (id_str[id_idx] && ft_strstart_with(line, id_str[id_idx]) == false)
{ id_idx++;
if (ft_strstart_with(line, id_str[i]) && (color_from_str(line if (color_from_str(line + ft_strlen(id_str[id_idx]), &color) == false)
+ ft_strlen(id_str[i]), &color) == false || color.a < 255)) return (info->last_error = ERROR_PARSE_BG_COLOR_FORMAT, false);
return (info->last_error = ERROR_PARSE, false); if (id_idx == BG_FLR && info->map.bg_colors[BG_FLR].color == 0)
if (ft_strstart_with(id_str[i], "F ")) info->map.bg_colors[BG_FLR] = color;
info->map.bg_colors[BG_FLR] = color; else if (id_idx == BG_SKY && info->map.bg_colors[BG_SKY].color == 0)
else info->map.bg_colors[BG_SKY] = color;
info->map.bg_colors[BG_CLG] = color; else
i++; return (info->last_error = ERROR_PARSE_ALREADY_SET, false);
}
if (color.color == 0)
return (info->last_error = ERROR_PARSE, false);
return (true); return (true);
} }
void *load_bgs(void *data) void *load_bgs(void *data)
{ {
const char *id_str[] = {"F ", "C ", NULL}; const char *id_str[] = {[BG_FLR] = "F ", [BG_SKY] = "C ", [2] = NULL};
t_info *info; t_info *info;
size_t i; size_t i;
@ -70,8 +73,8 @@ void *load_bgs(void *data)
info = (t_info *)data; info = (t_info *)data;
while (info->map.fraw[i]) while (info->map.fraw[i])
{ {
if (is_identifier(info->map.fraw[i], id_str) && load_bg(info, if (is_identifier(info->map.fraw[i], id_str) && \
info->map.fraw[i], id_str) == false) load_bg(info, info->map.fraw[i], id_str) == false)
return (NULL); return (NULL);
i++; i++;
} }

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 17:46:52 by bgoulard #+# #+# */ /* Created: 2024/12/01 17:46:52 by bgoulard #+# #+# */
/* Updated: 2024/12/01 17:53:44 by bgoulard ### ########.fr */ /* Updated: 2024/12/16 14:25:52 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,13 @@
#include "mlx_functions.h" #include "mlx_functions.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h>
static void sv_errno(t_info *inf, int err_code)
{
inf->errno_state = errno;
inf->last_error = err_code;
}
bool load_texture(t_info *info, const char *str, const char **id_str) bool load_texture(t_info *info, const char *str, const char **id_str)
{ {
@ -28,17 +35,17 @@ bool load_texture(t_info *info, const char *str, const char **id_str)
{ {
if (ft_strstart_with(str, id_str[i])) if (ft_strstart_with(str, id_str[i]))
{ {
texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " "); if (info->map.texture_[i].img != NULL)
return (sv_errno(info, ERROR_PARSE_ALREADY_SET), false);
texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " \t");
if (texture.path == NULL) if (texture.path == NULL)
return (info->errno_state = errno, return (sv_errno(info, ERROR_MALLOC), false);
info->last_error = ERROR_MALLOC, false);
if (ft_strend_with(texture.path, ".xpm") == false) if (ft_strend_with(texture.path, ".xpm") == false)
return (info->last_error = ERROR_TEXTURE_FORMAT, false); return (sv_errno(info, ERROR_TEXTURE_FORMAT), false);
texture.img = mlx_xpm_file_to_image(info->mlx_ptr, texture.path, texture.img = mlx_xpm_file_to_image(info->mlx_ptr, texture.path,
&texture.width, &texture.height); &texture.width, &texture.height);
if (texture.img == NULL) if (texture.img == NULL)
return (info->errno_state = errno, info->last_error = ERROR_MLX, return (sv_errno(info, ERROR_MLX), false);
false);
return (info->map.texture_[i] = texture, true); return (info->map.texture_[i] = texture, true);
} }
i++; i++;

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 17:47:15 by bgoulard #+# #+# */ /* Created: 2024/12/01 17:47:15 by bgoulard #+# #+# */
/* Updated: 2024/12/01 17:53:53 by bgoulard ### ########.fr */ /* Updated: 2024/12/16 09:33:53 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,8 @@
#include "ft_vector.h" #include "ft_vector.h"
#include "ft_math.h" #include "ft_math.h"
#include <stdio.h>
void *ft_strchrs(const char *str, const char *chrs) void *ft_strchrs(const char *str, const char *chrs)
{ {
while (*str) while (*str)
@ -68,16 +70,28 @@ t_vector *load_vector(t_map *map)
int set_player(t_info *info, int i, t_vector *str_map) int set_player(t_info *info, int i, t_vector *str_map)
{ {
t_dpoint pos; t_dpoint pos;
char *str;
ft_bzero(&pos, sizeof(t_dpoint)); ft_bzero(&pos, sizeof(t_dpoint));
if (info->player.pos.x != 0 || info->player.pos.y != 0 || i == 0) if (info->player.pos.x != 0 || info->player.pos.y != 0 || i == 0)
return (ft_vec_destroy(&str_map), info->last_error = ERROR_PARSE, \ return (ft_vec_destroy(&str_map), info->last_error = ERROR_PARSE, \
EXIT_FAILURE); EXIT_FAILURE);
str = ft_strchrs(ft_vec_at(str_map, i), "SNWE");
pos.y = i + .5; pos.y = i + .5;
pos.x = ft_strchrs(ft_vec_at(str_map, i), "SNWE") pos.x = str - (char *)ft_vec_at(str_map, i) + .5;
- ft_vec_at(str_map, i) + .5; info->player.dir = (t_dpoint){.x = 0, 0};
info->player.pos = pos; info->player.pos = pos;
info->player.pos_i = (t_ipoint){.x = (int)pos.x, .y = (int)pos.y}; info->player.pos_i = (t_ipoint){.x = (int)pos.x, .y = (int)pos.y};
if (*str == 'N')
info->player.dir.y = -1;
else if (*str == 'S')
info->player.dir.y = 1;
else if (*str == 'W')
info->player.dir.x = -1;
else if (*str == 'E')
info->player.dir.x = 1;
info->player.plane = (t_dpoint){.x = info->player.dir.y, \
.y = -info->player.dir.x};
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */ /* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */
/* Updated: 2024/12/01 17:51:13 by bgoulard ### ########.fr */ /* Updated: 2024/12/16 08:49:53 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,8 +37,8 @@ void parse_map(t_info *info)
opt = (t_optional){.val = info, .pres = OPT_SOME}; opt = (t_optional){.val = info, .pres = OPT_SOME};
info->map.path = info->cli_ctx.file; info->map.path = info->cli_ctx.file;
if (ft_optional_chain(&opt, function_list) == false)
return (c3_perror(info), (void)0);
info->player.plane = (t_dpoint){.x = 0, .y = 2 * atan(deg2rad(FOV / 2))}; info->player.plane = (t_dpoint){.x = 0, .y = 2 * atan(deg2rad(FOV / 2))};
info->player.dir = (t_dpoint){.x = -1, .y = 0}; info->player.dir = (t_dpoint){.x = -1, .y = 0};
if (ft_optional_chain(&opt, function_list) == false)
return (c3_perror(info), (void)0);
} }

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 17:49:12 by bgoulard #+# #+# */ /* Created: 2024/12/01 17:49:12 by bgoulard #+# #+# */
/* Updated: 2024/12/01 17:54:03 by bgoulard ### ########.fr */ /* Updated: 2024/12/16 09:45:25 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -49,6 +49,6 @@ void *traverse_map(void *data)
info = (t_info *)data; info = (t_info *)data;
pos_start = (t_ipoint){.x = info->player.pos.x, .y = info->player.pos.y}; pos_start = (t_ipoint){.x = info->player.pos.x, .y = info->player.pos.y};
if (flood_fill(info->map.map, pos_start, info->map.size) == false) if (flood_fill(info->map.map, pos_start, info->map.size) == false)
return (info->last_error = ERROR_PARSE, NULL); return (info->last_error = ERROR_MAP_OPEN, NULL);
return (info); return (info);
} }

View file

@ -6,7 +6,7 @@
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */ /* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 17:43:17 by bgoulard #+# #+# */ /* Created: 2024/12/01 17:43:17 by bgoulard #+# #+# */
/* Updated: 2024/12/01 17:45:58 by bgoulard ### ########.fr */ /* Updated: 2024/12/16 09:39:50 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,9 +21,7 @@ t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos)
{ {
if (pos.x < 0 || pos.y < 0 || pos.x >= dimensions.x if (pos.x < 0 || pos.y < 0 || pos.x >= dimensions.x
|| pos.y >= dimensions.y) || pos.y >= dimensions.y)
return (printf("runtime error: %s:%d (pos:%d,%d on dims:%d,%d)\n", return (NULL);
__func__, __LINE__, pos.x, pos.y, dimensions.x, dimensions.y),
NULL);
return (map + (pos.y * dimensions.x + pos.x)); return (map + (pos.y * dimensions.x + pos.x));
} }

70
raycast/draw_call.c Normal file
View file

@ -0,0 +1,70 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* draw_call.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/16 14:43:09 by bgoulard #+# #+# */
/* Updated: 2024/12/16 14:47:56 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d_struct.h"
#include "cub3d.h"
#include "ft_math.h"
#include <math.h>
#define PT_LINEH 0
#define PT_D_START 1
#define PT_D_END 2
#define PT_X 3
#define TI_SIDE 0
#define TI_X 1
#define TD_PERP_WALL_DIST 0
#define TD_WALL_X 1
static void draw__(t_texture *texture, int *passthroug, t_info *data, \
double wallX)
{
t_ipoint text_off;
const double _step = (double)texture->height / passthroug[PT_LINEH];
double text_offset;
char *ptr_pix;
text_offset = (passthroug[PT_D_START] - (double)data->screen_size.y / 2 + \
(double)passthroug[PT_LINEH] / 2) * _step;
text_off.x = (wallX * texture->width);
while (passthroug[PT_D_START] < passthroug[PT_D_END])
{
text_off.y = (int)text_offset & (texture->height - 1);
text_offset += _step;
ptr_pix = texture->img->data + (text_off.y * \
texture->img->image->bytes_per_line + text_off.x * \
(texture->img->image->bits_per_pixel / 8));
my_mlx_pixel_put(data, passthroug[PT_X], passthroug[PT_D_START], \
*(unsigned int *)ptr_pix);
passthroug[PT_D_START]++;
}
}
// int texX = (int)(wallX * (double)texture->width);
void draw_(int *ti, double *td, t_ipoint step, t_info *data)
{
const int line_height = (int)(data->screen_size.y / \
td[TD_PERP_WALL_DIST]) * cos(deg2rad(FOV / 2));
int draw_start;
int draw_end;
t_texture *texture;
draw_start = ft_clamp(-line_height / 2 + data->screen_size.y / 2, 0, \
data->screen_size.y - 1);
draw_end = ft_clamp(line_height / 2 + data->screen_size.y / 2, 0, \
data->screen_size.y - 1);
texture = get_texture(ti[TI_SIDE], step, data);
draw__(texture, (int []){line_height, draw_start, draw_end, ti[TI_X]}, \
data, td[TD_WALL_X]);
}

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/05 16:44:04 by rparodi ### ########.fr */ /* Updated: 2024/12/16 14:51:46 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,51 +15,31 @@
#include "mlx_functions.h" #include "mlx_functions.h"
#include "ft_string.h" #include "ft_string.h"
#include "ft_math.h"
#include <math.h> #include <math.h>
#include <unistd.h> #include <unistd.h>
void set_step(t_ipoint *step, t_dpoint raydir); void search_hit(t_dpoint delta_dist, t_ipoint *pos_i, t_ipoint step, \
void set_side_dist(t_dpoint *side_dist, t_dpoint *tb, t_ipoint pos_i); void *_data[3])
/// line 33: normalize draw_start and draw_end
void draw_(int side, double perpWallDist, t_ipoint step, int x, t_info *data)
{ {
const int line_height = (int)(data->screen_size.y / perpWallDist) \ int *side;
* cos(deg2rad(FOV / 2)); t_info *data;
int draw_start; t_dpoint *side_dist;
int draw_end;
int color;
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 (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])
{
int *side;
t_info *data;
side = (int *)_data[1]; side = (int *)_data[1];
data = (t_info *)_data[0]; data = (t_info *)_data[0];
side_dist = (t_dpoint *)_data[2];
while (true) while (true)
{ {
if (sideDist->x < sideDist->y) if (side_dist->x < side_dist->y)
{ {
sideDist->x += deltaDist.x; side_dist->x += delta_dist.x;
pos_i->x += step.x; pos_i->x += step.x;
*side = 0; *side = 0;
} }
else else
{ {
sideDist->y += deltaDist.y; side_dist->y += delta_dist.y;
pos_i->y += step.y; pos_i->y += step.y;
*side = 1; *side = 1;
} }
@ -69,11 +49,14 @@ void search_hit(t_dpoint *sideDist, t_dpoint deltaDist, t_ipoint *pos_i, \
} }
} }
// t_dpoint prep_distoff ->
// x - perpwall_dist
// y - wall_off
void column_handler(t_ipoint pos_i, t_dpoint ray_dir, t_info *data, int x) void column_handler(t_ipoint pos_i, t_dpoint ray_dir, t_info *data, int x)
{ {
t_dpoint side_dist; t_dpoint side_dist;
t_dpoint delta_dist; t_dpoint delta_dist;
double perp_wall_dist; t_dpoint perp_distoff;
t_ipoint step; t_ipoint step;
int side; int side;
@ -81,17 +64,23 @@ void column_handler(t_ipoint pos_i, t_dpoint ray_dir, t_info *data, int x)
set_step(&step, ray_dir); set_step(&step, ray_dir);
set_side_dist(&side_dist, (t_dpoint[]){ray_dir, data->player.pos, \ set_side_dist(&side_dist, (t_dpoint[]){ray_dir, data->player.pos, \
delta_dist}, pos_i); delta_dist}, pos_i);
search_hit(&side_dist, delta_dist, &pos_i, step, (void *[]){data, &side}); search_hit(delta_dist, &pos_i, step, (void *[]){data, &side, &side_dist});
if (side == 0) if (side == 0)
perp_wall_dist = (pos_i.x - data->player.pos.x + \ perp_distoff.x = (pos_i.x - data->player.pos.x + \
(double)(1 - step.x) / 2) / ray_dir.x; (double)(1 - step.x) / 2) / ray_dir.x;
else else
perp_wall_dist = (pos_i.y - data->player.pos.y + \ perp_distoff.x = (pos_i.y - data->player.pos.y + \
(double)(1 - step.y) / 2) / ray_dir.y; (double)(1 - step.y) / 2) / ray_dir.y;
draw_(side, perp_wall_dist, step, x, data); perp_distoff.y = data->player.pos.x + perp_distoff.x * ray_dir.x;
if (side == 0)
perp_distoff.y = data->player.pos.y + perp_distoff.x * ray_dir.y;
perp_distoff.y -= floor(perp_distoff.y);
draw_((int []){side, x}, (double []){perp_distoff.x, perp_distoff.y}, \
step, data);
} }
int render_frame(t_info *data) int render_frame(t_info *data)
{ {
double camera_x; double camera_x;
double coef; double coef;
@ -101,7 +90,7 @@ int render_frame(t_info *data)
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 \ ft_bzero(data->camera.img_addr, data->screen_size.x * data->screen_size.y \
* (data->camera.bpp / 8)); * (data->camera.bpp / 8));
draw_floor(data); draw_bg(data);
while (x < data->screen_size.x) while (x < data->screen_size.x)
{ {
camera_x = x * coef - 1; camera_x = x * coef - 1;
@ -112,28 +101,6 @@ int render_frame(t_info *data)
x++; x++;
} }
mlx_put_image_to_window(data->mlx_ptr, data->win_ptr, \ mlx_put_image_to_window(data->mlx_ptr, data->win_ptr, \
data->camera.screen_buff, 0, 0); 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 of the function...
// (Why 'int (*)(void*)' when you dont use the int)
//
// This function is called each time the mlx loops over the mlx pointer in
// mlx_loop. here we do calc on time since last frame and player position
// to know if we need to re-draw the screen. if yes call raph function for
// calc.
// The need to redraw can also be expressed in the diferent key_pressed
// functions, I would recomend to make a bool field for that in the info
// 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)
{
t_info *info;
info = inf_ptr;
mlx_clear_window(info->mlx_ptr, info->win_ptr);
return (EXIT_SUCCESS);
}

View file

@ -6,34 +6,36 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/01 18:55:11 by rparodi #+# #+# */ /* Created: 2024/12/01 18:55:11 by rparodi #+# #+# */
/* Updated: 2024/12/05 16:43:53 by rparodi ### ########.fr */ /* Updated: 2024/12/16 09:43:18 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d.h" #include "cub3d.h"
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "mlx_functions.h"
#include "ft_string.h" #include "ft_string.h"
#include "ft_math.h"
#include <math.h>
#include <unistd.h> #include <unistd.h>
void draw_floor(t_info *data) void draw_bg(t_info *data)
{ {
t_ipoint temp; t_ipoint temp;
int switch_point;
int color;
temp.y = data->screen_size.y / 2; ft_bzero(&temp, sizeof(t_ipoint));
switch_point = data->screen_size.y / 2;
color = data->map.bg_colors[BG_SKY].color;
while (temp.y < data->screen_size.y) while (temp.y < data->screen_size.y)
{ {
temp.x = 0; temp.x = 0;
while (temp.x < data->screen_size.x) while (temp.x < data->screen_size.x)
{ {
my_mlx_pixel_put(data, temp.x, temp.y, 0xFFFFFF); my_mlx_pixel_put(data, temp.x, temp.y, color);
temp.x++; temp.x++;
} }
temp.y++; temp.y++;
if (temp.y >= switch_point)
color = data->map.bg_colors[BG_FLR].color;
} }
} }

25
raycast/get_texture.c Normal file
View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_texture.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/16 14:46:04 by bgoulard #+# #+# */
/* Updated: 2024/12/16 14:46:14 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d_struct.h"
t_texture *get_texture(int side, t_ipoint step, t_info *data)
{
if (side == 0 && step.x > 0)
return (&data->map.texture_[0]);
else if (side == 0 && step.x < 0)
return (&data->map.texture_[1]);
else if (side == 1 && step.y > 0)
return (&data->map.texture_[2]);
else
return (&data->map.texture_[3]);
}

View file

@ -6,13 +6,13 @@
/* 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/12/01 18:39:11 by rparodi ### ########.fr */ /* Updated: 2024/12/16 14:08:46 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cub3d.h" #include "cub3d.h"
#include "cub3d_struct.h" #include "cub3d_struct.h"
#include "mlx_functions.h"
#include "raycast.h" #include "raycast.h"
#include <stdlib.h> #include <stdlib.h>
@ -55,6 +55,8 @@ double rc_launch(t_info *info, double angle)
int shelves_launch(t_info *info) int shelves_launch(t_info *info)
{ {
render_frame(info); displacement_hook(info);
if (info->redraw)
(render_frame(info), info->redraw = false);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 11:09:00 by rparodi #+# #+# */ /* Created: 2024/10/31 11:09:00 by rparodi #+# #+# */
/* Updated: 2024/11/27 12:00:34 by bgoulard ### ########.fr */ /* Updated: 2024/12/16 14:37:11 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,7 @@
#include <unistd.h> #include <unistd.h>
const char *const g_error_message[] = { static const char *g_error_message[] = {
"no error", "no error",
"unknown error", "unknown error",
"could not open file", "could not open file",
@ -27,6 +27,9 @@ const char *const g_error_message[] = {
"missing file", "missing file",
"malloc error", "malloc error",
"parse error", "parse error",
"map open error",
"bad format for background color",
"variable was set multiple times",
"cli error", "cli error",
"mlx error", "mlx error",
"texture format error", "texture format error",

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 18:53:12 by rparodi ### ########.fr */ /* Updated: 2024/12/16 09:38:44 by bgoulard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,69 +19,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <unistd.h>
void dump_map(t_tile *map, t_ipoint size)
{
int i;
int j;
i = 0;
while (i < size.y)
{
j = 0;
printf("\t\t\t");
while (j < size.x)
{
printf("%d", map[i * size.x + j].tile_type);
j++;
}
printf("\n");
i++;
}
}
// not normed but we'll take care of this as a niceties at the last
// possible moment :)
void dump_info(t_info *info)
{
const char *bool_str[2] = {"False", "True"};
size_t i;
i = 0;
printf("t_info:\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:\t(x:%lf, y:%lf)\n", \
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("\t\tfile:\t%s\n", info->cli_ctx.file);
printf("\t\tdebug:\t%s\n", bool_str[info->cli_ctx.debug]);
printf("\t\tsave:\t%s\n", bool_str[info->cli_ctx.save]);
printf("\t\thelp:\t%s\n", bool_str[info->cli_ctx.help]);
printf("\tmap:\n");
printf("\t\tpath:%s\n", info->map.path);
printf("\t\tfd:\t%d\n", info->map.fd);
printf("\t\tsize:\t(x:%d, y:%d)\n", info->map.size.x, info->map.size.y);
while (info->map.fraw[i])
{
printf("\t\tmap.fraw[%*zu]: %s\n", 3, i, info->map.fraw[i]);
i++;
}
printf("\t\ttexture[0]: %p\n", info->map.texture[0]);
printf("\t\ttexture[1]: %p\n", info->map.texture[1]);
printf("\t\ttexture[2]: %p\n", info->map.texture[2]);
printf("\t\ttexture[3]: %p\n", info->map.texture[3]);
printf("\t\tmap: %p\n", info->map.map);
dump_map(info->map.map, info->map.size);
printf("\tlast_error: %d\n", info->last_error);
printf("\terno_state: %d\n", info->errno_state);
printf("\n");
}
void check_err(t_info *info) void check_err(t_info *info)
{ {
@ -103,13 +41,16 @@ void run_cub3d(t_info *info)
return ; return ;
parse_map(info); parse_map(info);
if (info->cli_ctx.debug) if (info->cli_ctx.debug)
dump_info(info); ft_putstr_fd("no debug mod on production run", STDERR_FILENO);
if (info->last_error != NO_ERROR) if (info->last_error != NO_ERROR)
return ; return ;
if (info->cli_ctx.no_graphics == true)
return ;
info->camera.screen_buff = mlx_new_image(info->mlx_ptr, info->screen_size.x, info->camera.screen_buff = mlx_new_image(info->mlx_ptr, info->screen_size.x,
info->screen_size.y); info->screen_size.y);
info->camera.img_addr = mlx_get_data_addr(info->camera.screen_buff, info->camera.img_addr = mlx_get_data_addr(info->camera.screen_buff,
&info->camera.bpp, &info->camera.line_len, &info->camera.endian); &info->camera.bpp, &info->camera.line_len, &info->camera.endian);
info->redraw = true;
mlx_loop(info->mlx_ptr); mlx_loop(info->mlx_ptr);
} }