versiontexturecouleur

This commit is contained in:
Ladebeze66 2024-01-23 17:39:25 +01:00
parent 84c17810af
commit ffca051a01
898 changed files with 381 additions and 124265 deletions

View File

@ -6,40 +6,59 @@
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/14 16:54:48 by fgras-ca #+# #+# #
# Updated: 2024/01/14 21:30:03 by fgras-ca ### ########.fr #
# Updated: 2024/01/23 17:36:22 by fgras-ca ### ########.fr #
# #
# **************************************************************************** #
NAME = cub3d
LOGO = @echo "🅹🅾🅷🅽 🅰🅽🅳 🅻🅰🅳🅴🅱🅴🆉🅴"
SRC = main.c \
./utils/ft_utils_gnl.c \
./utils/ft_utils_split.c \
./utils/ft_utils_str_1.c \
./utils/ft_utils_convert.c \
./parsing/ft_map_check.c \
./parsing/ft_read_map.c \
./parsing/ft_map_dimensions.c \
./parsing/ft_parsing.c \
./move/ft_collision.c \
./move/ft_key.c \
./move/ft_move.c \
./draw/ft_draw.c \
./draw/ft_background.c \
./draw/ft_2d_view.c \
./draw/ft_textures.c \
./draw/ft_3d_view.c \
./draw/ft_horizontal_ray.c \
./draw/ft_vertical_ray.c \
./draw/ft_pixel.c \
./draw/ft_ray_utils.c \
./window/ft_window.c \
./window/ft_player.c \
./window/ft_map.c \
./draw/ft_load_textures.c \
./window/ft_rescale.c \
RST = \033[0m
GRAY = \033[0;90m
RED = \033[0;91m
GREEN = \033[0;92m
YELLOW = \033[0;93m
BLUE = \033[0;94m
MAGENTA = \033[0;95m
CYAN = \033[0;96m
WHITE = \033[0;97m
ORANGE = \033[38;5;214m
NAME = cub3d
LIBFT = libft.a
SRC = main.c \
./utils/ft_utils_gnl.c \
./utils/ft_utils_split.c \
./utils/ft_utils_convert.c \
./utils/ft_utils_str_1.c \
./parsing/ft_map_check.c \
./parsing/ft_read_map.c \
./parsing/ft_map_dimensions.c \
./parsing/ft_parsing.c \
./move/ft_collision.c \
./move/ft_key.c \
./move/ft_move.c \
./draw/ft_draw.c \
./draw/ft_background.c \
./draw/ft_2d_view.c \
./draw/ft_textures.c \
./draw/ft_3d_view.c \
./draw/ft_horizontal_ray.c \
./draw/ft_vertical_ray.c \
./draw/ft_pixel.c \
./draw/ft_ray_utils.c \
./window/ft_window.c \
./window/ft_player.c \
./window/ft_map.c \
./draw/ft_load_textures.c \
./window/ft_rescale.c \
./window/ft_refresh_window.c \
./parsing/ft_find_map_start.c \
./parsing/ft_textures_and_colors.c \
SRC_DIR_LIBFT = libft/
SRC_LIBFT = $(addprefix $(SRC_DIR_LIBFT), $(LIBFT))
OBJ = $(SRC:.c=.o)
@ -47,17 +66,25 @@ FLAG = cc -Wall -Wextra -Werror -g
RM = rm -f
$(NAME): $(OBJ)
$(FLAG) -Iinclude $(OBJ) -Llib -lmlx -lXext -lX11 -lm -o $(NAME)
all: $(NAME)
$(NAME): $(OBJ)
@echo "$(RED)Compilation cub3D... $(RST)"
@$(FLAG) -Iinclude $(OBJ) -Llib -lmlx -lXext -lX11 -lm -o $(NAME)
@echo "$(GREEN)Compilation complete. $(ORANGE)Type "./cub3d" for execute the program!!$(RST)"
$(LOGO)
clean:
$(RM) $(OBJ)
@echo "$(RED)Deleating files objects... $(RST)"
$(RM) $(OBJ)
@echo "$(GREEN)files deleted!! $(RST)"
$(LOGO)
fclean: clean
$(RM) $(NAME)
fclean: clean
@echo "$(RED)Delete program name... $(RST)"
$(RM) $(NAME)
@echo "$(GREEN)File program deleted!! $(RST)"
re: fclean all
re: fclean all
.PHONY: all clean fclean re

View File

@ -1,4 +0,0 @@
Désolé j'ai pas push avant j'ai du recommencer 2 fois il reste juste les fonctions du main et de draw/ft_2d_view.c a mettre a la norme ça a été plus compliqué que je le pensai pour tout mettre à la norme.
Une vrai galère, il me reste également a implémenter le personnage et les textures dans le .cub mais bon ça devrait aller plus vite maintenant
Ya par moment des problèmes de collision qu'il va falloir régler mais bon ça devrai pas être trop
Je serai là lundi aprés-midi à demain

103
cub3d.h
View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 16:56:52 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 23:09:44 by fgras-ca ### ########.fr */
/* Updated: 2024/01/16 16:50:38 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -38,7 +38,7 @@
# define WIDTH 1780
# define HEIGHT 720
# define BOV 500
# define MAX_LINE_LENGTH 100000
typedef enum {
NORTH,
SOUTH,
@ -116,6 +116,15 @@ typedef struct s_struture_windows
int width;
} t_structure_windows;
typedef struct s_texture
{
char *north;
char *south;
char *west;
char *east;
unsigned int floor_color;
unsigned int ceil_color;
} t_texture;
typedef struct s_structure_main
{
int fd;
@ -126,6 +135,7 @@ typedef struct s_structure_main
t_structure_img s_img;
t_structure_map s_map;
t_structure_player s_player;
t_texture *t;
} t_structure_main;
typedef struct s_res_params {
@ -259,19 +269,86 @@ typedef struct s_texture_data
int endian;
} t_texture_data;
typedef struct s_init_params {
t_structure_main *w;
int tilesize;
int numrays;
float FOV;
float DR;
} t_init_params;
typedef struct s_ray_properties {
float disH;
float disV;
WallDirection hwalldir;
WallDirection vwalldir;
float hx;
float hy;
float vx;
float vy;
} t_ray_properties;
typedef struct {
int tilesize;
int numrays;
float FOV;
float DR;
float ra;
} t_base_params;
typedef struct {
float disH;
float disV;
float disT;
float hx;
float hy;
float vx;
float vy;
WallDirection hwalldir;
WallDirection vwalldir;
} t_ray_state;
typedef struct {
t_structure_main *w;
int r;
int color;
} t_ray_calc;
typedef struct {
t_base_params base_params;
t_ray_state ray_state;
t_ray_calc ray_calc;
t_ray_params rayparams;
t_ray_calc_params hrayparams;
t_ray_calc_params vrayparams;
} t_drawrays2d_params;
typedef struct {
int jkl;
int yui;
} t_state;
typedef struct {
t_structure_main *w;
t_state state;
} t_global_struct;
//ft_utils_split.c 5 / 5
char **ft_split(char const *s, char c);
//ft_utils_gnl.c 4 / 5
char *get_next_line(int fd);
//ft_utils_str_1.c
size_t ft_strlen(const char *s);
char *ft_strdup(const char *src);
char *ft_strjoin(char *left_str, char *buff);
char *ft_strchr(const char *s, int c);
/*ft_key.c 3/5*/
int *kill_prog(t_structure_main *w);
void move(int key, t_structure_main *w);
int deal_key(int key, t_structure_main *w);
size_t ft_strlen(const char *s);
char *ft_strdup(const char *src);
char *ft_strjoin(char *left_str, char *buff);
char *ft_strchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, size_t n);
int ft_strcmp(const char *s1, const char *s2);
void *ft_memcpy(void *dest, const void *src, size_t n);
void *ft_memset(void *s, int c, size_t n);
/*collision*/
void calculate_future_position(t_position_params *params);
int check_collision(t_structure_main *w, int future_px, int future_py);
@ -279,9 +356,8 @@ void calculate_future_position_right_left(t_position_params *params);
//ft_utils_convert.c 1/5
char *ft_itoa(int nb);
/*parsing*/
char *read_map(const char* filename, int* length);
int is_map_closed(char* map, int width, int height);
bool parse_map(const char* filename, t_structure_map *map_info);
bool parse_map(const char *map_content, int length, t_structure_map *map_info);
int check_borders(char *map, int maxWidth, int height);
int check_interior(char *map, int maxWidth, int height);
void exit_error(t_structure_main *w);
@ -290,6 +366,12 @@ void process_character(t_map_params *params, int *i);
void get_map_dimensions(t_map_params *params);
void fill_map_space(t_structure_map *map_info, int maxWidth, int height);
void copy_map_data(t_map_params *params);
bool load_cub_file(const char *filename, t_texture *textures, t_structure_map *map_info);
bool parse_texture_line(const char *line, t_texture *textures);
bool handle_map(int fd, char **map_buffer, int *map_length);
bool parse_color_line(const char *line, unsigned int *color);
bool is_valid_texture(const char *line);
bool handle_textures(int fd, t_texture *textures);
/*textures*/
void load_wall_textures(t_structure_main *w);
void draw_texture(t_texture_params *tex_params);
@ -305,7 +387,7 @@ void draw_map(t_structure_main *w);
void put_pixel_img(t_structure_main *w, int x, int y, int color);
void draw_square_raw(t_square_params *params);
void draw_line(t_line_params *params);
void drawRays2D(t_structure_main *w);
void drawrays2d(t_structure_main *w);
//Ray
void calculateverticalray(t_ray_calc_params *params);
void handle_ra_vertical(t_ray_calc_params *params, float nTan, int tileSize);
@ -316,5 +398,6 @@ float dist(float ax, float ay, float bx, float by);
void init_windows(t_structure_main *w);
void init_player(t_structure_main *w);
void init_mlx_and_window(t_structure_main *w);
void sleep_mouse(t_global_struct *global_struct);
#endif

View File

@ -6,84 +6,89 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 17:49:42 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 23:34:45 by fgras-ca ### ########.fr */
/* Updated: 2024/01/17 21:47:17 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void drawRays2D(t_structure_main *w)
float correctfisheye(float distance, float ra, float playerAngle)
{
int r;
int color;
int tilesize;
int numrays;
float ra;
float disH;
float disV;
float disT;
float hx;
float hy;
float vx;
float vy;
float FOV;
float DR;
WallDirection hwalldir;
WallDirection vwalldir;
t_ray_params rayparams;
t_ray_calc_params hrayparams;
t_ray_calc_params vrayparams;
float ca;
tilesize = w->s_map.mapS;
numrays = NUMRAY;
FOV = FOVIEW * (PI / 180);
DR = FOV / numrays;
ca = playerAngle - ra;
if (ca < 0)
ca += 2 * PI;
if (ca > 2 * PI)
ca -= 2 * PI;
return (distance * cos(ca));
}
void init_base_params(t_base_params *params, t_structure_main *w)
{
params->tilesize = w->s_map.mapS;
params->numrays = NUMRAY;
params->FOV = FOVIEW * (PI / 180);
params->DR = params->FOV / params->numrays;
params->ra = w->s_player.pa - (params->FOV / 2);
printf("DEBUG: Initialisation des paramètres de base\n");
printf("DEBUG: tilesize (taille de la tuile) = %d\n", w->s_map.mapS);
printf("DEBUG: numrays (nombre de rayons) = %d\n", NUMRAY);
printf("DEBUG: FOV (champ de vision) = %f\n", FOVIEW * (PI / 180));
printf("DEBUG: DR (delta rayon) = %f\n", params->FOV / params->numrays);
printf("DEBUG: ra (rayon actuel) = %f\n", w->s_player.pa - (params->FOV / 2));
draw_background(w);
}
ra = w->s_player.pa - (FOV / 2);
r = 0;
while (r++ < numrays)
void calculate_ray(t_base_params *base, t_ray_state *state,
t_ray_calc *calc, t_ray_params *rayparams)
{
if (state->disH < state->disV)
{
ra = fmod(ra + 2 * PI, 2 * PI);
rayparams->disT = state->disH;
calc->color = 0xFF0000;
rayparams->wallDir = state->hwalldir;
rayparams->rx = state->hx;
rayparams->ry = state->hy;
}
else
{
rayparams->disT = state->disV;
calc->color = 0x00FF00;
rayparams->wallDir = state->vwalldir;
rayparams->rx = state->vx;
rayparams->ry = state->vy;
}
rayparams->disT = correctfisheye(rayparams->disT,
base->ra, calc->w->s_player.pa);
rayparams->w = calc->w;
rayparams->tileSize = base->tilesize;
rayparams->r = calc->r;
rayparams->numRays = base->numrays;
rayparams->color = calc->color;
}
hrayparams.w = w;
hrayparams.ra = ra;
hrayparams.disRay = &disH;
hrayparams.rx = &hx;
hrayparams.ry = &hy;
hrayparams.wallDir = &hwalldir;
calculatehorizontalray(&hrayparams);
vrayparams.w = w;
vrayparams.ra = ra;
vrayparams.disRay = &disV;
vrayparams.rx = &vx;
vrayparams.ry = &vy;
vrayparams.wallDir = &vwalldir;
calculateverticalray(&vrayparams);
if (disH < disV)
{
disT = disH;
color = 0xFF0000;
rayparams.wallDir = hwalldir;
rayparams.rx = hx;
rayparams.ry = hy;
}
else
{
disT = disV;
color = 0x00FF00;
rayparams.wallDir = vwalldir;
rayparams.rx = vx;
rayparams.ry = vy;
}
disT = correctFisheye(disT, ra, w->s_player.pa);
rayparams.w = w;
rayparams.tileSize = tilesize;
rayparams.r = r;
rayparams.disT = disT;
rayparams.numRays = numrays;
rayparams.color = color;
drawray(&rayparams);
ra += DR;
void drawrays2d(t_structure_main *w)
{
t_drawrays2d_params params;
init_base_params(&params.base_params, w);
params.ray_calc.w = w;
params.ray_calc.r = 0;
while (params.ray_calc.r++ < params.base_params.numrays)
{
params.base_params.ra = fmod(params.base_params.ra + 2 * PI, 2 * PI);
params.hrayparams = (t_ray_calc_params){w, params.base_params.ra,
&params.ray_state.disH, &params.ray_state.hx,
&params.ray_state.hy, &params.ray_state.hwalldir};
params.vrayparams = (t_ray_calc_params){w, params.base_params.ra,
&params.ray_state.disV, &params.ray_state.vx,
&params.ray_state.vy, &params.ray_state.vwalldir};
calculatehorizontalray(&params.hrayparams);
calculateverticalray(&params.vrayparams);
calculate_ray(&params.base_params, &params.ray_state,
&params.ray_calc, &params.rayparams);
drawray(&params.rayparams);
params.base_params.ra += params.base_params.DR;
}
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 23:19:39 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 17:37:52 by fgras-ca ### ########.fr */
/* Updated: 2024/01/20 11:18:10 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,13 +35,13 @@ void draw_background(t_structure_main *w)
sky_params.w = w;
sky_params.startHeight = 0;
sky_params.endHeight = w->s_win.height / 2;
sky_params.color = 0xB2FFFF;
sky_params.color = w->t->ceil_color;
sky_params.backgroundOffsetX = backgroundoffsetx;
draw_sky_ground(&sky_params);
ground_params.w = w;
ground_params.startHeight = w->s_win.height / 2;
ground_params.endHeight = w->s_win.height;
ground_params.color = 0x280000;
ground_params.color = w->t->floor_color;
ground_params.backgroundOffsetX = backgroundoffsetx;
draw_sky_ground(&ground_params);
}

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 20:30:59 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 20:56:42 by fgras-ca ### ########.fr */
/* Updated: 2024/01/20 11:12:22 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,10 +32,10 @@ void load_texture(t_structure_main *w, char *file_path, void **texture_ptr)
void load_wall_textures(t_structure_main *w)
{
load_texture(w, "textures/NO.xpm", (void **)&w->s_img.north_texture);
load_texture(w, "textures/SO.xpm", (void **)&w->s_img.south_texture);
load_texture(w, "textures/WE.xpm", (void **)&w->s_img.west_texture);
load_texture(w, "textures/EA.xpm", (void **)&w->s_img.east_texture);
load_texture(w, w->t->north, (void **)&w->s_img.north_texture);
load_texture(w, w->t->south, (void **)&w->s_img.south_texture);
load_texture(w, w->t->west, (void **)&w->s_img.west_texture);
load_texture(w, w->t->east, (void **)&w->s_img.east_texture);
}
void exit_error(t_structure_main *w)

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 17:39:11 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 21:26:16 by fgras-ca ### ########.fr */
/* Updated: 2024/01/20 11:05:59 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
@ -60,6 +60,7 @@ void draw_texture(t_texture_params *params)
void *get_selected_texture(t_structure_main *w, WallDirection wallDir)
{
void *texture = NULL;
if (wallDir == NORTH)
return (w->s_img.north_texture);
else if (wallDir == SOUTH)
@ -74,6 +75,10 @@ void *get_selected_texture(t_structure_main *w, WallDirection wallDir)
exit_error(w);
return (NULL);
}
if (texture == NULL)
fprintf(stderr, "get_selected_texture: selected texture is NULL\n");
else
printf("get_selected_texture: selected texture = %p\n", texture);
}
t_texture_data get_texture_data(void *texture)
@ -97,6 +102,7 @@ int get_texture_color(t_structure_main *w, WallDirection wallDir,
{
return (0);
}
texture_data = get_texture_data(selected_texture);
pixel_pos = (textureX + textureY * w->s_img.texture_width)
* (texture_data.bpp / 8);

99
main.c
View File

@ -1,76 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 18:45:52 by fgras-ca #+# #+# */
/* Updated: 2024/01/20 11:08:57 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
float correctFisheye(float distance, float ra, float playerAngle) {
float ca = playerAngle - ra;
if (ca < 0) ca += 2 * PI;
if (ca > 2 * PI) ca -= 2 * PI;
return distance * cos(ca);
}
int jkl = -1;
int yui = 0;
void test2(t_structure_main *w)
int setup_and_load_map(int argc, char **argv, t_structure_main *w, t_texture *textures)
{
t_rescale_params params;
mlx_destroy_image(w->s_win.mlx, w->s_img.buffer);
w->s_img.buffer = mlx_new_image(w->s_win.mlx, w->s_win.width, w->s_win.height);
w->s_img.addr = mlx_get_data_addr(w->s_img.buffer, &(w->s_img.bpp), &(w->s_img.line_len), &(w->s_img.endian));
draw_map(w);
drawRays2D(w);
mlx_put_image_to_window(w->s_win.mlx, w->s_win.win, w->s_img.buffer, 0, 0);
int new_sprite_width = w->s_map.mapS;
int new_sprite_height = w->s_map.mapS;
int sprite_x = w->s_player.px - new_sprite_width / 2;
int sprite_y = w->s_player.py - new_sprite_height / 2;
params.original_img = w->s_img.roomadslam[jkl];
params.original_width = 112;
params.original_height = 112;
params.new_width = w->s_map.mapS;
params.new_height = w->s_map.mapS;
params.px = w->s_player.px - w->s_map.mapS / 2;
params.py = w->s_player.py - w->s_map.mapS / 2;
rescale_image(&params, w);
}
void test(t_structure_main *w)
{
int x;
int y;
if (yui < 10)
if (argc > 2)
{
usleep(1000);
yui++;
}
else
{
yui = 0;
jkl++;
test2(w);
mlx_mouse_get_pos(w->s_win.mlx, w->s_win.win, &x, &y);
if ((x > 0 && x < w->s_win.height) && (y > 0 && y < w->s_win.width))
if (x != w->s_win.height/2)
{
if (x < w->s_win.height/2)
deal_key(65361,w);
else
deal_key(65363,w);
}
}
if (jkl == 10)
jkl = -1;
}
int setup_and_load_map(int argc, char **argv, t_structure_main *w)
{
if (argc != 2)
{
fprintf(stderr, "Usage: %s <map_file>\n", argv[0]);
fprintf(stderr, "Usage: %s\n", argv[0]);
return (0);
}
parse_map("map.cub", &w->s_map);
if (!parse_map(argv[1], &w->s_map))
if (!load_cub_file("map.cub", textures, &w->s_map))
{
printf("Failed to load the map or map is not closed. Exiting...\n");
return (0);
@ -86,12 +35,22 @@ int setup_and_load_map(int argc, char **argv, t_structure_main *w)
int main(int argc, char **argv)
{
t_structure_main w;
t_global_struct global_struct;
if (!setup_and_load_map(argc, argv, &w))
w.t = malloc(sizeof(t_texture));
if (w.t == NULL)
{
// Gérer l'erreur d'allocation
}
ft_memset(w.t, 0, sizeof(t_texture));
global_struct.w = &w;
global_struct.state.jkl = -1;
global_struct.state.yui = 0;
if (!setup_and_load_map(argc, argv, &w, w.t))
return (1);
init_windows(&w);
mlx_loop_hook(w.s_win.mlx, (void *)test, &w);
mlx_hook(w.s_win.win, 2, 1L<<0, deal_key, &w);
mlx_loop_hook(w.s_win.mlx, (void *)sleep_mouse, &global_struct);
mlx_hook(w.s_win.win, 2, 1L << 0, deal_key, &w);
mlx_hook(w.s_win.win, 17, 0, (void *)kill_prog, &w);
mlx_loop(w.s_win.mlx);
if (w.s_map.map)

View File

@ -1,3 +1,10 @@
NO textures/NO.xpm
SO textures/SO.xpm
WE textures/WE.xpm
EA textures/EA.xpm
F 80, 75, 61
C 165, 176, 196
1111111 11111111111111111111 11111111111 11111111111111111111111111111111111111111111111111111111111111
111111000001110000000000000000001 10000000001 10000000000000000000000000000000001111111111111111111111111111
1111100000000000000000000000000000001 10000000001 10000000000000000000000000000000000000000000000000000000000001

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/15 20:04:23 by fgras-ca #+# #+# */
/* Updated: 2024/01/21 11:54:07 by fgras-ca ### ########.fr */
/* Updated: 2024/01/20 11:14:36 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */

Some files were not shown because too many files have changed in this diff Show More