So_long/srcs/utils.c

109 lines
2.9 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lfirmin <lfirmin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/12 16:24:49 by lfirmin #+# #+# */
/* Updated: 2024/11/17 13:04:59 by lfirmin ### ########.fr */
/* */
/* ************************************************************************** */
#include "../include/so_long.h"
t_data *init_data(void)
{
t_data *data;
data = malloc(sizeof(t_data));
if (!data)
return (NULL);
data->map = NULL;
data->colect = 0;
data->p = 0;
data->e = 0;
return (data);
}
void clean_data(t_data *data)
{
int i;
if (!data)
return ;
if (data->map)
{
i = 0;
while (data->map[i])
{
free(data->map[i]);
i++;
}
free(data->map);
}
free(data);
}
int init_window(t_game *game)
{
int screen_width;
int screen_height;
game->mlx = mlx_init();
if (!game->mlx)
return (1);
game->win = mlx_new_window(game->mlx,
game->map_width * 32 - 32,
game->map_height * 32,
"so_long");
mlx_get_screen_size(game->mlx, &screen_width, &screen_height);
if (screen_width < (game->map_width * 32 - 32))
return (ft_printf_fd(2, "Error : Map too big in width\n"), \
close_game(game), 1);
if (screen_height < (game->map_height * 32))
return (ft_printf_fd(2, "Error : Map too big in height\n"), \
close_game(game), 1);
if (!game->win)
return (ft_printf_fd(2, "Error\nMLX initialization failed\n"), \
close_game(game), 1);
return (0);
}
int init_images(t_game *game)
{
int img_width;
int img_height;
game->img_floor = mlx_xpm_file_to_image(game->mlx,
"assets/floor.xpm", &img_width, &img_height);
game->img_wall = mlx_xpm_file_to_image(game->mlx,
"assets/wall.xpm", &img_width, &img_height);
game->img_player = mlx_xpm_file_to_image(game->mlx,
"assets/player.xpm", &img_width, &img_height);
game->img_collect = mlx_xpm_file_to_image(game->mlx,
"assets/collectible.xpm", &img_width, &img_height);
game->img_exit = mlx_xpm_file_to_image(game->mlx,
"assets/exit.xpm", &img_width, &img_height);
if (!game->img_floor || !game->img_wall || !game->img_player
|| !game->img_collect || !game->img_exit)
{
ft_printf_fd(2, "Error : Image not found\n");
close_game(game);
return (1);
}
return (0);
}
int init_game(t_game *game)
{
if (init_window(game))
return (1);
if (init_images(game))
return (1);
game->moves = 0;
find_player_pos(game);
find_exit_pos(game);
return (0);
}