cub3d/src/utils/inits.c

78 lines
3 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inits.c :+: :+: :+: */
/* +:+ +:+ +:+ */
2025-07-31 13:43:08 +02:00
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
2025-07-31 13:43:08 +02:00
/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
/* Updated: 2025/08/11 12:44:57 by tchampio ### ########.fr */
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
2025-07-31 14:07:56 +02:00
#include "time.h"
#include "../../libft/includes/libft.h"
#include "../../mlx/mlx.h"
#include "../map/map_checker.h"
#include "frees.h"
t_img_data *load_single_texture(t_cub3d_data *data, char *path)
2025-07-31 13:43:08 +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);
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");
destroy(data, 1);
2025-07-31 13:43:08 +02:00
}
ft_printf("image: %p\n", img);
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);
}
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);
data->screen_matrix = ft_calloc(sizeof(int), WIDTH * HEIGHT);
2025-07-31 13:43:08 +02:00
load_textures(data);
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);
}