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-07-02 15:35:05 +02:00
|
|
|
/* Updated: 2025/07/02 15:32:23 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-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));
|
2025-06-24 11:51:49 +02:00
|
|
|
*(unsigned int *)dst = color;
|
2025-05-06 12:25:17 +02:00
|
|
|
}
|
|
|
|
|
|
2025-06-24 11:51:49 +02:00
|
|
|
void draw_2d_wall(unsigned int color, t_mlx_data *data,
|
|
|
|
|
int size, int x, int y)
|
2025-06-24 00:17:33 +02:00
|
|
|
{
|
|
|
|
|
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-25 18:08:19 +02:00
|
|
|
int destroy(t_cub3d_data *data)
|
2025-06-20 23:59:27 +02:00
|
|
|
{
|
2025-06-25 18:08:19 +02:00
|
|
|
free_map(data->map);
|
|
|
|
|
if (data->mlx_win)
|
|
|
|
|
mlx_destroy_window(data->mlx, data->mlx_win);
|
|
|
|
|
if (data->mlx_data)
|
|
|
|
|
mlx_destroy_image(data->mlx, data->mlx_data->img);
|
|
|
|
|
free(data->mlx_data);
|
|
|
|
|
if (data->mlx)
|
|
|
|
|
mlx_destroy_display(data->mlx);
|
|
|
|
|
free(data->mlx);
|
2025-06-06 13:57:31 +02:00
|
|
|
exit(0);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2025-06-25 18:08:19 +02:00
|
|
|
int key_destroy(int keycode, t_cub3d_data *data)
|
2025-06-06 13:57:31 +02:00
|
|
|
{
|
|
|
|
|
if (keycode == 65307)
|
2025-06-25 18:08:19 +02:00
|
|
|
destroy(data);
|
2025-06-06 13:57:31 +02:00
|
|
|
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 11:51:49 +02:00
|
|
|
while (map->map[i])
|
2025-06-24 00:17:33 +02:00
|
|
|
{
|
|
|
|
|
j = 0;
|
2025-06-24 11:51:49 +02:00
|
|
|
while (map->map[i][j])
|
2025-06-24 00:17:33 +02:00
|
|
|
{
|
2025-06-24 11:51:49 +02:00
|
|
|
if (map->map[i][j] == '1')
|
|
|
|
|
draw_2d_wall(map->f_color, data, 20, 20 * j, 20 * i);
|
2025-07-02 15:35:05 +02:00
|
|
|
else if (map->map[i][j] == 'Z' || map->map[i][j] == 'z')
|
|
|
|
|
draw_2d_wall(0x0008D9D6, data, 20, 20 * j, 20 * i);
|
|
|
|
|
else if (map->map[i][j] == 'D' || map->map[i][j] == 'd')
|
|
|
|
|
draw_2d_wall(0x00FF2E63, data, 20, 20 * j, 20 * i);
|
|
|
|
|
else if (map->map[i][j] == 's')
|
|
|
|
|
draw_2d_wall(0x00E84545, data, 20, 20 * j, 20 * i);
|
|
|
|
|
else if (map->map[i][j] == 'M')
|
|
|
|
|
draw_2d_wall(0x00F4CE14, data, 20, 20 * j, 20 * i);
|
2025-06-24 00:17:33 +02:00
|
|
|
else if (ft_strchr("NSEW", map->map[i][j]))
|
2025-06-24 11:51:49 +02:00
|
|
|
draw_2d_wall(0x00FF0000, data, 20, 20 * j, 20 * i);
|
2025-06-24 00:17:33 +02:00
|
|
|
j++;
|
|
|
|
|
}
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-05 17:09:11 +02:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
|
{
|
2025-06-24 11:51:49 +02:00
|
|
|
t_cub3d_data data;
|
2025-05-06 12:25:17 +02:00
|
|
|
|
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-06-30 16:16:45 +02:00
|
|
|
data.map = ft_calloc(sizeof(t_mapdata), 1);
|
2025-06-24 11:51:49 +02:00
|
|
|
if (!check_cubfile(argv[1], data.map))
|
|
|
|
|
return (ft_printf("Error: Wrong map file. Reason: %s\n", data.map->error), 1);
|
|
|
|
|
data.mlx = mlx_init();
|
|
|
|
|
data.mlx_win = mlx_new_window(data.mlx, 800, 600, "Cub3d");
|
2025-06-25 18:08:19 +02:00
|
|
|
data.mlx_data = ft_calloc(sizeof(t_mlx_data), 1);
|
2025-06-24 11:51:49 +02:00
|
|
|
data.mlx_data->img = mlx_new_image(data.mlx, 800, 600);
|
|
|
|
|
data.mlx_data->addr = mlx_get_data_addr(data.mlx_data->img, &data.mlx_data->bits_per_pixel, &data.mlx_data->line_length, &data.mlx_data->endian);
|
2025-06-25 18:08:19 +02:00
|
|
|
mlx_hook(data.mlx_win, 17, 0L, destroy, &data);
|
2025-06-24 11:51:49 +02:00
|
|
|
draw_map(data.map, data.mlx_data);
|
2025-06-25 18:08:19 +02:00
|
|
|
mlx_key_hook(data.mlx_win, key_destroy, &data);
|
2025-06-24 11:51:49 +02:00
|
|
|
mlx_put_image_to_window(data.mlx, data.mlx_win, data.mlx_data->img, 0, 0);
|
2025-06-30 16:16:45 +02:00
|
|
|
#ifdef BONUS
|
2025-07-02 15:35:05 +02:00
|
|
|
mlx_string_put(data.mlx, data.mlx_win, 10, 10, 0x00FFFFFF, "compiled with bonuses");
|
2025-06-30 16:16:45 +02:00
|
|
|
#endif
|
2025-06-24 11:51:49 +02:00
|
|
|
mlx_loop(data.mlx);
|
2025-05-05 17:09:11 +02:00
|
|
|
}
|