Archived
1
0
This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
so_long/main.c

133 lines
3.3 KiB
C
Raw Normal View History

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-24 13:45:01 +01:00
/* Updated: 2024/01/24 12:17:09 by adjoly ### ########.fr */
2024-01-06 17:15:02 +01:00
/* */
/* ************************************************************************** */
2024-01-24 13:45:01 +01:00
#include "MacroLibX/includes/mlx.h"
#include "printf/ft_printf.h"
2024-01-06 17:15:02 +01:00
#include "so_long.h"
2024-01-24 13:45:01 +01:00
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
2024-01-22 14:13:58 +01:00
void ft_freeimg(t_window *win)
{
2024-01-22 14:13:58 +01:00
mlx_destroy_image(win->mlx, win->img->collectible);
mlx_destroy_image(win->mlx, win->img->exit);
mlx_destroy_image(win->mlx, win->img->ground);
mlx_destroy_image(win->mlx, win->img->player);
mlx_destroy_image(win->mlx, win->img->wall);
free (win->img);
2024-01-19 15:41:43 +01:00
}
2024-01-24 13:45:01 +01:00
size_t ft_mapsize(char **map)
{
size_t i;
i = 0;
while (*map && i < SIZE_MAX)
{
map++;
i++;
}
return (i);
}
2024-01-22 14:13:58 +01:00
void ft_freemap(char **map)
2024-01-19 15:41:43 +01:00
{
2024-01-24 13:45:01 +01:00
int i;
i = 0;
2024-01-24 13:45:01 +01:00
while (map[i])
{
2024-01-22 14:13:58 +01:00
free(map[i]);
i++;
}
2024-01-24 13:45:01 +01:00
free(map);
}
2024-01-06 17:15:02 +01:00
int win_close(int event, void *param)
{
2024-01-22 14:13:58 +01:00
t_window *win;
win = (t_window *)param;
2024-01-06 17:15:02 +01:00
if (event == 0)
2024-01-22 14:13:58 +01:00
ft_exit(win);
2024-01-06 17:15:02 +01:00
return (0);
}
2024-01-13 19:04:22 +01:00
void ft_exit(t_window *win)
2024-01-07 22:45:08 +01:00
{
2024-01-22 14:13:58 +01:00
ft_freeimg(win);
2024-01-13 19:04:22 +01:00
mlx_destroy_window(win->mlx, win->win);
2024-01-24 13:45:01 +01:00
mlx_loop_end(win->mlx);
2024-01-13 19:04:22 +01:00
mlx_destroy_display(win->mlx);
ft_freemap(win->map);
2024-01-24 13:45:01 +01:00
free(win->p_coords);
free(win->e_coords);
free(win);
2024-01-22 14:13:58 +01:00
exit (EXIT_SUCCESS);
2024-01-13 19:04:22 +01:00
}
2024-01-07 22:45:08 +01:00
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;
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-24 13:45:01 +01:00
map = ft_read_map(av[1], map);
2024-01-19 15:41:43 +01:00
if (!map)
2024-01-22 14:13:58 +01:00
exit(EXIT_SUCCESS);
if (!map[0])
2024-01-24 13:45:01 +01:00
ft_send_error("Map is empty\n", map);
2024-01-19 15:41:43 +01:00
ft_check_map_error(map);
2024-01-24 13:45:01 +01:00
ft_freemap(map);
map = ft_read_map(av[1], map);
if (!map)
exit(EXIT_SUCCESS);
map_size.x = ft_strlen(map[0]);
map_size.y = ft_mapsize(map);
2024-01-22 14:13:58 +01:00
win = malloc(sizeof(map) + sizeof(t_window));
2024-01-19 15:41:43 +01:00
if (!win)
{
ft_freemap(map);
return (0);
}
2024-01-22 14:13:58 +01:00
// protect all three
win->p_coords = malloc(sizeof(t_coords));
win->e_coords = malloc(sizeof(t_coords));
win->img = malloc(sizeof(t_img));
2024-01-07 22:45:08 +01:00
win->mlx = mlx_init();
2024-01-19 15:41:43 +01:00
if (!win->mlx)
// free map img win;
return (0);
2024-01-24 13:45:01 +01:00
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)
// free map img win ; destroy mlx
return (0);
win->map = map;
2024-01-13 19:04:22 +01:00
win->mov_count = 0;
2024-01-24 13:45:01 +01:00
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);
2024-01-22 14:13:58 +01:00
ft_alloc_img(win);
ft_printmap(win->map, win);
2024-01-07 22:45:08 +01:00
mlx_loop(win->mlx);
2024-01-19 15:41:43 +01:00
ft_exit(win);
2024-01-06 17:15:02 +01:00
return (0);
}