2025-07-31 13:31:05 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
|
|
|
|
/* inits.c :+: :+: :+: */
|
|
|
|
|
/* +:+ +:+ +:+ */
|
2025-07-31 13:43:08 +02:00
|
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
2025-07-31 13:31:05 +02:00
|
|
|
/* +#+#+#+#+#+ +#+ */
|
2025-07-31 13:43:08 +02:00
|
|
|
/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
|
2025-08-14 21:27:53 +02:00
|
|
|
/* Updated: 2025/08/14 21:26:14 by tchampio ### ########.fr */
|
2025-07-31 13:31:05 +02:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include "../cub3d_data.h"
|
2025-07-31 14:07:56 +02:00
|
|
|
#include "time.h"
|
2025-07-31 13:31:05 +02:00
|
|
|
#include "../../libft/includes/libft.h"
|
|
|
|
|
#include "../../mlx/mlx.h"
|
|
|
|
|
#include "../map/map_checker.h"
|
2025-08-12 15:57:27 +02:00
|
|
|
#include "../sprites/create_sprite.h"
|
2025-07-31 13:31:05 +02:00
|
|
|
#include "frees.h"
|
2025-08-12 15:41:42 +02:00
|
|
|
#include <stdio.h>
|
2025-07-31 13:31:05 +02:00
|
|
|
|
2025-08-05 13:08:47 +02:00
|
|
|
t_img_data *load_single_texture(t_cub3d_data *data, char *path)
|
2025-07-31 13:43:08 +02:00
|
|
|
{
|
2025-08-05 13:08:47 +02:00
|
|
|
int width;
|
|
|
|
|
int height;
|
|
|
|
|
void *img;
|
|
|
|
|
t_img_data *img_data;
|
2025-07-31 13:43:08 +02:00
|
|
|
|
|
|
|
|
img = mlx_xpm_file_to_image(data->mlx, path, &width, &height);
|
|
|
|
|
if (img == NULL)
|
|
|
|
|
{
|
|
|
|
|
ft_printf("Error: failed to open image at %s\n", path);
|
2025-08-05 15:10:12 +02:00
|
|
|
destroy(data, 1);
|
2025-07-31 13:43:08 +02:00
|
|
|
}
|
|
|
|
|
if (width != height || width != TEXTURE_SIZE)
|
|
|
|
|
{
|
|
|
|
|
ft_printf("Error: textures are not the right size\n");
|
2025-08-05 15:10:12 +02:00
|
|
|
destroy(data, 1);
|
2025-07-31 13:43:08 +02:00
|
|
|
}
|
|
|
|
|
ft_printf("image: %p\n", img);
|
2025-08-05 13:08:47 +02:00
|
|
|
img_data = ft_calloc(sizeof(t_img_data), 1);
|
|
|
|
|
img_data->img = img;
|
|
|
|
|
img_data->addr = mlx_get_data_addr(img_data->img,
|
|
|
|
|
&img_data->bits_per_pixel, &img_data->line_length,
|
|
|
|
|
&img_data->endian);
|
|
|
|
|
return (img_data);
|
2025-07-31 13:43:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void load_textures(t_cub3d_data *data)
|
|
|
|
|
{
|
|
|
|
|
data->no_texture = load_single_texture(data, data->map->no_texture);
|
|
|
|
|
data->so_texture = load_single_texture(data, data->map->so_texture);
|
|
|
|
|
data->we_texture = load_single_texture(data, data->map->we_texture);
|
|
|
|
|
data->ea_texture = load_single_texture(data, data->map->ea_texture);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-12 15:41:42 +02:00
|
|
|
void place_base_sprites(t_cub3d_data *data, char **map)
|
|
|
|
|
{
|
|
|
|
|
int y;
|
|
|
|
|
int x;
|
2025-08-12 15:57:27 +02:00
|
|
|
int c;
|
2025-08-12 15:41:42 +02:00
|
|
|
|
|
|
|
|
y = 0;
|
2025-08-12 15:57:27 +02:00
|
|
|
c = 0;
|
2025-08-12 15:41:42 +02:00
|
|
|
while (y < data->map->mapheight)
|
|
|
|
|
{
|
|
|
|
|
x = 0;
|
|
|
|
|
while (x < (int)ft_strlen(map[y]))
|
|
|
|
|
{
|
2025-08-12 16:15:02 +02:00
|
|
|
if (map[y][x] == 'M' || map[y][x] == 'Q'
|
|
|
|
|
|| map[y][x] == 'J' || map[y][x] == 'D')
|
2025-08-14 21:27:53 +02:00
|
|
|
{
|
|
|
|
|
if (c < MAX_SPRITES)
|
|
|
|
|
data->sprite_list[c++] = place_right_sprite(data,
|
|
|
|
|
map[y][x], x, y);
|
|
|
|
|
}
|
2025-08-12 15:41:42 +02:00
|
|
|
x++;
|
|
|
|
|
}
|
|
|
|
|
y++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-31 13:31:05 +02:00
|
|
|
void init_cub3d_data(t_cub3d_data *data, char **argv)
|
|
|
|
|
{
|
|
|
|
|
ft_bzero(data, sizeof(*data));
|
|
|
|
|
data->map = ft_calloc(sizeof(t_mapdata), 1);
|
|
|
|
|
if (!check_cubfile(argv[1], data->map))
|
|
|
|
|
return (ft_printf("Error: Wrong map file. Reason: %s\n",
|
|
|
|
|
data->map->error), free_map(data->map), exit(1));
|
|
|
|
|
data->mlx = mlx_init();
|
|
|
|
|
if (data->mlx == NULL)
|
|
|
|
|
return (ft_printf("Error: Failed to initalize mlx\n"),
|
|
|
|
|
free_map(data->map), exit(1));
|
|
|
|
|
data->mlx_win = mlx_new_window(data->mlx, WIDTH, HEIGHT, "Cub3d");
|
|
|
|
|
data->img_data = ft_calloc(sizeof(t_img_data), 1);
|
|
|
|
|
data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT);
|
|
|
|
|
data->img_data->addr = mlx_get_data_addr(data->img_data->img,
|
|
|
|
|
&data->img_data->bits_per_pixel, &data->img_data->line_length,
|
|
|
|
|
&data->img_data->endian);
|
|
|
|
|
init_player(&data->player, data->map);
|
2025-07-31 14:05:30 +02:00
|
|
|
data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT);
|
2025-07-31 13:43:08 +02:00
|
|
|
load_textures(data);
|
2025-08-11 11:51:23 +02:00
|
|
|
data->sprite_list = ft_calloc(sizeof(t_sprite *), MAX_SPRITES);
|
2025-08-06 14:06:15 +02:00
|
|
|
ft_memset(data->sprite_distances, -1, MAX_SPRITES);
|
2025-08-12 15:41:42 +02:00
|
|
|
place_base_sprites(data, data->map->map);
|
2025-07-31 13:31:05 +02:00
|
|
|
}
|