mirror of
https://github.com/Ladebeze66/cub3D.git
synced 2026-11-09 13:47:10 +01:00
norme
This commit is contained in:
65
window/ft_map.c
Normal file
65
window/ft_map.c
Normal 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
39
window/ft_player.c
Normal 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
92
window/ft_rescale.c
Normal 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
106
window/ft_window.c
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user