2024-01-06 17:15:02 +01:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* main.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2024/01/06 16:18:56 by adjoly #+# #+# */
|
2024-01-28 18:28:31 +01:00
|
|
|
/* Updated: 2024/01/28 18:28:06 by adjoly ### ########.fr */
|
2024-01-06 17:15:02 +01:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
#include "so_long.h"
|
2024-01-13 17:17:22 +01:00
|
|
|
|
2024-01-28 18:26:25 +01:00
|
|
|
t_window *ft_init_window(char **map)
|
2024-01-13 17:17:22 +01:00
|
|
|
{
|
2024-01-28 18:26:25 +01:00
|
|
|
t_window *win;
|
2024-01-24 13:45:01 +01:00
|
|
|
|
2024-01-28 18:26:25 +01:00
|
|
|
win = malloc(sizeof(map) + sizeof(t_window));
|
|
|
|
if (!win)
|
|
|
|
ft_clear_alloc(win, map);
|
|
|
|
win->p_coords = malloc(sizeof(t_coords));
|
|
|
|
if (!win->p_coords)
|
|
|
|
ft_clear_alloc(win, map);
|
|
|
|
win->e_coords = malloc(sizeof(t_coords));
|
|
|
|
if (!win->e_coords)
|
|
|
|
ft_clear_alloc(win, map);
|
|
|
|
win->img = malloc(sizeof(t_img));
|
|
|
|
if (!win->img)
|
|
|
|
ft_clear_alloc(win, map);
|
|
|
|
win->mlx = mlx_init();
|
|
|
|
if (!win->mlx)
|
|
|
|
ft_clear_alloc(win, map);
|
|
|
|
return (win);
|
2024-01-24 13:45:01 +01:00
|
|
|
}
|
2024-01-22 14:13:58 +01:00
|
|
|
|
2024-01-28 18:26:25 +01:00
|
|
|
char **ft_map_setup(char **map, char **av)
|
2024-01-19 15:41:43 +01:00
|
|
|
{
|
2024-01-28 18:26:25 +01:00
|
|
|
map = ft_read_map(av[1], map);
|
|
|
|
if (!map)
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
if (!map[0])
|
|
|
|
ft_send_error("Map is empty\n", map);
|
|
|
|
ft_check_map_error(map);
|
|
|
|
ft_freemap(map);
|
|
|
|
map = ft_read_map(av[1], map);
|
|
|
|
if (!map)
|
|
|
|
ft_send_error("Memory allocation failed\n", map);
|
|
|
|
return (map);
|
2024-01-13 17:17:22 +01:00
|
|
|
}
|
2024-01-06 17:15:02 +01:00
|
|
|
|
2024-01-28 18:26:25 +01:00
|
|
|
void ft_play(t_window *win)
|
2024-01-06 17:15:02 +01:00
|
|
|
{
|
2024-01-28 18:26:25 +01:00
|
|
|
win->mov_count = 0;
|
|
|
|
mlx_on_event(win->mlx, win->win, MLX_WINDOW_EVENT, win_close, win);
|
|
|
|
mlx_on_event(win->mlx, win->win, MLX_KEYDOWN, ft_key_event, win);
|
|
|
|
ft_alloc_img(win);
|
|
|
|
ft_printmap(win->map, win);
|
|
|
|
mlx_loop(win->mlx);
|
|
|
|
ft_exit(win);
|
2024-01-06 17:15:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int ac, char **av)
|
|
|
|
{
|
2024-01-07 22:45:08 +01:00
|
|
|
t_window *win;
|
2024-01-13 17:17:22 +01:00
|
|
|
char **map;
|
2024-01-24 13:45:01 +01:00
|
|
|
t_coords map_size;
|
2024-01-06 17:15:02 +01:00
|
|
|
|
|
|
|
(void) ac;
|
2024-01-19 15:41:43 +01:00
|
|
|
map = NULL;
|
2024-01-22 14:13:58 +01:00
|
|
|
if (ac != 2)
|
|
|
|
ft_send_error("Invalid number of argument\n", map);
|
2024-01-19 15:41:43 +01:00
|
|
|
if (ft_valid_file_ext(av[1]) == 1)
|
|
|
|
ft_send_error("Invalid map file extension (not .ber)\n", map);
|
2024-01-22 14:13:58 +01:00
|
|
|
if (ft_check_file(av[1]) == 1)
|
|
|
|
ft_send_error("File cannot be opened or doesn't exist\n", map);
|
2024-01-28 18:26:25 +01:00
|
|
|
if (ft_checknl(av[1]) == 1)
|
|
|
|
ft_send_error("File containt empty line\n", map);
|
|
|
|
map = ft_map_setup(map, av);
|
2024-01-24 13:45:01 +01:00
|
|
|
map_size.x = ft_strlen(map[0]);
|
|
|
|
map_size.y = ft_mapsize(map);
|
2024-01-28 18:26:25 +01:00
|
|
|
win = ft_init_window(map);
|
|
|
|
win->win = mlx_new_window(win->mlx,
|
|
|
|
map_size.x * T_SIZE, map_size.y * T_SIZE, "so_fluffy");
|
2024-01-19 15:41:43 +01:00
|
|
|
if (!win->win)
|
2024-01-28 18:26:25 +01:00
|
|
|
ft_destroy_display(win, map);
|
2024-01-13 17:17:22 +01:00
|
|
|
win->map = map;
|
2024-01-28 18:26:25 +01:00
|
|
|
ft_play(win);
|
2024-01-06 17:15:02 +01:00
|
|
|
}
|