This commit is contained in:
Ladebeze66 2024-01-14 23:36:45 +01:00
parent 2fd2c96579
commit b378e60269
44 changed files with 3041 additions and 7203 deletions

View File

@ -1,13 +1,45 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# 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 #
# #
# **************************************************************************** #
NAME = cub3d
SRC = main.c \
ft_utils_gnl.c \
ft_utils_split.c \
ft_utils_str_1.c \
ft_key.c \
ft_utils_convert.c \
ft_parsing.c \
blend_color.c \
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 \
OBJ = $(SRC:.c=.o)

View File

@ -1,2 +0,0 @@
# cub3D
# cub3D

View File

@ -1,28 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* blend_color.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/09 13:33:36 by fgras-ca #+# #+# */
/* Updated: 2024/01/09 13:43:05 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
int blendColor(int color1, int color2, float alpha) {
int r = ((color1 >> 16 & 0xFF) * (1 - alpha)) + ((color2 >> 16 & 0xFF) * alpha);
int g = ((color1 >> 8 & 0xFF) * (1 - alpha)) + ((color2 >> 8 & 0xFF) * alpha);
int b = ((color1 & 0xFF) * (1 - alpha)) + ((color2 & 0xFF) * alpha);
return (r << 16) | (g << 8) | b;
}
void drawTransparentSquare(t_structure_main *w, int x, int y, int color, float alpha) {
int backgroundColor = 0x000000; // Couleur d'arrière-plan, à ajuster selon vos besoins
int blendedColor = blendColor(color, backgroundColor, alpha);
draw_square(w, x, y, blendedColor); // Utilisez votre fonction existante pour dessiner le carré
}

239
cub3d.h
View File

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cub3d.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#ifndef CUB3D_H
# define CUB3D_H
@ -18,14 +30,21 @@
# define PI 3.14159265359
# define P2 PI/2
# define P3 3*PI/2
//# define DR 0.0174533
# define COLBUF 2
# define NUMRAY 1280
# define FOVIEW 60
# define DISRAY 1000000
# define DOF 120
# define WIDTH 1780
# define HEIGHT 720
# define BOV 500
typedef enum {
NORTH,
SOUTH,
WEST,
EAST,
NONE // Utilisé lorsqu'aucun mur n'est touché
NORTH,
SOUTH,
WEST,
EAST,
NONE // Utilisé lorsqu'aucun mur n'est touché
} WallDirection;
typedef struct s_img
@ -81,12 +100,12 @@ typedef struct s_struture_img
void *roomadslam[10];
void *tgsway[20];
void *pedro_wall;
void *north_texture;
void *south_texture;
void *west_texture;
void *east_texture;
int texture_width;
int texture_height;
void *north_texture;
void *south_texture;
void *west_texture;
void *east_texture;
int texture_width;
int texture_height;
} t_structure_img;
typedef struct s_struture_windows
@ -100,50 +119,202 @@ typedef struct s_struture_windows
typedef struct s_structure_main
{
int fd;
struct timeval start_time;
struct timeval end_time;
int frame_count;
struct timeval start_time;
struct timeval end_time;
int frame_count;
t_structure_windows s_win;
t_structure_img s_img;
t_structure_map s_map;
t_structure_player s_player;
} t_structure_main;
typedef struct s_res_params {
char *original_data;
void *rescaled_img;
char *rescaled_data;
float x_ratio;
float y_ratio;
int original_x;
int original_y;
int pixel_pos_rescaled;
int pixel_pos_original;
} t_res_params;
typedef struct s_rescale_params {
void *original_img;
int original_width;
int original_height;
int new_width;
int new_height;
int px;
int py;
t_res_params *res;
} t_rescale_params;
typedef struct s_position_params {
int *future_px;
int *future_py;
int pdx;
int pdy;
int collisionBuffer;
int px;
int py;
double pa;
char direction;
} t_position_params;
typedef struct s_map_params {
t_structure_map *map_info;
const char *buffer;
int length;
int *maxWidth;
int *height;
int *currentWidth;
int *isNewLine;
} t_map_params;
typedef struct s_square_params {
t_structure_main *w;
int x;
int y;
int xo;
int yo;
int color;
} t_square_params;
typedef struct s_sky_ground_params {
t_structure_main *w;
int startHeight;
int endHeight;
int color;
int backgroundOffsetX;
} t_sky_ground_params;
typedef struct s_line_params {
t_structure_main *w;
int x0;
int y0;
int x1;
int y1;
int color;
} t_line_params;
typedef struct s_line_deltas {
int dx;
int dy;
int sx;
int sy;
int err;
t_line_params *params;
} t_line_deltas;
typedef struct s_ray_params {
t_structure_main *w;
int r;
int tileSize;
float rx;
float ry;
float disT;
WallDirection wallDir;
int numRays;
int color;
int start3DHeight;
int max3DHeight;
float lineH;
float lineOff;
int backgroundOffsetX;
int raywidth;
} t_ray_params;
typedef struct s_texture_params {
t_structure_main *w;
int startX;
int endX;
float lineOff;
float lineH;
WallDirection wallDir;
float rx;
float ry;
float disT;
} t_texture_params;
typedef struct s_ray_calc_params {
t_structure_main *w;
float ra;
float *disRay;
float *rx;
float *ry;
WallDirection *wallDir;
float xo;
float yo;
int dof;
} t_ray_calc_params;
typedef struct s_texture_data
{
char *data;
int bpp;
int size_line;
int endian;
} t_texture_data;
//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
/*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);
/*collision*/
void calculate_future_position(t_position_params *params);
int check_collision(t_structure_main *w, int future_px, int future_py);
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);
void drawRay(t_structure_main *w, int r, float rx, float ry, float disT, WallDirection wallDir, int numRays, int color);
//wall texturesv
void exit_error(t_structure_main *w);
void load_wall_textures(t_structure_main *w);
//transparence
int blendColor(int color1, int color2, float alpha);
void drawTransparentSquare(t_structure_main *w, int x, int y, int color, float alpha);
/*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);
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);
void update_max_dimensions(t_map_params *params);
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);
/*textures*/
void load_wall_textures(t_structure_main *w);
void draw_texture(t_texture_params *tex_params);
int get_texture_color(t_structure_main *w, WallDirection wallDir, int textureX, int textureY);
/*3D view*/
void drawray(t_ray_params *ray_params);
void draw_background(t_structure_main *w);
float correctFisheye(float distance, float ra, float playerAngle);
//2D map
void draw_square(t_structure_main *w, int x, int y, int color);
void draw_map(t_structure_main *w);
void rescale_image(t_rescale_params *params, t_structure_main *w);
void draw_square(t_structure_main *w, int x, int y, int color);
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);
//Ray
void calculateverticalray(t_ray_calc_params *params);
void handle_ra_vertical(t_ray_calc_params *params, float nTan, int tileSize);
void calculatehorizontalray(t_ray_calc_params *params);
void handle_ra_not_equal_pi(t_ray_calc_params *params, float atan, int tilesize);
float dist(float ax, float ay, float bx, float by);
//window
void init_windows(t_structure_main *w);
void init_player(t_structure_main *w);
void init_mlx_and_window(t_structure_main *w);
#endif

89
draw/ft_2d_view.c Normal file
View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_2d_view.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void drawRays2D(t_structure_main *w)
{
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;
tilesize = w->s_map.mapS;
numrays = NUMRAY;
FOV = FOVIEW * (PI / 180);
DR = FOV / numrays;
draw_background(w);
ra = w->s_player.pa - (FOV / 2);
r = 0;
while (r++ < numrays)
{
ra = fmod(ra + 2 * PI, 2 * PI);
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;
}
}

61
draw/ft_3d_view.c Normal file
View File

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_3d_view.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 17:35:53 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 17:19:42 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void init_line_params(t_line_params *lineparams, t_ray_params *rayParams)
{
lineparams->w = rayParams->w;
lineparams->x0 = (int)rayParams->w->s_player.px;
lineparams->y0 = (int)rayParams->w->s_player.py;
lineparams->x1 = (int)rayParams->rx;
lineparams->y1 = (int)rayParams->ry;
lineparams->color = rayParams->color;
}
void init_texture_params(t_texture_params *textureparams,
t_ray_params *rayParams)
{
textureparams->w = rayParams->w;
textureparams->lineOff = rayParams->lineOff;
textureparams->lineH = rayParams->lineH;
textureparams->wallDir = rayParams->wallDir;
textureparams->rx = rayParams->rx + rayParams->backgroundOffsetX;
textureparams->ry = rayParams->ry;
textureparams->disT = rayParams->disT;
}
void drawray(t_ray_params *rayparams)
{
t_line_params lineparams;
t_texture_params textureparams;
rayparams->start3DHeight = 0;
rayparams->max3DHeight = rayparams->w->s_win.height
+ rayparams->start3DHeight;
rayparams->lineH = (rayparams->tileSize * rayparams->max3DHeight)
/ rayparams->disT;
if (rayparams->lineH > rayparams->max3DHeight)
{
rayparams->lineH = rayparams->max3DHeight;
}
rayparams->lineOff = ((rayparams->max3DHeight - rayparams->lineH) / 2);
rayparams->backgroundOffsetX = BOV;
rayparams->raywidth = rayparams->w->s_win.width / rayparams->numRays;
textureparams.startX = rayparams->r * rayparams->raywidth
+ rayparams->backgroundOffsetX;
textureparams.endX = textureparams.startX + rayparams->raywidth;
init_line_params(&lineparams, rayparams);
draw_line(&lineparams);
init_texture_params(&textureparams, rayparams);
draw_texture(&textureparams);
}

47
draw/ft_background.c Normal file
View File

@ -0,0 +1,47 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_background.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void draw_sky_ground(t_sky_ground_params *params)
{
t_square_params square_params;
square_params.w = params->w;
square_params.x = params->backgroundOffsetX;
square_params.y = params->startHeight;
square_params.xo = params->w->s_win.width + params->backgroundOffsetX;
square_params.yo = params->endHeight;
square_params.color = params->color;
draw_square_raw(&square_params);
}
void draw_background(t_structure_main *w)
{
int backgroundoffsetx;
t_sky_ground_params sky_params;
t_sky_ground_params ground_params;
backgroundoffsetx = BOV;
sky_params.w = w;
sky_params.startHeight = 0;
sky_params.endHeight = w->s_win.height / 2;
sky_params.color = 0xB2FFFF;
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.backgroundOffsetX = backgroundoffsetx;
draw_sky_ground(&ground_params);
}

89
draw/ft_draw.c Normal file
View File

@ -0,0 +1,89 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_draw.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:38:28 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 17:54:44 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void draw_square(t_structure_main *w, int x, int y, int color)
{
int xo;
int yo;
int size;
int i;
int j;
xo = x * w->s_map.mapS;
yo = y * w->s_map.mapS;
size = w->s_map.mapS;
i = 0;
while (i < size)
{
j = 0;
while (j < size)
{
put_pixel_img(w, xo + j, yo + i, color);
j++;
}
i++;
}
}
static void init_line_deltas(t_line_deltas *deltas)
{
(deltas->dx) = abs(deltas->params->x1 - deltas->params->x0);
(deltas->dy) = abs(deltas->params->y1 - deltas->params->y0);
(deltas->err) = (deltas->dx) - (deltas->dy);
if (deltas->params->x0 < deltas->params->x1)
(deltas->sx) = 1;
else
(deltas->sx) = -1;
if (deltas->params->y0 < deltas->params->y1)
(deltas->sy) = 1;
else
(deltas->sy) = -1;
}
static void trace_line(t_line_deltas *deltas)
{
int e2;
while (deltas->params->x0 != deltas->params->x1
|| deltas->params->y0 != deltas->params->y1)
{
put_pixel_img(deltas->params->w, deltas->params->x0,
deltas->params->y0, deltas->params->color);
e2 = 2 * (deltas->err);
if (e2 > -((deltas->dy)))
{
(deltas->err) -= (deltas->dy);
deltas->params->x0 += (deltas->sx);
}
if (e2 < (deltas->dx))
{
(deltas->err) += (deltas->dx);
deltas->params->y0 += (deltas->sy);
}
}
}
void draw_line(t_line_params *params)
{
t_line_deltas deltas;
deltas.params = params;
init_line_deltas(&deltas);
trace_line(&deltas);
}
float dist(float ax, float ay, float bx, float by)
{
return (sqrt((bx - ax) * (bx - ax) + (by - ay) * (by - ay)));
}

82
draw/ft_horizontal_ray.c Normal file
View File

@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_horizontal_ray.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 19:53:47 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 18:34:26 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void handle_ra_equal_pi(t_ray_calc_params *params)
{
*params->rx = params->w->s_player.px;
*params->ry = params->w->s_player.py;
params->dof = DOF;
}
void inithorizontalray(t_ray_calc_params *params)
{
float atan;
int tilesize;
atan = -1 / tan(params->ra);
tilesize = params->w->s_map.mapS;
if (params->ra != PI)
handle_ra_not_equal_pi(params, atan, tilesize);
else
handle_ra_equal_pi(params);
}
void update_ray_params(t_ray_calc_params *params, int mx, int my)
{
int mp;
mp = my * params->w->s_map.mapX + mx;
if (mp >= 0 && mp < params->w->s_map.mapX * params->w->s_map.mapY
&& params->w->s_map.map[mp] == '1')
{
params->dof = DOF;
*params->disRay = dist(params->w->s_player.px, params->w->s_player.py,
*params->rx, *params->ry);
if (params->ra > PI)
*params->wallDir = NORTH;
else
*params->wallDir = SOUTH;
}
}
void processhorizontalray(t_ray_calc_params *params)
{
int mx;
int my;
int tilesize;
tilesize = params->w->s_map.mapS;
*params->disRay = DISRAY;
while (params->dof < DOF)
{
mx = (int)(*params->rx) / tilesize;
my = (int)(*params->ry) / tilesize;
update_ray_params(params, mx, my);
if (params->dof < DOF)
{
*params->rx += params->xo;
*params->ry += params->yo;
params->dof++;
}
}
}
void calculatehorizontalray(t_ray_calc_params *params)
{
params->xo = 0;
params->yo = 0;
params->dof = 0;
inithorizontalray(params);
processhorizontalray(params);
}

44
draw/ft_load_textures.c Normal file
View File

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_load_textures.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void load_texture(t_structure_main *w, char *file_path, void **texture_ptr)
{
int width;
int height;
width = 0;
height = 0;
*texture_ptr = mlx_xpm_file_to_image(w->s_win.mlx,
file_path, &width, &height);
if (!(*texture_ptr))
{
fprintf(stderr, "Failed to load texture: %s\n", file_path);
exit_error(w);
}
w->s_img.texture_width = width;
w->s_img.texture_height = height;
}
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);
}
void exit_error(t_structure_main *w)
{
exit(1);
}

50
draw/ft_pixel.c Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pixel.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 17:54:04 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 17:54:40 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void put_pixel_img(t_structure_main *w, int x, int y, int color)
{
char *dst;
if (x >= 0 && y >= 0 && x < w->s_win.width && y < w->s_win.height)
{
dst = w->s_img.addr + (y * w->s_img.line_len + x * (w->s_img.bpp / 8));
*(unsigned int *)dst = color;
}
else
{
}
}
void draw_square_raw(t_square_params *params)
{
int i;
int j;
int size_x;
int size_y;
i = 0;
size_x = abs(params->xo - params->x);
size_y = abs(params->yo - params->y);
while (i < size_y)
{
j = 0;
while (j < size_x)
{
put_pixel_img(params->w, params->x + j,
params->y + i, params->color);
j++;
}
i++;
}
}

59
draw/ft_ray_utils.c Normal file
View File

@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ray_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 18:25:39 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 18:41:22 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void handle_ra_vertical(t_ray_calc_params *params, float nTan, int tileSize)
{
if (params->ra > P2 && params->ra < P3)
{
*params->rx = (((int)params->w->s_player.px / tileSize)
* tileSize) - 0.0001;
params->xo = -tileSize;
}
else if (params->ra < P2 || params->ra > P3)
{
*params->rx = (((int)params->w->s_player.px / tileSize)
* tileSize) + tileSize;
params->xo = tileSize;
}
else
{
*params->rx = params->w->s_player.px;
*params->ry = params->w->s_player.py;
params->dof = DOF;
return ;
}
*params->ry = (params->w->s_player.px - *params->rx)
* nTan + params->w->s_player.py;
params->yo = -params->xo * nTan;
}
void handle_ra_not_equal_pi(t_ray_calc_params *params,
float atan, int tilesize)
{
if (params->ra > PI)
{
*params->ry = (((int)params->w->s_player.py / tilesize)
* tilesize) - 0.0001;
params->yo = -tilesize;
}
else
{
*params->ry = (((int)params->w->s_player.py / tilesize)
* tilesize) + tilesize;
params->yo = tilesize;
}
*params->rx = (params->w->s_player.py - *params->ry)
* atan + params->w->s_player.px;
params->xo = -params->yo * atan;
}

104
draw/ft_textures.c Normal file
View File

@ -0,0 +1,104 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_textures.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void draw_texture_line(t_texture_params *params, float y, int textureY)
{
int x;
int texturewidth;
int texturex;
int color;
texturewidth = params->w->s_img.texture_width;
x = params->startX;
while (x < params->endX)
{
if (params->wallDir == NORTH || params->wallDir == SOUTH)
texturex = (int)(params->rx * texturewidth
/ params->w->s_map.mapS) % texturewidth;
else
texturex = (int)(params->ry * texturewidth
/ params->w->s_map.mapS) % texturewidth;
if (texturex >= texturewidth)
texturex = texturewidth - 1;
color = get_texture_color(params->w,
params->wallDir, texturex, textureY);
put_pixel_img(params->w, x, y, color);
x++;
}
}
void draw_texture(t_texture_params *params)
{
int textureheight;
float y;
float perspectivefactor;
int texturey;
textureheight = params->w->s_img.texture_height;
y = params->lineOff;
while (y < params->lineOff + params->lineH)
{
perspectivefactor = (y - params->lineOff) / params->lineH;
texturey = perspectivefactor * textureheight;
if (texturey >= textureheight)
texturey = textureheight - 1;
draw_texture_line(params, y, texturey);
y++;
}
}
void *get_selected_texture(t_structure_main *w, WallDirection wallDir)
{
if (wallDir == NORTH)
return (w->s_img.north_texture);
else if (wallDir == SOUTH)
return (w->s_img.south_texture);
else if (wallDir == WEST)
return (w->s_img.west_texture);
else if (wallDir == EAST)
return (w->s_img.east_texture);
else
{
fprintf(stderr, "Invalid wall direction.\n");
exit_error(w);
return (NULL);
}
}
t_texture_data get_texture_data(void *texture)
{
t_texture_data texture_data;
texture_data.data = mlx_get_data_addr(texture, &texture_data.bpp,
&texture_data.size_line, &texture_data.endian);
return (texture_data);
}
int get_texture_color(t_structure_main *w, WallDirection wallDir,
int textureX, int textureY)
{
void *selected_texture;
t_texture_data texture_data;
int pixel_pos;
selected_texture = get_selected_texture(w, wallDir);
if (selected_texture == NULL)
{
return (0);
}
texture_data = get_texture_data(selected_texture);
pixel_pos = (textureX + textureY * w->s_img.texture_width)
* (texture_data.bpp / 8);
return (*(int *)(texture_data.data + pixel_pos));
}

72
draw/ft_vertical_ray.c Normal file
View File

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_vertical_ray.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/12 20:03:21 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 18:39:19 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void initverticalray(t_ray_calc_params *params)
{
float ntan;
int tilesize;
ntan = -tan(params->ra);
tilesize = params->w->s_map.mapS;
handle_ra_vertical(params, ntan, tilesize);
}
void update_vertical_ray_params(t_ray_calc_params *params, int mx, int my)
{
int mp;
mp = my * params->w->s_map.mapX + mx;
if (mp >= 0 && mp < params->w->s_map.mapX * params->w->s_map.mapY
&& params->w->s_map.map[mp] == '1')
{
params->dof = DOF;
*params->disRay = dist(params->w->s_player.px, params->w->s_player.py,
*params->rx, *params->ry);
if (params->ra > P2 && params->ra < P3)
*params->wallDir = WEST;
else
*params->wallDir = EAST;
}
}
void processverticalray(t_ray_calc_params *params)
{
int mx;
int my;
int tilesize;
tilesize = params->w->s_map.mapS;
*params->disRay = DISRAY;
while (params->dof < DOF)
{
mx = (int)(*params->rx) / tilesize;
my = (int)(*params->ry) / tilesize;
update_vertical_ray_params(params, mx, my);
if (params->dof < DOF)
{
*params->rx += params->xo;
*params->ry += params->yo;
params->dof++;
}
}
}
void calculateverticalray(t_ray_calc_params *params)
{
params->xo = 0;
params->yo = 0;
params->dof = 0;
initverticalray(params);
processverticalray(params);
}

139
ft_key.c
View File

@ -1,139 +0,0 @@
#include "cub3d.h"
int *kill_prog(t_structure_main *w) {
mlx_destroy_window(w->s_win.mlx, w->s_win.win);
mlx_destroy_image(w->s_win.mlx, w->s_img.buffer);
mlx_destroy_image(w->s_win.mlx, w->s_img.img_player);
mlx_destroy_image(w->s_win.mlx, w->s_img.img_wall);
mlx_destroy_image(w->s_win.mlx, w->s_img.pedro_wall);
// Libérer les images de roomadslam
int i = -1;
while (++i <= 10)
mlx_destroy_image(w->s_win.mlx, w->s_img.roomadslam[i]);
// Libérer la carte si elle a été allouée
if (w->s_map.map) {
free(w->s_map.map);
w->s_map.map = NULL; // Sécurité supplémentaire après la libération
}
mlx_destroy_display(w->s_win.mlx);
free(w->s_win.mlx);
exit(0);
return (0);
}
void move(int key, t_structure_main *w) {
int xo = 0;
if (w->s_player.pdx < 0) { xo = -w->s_map.mapS / 2; } else { xo = w->s_map.mapS / 2; }
int yo = 0;
if (w->s_player.pdy < 0) { yo = -w->s_map.mapS / 2; } else { yo = w->s_map.mapS / 2; }
// Convertir la position du joueur en coordonnées de grille
int ipx = w->s_player.px / w->s_map.mapS;
int ipy = w->s_player.py / w->s_map.mapS;
// Calculer les positions potentielles après le mouvement
int ipx_add_xo = (w->s_player.px + xo) / w->s_map.mapS;
int ipy_add_yo = (w->s_player.py + yo) / w->s_map.mapS;
int ipx_sub_xo = (w->s_player.px - xo) / w->s_map.mapS;
int ipy_sub_yo = (w->s_player.py - yo) / w->s_map.mapS;
int collisionBuffer = 2; // Marge de collision pour éviter de coller aux murs
// Déplacement vers l'avant
if (key == 119) {
int future_py = (int)(w->s_player.py + w->s_player.pdy + collisionBuffer); // Calculez la position future en y avec la marge
int future_px = (int)(w->s_player.px + w->s_player.pdx + collisionBuffer); // Calculez la position future en x
int future_ipy = future_py / w->s_map.mapS; // Convertissez en coordonnées de grille
int future_ipx = future_px / w->s_map.mapS; // Convertissez en coordonnées de grille
if (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0') {
w->s_player.px += w->s_player.pdx;
w->s_player.py += w->s_player.pdy;
}
}
// Déplacement vers la droite
else if (key == 100) {
int future_px = w->s_player.px + (int)(cos(w->s_player.pa + M_PI_2) * 5);
int future_py = w->s_player.py + (int)(sin(w->s_player.pa + M_PI_2) * 5);
int collision_px = future_px + (int)(cos(w->s_player.pa) * collisionBuffer);
int collision_py = future_py + (int)(sin(w->s_player.pa) * collisionBuffer);
int future_ipx = collision_px / w->s_map.mapS;
int future_ipy = collision_py / w->s_map.mapS;
if (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0') {
w->s_player.px = future_px;
w->s_player.py = future_py;
}
}
// Déplacement vers l'arrière (s)
// Déplacement vers l'arrière
else if (key == 115) {
int future_px = w->s_player.px - (int)(w->s_player.pdx * collisionBuffer);
int future_py = w->s_player.py - (int)(w->s_player.pdy * collisionBuffer);
int future_ipx = future_px / w->s_map.mapS;
int future_ipy = future_py / w->s_map.mapS;
if (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0') {
w->s_player.px -= w->s_player.pdx;
w->s_player.py -= w->s_player.pdy;
}
}
// Déplacement vers la gauche
else if (key == 97) {
int future_px = w->s_player.px + (int)(cos(w->s_player.pa - M_PI_2) * 5);
int future_py = w->s_player.py + (int)(sin(w->s_player.pa - M_PI_2) * 5);
int collision_px = future_px + (int)(cos(w->s_player.pa) * collisionBuffer);
int collision_py = future_py + (int)(sin(w->s_player.pa) * collisionBuffer);
int future_ipx = collision_px / w->s_map.mapS;
int future_ipy = collision_py / w->s_map.mapS;
if (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0') {
w->s_player.px = future_px;
w->s_player.py = future_py;
}
}
}
int deal_key(int key, t_structure_main *w)
{
//printf("key pressed = %d\n", key);
if (key == 65307)
kill_prog(w);
else if (key == 65361)
{
w->s_player.pa -= 16*((PI/3)/1280);
if (w->s_player.pa < 0)
w->s_player.pa += 2 * PI;
w->s_player.pdx = cos(w->s_player.pa) * 5;
w->s_player.pdy = sin(w->s_player.pa) * 5;
}
else if (key == 65363)
{
w->s_player.pa += 16*((PI/3)/1280);
if (w->s_player.pa > 2 * PI)
w->s_player.pa -= 2 * PI;
w->s_player.pdx = cos(w->s_player.pa) * 5;
w->s_player.pdy = sin(w->s_player.pa) * 5;
}
else if (key == 65362)
;
else if (key == 65364)
;
else if (key == 119 || key == 100 || key == 115 || key == 97) // w, d, s, a
move(key,w);
else if (key == 114)
;
else if (key == 102)
;
else
return (key);
return (key);
}

View File

@ -1,157 +0,0 @@
#include "cub3d.h"
char* read_map(const char* filename, int* length) {
FILE *file = fopen(filename, "r");
if (!file) {
perror("Error opening file");
return NULL;
}
fseek(file, 0, SEEK_END);
*length = ftell(file);
fseek(file, 0, SEEK_SET);
char *buffer = malloc(*length + 1);
if (!buffer) {
perror("Error allocating memory for map");
fclose(file);
return NULL;
}
fread(buffer, 1, *length, file);
buffer[*length] = '\0';
fclose(file);
return buffer;
}
int is_map_closed(char* map, int maxWidth, int height) {
// Vérifier le bord supérieur et inférieur pour chaque colonne
for (int x = 0; x < maxWidth; x++) {
if (map[x] != '1' && map[x] != ' ') { // Haut
printf("Open space on top border at (%d,0)\n", x);
return 0;
}
if (map[(height - 1) * maxWidth + x] != '1' && map[(height - 1) * maxWidth + x] != ' ') { // Bas
printf("Open space on bottom border at (%d,%d)\n", x, height - 1);
return 0;
}
}
// Vérifier les bords gauche et droit pour chaque ligne
for (int y = 0; y < height; y++) {
int lineLength = 0; // Longueur réelle de la ligne courante
for (int x = 0; x < maxWidth && map[y * maxWidth + x] != ' '; x++) {
lineLength++;
}
if (lineLength > 0) {
if (map[y * maxWidth] != '1') { // Gauche
printf("Open space on left border at (0,%d)\n", y);
return 0;
}
if (map[y * maxWidth + lineLength - 1] != '1') { // Droite
printf("Open space on right border at (%d,%d)\n", lineLength - 1, y);
return 0;
}
}
// Vérification de l'intérieur de la carte pour les espaces non enclos
for (int x = 1; x < lineLength - 1; x++) {
char c = map[y * maxWidth + x];
if (c == ' ') {
if (map[y * maxWidth + (x - 1)] != '1' || // Gauche
map[y * maxWidth + (x + 1)] != '1' || // Droite
(y > 0 && map[(y - 1) * maxWidth + x] != '1') || // Haut
(y < height - 1 && map[(y + 1) * maxWidth + x] != '1')) { // Bas
printf("Space not enclosed by walls at (%d,%d)\n", x, y);
return 0;
}
}
}
}
return 1; // Map is closed
}
bool parse_map(const char* filename, t_structure_map *map_info) {
int length;
char* buffer = read_map(filename, &length);
if (!buffer) {
printf("Failed to read the map file.\n");
return (false);
}
int maxWidth = 0;
int currentWidth = 0; // La largeur de la ligne actuelle
int height = 0;
for (int i = 0; i < length; i++) {
if (buffer[i] == '\n') {
height++; // Nouvelle ligne, augmenter le compteur de hauteur
if (currentWidth > maxWidth) {
maxWidth = currentWidth; // Mise à jour de la largeur maximale si nécessaire
}
currentWidth = 0; // Réinitialiser la largeur pour la nouvelle ligne
} else if (buffer[i] != '\r') { // Ignorer les retours chariot
currentWidth++; // Augmenter la largeur de la ligne actuelle
}
}
// Gérer la dernière ligne si elle ne se termine pas par '\n'
if (currentWidth > 0) {
height++;
if (currentWidth > maxWidth) {
maxWidth = currentWidth;
}
}
// Vérification des dimensions de la carte
if (maxWidth <= 0 || height <= 0) {
printf("Invalid map dimensions: maxWidth=%d, height=%d\n", maxWidth, height);
free(buffer);
return (false);
}
// Allocation de la mémoire pour la carte
map_info->mapX = maxWidth;
map_info->mapY = height;
map_info->map = malloc(maxWidth * height);
if (!map_info->map) {
perror("Failed to allocate memory for map");
free(buffer);
return (false);
}
// Remplir la carte avec des espaces pour gérer les lignes de longueur variable
for (int i = 0; i < maxWidth * height; i++) {
map_info->map[i] = ' ';
}
// Copie des données de la carte
int x = 0, y = 0;
for (int i = 0; i < length; i++) {
if (buffer[i] == '\n') {
y++;
x = 0;
} else if (buffer[i] != '\r') { // Ignorer les retours chariot
if (x < maxWidth) {
map_info->map[y * maxWidth + x] = buffer[i];
x++;
}
}
}
printf("Width: %d, Height: %d, Total length: %d\n", maxWidth, height, length);
// Vérifier si la carte est fermée
if (!is_map_closed(map_info->map, maxWidth, height)) {
printf("Map is not closed!\n");
free(buffer);
return false; // Indique un échec
} else {
printf("Map is valid and closed.\n");
}
free(buffer);
return true; // Indique un succès
}

View File

@ -1,50 +0,0 @@
#include "cub3d.h"
static size_t ft_intlen(long n)
{
size_t len;
len = 0;
if (n == 0)
len++;
if (n < 0)
{
n *= -1;
len++;
}
while (n > 0)
{
n = n / 10;
len++;
}
return (len);
}
char *ft_itoa(int nb)
{
char *dst;
int i;
long n;
n = nb;
i = ft_intlen(n);
dst = (char *)malloc((i + 1) * sizeof(char));
if (!dst)
return (NULL);
dst[i--] = '\0';
if (n == 0)
dst[0] = '0';
if (n < 0)
{
dst[0] = '-';
n = n * -1;
}
while (n > 0)
{
dst[i] = '0' + (n % 10);
n = n / 10;
i--;
}
return (dst);
}

715
main.c
View File

@ -1,572 +1,43 @@
#include "cub3d.h"
void put_pixel_img(t_structure_main *w, int x, int y, int color) {
char *dst;
// Correction : x vérifié avec la largeur et y avec la hauteur
if (x >= 0 && y >= 0 && x < w->s_win.width && y < w->s_win.height) {
dst = w->s_img.addr + (y * w->s_img.line_len + x * (w->s_img.bpp / 8));
*(unsigned int *)dst = color;
} else {
// Ajout de logs pour les cas où les coordonnées sont hors limites
//printf("put_pixel_img: pixel hors limites (x: %d, y: %d)\n", x, y);
}
}
void draw_square_raw(t_structure_main *w, int x, int y, int xo, int yo, int color)
{
int i;
int j;
int size_x = abs(xo - x);
int size_y = abs(yo - y);
//printf("draw_square_raw called with x: %d, y: %d, xo: %d, yo: %d\n", x, y, xo, yo);
//printf("Calculated size_x: %d, size_y: %d\n", size_x, size_y);
for (i = 0; i < size_y; i++)
{
for (j = 0; j < size_x; j++)
{
put_pixel_img(w, (x) + j, (y) + i, color);
//printf("Drawing pixel at x: %d, y: %d\n", (x) + j, (y) + i);
}
}
//printf("draw_square_raw completed\n");
}
void draw_square(t_structure_main *w, int x, int y, int color) {
int xo = x * w->s_map.mapS; // Coordonnée X en pixels
int yo = y * w->s_map.mapS; // Coordonnée Y en pixels
int size = w->s_map.mapS; // Taille du carré
// Dessiner le carré en utilisant la taille et les coordonnées calculées
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
put_pixel_img(w, xo + j, yo + i, color);
}
}
}
void draw_line(t_structure_main *w, int x0, int y0, int x1, int y1, int color)
{
int dx = abs(x1 - x0);
int dy = abs(y1 - y0);
int sx, sy;
//printf("draw_line - x0: %d, y0: %d, x1: %d, y1: %d\n", x0, y0, x1, y1);
if (x0 < x1)
sx = 1;
else
sx = -1;
if (y0 < y1)
sy = 1;
else
sy = -1;
int err = dx - dy;
while (1)
{
put_pixel_img(w, x0, y0, color);
if (x0 == x1 && y0 == y1)
break;
int e2 = 2 * err;
if (e2 > -dy)
{
err -= dy;
x0 += sx;
}
if (e2 < dx)
{
err += dx;
y0 += sy;
}
//printf("Tracing - x0: %d, y0: %d\n", x0, y0);
}
//printf("draw_line completed\n");
}
float dist(float ax, float ay, float bx, float by)
{
return ( sqrt((bx-ax)*(bx-ax) + (by-ay)*(by-ay)));
}
void rescale_image(void *mlx, void *win, void *original_img, int original_width, int original_height, int new_width, int new_height, int px, int py, t_structure_main *w)
{
// Créez une nouvelle image avec la taille souhaitée
void *rescaled_img = mlx_new_image(w->s_win.mlx, new_width, new_height);
if (!rescaled_img)
{
// Gestion de l'erreur
perror("Erreur lors de la création de la nouvelle image");
return;
}
// Obtenez les adresses des données des images
char *original_data = mlx_get_data_addr(original_img, &(w->s_img.bpp), &(w->s_img.line_len), &(w->s_img.endian));
char *rescaled_data = mlx_get_data_addr(rescaled_img, &(w->s_img.bpp), &(w->s_img.line_len), &(w->s_img.endian));
// Facteurs de redimensionnement
float x_ratio = (float)original_width / new_width;
float y_ratio = (float)original_height / new_height;
// Parcourez chaque pixel de la nouvelle image
for (int y = 0; y < new_height; ++y)
{
for (int x = 0; x < new_width; ++x)
{
// Calculez les coordonnées correspondantes dans l'image d'origine
int original_x = x_ratio * x;
int original_y = y_ratio * y;
// Copiez la couleur du pixel de l'image d'origine vers la nouvelle image
// Vous devrez ajuster les offsets en fonction de votre format de pixel
// et de la profondeur des couleurs
rescaled_data[y * new_width * 4 + x * 4 + 0] = original_data[original_y * original_width * 4 + original_x * 4 + 0];
rescaled_data[y * new_width * 4 + x * 4 + 1] = original_data[original_y * original_width * 4 + original_x * 4 + 1];
rescaled_data[y * new_width * 4 + x * 4 + 2] = original_data[original_y * original_width * 4 + original_x * 4 + 2];
rescaled_data[y * new_width * 4 + x * 4 + 3] = original_data[original_y * original_width * 4 + original_x * 4 + 3];
}
}
// Affichez la nouvelle image
mlx_put_image_to_window(w->s_win.mlx, w->s_win.win, rescaled_img, px,py);
// Libérez la mémoire de la nouvelle image (si vous n'en avez plus besoin)
mlx_destroy_image(w->s_win.mlx, rescaled_img);
}
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);
float ca = playerAngle - ra;
if (ca < 0) ca += 2 * PI;
if (ca > 2 * PI) ca -= 2 * PI;
return distance * cos(ca);
}
void calculateVerticalRay(t_structure_main *w, float ra, float *disV, float *vx, float *vy, WallDirection *wallDir) {
int dof = 0, mx, my, mp;
float rx, ry, xo, yo;
int tileSize = w->s_map.mapS;
*disV = 100000; // Distance initialement grande
*wallDir = NONE;
float nTan = -tan(ra);
if (ra > P2 && ra < P3) {
rx = (((int)w->s_player.px / tileSize) * tileSize) - 0.0001;
ry = (w->s_player.px - rx) * nTan + w->s_player.py;
xo = -tileSize;
yo = -xo * nTan;
} else if (ra < P2 || ra > P3) {
rx = (((int)w->s_player.px / tileSize) * tileSize) + tileSize;
ry = (w->s_player.px - rx) * nTan + w->s_player.py;
xo = tileSize;
yo = -xo * nTan;
} else { // Rayon directement en haut ou en bas
rx = w->s_player.px;
ry = w->s_player.py;
dof = 35;
}
while (dof < 35) {
mx = (int)(rx) / tileSize;
my = (int)(ry) / tileSize;
mp = my * w->s_map.mapX + mx;
if (mp >= 0 && mp < w->s_map.mapX * w->s_map.mapY && w->s_map.map[mp] == '1') {
dof = 35;
*vx = rx;
*vy = ry;
*disV = dist(w->s_player.px, w->s_player.py, rx, ry);
if (ra > P2 && ra < P3) {
*wallDir = WEST;
} else {
*wallDir = EAST;
}
} else {
rx += xo;
ry += yo;
dof += 1;
}
}
//printf("tileSize: %d\n", tileSize);
//printf("Ray Angle (ra): %f\n", ra);
//printf("Vertical Ray: Angle = %f, Iteration = %d, rx = %f, ry = %f, disV = %f, wallDir = %d\n", ra, dof, rx, ry, *disV, *wallDir);
}
void calculateHorizontalRay(t_structure_main *w, float ra, float *disH, float *hx, float *hy, WallDirection *wallDir) {
int dof = 0, mx, my, mp;
float rx, ry, xo, yo;
int tileSize = w->s_map.mapS;
*disH = 100000; // Distance initialement grande
*wallDir = NONE;
float aTan = -1 / tan(ra);
if (ra > PI) {
ry = (((int)w->s_player.py / tileSize) * tileSize) - 0.0001;
rx = (w->s_player.py - ry) * aTan + w->s_player.px;
yo = -tileSize;
xo = -yo * aTan;
} else if (ra < PI) {
ry = (((int)w->s_player.py / tileSize) * tileSize) + tileSize;
rx = (w->s_player.py - ry) * aTan + w->s_player.px;
yo = tileSize;
xo = -yo * aTan;
} else { // Rayon directement à gauche ou à droite
rx = w->s_player.px;
ry = w->s_player.py;
dof = 35;
}
while (dof < 35) {
mx = (int)(rx) / tileSize;
my = (int)(ry) / tileSize;
mp = my * w->s_map.mapX + mx;
if (mp >= 0 && mp < w->s_map.mapX * w->s_map.mapY && w->s_map.map[mp] == '1') {
dof = 35;
*hx = rx;
*hy = ry;
*disH = dist(w->s_player.px, w->s_player.py, rx, ry);
if (ra > PI) {
*wallDir = NORTH;
} else {
*wallDir = SOUTH;
}
} else {
rx += xo;
ry += yo;
dof += 1;
}
}
//printf("tileSize: %d\n", tileSize);
//printf("Ray Angle (ra): %f\n", ra);
//printf("Horizontal Ray: Angle = %f, Iteration = %d, rx = %f, ry = %f, disH = %f, wallDir = %d\n", ra, dof, rx, ry, *disH, *wallDir);
}
void load_wall_textures(t_structure_main *w) {
// Initialiser la largeur et la hauteur des textures
int width = 0;
int height = 0;
// Charger la texture Nord et vérifier si elle est chargée correctement
w->s_img.north_texture = mlx_xpm_file_to_image(w->s_win.mlx, "textures/NO.xpm", &width, &height);
if (!w->s_img.north_texture) {
fprintf(stderr, "Failed to load North texture.\n");
exit_error(w); // Remplacez par votre propre gestion des erreurs
}
// Charger la texture Sud et vérifier si elle est chargée correctement
w->s_img.south_texture = mlx_xpm_file_to_image(w->s_win.mlx, "textures/SO.xpm", &width, &height);
if (!w->s_img.south_texture) {
fprintf(stderr, "Failed to load South texture.\n");
exit_error(w);
}
// Charger la texture Ouest et vérifier si elle est chargée correctement
w->s_img.west_texture = mlx_xpm_file_to_image(w->s_win.mlx, "textures/WE.xpm", &width, &height);
if (!w->s_img.west_texture) {
fprintf(stderr, "Failed to load West texture.\n");
exit_error(w);
}
// Charger la texture Est et vérifier si elle est chargée correctement
w->s_img.east_texture = mlx_xpm_file_to_image(w->s_win.mlx, "textures/EA.xpm", &width, &height);
if (!w->s_img.east_texture) {
fprintf(stderr, "Failed to load East texture.\n");
exit_error(w);
}
// Stocker la largeur et la hauteur pour une utilisation ultérieure
w->s_img.texture_width = width;
w->s_img.texture_height = height;
//printf("Texture Width: %d, Texture Height: %d\n", w->s_img.texture_width, w->s_img.texture_height);
//printf("All wall textures loaded successfully.\n");
}
void exit_error(t_structure_main *w) {
// Libérer les ressources, fermer la fenêtre, etc.
// ...
exit(1);
}
void draw_background(t_structure_main *w) {
// Vérifier si les valeurs de la fenêtre sont initialisées
if (w->s_win.height <= 0 || w->s_win.width <= 0) {
fprintf(stderr, "Erreur: Dimensions de la fenêtre non initialisées ou incorrectes.\n");
return;
}
// Largeur de décalage vers la droite
int backgroundOffsetX = 0; // Vous pouvez ajuster cette valeur selon votre besoin
int start3DHeight = 0;
int end3DHeight = w->s_win.height;
int half3DHeight = (end3DHeight - start3DHeight) / 2;
//printf("end3DHeight: %d\n", end3DHeight);
// Vérifier la cohérence des hauteurs calculées
if (half3DHeight <= 0) {
fprintf(stderr, "Erreur: Calcul de la moitié de la hauteur 3D incorrect.\n");
return;
}
int skyEndHeight = start3DHeight + half3DHeight;
// Dessiner le ciel (sky) avec un décalage vers la droite
draw_square_raw(w, backgroundOffsetX, start3DHeight, w->s_win.width + backgroundOffsetX, half3DHeight + start3DHeight, 0xB2FFFF);
// Dessiner le sol (ground) avec un décalage vers la droite
draw_square_raw(w, backgroundOffsetX, skyEndHeight, w->s_win.width + backgroundOffsetX, w->s_win.height, 0x280000);
}
int getTextureColor(t_structure_main *w, WallDirection wallDir, int textureX, int textureY) {
char *texture_data;
int bpp, size_line, endian;
// Sélectionner la texture en fonction de la direction du mur
void *selected_texture;
switch (wallDir) {
case NORTH:
selected_texture = w->s_img.north_texture;
break;
case SOUTH:
selected_texture = w->s_img.south_texture;
break;
case WEST:
selected_texture = w->s_img.west_texture;
break;
case EAST:
selected_texture = w->s_img.east_texture;
break;
default:
fprintf(stderr, "Invalid wall direction.\n");
exit_error(w); // Gestion des erreurs
return 0; // Retourne une couleur par défaut en cas d'erreur
}
// Obtenir l'adresse de la texture
texture_data = mlx_get_data_addr(selected_texture, &bpp, &size_line, &endian);
//printf("TextureX: %d, TextureY: %d\n", textureX, textureY);
//printf("BPP: %d, Size Line: %d, Endian: %d\n", bpp, size_line, endian);
// Calculer la position de la couleur dans la texture
int pixel_pos = (textureX + (textureY * w->s_img.texture_width)) * (bpp / 8);
//printf("Pixel Position: %d\n", pixel_pos);
// Récupérer la couleur de la texture à la position calculée
int color = *(int *)(texture_data + pixel_pos);
return color;
}
static void draw_texture(t_structure_main *w, int startX, int endX, int lineOff, int lineH, WallDirection wallDir, float rx, float ry, float disT) {
int textureWidth = w->s_img.texture_width;
int textureHeight = w->s_img.texture_height;
for (int y = lineOff; y < lineOff + lineH; y++) {
// La variable perspectiveFactor permet de mapper la texture en tenant compte de la perspective
float perspectiveFactor = (float)(y - lineOff) / lineH;
int textureY = perspectiveFactor * textureHeight;
if (textureY >= textureHeight) {
textureY = textureHeight - 1;
}
for (int x = startX; x < endX; x++) {
int textureX;
switch (wallDir) {
case NORTH:
case SOUTH:
// Assurez-vous que rx est normalisé correctement pour le mappage de texture
textureX = (int)(rx * textureWidth / w->s_map.mapS) % textureWidth;
break;
case WEST:
case EAST:
// Assurez-vous que ry est normalisé correctement pour le mappage de texture
textureX = (int)(ry * textureWidth / w->s_map.mapS) % textureWidth;
break;
}
if (textureX >= textureWidth) {
textureX = textureWidth - 1;
}
int color = getTextureColor(w, wallDir, textureX, textureY);
put_pixel_img(w, x, y, color);
}
}
}
void drawRay(t_structure_main *w, int r, float rx, float ry, float disT, WallDirection wallDir, int numRays, int color) {
int tileSize = w->s_map.mapS;
int start3DHeight = 0; // Début en haut de la fenêtre
int max3DHeight = w->s_win.height + start3DHeight;
// Assurez-vous que la fenêtre 3D ne dépasse pas la hauteur du background
/*if (max3DHeight > w->s_win.height) {
max3DHeight = w->s_win.height;
}*/
float lineH = (tileSize * max3DHeight) / disT;
lineH = lineH > max3DHeight ? max3DHeight : lineH;
float lineOff = ((max3DHeight - lineH) / 2);
printf("drawRay - rx: %f, ry: %f\n", rx, ry);
// Décalage horizontal de l'arrière-plan
int backgroundOffsetX = 0; // Vous pouvez ajuster cette valeur selon votre besoin
// Calculer les coordonnées de début et de fin du rayon ajustées pour le décalage
int rayWidth = w->s_win.width / numRays;
int startX = r * rayWidth;
int endX = startX + rayWidth;
// Ajouter le décalage horizontal à toutes les coordonnées X
startX += backgroundOffsetX;
endX += backgroundOffsetX;
//printf("lineH: %f, lineOff: %f, rayWidth: %d\n", lineH, lineOff, rayWidth);
// Ajouter des instructions de débogage pour afficher les valeurs de startX et endX
//printf("Ray %d - startX: %d, endX: %d\n", r, startX, endX);
printf("drawRay - rx: %f, ry: %f\n", rx, ry);
// Dessiner le rayon
draw_line(w, (int)w->s_player.px, (int)w->s_player.py, (int)rx + backgroundOffsetX, (int)ry, color);
// Dessiner la texture ajustée pour le décalage
draw_texture(w, startX, endX, lineOff, lineH, wallDir, rx + backgroundOffsetX, ry, disT);
}
void drawRays2D(t_structure_main *w) {
int r, color;
float ra, disH, disV, disT, hx, hy, vx, vy;
WallDirection hWallDir, vWallDir;
int tileSize = w->s_map.mapS;
int numRays = 1280;
float FOV = 60 * (PI / 180);
float DR = FOV / numRays;
// Ajouter des logs pour vérifier les valeurs
//printf("numRays: %d\n", numRays);
//printf("FOV: %f\n", FOV);
//printf("DR: %f\n", DR);
draw_background(w);
ra = w->s_player.pa - (FOV / 2);
for (r = 0; r < numRays; r++) {
ra = fmod(ra + 2 * PI, 2 * PI); // Maintenir ra dans l'intervalle [0, 2*PI]
calculateHorizontalRay(w, ra, &disH, &hx, &hy, &hWallDir);
calculateVerticalRay(w, ra, &disV, &vx, &vy, &vWallDir);
disT = (disH < disV) ? disH : disV;
color = (disH < disV) ? 0xFF0000 : 0x00FF00;
WallDirection wallDir = (disH < disV) ? hWallDir : vWallDir;
disT = correctFisheye(disT, ra, w->s_player.pa);
// Ajouter des logs pour déboguer
//printf("Ray ID: %d\n", r);
//printf("Ray Angle: %f\n", ra);
//printf("Shortest Distance: %f\n", disT);
//printf("Wall Direction: %d\n", wallDir);
drawRay(w, r, (disH < disV) ? hx : vx, (disH < disV) ? hy : vy, disT, wallDir, numRays, color);
//draw_map(w);
ra += DR;
}
}
void draw_map(t_structure_main *w) {
if (!w->s_map.map) {
printf("Erreur: La carte n'a pas été chargée correctement.\n");
return;
}
//printf("Drawing map of size: %d x %d\n", w->s_map.mapX, w->s_map.mapY);
for (int y = 0; y < w->s_map.mapY; y++) {
int lineLength = 0; // Longueur réelle de la ligne
while (lineLength < w->s_map.mapX && w->s_map.map[y * w->s_map.mapX + lineLength] != '\n' && w->s_map.map[y * w->s_map.mapX + lineLength] != '\0') {
lineLength++;
}
for (int x = 0; x < lineLength; x++) {
int index = y * w->s_map.mapX + x;
if (index >= w->s_map.mapX * w->s_map.mapY) {
fprintf(stderr, "Erreur: Tentative d'accès hors limites de la carte à l'indice %d.\n", index);
continue;
}
int color;
float alpha; // Définissez la transparence ici (0.0 pour totalement transparent, 1.0 pour totalement opaque)
switch (w->s_map.map[index]) {
case '1':
color = 0xFFFFFF;
break;
case '0':
color = 0x000000;
break;
default:
color = 0x000000;
break;
}
//printf("Drawing square at (x: %d, y: %d) with color: 0x%06X\n", x, y, color);
draw_square(w, x, y, color);
}
}
}
int jkl = -1;
int yui = 0;
void test2(t_structure_main *w) {
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);
void test2(t_structure_main *w)
{
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);
//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;
// Log des valeurs pour le débogage
/*printf("Sprite (x, y): (%d, %d), Size (w, h): (%d, %d), MapS: %d\n",
sprite_x, sprite_y, new_sprite_width, new_sprite_height, w->s_map.mapS);*/
// Redimensionner et dessiner le sprite du personnage
rescale_image(w->s_win.mlx, w->s_win.win, w->s_img.roomadslam[jkl],
112, 112, new_sprite_width, new_sprite_height, sprite_x, sprite_y, 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)
void test(t_structure_main *w)
{
int x;
int y;
//////////gettimeofday(&(w->start_time), NULL);
if (yui < 10)
{
usleep(1000);
@ -577,7 +48,6 @@ void test(t_structure_main *w)
yui = 0;
jkl++;
test2(w);
//mlx_mouse_move(w->s_win.mlx, w->s_win.win, w->s_win.height/2, w->s_win.width/2);
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)
@ -592,116 +62,41 @@ void test(t_structure_main *w)
jkl = -1;
}
void init_windows(t_structure_main *w)
int setup_and_load_map(int argc, char **argv, t_structure_main *w)
{
int temp;
w->s_win.width = 1280;
w->s_win.height = 720;
w->s_win.mlx = mlx_init();
if (w->s_win.mlx == NULL) {
fprintf(stderr, "Erreur : Échec de mlx_init.\n");
return; // Ajoutez un retour ou une gestion d'erreur appropriée
if (argc != 2)
{
fprintf(stderr, "Usage: %s <map_file>\n", argv[0]);
return (0);
}
w->s_win.win = mlx_new_window(w->s_win.mlx, w->s_win.width, w->s_win.height, "WF99");
if (w->s_win.win == NULL) {
fprintf(stderr, "Erreur : Échec de mlx_new_window.\n");
return; // Ajoutez un retour ou une gestion d'erreur appropriée
parse_map("map.cub", &w->s_map);
if (!parse_map(argv[1], &w->s_map))
{
printf("Failed to load the map or map is not closed. Exiting...\n");
return (0);
}
w->s_player.px = 10;
w->s_player.py = 10;
w->s_player.pa = 0.1;
w->s_player.pdx = cos(w->s_player.pa) * 5;
w->s_player.pdy = sin(w->s_player.pa) * 5;
load_wall_textures(w);
// Après le chargement des textures
printf("Window Dimensions: Width = %d, Height = %d\n", w->s_win.width, w->s_win.height);
printf("Texture Dimensions: Width = %d, Height = %d\n", w->s_img.texture_width, w->s_img.texture_height);
w->s_img.img_player = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/player.xpm", &temp,&temp);
w->s_img.img_wall = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/wall.xpm", &temp,&temp);
w->s_img.buffer = mlx_new_image(w->s_win.mlx, w->s_win.width, w->s_win.height);
// Vérifiez ensuite que ces dimensions correspondent à la taille de la fenêtre
if (w->s_img.buffer == NULL) {
fprintf(stderr, "Erreur : Échec de mlx_new_image pour le buffer.\n");
return; // Gestion d'erreur appropriée
} else {
// Vérification des dimensions du buffer
int bpp, size_line, endian;
char *buffer_addr = mlx_get_data_addr(w->s_img.buffer, &bpp, &size_line, &endian);
if (buffer_addr == NULL) {
fprintf(stderr, "Erreur : Impossible d'obtenir l'adresse du buffer.\n");
} else {
fprintf(stdout, "Buffer créé avec Largeur: %d, Hauteur: %d\n", w->s_win.width, w->s_win.height);
fprintf(stdout, "BPP (bits per pixel) : %d, Size line : %d, Endian : %d\n", bpp, size_line, endian);
}
if (w->s_map.map == NULL)
{
printf("Failed to load the map.\n");
return (0);
}
return (1);
}
// Initialiser le compteur de FPS
gettimeofday(&(w->start_time), NULL);
w->end_time = w->start_time;
w->frame_count = 0;
int main(int argc, char **argv)
{
t_structure_main w;
int mapS_x = w->s_win.width / (w->s_map.mapX * 4);
int mapS_y = w->s_win.height / (w->s_map.mapY * 2);
w->s_map.mapS = (mapS_x < mapS_y) ? mapS_x : mapS_y;
w->s_img.roomadslam[0] = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/roomadslam/frame_02_delay-0.1s.xpm", &temp,&temp);
w->s_img.roomadslam[1] = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/roomadslam/frame_03_delay-0.1s.xpm", &temp,&temp);
w->s_img.roomadslam[2] = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/roomadslam/frame_04_delay-0.1s.xpm", &temp,&temp);
w->s_img.roomadslam[3] = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/roomadslam/frame_05_delay-0.05s.xpm", &temp,&temp);
w->s_img.roomadslam[4] = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/roomadslam/frame_06_delay-0.05s.xpm", &temp,&temp);
w->s_img.roomadslam[5] = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/roomadslam/frame_07_delay-0.05s.xpm", &temp,&temp);
w->s_img.roomadslam[6] = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/roomadslam/frame_08_delay-0.05s.xpm", &temp,&temp);
w->s_img.roomadslam[7] = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/roomadslam/frame_09_delay-0.05s.xpm", &temp,&temp);
w->s_img.roomadslam[8] = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/roomadslam/frame_10_delay-0.05s.xpm", &temp,&temp);
w->s_img.roomadslam[9] = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/roomadslam/frame_11_delay-0.05s.xpm", &temp,&temp);
w->s_img.roomadslam[10] = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/roomadslam/frame_12_delay-0.05s.xpm", &temp,&temp);
w->s_img.pedro_wall = mlx_xpm_file_to_image(w->s_win.mlx, "sprite/pedrop/frame_061_delay-0.03s.xpm", &temp,&temp);
//if (!w->s_img.pedro_wall)
// printf("nop\n");
}
int main(int argc, char **argv) {
t_structure_main w;
// Vérifier si un nom de fichier a été fourni
if (argc < 2) {
printf("Usage: %s <path to map file>\n", argv[0]);
return 1;
}
// Parser la carte
parse_map(argv[1], &w.s_map);
if (!parse_map(argv[1], &w.s_map)) {
printf("Failed to load the map or map is not closed. Exiting...\n");
return 1; // Quitter avec un code d'erreur
}
// Vérifier si la carte a été chargée correctement
if (w.s_map.map == NULL) {
printf("Failed to load the map.\n");
return 1;
}
// Initialiser la fenêtre et d'autres composants
if (!setup_and_load_map(argc, argv, &w))
return (1);
init_windows(&w);
// Configurer les hooks et entrer dans la boucle principale
mlx_loop_hook(w.s_win.mlx, (void *)test, &w);
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);
// Libérer la carte une fois que vous avez terminé
if (w.s_map.map) {
free(w.s_map.map);
}
return 0;
mlx_loop_hook(w.s_win.mlx, (void *)test, &w);
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)
{
free(w.s_map.map);
}
return (0);
}

46
map.cub
View File

@ -1,23 +1,23 @@
11111111111111111111
10000100000000000001
100001000000000000011111
100001000000000000000001
100001111111111110000001
1000000000000000100000011111111111
1000000000000000100000000000000001
1011110000000000100000000000000001
101 10000000000100000000011111111
101111100000000011111000001
1000001000000000000010000011111111111111111111111
1000001000000000000010000000000000000000000000001
1000001000000000000011111111111111111111110000001
1000001000000000000010000000000000000000000000001
1000001000000000000010000000000000000000000000001
1000000000000000000000000011111111111111111111111
100000000000000000000000001
100000000000000000000000001
100000000000000000000000001
100011110001110000000000001
10001 10001 10000000000001
11111 10001 10000000000001
11111 11111111111111
1111111 11111111111111111111 11111111111 11111111111111111111111111111111111111111111111111111111111111
111111000001110000000000000000001 10000000001 10000000000000000000000000000000001111111111111111111111111111
1111100000000000000000000000000000001 10000000001 10000000000000000000000000000000000000000000000000000000000001
1000000000000000000000011111111111111 10000000001 10000000000000000000000000000000000000000000000000000000000001111
1000000000000000000000011111111111111111110000000001 10000000000000000000000000000000001111111100000000000000000000001
100000000000000000000001111111111111111111000000000111110000000000000000000000000000000001 100000000000000000000001
100000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 111111111111000000000001
100000000000000000111100000000000000000000000000000000000000000000000000000000000000000001 1000000000001
1000000000000000001 100000000000000000000000000000000000000000000000000000000000011111111 1000000000001
100000000000000000111100000000000000000000000000000000000000000000000000000000000011111111111111111111111 1000000000001
100000100000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111 1000000000001
110000100000011111100000000000000000000000000000000000000000000000000000000000000000000000000000000000001 1000000000001
10000100000011 1100000000000000000000000000000000000000000000000000000000000000000000000000000000000001 1000000000001
110000100000011 1100000000000000000000000000000000000000000000000000000000000000000000000000000000000001 1000000000001
100000100000011111100000000000000000000000000000000000000000000000000000000000000000000000000000000000001 1000000000001
100000000000000000000000000000000000000000000000000000000000000000000000000000000011111111111111111111111 1000000000001
100000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111 1000000000001
10000000000000000000000000111111111111111111111111111111111111111111111111111111111 1000000000001
100000000000000000000000001111111111111111111111111111111111111111111111111111111111111111111111111111111111000000000001
100000000001110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
100011110001 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
11111 10001 10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
11111 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111

104
memo.txt
View File

@ -1,104 +0,0 @@
void *mlx_init();
/*
** needed before everything else.
** return (void *)0 if failed
*/
/*
** Basic actions
*/
void *mlx_new_window(void *mlx_ptr, int size_x, int size_y, char *title);
/*
** return void *0 if failed
*/
int mlx_clear_window(void *mlx_ptr, void *win_ptr);
int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color);
/*
** origin for x & y is top left corner of the window
** y down is positive
** color is 0x00RRGGBB
*/
/*
** Image stuff
*/
void *mlx_new_image(void *mlx_ptr,int width,int height);
/*
** return void *0 if failed
** obsolete : image2 data is stored using bit planes
** void *mlx_new_image2(void *mlx_ptr,int width,int height);
*/
char *mlx_get_data_addr(void *img_ptr, int *bits_per_pixel,
int *size_line, int *endian);
/*
** endian : 0 = sever X is little endian, 1 = big endian
** for mlx_new_image2, 2nd arg of mlx_get_data_addr is number_of_planes
*/
int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr,
int x, int y);
int mlx_get_color_value(void *mlx_ptr, int color);
/*
** dealing with Events
*/
int mlx_mouse_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_key_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_expose_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_loop_hook (void *mlx_ptr, int (*funct_ptr)(), void *param);
int mlx_loop (void *mlx_ptr);
int mlx_loop_end (void *mlx_ptr);
/*
** hook funct are called as follow :
**
** expose_hook(void *param);
** key_hook(int keycode, void *param);
** mouse_hook(int button, int x,int y, void *param);
** loop_hook(void *param);
**
*/
/*
** Usually asked...
*/
int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color,
char *string);
void mlx_set_font(void *mlx_ptr, void *win_ptr, char *name);
void *mlx_xpm_to_image(void *mlx_ptr, char **xpm_data,
int *width, int *height);
void *mlx_xpm_file_to_image(void *mlx_ptr, char *filename,
int *width, int *height);
int mlx_destroy_window(void *mlx_ptr, void *win_ptr);
int mlx_destroy_image(void *mlx_ptr, void *img_ptr);
int mlx_destroy_display(void *mlx_ptr);
/*
** generic hook system for all events, and minilibX functions that
** can be hooked. Some macro and defines from X11/X.h are needed here.
*/
int mlx_hook(void *win_ptr, int x_event, int x_mask,
int (*funct)(), void *param);
int mlx_do_key_autorepeatoff(void *mlx_ptr);
int mlx_do_key_autorepeaton(void *mlx_ptr);
int mlx_do_sync(void *mlx_ptr);
int mlx_mouse_get_pos(void *mlx_ptr, void *win_ptr, int *x, int *y);
int mlx_mouse_move(void *mlx_ptr, void *win_ptr, int x, int y);
int mlx_mouse_hide(void *mlx_ptr, void *win_ptr);
int mlx_mouse_show(void *mlx_ptr, void *win_ptr);
int mlx_get_screen_size(void *mlx_ptr, int *sizex, int *sizey);

50
move/ft_collision.c Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_collision.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:09:52 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 20:35:31 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void calculate_future_position(t_position_params *params)
{
*(params->future_px) = params->px + params->pdx + params->collisionBuffer;
*(params->future_py) = params->py + params->pdy + params->collisionBuffer;
}
int check_collision(t_structure_main *w, int future_px, int future_py)
{
int future_ipx;
int future_ipy;
future_ipx = future_px / w->s_map.mapS;
future_ipy = future_py / w->s_map.mapS;
return (w->s_map.map[future_ipy * w->s_map.mapX + future_ipx] == '0');
}
void calculate_future_position_right_left(t_position_params *params)
{
double angle;
int collision_px;
int collision_py;
angle = params->pa;
if (params->direction == 'd')
angle += M_PI_2;
else
angle -= M_PI_2;
*(params->future_px) = params->px + (int)(cos(angle) * 5);
*(params->future_py) = params->py + (int)(sin(angle) * 5);
collision_px = *(params->future_px) + (int)(cos(params->pa)
* params->collisionBuffer);
collision_py = *(params->future_py) + (int)(sin(params->pa)
* params->collisionBuffer);
*(params->future_px) = collision_px;
*(params->future_py) = collision_py;
}

85
move/ft_key.c Normal file
View File

@ -0,0 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_key.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:17:56 by fgras-ca #+# #+# */
/* Updated: 2024/01/12 20:28:07 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
int *kill_prog(t_structure_main *w)
{
int i;
mlx_destroy_window(w->s_win.mlx, w->s_win.win);
mlx_destroy_image(w->s_win.mlx, w->s_img.buffer);
mlx_destroy_image(w->s_win.mlx, w->s_img.img_player);
mlx_destroy_image(w->s_win.mlx, w->s_img.img_wall);
mlx_destroy_image(w->s_win.mlx, w->s_img.pedro_wall);
i = -1;
while (++i <= 10)
mlx_destroy_image(w->s_win.mlx, w->s_img.roomadslam[i]);
if (w->s_map.map)
{
free(w->s_map.map);
w->s_map.map = NULL;
}
mlx_destroy_display(w->s_win.mlx);
free(w->s_win.mlx);
exit(0);
return (0);
}
void adjust_player_angle(t_structure_main *w, int key)
{
double angle_adjustment;
if (key != 65361 && key != 65363)
return ;
angle_adjustment = 16 * ((PI / 3) / NUMRAY);
if (key == 65361)
{
w->s_player.pa -= angle_adjustment;
if (w->s_player.pa < 0)
w->s_player.pa += 2 * PI;
}
else
{
w->s_player.pa += angle_adjustment;
if (w->s_player.pa > 2 * PI)
w->s_player.pa -= 2 * PI;
}
w->s_player.pdx = cos(w->s_player.pa) * 5;
w->s_player.pdy = sin(w->s_player.pa) * 5;
}
void handle_movement_keys(int key, t_structure_main *w)
{
if (key == 119 || key == 100 || key == 115 || key == 97)
move(key, w);
}
int deal_key(int key, t_structure_main *w)
{
if (key == 65307)
{
kill_prog(w);
}
else if (key == 65361 || key == 65363)
{
adjust_player_angle(w, key);
}
else if (key == 65362 || key == 65364 || key == 114 || key == 102)
{
}
else
{
handle_movement_keys(key, w);
}
return (key);
}

113
move/ft_move.c Normal file
View File

@ -0,0 +1,113 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_move.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:08:25 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 20:35:09 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void move_forward(t_structure_main *w, int key)
{
t_position_params params;
int future_px;
int future_py;
if (key != 119)
return ;
params.future_px = &future_px;
params.future_py = &future_py;
params.pdx = w->s_player.pdx;
params.pdy = w->s_player.pdy;
params.collisionBuffer = COLBUF;
params.px = w->s_player.px;
params.py = w->s_player.py;
calculate_future_position(&params);
if (check_collision(w, future_px, future_py))
{
w->s_player.px += w->s_player.pdx;
w->s_player.py += w->s_player.pdy;
}
}
void move_backward(t_structure_main *w, int key)
{
t_position_params params;
int future_px;
int future_py;
if (key != 115)
return ;
params.future_px = &future_px;
params.future_py = &future_py;
params.pdx = -w->s_player.pdx;
params.pdy = -w->s_player.pdy;
params.collisionBuffer = COLBUF;
params.px = w->s_player.px;
params.py = w->s_player.py;
calculate_future_position(&params);
if (check_collision(w, future_px, future_py))
{
w->s_player.px -= w->s_player.pdx;
w->s_player.py -= w->s_player.pdy;
}
}
void move_right(t_structure_main *w, int key)
{
t_position_params params;
int future_px;
int future_py;
if (key != 100)
return ;
params.future_px = &future_px;
params.future_py = &future_py;
params.pa = w->s_player.pa;
params.collisionBuffer = COLBUF;
params.px = w->s_player.px;
params.py = w->s_player.py;
params.direction = 'd';
calculate_future_position_right_left(&params);
if (check_collision(w, future_px, future_py))
{
w->s_player.px = future_px;
w->s_player.py = future_py;
}
}
void move_left(t_structure_main *w, int key)
{
t_position_params params;
int future_px;
int future_py;
if (key != 97)
return ;
params.future_px = &future_px;
params.future_py = &future_py;
params.pa = w->s_player.pa;
params.collisionBuffer = COLBUF;
params.px = w->s_player.px;
params.py = w->s_player.py;
params.direction = 'a';
calculate_future_position_right_left(&params);
if (check_collision(w, future_px, future_py))
{
w->s_player.px = future_px;
w->s_player.py = future_py;
}
}
void move(int key, t_structure_main *w)
{
move_forward(w, key);
move_backward(w, key);
move_right(w, key);
move_left(w, key);
}

110
parsing/ft_map_check.c Normal file
View File

@ -0,0 +1,110 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:49:45 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 20:02:42 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
int is_map_closed(char *map, int maxWidth, int height)
{
if (!check_borders(map, maxWidth, height))
return (0);
if (!check_interior(map, maxWidth, height))
return (0);
return (1);
}
int check_line_border(char *line, int lineLength, int y)
{
if (lineLength == 0)
return (1);
if (line[0] != '1')
return (0);
if (line[lineLength - 1] != '1')
return (0);
return (1);
}
int check_horizontal_borders(char *map, int maxWidth, int height)
{
int x;
x = 0;
while (x < maxWidth)
{
if (map[x] != '1' && map[x] != ' ')
{
printf("Border issue detected at top edge at (%d,0)\n", x);
return (0);
}
if (map[(height - 1) * maxWidth + x] != '1'
&& map[(height - 1) * maxWidth + x] != ' ')
return (0);
x++;
}
return (1);
}
int check_vertical_borders(char *map, int maxWidth, int height)
{
int x;
int columnheight;
x = 0;
while (x < maxWidth)
{
columnheight = 0;
while (columnheight < height && map[columnheight * maxWidth + x] != ' ')
{
columnheight++;
}
if (columnheight > 0)
{
if (map[x] != '1')
{
printf("Border issue detected at top of column at (%d,0)\n", x);
return (0);
}
if (map[(columnheight - 1) * maxWidth + x] != '1')
return (0);
}
x++;
}
return (1);
}
int check_borders(char *map, int maxWidth, int height)
{
int y;
int linelength;
int x;
y = 0;
while (y < height)
{
linelength = 0;
x = 0;
while (x < maxWidth && map[y * maxWidth + x] != ' ')
{
linelength++;
x++;
}
if (!check_line_border(&map[y * maxWidth], linelength, y))
{
return (0);
}
y++;
}
if (!check_horizontal_borders(map, maxWidth, height))
{
return (0);
}
return (check_vertical_borders(map, maxWidth, height));
}

View File

@ -0,0 +1,99 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_dimensions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:17:19 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:19:14 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void update_max_dimensions(t_map_params *params)
{
(*(params->height))++;
if (*(params->currentWidth) > *(params->maxWidth))
{
*(params->maxWidth) = *(params->currentWidth);
}
*(params->currentWidth) = 0;
*(params->isNewLine) = 1;
}
void process_character(t_map_params *params, int *i)
{
if (params->buffer[*i] == '\n')
{
update_max_dimensions(params);
}
else if (params->buffer[*i] != '\r')
{
(*(params->currentWidth))++;
*(params->isNewLine) = 0;
}
(*i)++;
}
void get_map_dimensions(t_map_params *params)
{
int i;
int currentwidth;
int isnewline;
i = 0;
currentwidth = 0;
isnewline = 1;
*(params->maxWidth) = 0;
*(params->height) = 0;
params->currentWidth = &currentwidth;
params->isNewLine = &isnewline;
while (i < params->length)
process_character(params, &i);
if (!*(params->isNewLine))
update_max_dimensions(params);
printf("Map dimensions: maxWidth=%d, height=%d\n",
*(params->maxWidth), *(params->height));
}
void fill_map_space(t_structure_map *map_info, int maxWidth, int height)
{
int i;
int totalsize;
totalsize = maxWidth * height;
i = 0;
while (i < totalsize)
{
map_info->map[i] = ' ';
i++;
}
}
void copy_map_data(t_map_params *params)
{
int x;
int y;
int i;
x = 0;
y = 0;
i = 0;
while (i < params->length)
{
if (params->buffer[i] == '\n')
{
y++;
x = 0;
}
else if (params->buffer[i] != '\r' && x < *params->maxWidth)
{
params->map_info->map[y * *params->maxWidth + x]
= params->buffer[i];
x++;
}
i++;
}
}

96
parsing/ft_parsing.c Normal file
View File

@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:43:46 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:17:53 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
bool init_map_info(t_structure_map *map_info, int maxWidth, int height)
{
map_info->mapX = maxWidth;
map_info->mapY = height;
map_info->map = malloc(maxWidth * height);
if (!map_info->map)
{
perror("Failed to allocate memory for map");
return (false);
}
fill_map_space(map_info, maxWidth, height);
return (true);
}
bool copy_map_data_and_check(t_map_params *params)
{
copy_map_data(params);
if (!is_map_closed(params->map_info->map,
*params->maxWidth, *params->height))
{
printf("Map is not closed!\n");
free(params->map_info->map);
return (false);
}
return (true);
}
bool read_and_verify_map(const char *filename, char **buffer, int *length)
{
*length = 0;
*buffer = read_map(filename, length);
if (!(*buffer))
{
printf("Failed to read the map file.\n");
return (false);
}
return (true);
}
bool process_map(const char *buffer, int length, t_structure_map *map_info)
{
int maxwidth;
int height;
t_map_params map_params;
maxwidth = 0;
height = 0;
map_params.map_info = map_info;
map_params.buffer = buffer;
map_params.length = length;
map_params.maxWidth = &maxwidth;
map_params.height = &height;
map_params.currentWidth = NULL;
map_params.isNewLine = NULL;
get_map_dimensions(&map_params);
if (maxwidth <= 0 || height <= 0)
{
printf("Invalid map dimensions: maxWidth=%d, height=%d\n",
maxwidth, height);
return (false);
}
if (!init_map_info(map_info, maxwidth, height))
{
return (false);
}
return (copy_map_data_and_check(&map_params));
}
bool parse_map(const char *filename, t_structure_map *map_info)
{
char *buffer;
int length;
bool result;
if (!read_and_verify_map(filename, &buffer, &length))
{
return (false);
}
result = process_map(buffer, length, map_info);
free(buffer);
return (result);
}

88
parsing/ft_read_map.c Normal file
View File

@ -0,0 +1,88 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_read_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 19:28:43 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 20:02:52 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
char *read_map(const char *filename, int *length)
{
char *buffer;
FILE *file;
file = fopen(filename, "r");
if (!file)
{
perror("Error opening file");
return (NULL);
}
fseek(file, 0, SEEK_END);
*length = ftell(file);
fseek(file, 0, SEEK_SET);
buffer = malloc(*length + 1);
if (!buffer)
{
perror("Error allocating memory for map");
fclose(file);
return (NULL);
}
fread(buffer, 1, *length, file);
buffer[*length] = '\0';
fclose(file);
return (buffer);
}
static int check_line(char *map, int maxWidth, int y, int linelength)
{
int x;
char c;
x = 1;
while (x < linelength - 1)
{
c = map[y * maxWidth + x];
if (c == ' ' && (map[y * maxWidth + (x - 1)] != '1'
|| map[y * maxWidth + (x + 1)] != '1'
|| (y > 0 && map[(y - 1) * maxWidth + x] != '1')
|| (y < maxWidth - 1 && map[(y + 1) * maxWidth + x] != '1')))
return (0);
x++;
}
return (1);
}
static int check_row(char *map, int maxWidth, int y)
{
int x;
int linelength;
x = 0;
linelength = 0;
while (x < maxWidth && map[y * maxWidth + x] != ' ')
{
linelength++;
x++;
}
return (check_line(map, maxWidth, y, linelength));
}
int check_interior(char *map, int maxWidth, int height)
{
int y;
y = 0;
while (y < height)
{
if (!check_row(map, maxWidth, y))
return (0);
y++;
}
return (1);
}

71
test.c
View File

@ -1,71 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include "include/mlx.h"
struct s_
{
void *mlx;
void *win;
void *wall;
}s_;
void key(int key)
{
printf("_key_ %d\n", key);
if (key == 65307)
exit(0);
}
void next_frame()
{
}
int main(void)
{
int test;
int width;
int height;
int bits_per_pixel;
int size_line;
int endian;
s_.mlx = mlx_init();
s_.win = mlx_new_window(s_.mlx, 400, 200, "Test");
s_.wall = mlx_xpm_file_to_image(s_.mlx, "sprite/pedrop/frame_061_delay-0.03s.xpm", &width, &height);
if (!s_.wall)
printf("deded\n");
char *mlx_data_addr = mlx_get_data_addr(s_.wall, &bits_per_pixel, &size_line, &endian);
int pos = (10 * size_line + 10 * (bits_per_pixel / 8));
char *dst = mlx_data_addr + pos;
printf("-------%s-------\n",dst);
int i = -1;
while (++i < 4)
{
printf("%d hex__%x\n", i, dst[i]);
// printf("%d int__%d\n", i, dst[i]);
}
int color = 0x10ff99;
*(unsigned int *)dst = color;
printf("----%s----\n",dst);
i = -1;
while (++i < 4)
{
printf("%d hex__%x\n", i, dst[i]);
// printf("%d int__%d\n", i, dst[i]);
}
mlx_put_image_to_window(s_.mlx, s_.win, s_.wall, 0,0);
mlx_hook(s_.win, 2, 1L<<0, (void *)key, NULL);
mlx_loop_hook(s_.mlx, (void*)next_frame, NULL);
mlx_loop(s_.mlx);
return (0);
}

View File

@ -1 +0,0 @@
cc -Iinclude test.c -Llib -lmlx -lXext -lX11 -lm -o test && ./test && rm test

View File

@ -1,8 +0,0 @@
1 1 1 1 1 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 0 0 0 1
1 0 0 0 N 0 0 1
1 0 0 0 0 0 0 1
1 1 1 1 1 1 1 1

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

61
utils/ft_utils_convert.c Normal file
View File

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils_convert.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:21:26 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:23:53 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
static size_t ft_intlen(long n)
{
size_t len;
len = 0;
if (n == 0)
len++;
if (n < 0)
{
n *= -1;
len++;
}
while (n > 0)
{
n = n / 10;
len++;
}
return (len);
}
char *ft_itoa(int nb)
{
char *dst;
int i;
long n;
n = nb;
i = ft_intlen(n);
dst = (char *)malloc((i + 1) * sizeof(char));
if (!dst)
return (NULL);
dst[i--] = '\0';
if (n == 0)
dst[0] = '0';
if (n < 0)
{
dst[0] = '-';
n = n * -1;
}
while (n > 0)
{
dst[i] = '0' + (n % 10);
n = n / 10;
i--;
}
return (dst);
}

View File

@ -1,4 +1,16 @@
#include "cub3d.h"
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils_gnl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:21:44 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:24:01 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
char *ft_get_line(char *left_str)
{
@ -91,4 +103,4 @@ char *get_next_line(int fd)
line = ft_get_line(left_str);
left_str = ft_new_left_str(left_str);
return (line);
}
}

View File

@ -1,4 +1,16 @@
#include "cub3d.h"
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:21:53 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:24:06 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
static size_t words_count(char *s, char c)
{

View File

@ -1,4 +1,16 @@
#include "cub3d.h"
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils_str_1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 22:22:01 by fgras-ca #+# #+# */
/* Updated: 2024/01/11 22:24:10 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
size_t ft_strlen(const char *s)
{

1
v.sh
View File

@ -1 +0,0 @@
make && make clean && clear && ./cub3d

65
window/ft_map.c Normal file
View File

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 20:19:01 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 20:28:31 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void draw_map_line(t_structure_main *w, int y, int lineLength)
{
int x;
int index;
int color;
x = 0;
while (x < lineLength)
{
index = y * w->s_map.mapX + x;
if (w->s_map.map[index] == '1')
{
color = 0xFFFFFF;
}
else if (w->s_map.map[index] == '0')
{
color = 0x000000;
}
else
{
color = 0x000000;
}
draw_square(w, x, y, color);
x++;
}
}
void draw_map(t_structure_main *w)
{
int y;
int linelength;
y = 0;
if (!w->s_map.map)
{
printf("Erreur: La carte n'a pas été chargée correctement.\n");
return ;
}
while (y < w->s_map.mapY)
{
linelength = 0;
while (linelength < w->s_map.mapX
&& w->s_map.map[y * w->s_map.mapX + linelength]
!= '\n' && w->s_map.map[y * w->s_map.mapX + linelength] != '\0')
{
linelength++;
}
draw_map_line(w, y, linelength);
y++;
}
}

39
window/ft_player.c Normal file
View File

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_player.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 20:09:23 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 20:16:05 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void init_player(t_structure_main *w)
{
w->s_player.px = 10;
w->s_player.py = 10;
w->s_player.pa = 0.1;
w->s_player.pdx = cos(w->s_player.pa) * 5;
w->s_player.pdy = sin(w->s_player.pa) * 5;
}
void init_mlx_and_window(t_structure_main *w)
{
w->s_win.mlx = mlx_init();
if (w->s_win.mlx == NULL)
{
fprintf(stderr, "Erreur : Échec de mlx_init.\n");
exit(1);
}
w->s_win.win = mlx_new_window(w->s_win.mlx, w->s_win.width,
w->s_win.height, "WF99");
if (w->s_win.win == NULL)
{
fprintf(stderr, "Erreur : Échec de mlx_new_window.\n");
exit(1);
}
}

92
window/ft_rescale.c Normal file
View File

@ -0,0 +1,92 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_rescale.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 21:29:08 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 23:10:00 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void initialize_res_params(t_rescale_params *params, t_structure_main *w)
{
if (!params || !w)
{
fprintf(stderr,
"Erreur: paramètres invalides pour initialize_res_params.\n");
return ;
}
params->res = malloc(sizeof(t_res_params));
if (!params->res)
{
perror("Erreur d'allocation mémoire pour t_res_params");
return ;
}
params->res->rescaled_img = mlx_new_image(w->s_win.mlx,
params->new_width, params->new_height);
if (!params->res->rescaled_img)
{
perror("Erreur lors de la création de la nouvelle image");
return ;
}
params->res->original_data = mlx_get_data_addr(params->original_img,
&(w->s_img.bpp), &(w->s_img.line_len), &(w->s_img.endian));
params->res->rescaled_data = mlx_get_data_addr(params->res->rescaled_img,
&(w->s_img.bpp), &(w->s_img.line_len), &(w->s_img.endian));
params->res->x_ratio = (float)params->original_width / params->new_width;
params->res->y_ratio = (float)params->original_height / params->new_height;
}
void copy_pixel_data(t_res_params *res)
{
res->rescaled_data[res->pixel_pos_rescaled]
= res->original_data[res->pixel_pos_original];
res->rescaled_data[res->pixel_pos_rescaled + 1]
= res->original_data[res->pixel_pos_original + 1];
res->rescaled_data[res->pixel_pos_rescaled + 2]
= res->original_data[res->pixel_pos_original + 2];
res->rescaled_data[res->pixel_pos_rescaled + 3]
= res->original_data[res->pixel_pos_original + 3];
}
int validate_parameters(t_rescale_params *params, t_structure_main *w)
{
if (!params || !w)
{
fprintf(stderr, "Erreur: paramètres invalides.\n");
return (0);
}
return (1);
}
void rescale_image(t_rescale_params *params, t_structure_main *w)
{
int x;
int y;
if (!validate_parameters(params, w))
return ;
initialize_res_params(params, w);
y = 0;
while (y++ < params->new_height)
{
x = 0;
while (x++ < params->new_width)
{
params->res->original_x = (int)(params->res->x_ratio * x);
params->res->original_y = (int)(params->res->y_ratio * y);
params->res->pixel_pos_rescaled = (y * params->new_width + x) * 4;
params->res->pixel_pos_original = (params->res->original_y
* params->original_width + params->res->original_x) * 4;
copy_pixel_data(params->res);
}
}
mlx_put_image_to_window(w->s_win.mlx, w->s_win.win,
params->res->rescaled_img, params->px, params->py);
mlx_destroy_image(w->s_win.mlx, params->res->rescaled_img);
free(params->res);
}

106
window/ft_window.c Normal file
View File

@ -0,0 +1,106 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_window.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 19:15:53 by fgras-ca #+# #+# */
/* Updated: 2024/01/14 20:15:35 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d.h"
void load_roomadslam_textures(t_structure_main *w, int *temp)
{
w->s_img.roomadslam[0] = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/roomadslam/frame_02_delay-0.1s.xpm", temp, temp);
w->s_img.roomadslam[1] = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/roomadslam/frame_03_delay-0.1s.xpm", temp, temp);
w->s_img.roomadslam[2] = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/roomadslam/frame_04_delay-0.1s.xpm", temp, temp);
w->s_img.roomadslam[3] = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/roomadslam/frame_05_delay-0.05s.xpm", temp, temp);
w->s_img.roomadslam[4] = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/roomadslam/frame_06_delay-0.05s.xpm", temp, temp);
w->s_img.roomadslam[5] = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/roomadslam/frame_07_delay-0.05s.xpm", temp, temp);
w->s_img.roomadslam[6] = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/roomadslam/frame_08_delay-0.05s.xpm", temp, temp);
w->s_img.roomadslam[7] = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/roomadslam/frame_09_delay-0.05s.xpm", temp, temp);
w->s_img.roomadslam[8] = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/roomadslam/frame_10_delay-0.05s.xpm", temp, temp);
w->s_img.roomadslam[9] = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/roomadslam/frame_11_delay-0.05s.xpm", temp, temp);
w->s_img.roomadslam[10] = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/roomadslam/frame_12_delay-0.05s.xpm", temp, temp);
}
void load_textures(t_structure_main *w, int *temp)
{
w->s_img.img_player = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/player.xpm", temp, temp);
w->s_img.img_wall = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/wall.xpm", temp, temp);
load_roomadslam_textures(w, temp);
w->s_img.pedro_wall = mlx_xpm_file_to_image(w->s_win.mlx,
"sprite/pedrop/frame_061_delay-0.03s.xpm", temp, temp);
}
void init_buffer(t_structure_main *w)
{
int bpp;
int size_line;
int endian;
char *buffer_addr;
w->s_img.buffer = mlx_new_image(w->s_win.mlx, w->s_win.width,
w->s_win.height);
if (w->s_img.buffer == NULL)
{
fprintf(stderr, "Erreur : Échec de mlx_new_image pour le buffer.\n");
return ;
}
buffer_addr = mlx_get_data_addr(w->s_img.buffer, &bpp, &size_line, &endian);
if (buffer_addr == NULL)
fprintf(stderr, "Erreur : Impossible d'obtenir l'adresse du buffer.\n");
else
printf("Buffer créé avec Largeur: %d, Hauteur: %d\n",
w->s_win.width, w->s_win.height);
}
void calculate_map_scale(t_structure_main *w)
{
int maps_x;
int maps_y;
maps_x = w->s_win.width / (w->s_map.mapX * 4);
maps_y = w->s_win.height / (w->s_map.mapY * 2);
if (maps_x < maps_y)
w->s_map.mapS = maps_x;
else
w->s_map.mapS = maps_y;
}
void init_windows(t_structure_main *w)
{
int temp;
w->s_win.width = WIDTH;
w->s_win.height = HEIGHT;
init_player(w);
init_mlx_and_window(w);
load_textures(w, &temp);
init_buffer(w);
load_wall_textures(w);
printf("Window Dimensions: Width = %d, Height = %d\n",
w->s_win.width, w->s_win.height);
printf("Texture Dimensions: Width = %d, Height = %d\n",
w->s_img.texture_width, w->s_img.texture_height);
gettimeofday(&(w->start_time), NULL);
w->end_time = w->start_time;
w->frame_count = 0;
calculate_map_scale(w);
}