mirror of
https://codeberg.org/ACME-Corporation/cub3d.git
synced 2025-12-06 09:58:09 +01:00
77 lines
3 KiB
C
77 lines
3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* inits.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */
|
|
/* Updated: 2025/08/11 12:44:57 by tchampio ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../cub3d_data.h"
|
|
#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)
|
|
{
|
|
int width;
|
|
int height;
|
|
void *img;
|
|
t_img_data *img_data;
|
|
|
|
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);
|
|
}
|
|
if (width != height || width != TEXTURE_SIZE)
|
|
{
|
|
ft_printf("Error: textures are not the right size\n");
|
|
destroy(data, 1);
|
|
}
|
|
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);
|
|
}
|
|
|
|
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);
|
|
load_textures(data);
|
|
data->sprite_list = ft_calloc(sizeof(t_sprite *), MAX_SPRITES);
|
|
ft_memset(data->sprite_distances, -1, MAX_SPRITES);
|
|
}
|