2025-05-05 17:09:11 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* main.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
2025-05-06 13:24:32 +02:00
|
|
|
/* By: tchampio <tchampio@student.42lehavre. +#+ +:+ +#+ */
|
2025-05-05 17:09:11 +02:00
|
|
|
/* +#+#+#+#+#+ +#+ */
|
2025-05-06 13:24:32 +02:00
|
|
|
/* Created: 2025/05/06 13:16:11 by tchampio #+# #+# */
|
2025-06-24 10:41:50 +02:00
|
|
|
/* Updated: 2025/06/24 10:40:02 by tchampio ### ########.fr */
|
2025-05-05 17:09:11 +02:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
2025-05-06 09:30:32 +02:00
|
|
|
#include "../includes/libft.h"
|
2025-06-06 18:38:21 +02:00
|
|
|
#include "../includes/cub3d_consts.h"
|
2025-06-24 00:17:33 +02:00
|
|
|
#include "../includes/structs.h"
|
2025-06-06 13:57:31 +02:00
|
|
|
#include "../mlx/mlx.h"
|
2025-06-04 19:16:59 +02:00
|
|
|
#include "../includes/maputils.h"
|
2025-05-06 13:24:32 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
2025-05-06 09:30:32 +02:00
|
|
|
|
2025-05-06 12:25:17 +02:00
|
|
|
void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color)
|
|
|
|
|
{
|
|
|
|
|
char *dst;
|
|
|
|
|
|
|
|
|
|
dst = data->addr + (y * data->line_length + x * (data->bits_per_pixel / 8));
|
|
|
|
|
*(unsigned int*)dst = color;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 00:17:33 +02:00
|
|
|
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++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-20 23:59:27 +02:00
|
|
|
void free_tab(char **tab)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
if (!tab)
|
|
|
|
|
return ;
|
|
|
|
|
i = 0;
|
|
|
|
|
while (tab[i])
|
|
|
|
|
{
|
|
|
|
|
free(tab[i]);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
free(tab);
|
|
|
|
|
}
|
2025-06-06 13:57:31 +02:00
|
|
|
int destroy(t_mapdata *map)
|
|
|
|
|
{
|
2025-06-20 23:59:27 +02:00
|
|
|
free_tab(map->map);
|
|
|
|
|
free_tab(map->mapflood);
|
2025-06-24 00:17:33 +02:00
|
|
|
free(map->ea_texture);
|
|
|
|
|
free(map->no_texture);
|
|
|
|
|
free(map->so_texture);
|
|
|
|
|
free(map->we_texture);
|
2025-06-20 23:59:27 +02:00
|
|
|
free(map->filename);
|
2025-06-06 13:57:31 +02:00
|
|
|
exit(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int key_destroy(int keycode, t_mapdata *map)
|
|
|
|
|
{
|
|
|
|
|
if (keycode == 65307)
|
|
|
|
|
destroy(map);
|
|
|
|
|
return (0);
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-24 00:17:33 +02:00
|
|
|
void draw_map(t_mapdata *map, t_mlx_data *data)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int j;
|
|
|
|
|
|
|
|
|
|
i = 0;
|
2025-06-24 10:41:50 +02:00
|
|
|
while (map->mapflood[i])
|
2025-06-24 00:17:33 +02:00
|
|
|
{
|
|
|
|
|
j = 0;
|
2025-06-24 10:41:50 +02:00
|
|
|
while (map->mapflood[i][j])
|
2025-06-24 00:17:33 +02:00
|
|
|
{
|
2025-06-24 10:41:50 +02:00
|
|
|
if (map->mapflood[i][j] == '1')
|
2025-06-24 00:17:33 +02:00
|
|
|
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++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 17:09:11 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
2025-05-06 12:25:17 +02:00
|
|
|
void *mlx;
|
|
|
|
|
void *mlx_win;
|
|
|
|
|
t_mlx_data data;
|
2025-05-06 13:24:32 +02:00
|
|
|
t_mapdata map;
|
2025-05-06 12:25:17 +02:00
|
|
|
|
2025-06-16 17:03:32 +02:00
|
|
|
ft_bzero(&map, sizeof(map));
|
2025-05-05 17:09:11 +02:00
|
|
|
if (argc < 2)
|
2025-06-06 18:38:21 +02:00
|
|
|
return (ft_printf("Error: Missing cub3d file\n"), 1);
|
2025-05-06 13:24:32 +02:00
|
|
|
if (!check_cubfile(argv[1], &map))
|
|
|
|
|
return (ft_printf("Error: Wrong map file. Reason: %s\n", map.error), 1);
|
2025-05-06 12:25:17 +02:00
|
|
|
mlx = mlx_init();
|
|
|
|
|
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);
|
2025-06-06 13:57:31 +02:00
|
|
|
mlx_hook(mlx_win, 17, 0L, destroy, &map);
|
2025-06-24 00:17:33 +02:00
|
|
|
draw_map(&map, &data);
|
2025-06-06 13:57:31 +02:00
|
|
|
mlx_key_hook(mlx_win, key_destroy, &map);
|
2025-05-06 12:25:17 +02:00
|
|
|
mlx_put_image_to_window(mlx, mlx_win, data.img, 0, 0);
|
|
|
|
|
mlx_loop(mlx);
|
2025-05-05 17:09:11 +02:00
|
|
|
}
|