avant d'aller bosser a carreouf

This commit is contained in:
Theo Champion 2025-06-24 00:17:33 +02:00
parent b185cddbf0
commit 321ef3db7c
7 changed files with 393 additions and 288 deletions

View file

@ -6,28 +6,19 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/06 13:16:11 by tchampio #+# #+# */
/* Updated: 2025/06/20 23:58:56 by tchampio ### ########.fr */
/* Updated: 2025/06/22 00:17:39 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/libft.h"
#include "../includes/cub3d_consts.h"
#include "../includes/structs.h"
#include "../mlx/mlx.h"
#include "../includes/maputils.h"
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
typedef struct s_mlx_data
{
void *img;
char *addr;
int bits_per_pixel;
int line_length;
int endian;
} t_mlx_data;
void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color)
{
char *dst;
@ -36,6 +27,24 @@ void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color)
*(unsigned int*)dst = color;
}
void draw_2d_wall(unsigned int color, t_mlx_data *data, int size, int x, int y)
{
int i;
int j;
i = 0;
while (i < size)
{
j = 0;
while (j < size)
{
my_mlx_pixel_put(data, x + i, y + j, color);
j++;
}
i++;
}
}
void free_tab(char **tab)
{
int i;
@ -54,10 +63,10 @@ int destroy(t_mapdata *map)
{
free_tab(map->map);
free_tab(map->mapflood);
free(map->EA_texture);
free(map->NO_texture);
free(map->SO_texture);
free(map->WE_texture);
free(map->ea_texture);
free(map->no_texture);
free(map->so_texture);
free(map->we_texture);
free(map->filename);
exit(0);
return 0;
@ -70,14 +79,31 @@ int key_destroy(int keycode, t_mapdata *map)
return (0);
}
void draw_map(t_mapdata *map, t_mlx_data *data)
{
int i;
int j;
i = 0;
while (map->map[i])
{
j = 0;
while (map->map[i][j])
{
if (map->map[i][j] == '1')
draw_2d_wall(map->f_color, data, 10, 10 * j, 10 * i);
else if (ft_strchr("NSEW", map->map[i][j]))
draw_2d_wall(0x00FF0000, data, 10, 10 * j, 10 * i);
j++;
}
i++;
}
}
int main(int argc, char **argv)
{
void *mlx;
void *mlx_win;
char *xpm = "./ressources/test.xpm";
void *xpm_image;
int xpm_height;
int xpm_width;
t_mlx_data data;
t_mapdata map;
@ -90,11 +116,9 @@ int main(int argc, char **argv)
mlx_win = mlx_new_window(mlx, 800, 600, "Cub3d");
data.img = mlx_new_image(mlx, 800, 600);
data.addr = mlx_get_data_addr(data.img, &data.bits_per_pixel, &data.line_length, &data.endian);
xpm_image = mlx_xpm_file_to_image(mlx, xpm, &xpm_width, &xpm_height);
//my_mlx_pixel_put(&data, 5, 5, 0x00FF0000);
mlx_hook(mlx_win, 17, 0L, destroy, &map);
draw_map(&map, &data);
mlx_key_hook(mlx_win, key_destroy, &map);
mlx_put_image_to_window(mlx, mlx_win, data.img, 0, 0);
mlx_put_image_to_window(mlx, mlx_win, xpm_image, 0, 0);
mlx_loop(mlx);
}

127
src/map/checkers.c Normal file
View file

@ -0,0 +1,127 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* checkers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/21 19:29:45 by tchampio #+# #+# */
/* Updated: 2025/06/21 19:32:04 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/maputils.h"
#include "../../includes/libft.h"
bool check_filename(t_mapdata *map, char *file)
{
int filename_size;
int i;
int j;
char end[5];
filename_size = ft_strlen(file);
i = filename_size;
j = 4;
ft_bzero(end, 5);
while (i > (filename_size - 5))
{
end[j] = file[i];
i--;
j--;
}
if (ft_strncmp(end, ".cub", 5))
return (false);
map->filename = ft_strdup(file);
return (true);
}
void populate_maps(t_mapdata *map, int fd)
{
char *line;
line = get_next_line(fd);
while (line)
{
if (line[0] != '\n')
add_map_line(line, map);
free(line);
line = get_next_line(fd);
}
free(line);
}
bool check_walls(t_mapdata *map)
{
int i;
int j;
i = 0;
while (i < map->mapheight)
{
j = 0;
while (map->map[i][j] == ' ')
j++;
if (map->map[i][j] != '1'
&& map->map[i][ft_strlen(map->map[i]) - 1] != '1')
return (false);
if (i == 0 || i == map->mapheight - 1)
{
while (map->map[i][j] == '1' || map->map[i][j] == ' '
|| map->map[i][j] == '\n')
j++;
if (ft_strlen(map->map[i]) != (unsigned int)j)
return (false);
}
if (has_forbidden_characters(map->map[i]))
return (false);
i++;
}
return (true);
}
bool check_bare_minimum(t_mapdata *map)
{
int i;
int spawncount;
int j;
i = 0;
spawncount = 0;
while (map->map[i])
{
j = 0;
while (map->map[i][j])
{
if (map->map[i][j] == 'N' || map->map[i][j] == 'E'
|| map->map[i][j] == 'S' || map->map[i][j] == 'W')
{
spawncount++;
map->startx = j;
map->starty = i;
}
j++;
}
i++;
}
if (spawncount == 1)
return (true);
return (false);
}
bool has_forbidden_characters(char *line)
{
static const char *allowedchars = " 10NSEW\n";
size_t strsize;
int i;
strsize = ft_strlen(line);
i = 0;
while (i < (int)strsize)
{
if (!ft_strchr(allowedchars, line[i]))
return (true);
i++;
}
return (false);
}

View file

@ -6,7 +6,7 @@
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/06 17:54:42 by tchampio #+# #+# */
/* Updated: 2025/06/21 00:10:08 by tchampio ### ########.fr */
/* Updated: 2025/06/21 19:40:13 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,10 +20,10 @@ void print_mapdata(const t_mapdata *data)
{
ft_printf(BOLD CYAN "=== Map Data ===\n" RESET);
ft_printf(BOLD "Filename: " RESET "%s\n", data->filename);
ft_printf(BOLD "NO Texture: " RESET "%s\n", data->NO_texture);
ft_printf(BOLD "SO Texture: " RESET "%s\n", data->SO_texture);
ft_printf(BOLD "WE Texture: " RESET "%s\n", data->WE_texture);
ft_printf(BOLD "EA Texture: " RESET "%s\n", data->EA_texture);
ft_printf(BOLD "NO Texture: " RESET "%s\n", data->no_texture);
ft_printf(BOLD "SO Texture: " RESET "%s\n", data->so_texture);
ft_printf(BOLD "WE Texture: " RESET "%s\n", data->we_texture);
ft_printf(BOLD "EA Texture: " RESET "%s\n", data->ea_texture);
ft_printf(BOLD "F color: " RESET "%x\n", data->f_color);
ft_printf(BOLD "C color: " RESET "%x\n", data->c_color);
ft_printf(BOLD "Validity: " RESET);
@ -38,105 +38,6 @@ void print_mapdata(const t_mapdata *data)
ft_printf(CYAN "=================\n" RESET);
}
unsigned long set_color(const char *s, t_mapdata *map)
{
int i;
unsigned long finalcolor;
char **tab;
int rgb[100];
i = 0;
tab = ft_split(s, ',');
while (tab[i])
{
if (i > 2)
ft_strlcpy(map->error, "too many colors", 16);
rgb[i] = ft_atoi(tab[i]);
free(tab[i]);
i++;
}
free(tab);
if (rgb[0] < 0 || rgb[0] > 255
|| rgb[1] < 0 || rgb[1] > 255
|| rgb[2] < 0 || rgb[2] > 255)
return (ft_strlcpy(map->error, "Invalid color", 14), 0);
finalcolor = ((rgb[0] & 0xff) << 16)
+ ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff);
return (finalcolor);
}
bool set_textures(char *line, t_mapdata *map)
{
char **tab;
int i;
tab = ft_split(line, ' ');
i = 0;
if (tab[0][0] == '1')
return (false);
if (tab[0] && tab[1])
{
if (!ft_strncmp(tab[0], "NO", 3) || !ft_strncmp(tab[0], "no", 3))
map->NO_texture = ft_strdup(tab[1]);
if (!ft_strncmp(tab[0], "SO", 3) || !ft_strncmp(tab[0], "so", 3))
map->SO_texture = ft_strdup(tab[1]);
if (!ft_strncmp(tab[0], "WE", 3) || !ft_strncmp(tab[0], "we", 3))
map->WE_texture = ft_strdup(tab[1]);
if (!ft_strncmp(tab[0], "EA", 3) || !ft_strncmp(tab[0], "ea", 3))
map->EA_texture = ft_strdup(tab[1]);
if (!ft_strncmp(tab[0], "F", 2) || !ft_strncmp(tab[0], "f", 2))
map->f_color = set_color(tab[1], map);
if (!ft_strncmp(tab[0], "C", 2) || !ft_strncmp(tab[0], "c", 2))
map->c_color = set_color(tab[1], map);
}
while (tab[i])
free(tab[i++]);
return (free(tab), true);
}
bool add_textures(int fd, t_mapdata *map)
{
char *line;
int set_lines;
line = get_next_line(fd);
set_lines = 0;
while (line && set_lines < 6)
{
if (!set_textures(line, map))
return (free(line), false);
else
set_lines++;
free(line);
line = get_next_line(fd);
}
free(line);
return (true);
}
bool check_filename(t_mapdata *map, char *file)
{
int filename_size;
int i;
int j;
char end[5];
filename_size = ft_strlen(file);
i = filename_size;
j = 4;
ft_bzero(end, 5);
while (i > (filename_size - 5))
{
end[j] = file[i];
i--;
j--;
}
if (ft_strncmp(end, ".cub", 5))
return (false);
map->filename = ft_strdup(file);
return (true);
}
void free_tab_length(char **tab, int length)
{
int i;
@ -152,148 +53,6 @@ void free_tab_length(char **tab, int length)
free(tab);
}
int copy_old_map(t_mapdata *map, char **newmap, char **newmapflood, int length)
{
int i;
i = 0;
if (!map->map)
return (0);
while (i < length)
{
newmap[i] = ft_strdup(map->map[i]);
newmapflood[i] = ft_strdup(map->mapflood[i]);
i++;
}
return (i);
}
void add_map_line(const char *line, t_mapdata *map)
{
static int length = 0;
char **newmap;
char **newmapflood;
int i;
newmap = ft_calloc(sizeof(char *), (length + 2));
if (!newmap)
return ;
newmapflood = ft_calloc(sizeof(char *), (length + 2));
if (!newmapflood)
return ;
i = copy_old_map(map, newmap, newmapflood, length);
newmap[i] = ft_strdup(line);
newmapflood[i] = ft_strdup(line);
free_tab_length(map->map, length);
free_tab_length(map->mapflood, length);
map->mapheight = ++length;
map->map = newmap;
map->mapflood = newmapflood;
}
void populate_maps(t_mapdata *map, int fd)
{
char *line;
line = get_next_line(fd);
while (line)
{
if (line[0] != '\n')
add_map_line(line, map);
free(line);
line = get_next_line(fd);
}
free(line);
}
bool has_forbidden_characters(char *line)
{
static const char *allowedchars = " 10NSEW\n";
size_t strsize;
int i;
strsize = ft_strlen(line);
i = 0;
while (i < (int)strsize)
{
if (!ft_strchr(allowedchars, line[i]))
return (true);
i++;
}
return (false);
}
bool check_walls(t_mapdata *map)
{
int i;
int j;
i = 0;
while (i < map->mapheight)
{
j = 0;
while (map->map[i][j] == ' ')
j++;
if (map->map[i][j] != '1'
&& map->map[i][ft_strlen(map->map[i]) - 1] != '1')
return (false);
if (i == 0 || i == map->mapheight - 1)
{
while (map->map[i][j] == '1' || map->map[i][j] == ' '
|| map->map[i][j] == '\n')
j++;
if (ft_strlen(map->map[i]) != (unsigned int)j)
return (false);
}
if (has_forbidden_characters(map->map[i]))
return (false);
i++;
}
return (true);
}
void print_map(char **map)
{
int i;
i = 0;
while (map && map[i])
{
ft_printf("line2: %s", map[i]);
i++;
}
ft_printf("(%p) %s", map[i], map[i]);
}
bool check_bare_minimum(t_mapdata *map)
{
int i;
int spawncount;
int j;
i = 0;
spawncount = 0;
while (map->map[i])
{
j = 0;
while (map->map[i][j])
{
if (map->map[i][j] == 'N' || map->map[i][j] == 'E'
|| map->map[i][j] == 'S' || map->map[i][j] == 'W')
{
spawncount++;
map->startx = j;
map->starty = i;
}
j++;
}
i++;
}
if (spawncount == 1)
return (true);
return (false);
}
bool check_cubfile(char *file, t_mapdata *map)
{
int fd;
@ -315,5 +74,6 @@ bool check_cubfile(char *file, t_mapdata *map)
false);
if (!check_bare_minimum(map))
return (close(fd), false);
map->isvalid = true;
return (true);
}

129
src/map/setters.c Normal file
View file

@ -0,0 +1,129 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* setters.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchampio <tchampio@student.42lehavre.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/06/21 19:35:43 by tchampio #+# #+# */
/* Updated: 2025/06/21 19:39:32 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/maputils.h"
#include "../../includes/libft.h"
unsigned long set_color(const char *s, t_mapdata *map)
{
int i;
unsigned long finalcolor;
char **tab;
int rgb[100];
i = 0;
tab = ft_split(s, ',');
while (tab[i])
{
if (i > 2)
ft_strlcpy(map->error, "too many colors", 16);
rgb[i] = ft_atoi(tab[i]);
free(tab[i]);
i++;
}
free(tab);
if (rgb[0] < 0 || rgb[0] > 255
|| rgb[1] < 0 || rgb[1] > 255
|| rgb[2] < 0 || rgb[2] > 255)
return (ft_strlcpy(map->error, "Invalid color", 14), 0);
finalcolor = ((rgb[0] & 0xff) << 16)
+ ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff);
return (finalcolor);
}
bool set_textures(char *line, t_mapdata *map)
{
char **tab;
int i;
tab = ft_split(line, ' ');
i = 0;
if (tab[0][0] == '1')
return (false);
if (tab[0] && tab[1])
{
if (!ft_strncmp(tab[0], "NO", 3) || !ft_strncmp(tab[0], "no", 3))
map->no_texture = ft_strdup(tab[1]);
if (!ft_strncmp(tab[0], "SO", 3) || !ft_strncmp(tab[0], "so", 3))
map->so_texture = ft_strdup(tab[1]);
if (!ft_strncmp(tab[0], "WE", 3) || !ft_strncmp(tab[0], "we", 3))
map->we_texture = ft_strdup(tab[1]);
if (!ft_strncmp(tab[0], "EA", 3) || !ft_strncmp(tab[0], "ea", 3))
map->ea_texture = ft_strdup(tab[1]);
if (!ft_strncmp(tab[0], "F", 2) || !ft_strncmp(tab[0], "f", 2))
map->f_color = set_color(tab[1], map);
if (!ft_strncmp(tab[0], "C", 2) || !ft_strncmp(tab[0], "c", 2))
map->c_color = set_color(tab[1], map);
}
while (tab[i])
free(tab[i++]);
return (free(tab), true);
}
bool add_textures(int fd, t_mapdata *map)
{
char *line;
int set_lines;
line = get_next_line(fd);
set_lines = 0;
while (line && set_lines < 6)
{
if (!set_textures(line, map))
return (free(line), false);
else
set_lines++;
free(line);
line = get_next_line(fd);
}
free(line);
return (true);
}
int copy_old_map(t_mapdata *map, char **newmap, char **newmapflood, int length)
{
int i;
i = 0;
if (!map->map)
return (0);
while (i < length)
{
newmap[i] = ft_strdup(map->map[i]);
newmapflood[i] = ft_strdup(map->mapflood[i]);
i++;
}
return (i);
}
void add_map_line(const char *line, t_mapdata *map)
{
static int length = 0;
char **newmap;
char **newmapflood;
int i;
newmap = ft_calloc(sizeof(char *), (length + 2));
if (!newmap)
return ;
newmapflood = ft_calloc(sizeof(char *), (length + 2));
if (!newmapflood)
return ;
i = copy_old_map(map, newmap, newmapflood, length);
newmap[i] = ft_strdup(line);
newmapflood[i] = ft_strdup(line);
free_tab_length(map->map, length);
free_tab_length(map->mapflood, length);
map->mapheight = ++length;
map->map = newmap;
map->mapflood = newmapflood;
}