woow
This commit is contained in:
@ -6,24 +6,35 @@
|
||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/17 10:17:52 by adjoly #+# #+# */
|
||||
/* Updated: 2024/01/18 13:51:13 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/01/22 13:24:41 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft/libft.h"
|
||||
#include "printf/ft_printf.h"
|
||||
#include "so_long.h"
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <limits.h>
|
||||
|
||||
char ft_check_charset(char c, char *charset)
|
||||
{
|
||||
while (*charset)
|
||||
unsigned short i;
|
||||
|
||||
i = 0;
|
||||
while (charset[i] && i < USHRT_MAX)
|
||||
{
|
||||
if (c != *charset)
|
||||
return (1);
|
||||
charset++;
|
||||
if (c == charset[i])
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
char ft_check_file(char *file_name)
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = open(file_name, O_RDONLY);
|
||||
if (fd < 1)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
@ -38,7 +49,7 @@ char ft_valid_char(char **map)
|
||||
x = 0;
|
||||
while (map[y][x])
|
||||
{
|
||||
if (ft_check_charset(map[y][x], "01CEP") == 0)
|
||||
if (ft_check_charset(map[y][x], "01CEP\n") == 1)
|
||||
return (1);
|
||||
x++;
|
||||
}
|
||||
@ -47,6 +58,78 @@ char ft_valid_char(char **map)
|
||||
return (0);
|
||||
}
|
||||
|
||||
char ft_check_player(char **map)
|
||||
{
|
||||
size_t y;
|
||||
size_t x;
|
||||
size_t p_count;
|
||||
|
||||
y = 0;
|
||||
p_count = 0;
|
||||
while (map[y])
|
||||
{
|
||||
x = 0;
|
||||
while (map[y][x])
|
||||
{
|
||||
if (map[y][x] == 'P')
|
||||
p_count++;
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
if (p_count == 1)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
char ft_check_exit(char **map)
|
||||
{
|
||||
size_t y;
|
||||
size_t x;
|
||||
size_t p_count;
|
||||
|
||||
y = 0;
|
||||
p_count = 0;
|
||||
while (map[y])
|
||||
{
|
||||
x = 0;
|
||||
while (map[y][x])
|
||||
{
|
||||
if (map[y][x] == 'E')
|
||||
p_count++;
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
if (p_count == 1)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
char ft_check_collectible(char **map)
|
||||
{
|
||||
size_t y;
|
||||
size_t x;
|
||||
size_t c_count;
|
||||
|
||||
y = 0;
|
||||
c_count = 0;
|
||||
while (map[y])
|
||||
{
|
||||
x = 0;
|
||||
while (map[y][x])
|
||||
{
|
||||
if (map[y][x] == 'C')
|
||||
c_count++;
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
if (c_count >= 1)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*char ft_floodfill(char **map)
|
||||
{
|
||||
while (expression)
|
||||
@ -61,7 +144,9 @@ char ft_valid_file_ext(char *file_name)
|
||||
unsigned short file_len;
|
||||
|
||||
file_len = ft_strlen(file_name) - 1;
|
||||
if (file_name[file_len] == 'r' && file_name[file_len - 1] == 'e' \
|
||||
if (file_len < 3)
|
||||
return (1);
|
||||
else if (file_name[file_len] == 'r' && file_name[file_len - 1] == 'e' \
|
||||
&& file_name[file_len - 2] == 'b' && file_name[file_len - 3] == '.')
|
||||
return (0);
|
||||
return (1);
|
||||
@ -80,6 +165,13 @@ void ft_check_map_error(char **map)
|
||||
{
|
||||
if (ft_valid_char(map) == 1)
|
||||
ft_send_error("Invalid character in map\n", map);
|
||||
else if (ft_check_player(map) == 1)
|
||||
ft_send_error("Invalid number of player\n", map);
|
||||
else if (ft_check_collectible(map) == 1)
|
||||
ft_send_error("Invalid number of collectible\n", map);
|
||||
else if (ft_check_exit(map) == 1)
|
||||
ft_send_error("Invalid number of exit\n", map);
|
||||
// else if ()
|
||||
// else if (ft_floodfill(map) == 1)
|
||||
// ft_send_error("Map is not finishable\n", map);
|
||||
// ft_send_error("Map is cannot be finished\n", map);
|
||||
}
|
||||
|
Binary file not shown.
@ -1,35 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lstclear_img.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/11 18:05:14 by adjoly #+# #+# */
|
||||
/* Updated: 2024/01/14 15:18:13 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "so_long.h"
|
||||
|
||||
void ft_lstdelone_img(t_list *lst, void *param, void (*del)(void *, void *))
|
||||
{
|
||||
|
||||
if (lst == NULL || del == NULL)
|
||||
return ;
|
||||
del(lst->content, param);
|
||||
free(lst);
|
||||
}
|
||||
|
||||
void ft_lstclear_img(t_list **lst, void *param, void (*del)(void *, void *))
|
||||
{
|
||||
t_list *tmp;
|
||||
|
||||
tmp = NULL;
|
||||
while (lst && *lst && del)
|
||||
{
|
||||
tmp = (*lst)->next;
|
||||
ft_lstdelone_img((*lst), param, del);
|
||||
*lst = tmp;
|
||||
}
|
||||
}
|
Binary file not shown.
@ -6,54 +6,40 @@
|
||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/07 19:13:01 by adjoly #+# #+# */
|
||||
/* Updated: 2024/01/19 15:25:00 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/01/21 15:29:10 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "MacroLibX/includes/mlx.h"
|
||||
#include "libft/libft.h"
|
||||
#include "printf/ft_printf.h"
|
||||
#include "so_long.h"
|
||||
|
||||
void ft_printmapchar(char **map)
|
||||
{
|
||||
while (*map)
|
||||
{
|
||||
ft_printf("%s\n", *map);
|
||||
map++;
|
||||
}
|
||||
return ;
|
||||
}
|
||||
|
||||
void ft_move_up(t_window *win)
|
||||
{
|
||||
if (win->map[win->p_coords->y - 1][win->p_coords->x] == 'C')
|
||||
{
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, G_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||
win->map[win->p_coords->y - 1][win->p_coords->x] = '0';
|
||||
win->p_coords->y = win->p_coords->y - 1;
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, 'P');
|
||||
win->c_count--;
|
||||
win->mov_count++;
|
||||
}
|
||||
else if (win->p_coords->y == win->e_coords->y && win->e_coords->x == win->p_coords->x && win->map[win->p_coords->y - 1][win->p_coords->x] != '1')
|
||||
else if (win->p_coords->y == win->e_coords->y
|
||||
&& win->e_coords->x == win->p_coords->x
|
||||
&& win->map[win->p_coords->y - 1][win->p_coords->x] != '1')
|
||||
{
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, E_PNG);
|
||||
win->p_coords->y = win->p_coords->y - 1;
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, 'P');
|
||||
win->mov_count++;
|
||||
}
|
||||
else if (win->map[win->p_coords->y - 1][win->p_coords->x] == 'E' && win->c_count == 0)
|
||||
else if (win->map[win->p_coords->y - 1][win->p_coords->x] == 'E'
|
||||
&& win->c_count == 0)
|
||||
{
|
||||
win->mov_count++;
|
||||
ft_printf("%d\n", win->c_count);
|
||||
exit(0);
|
||||
ft_printf("Mouvement : %d\n", win->mov_count + 1);
|
||||
ft_exit(win);
|
||||
}
|
||||
else if (win->map[win->p_coords->y - 1][win->p_coords->x] != '1')
|
||||
{
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, G_PNG);
|
||||
win->p_coords->y = win->p_coords->y - 1;
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y - 1, win, 'P');
|
||||
win->mov_count++;
|
||||
}
|
||||
}
|
||||
@ -62,31 +48,30 @@ void ft_move_down(t_window *win)
|
||||
{
|
||||
if (win->map[win->p_coords->y + 1][win->p_coords->x] == 'C')
|
||||
{
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, G_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||
win->map[win->p_coords->y + 1][win->p_coords->x] = '0';
|
||||
win->p_coords->y = win->p_coords->y + 1;
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, 'P');
|
||||
win->c_count--;
|
||||
win->mov_count++;
|
||||
}
|
||||
else if (win->p_coords->y == win->e_coords->y && win->e_coords->x == win->p_coords->x && win->map[win->p_coords->y + 1][win->p_coords->x] != '1')
|
||||
else if (win->p_coords->y == win->e_coords->y
|
||||
&& win->e_coords->x == win->p_coords->x
|
||||
&& win->map[win->p_coords->y + 1][win->p_coords->x] != '1')
|
||||
{
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, E_PNG);
|
||||
win->p_coords->y = win->p_coords->y + 1;
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, 'P');
|
||||
win->mov_count++;
|
||||
}
|
||||
else if (win->map[win->p_coords->y + 1][win->p_coords->x] == 'E' && win->c_count == 0)
|
||||
else if (win->map[win->p_coords->y + 1][win->p_coords->x] == 'E'
|
||||
&& win->c_count == 0)
|
||||
{
|
||||
win->mov_count++;
|
||||
ft_printf("%d\n", win->c_count);
|
||||
exit(0);
|
||||
ft_printf("Mouvement : %d\n", win->mov_count + 1);
|
||||
ft_exit(win);
|
||||
}
|
||||
else if (win->map[win->p_coords->y + 1][win->p_coords->x] != '1')
|
||||
{
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, G_PNG);
|
||||
win->p_coords->y = win->p_coords->y + 1;
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y + 1, win, 'P');
|
||||
win->mov_count++;
|
||||
}
|
||||
}
|
||||
@ -95,31 +80,30 @@ void ft_move_left(t_window *win)
|
||||
{
|
||||
if (win->map[win->p_coords->y][win->p_coords->x - 1] == 'C')
|
||||
{
|
||||
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, G_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||
win->map[win->p_coords->y][win->p_coords->x - 1] = '0';
|
||||
win->p_coords->x = win->p_coords->x - 1;
|
||||
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, 'P');
|
||||
win->c_count--;
|
||||
win->mov_count++;
|
||||
}
|
||||
else if (win->p_coords->y == win->e_coords->y && win->e_coords->x == win->p_coords->x && win->map[win->p_coords->y][win->p_coords->x - 1] != '1')
|
||||
else if (win->p_coords->y == win->e_coords->y
|
||||
&& win->e_coords->x == win->p_coords->x
|
||||
&& win->map[win->p_coords->y][win->p_coords->x - 1] != '1')
|
||||
{
|
||||
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, E_PNG);
|
||||
win->p_coords->x = win->p_coords->x - 1;
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
||||
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, 'P');
|
||||
win->mov_count++;
|
||||
}
|
||||
else if (win->map[win->p_coords->y][win->p_coords->x - 1] == 'E' && win->c_count == 0)
|
||||
else if (win->map[win->p_coords->y][win->p_coords->x - 1] == 'E'
|
||||
&& win->c_count == 0)
|
||||
{
|
||||
win->mov_count++;
|
||||
ft_printf("%d\n", win->c_count);
|
||||
exit(0);
|
||||
ft_printf("Mouvement : %d\n", win->mov_count + 1);
|
||||
ft_exit(win);
|
||||
}
|
||||
else if (win->map[win->p_coords->y][win->p_coords->x - 1] != '1')
|
||||
{
|
||||
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, G_PNG);
|
||||
win->p_coords->x = win->p_coords->x - 1;
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||
ft_putimg(win->p_coords->x - 1, win->p_coords->y, win, 'P');
|
||||
win->mov_count++;
|
||||
}
|
||||
}
|
||||
@ -128,31 +112,30 @@ void ft_move_right(t_window *win)
|
||||
{
|
||||
if (win->map[win->p_coords->y][win->p_coords->x + 1] == 'C')
|
||||
{
|
||||
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, G_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||
win->map[win->p_coords->y][win->p_coords->x + 1] = '0';
|
||||
win->p_coords->x = win->p_coords->x - 1;
|
||||
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, 'P');
|
||||
win->c_count--;
|
||||
win->mov_count++;
|
||||
}
|
||||
else if (win->p_coords->y == win->e_coords->y && win->e_coords->x == win->p_coords->x && win->map[win->p_coords->y][win->p_coords->x + 1] != '1')
|
||||
else if (win->p_coords->y == win->e_coords->y
|
||||
&& win->e_coords->x == win->p_coords->x
|
||||
&& win->map[win->p_coords->y][win->p_coords->x + 1] != '1')
|
||||
{
|
||||
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, E_PNG);
|
||||
win->p_coords->x = win->p_coords->x + 1;
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, 'E');
|
||||
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, 'P');
|
||||
win->mov_count++;
|
||||
}
|
||||
else if (win->map[win->p_coords->y][win->p_coords->x + 1] == 'E' && win->c_count == 0)
|
||||
else if (win->map[win->p_coords->y][win->p_coords->x + 1] == 'E'
|
||||
&& win->c_count == 0)
|
||||
{
|
||||
win->mov_count++;
|
||||
ft_printf("%d\n", win->c_count);
|
||||
exit(0);
|
||||
ft_printf("Mouvement : %d\n", win->mov_count + 1);
|
||||
ft_exit(win);
|
||||
}
|
||||
else if (win->map[win->p_coords->y][win->p_coords->x + 1] != '1')
|
||||
{
|
||||
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, P_PNG);
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, G_PNG);
|
||||
win->p_coords->x = win->p_coords->x + 1;
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, '0');
|
||||
ft_putimg(win->p_coords->x + 1, win->p_coords->y, win, 'P');
|
||||
win->mov_count++;
|
||||
}
|
||||
}
|
||||
@ -163,25 +146,15 @@ int ft_key_event(int key, void *param)
|
||||
|
||||
win = (t_window *)param;
|
||||
if (key == 41)
|
||||
mlx_loop_end(win->mlx);
|
||||
// UP/W
|
||||
if (key == 26)
|
||||
ft_exit(win);
|
||||
if (key == 26 || key == 82)
|
||||
ft_move_up(win);
|
||||
// DOWN/S
|
||||
else if (key == 22)
|
||||
else if (key == 22 || key == 81)
|
||||
ft_move_down(win);
|
||||
// LEFT/A
|
||||
else if (key == 4)
|
||||
else if (key == 4 || key == 80)
|
||||
ft_move_left(win);
|
||||
// RIGHT/D
|
||||
else if (key == 7)
|
||||
else if (key == 7 || key == 79)
|
||||
ft_move_right(win);
|
||||
ft_printf("Mouvement : %d\n", win->mov_count);
|
||||
// ft_printmap(win->map, win);
|
||||
// ft_printmapchar(win->map);
|
||||
// ft_printf("%d\n", win->e_coords->x);
|
||||
// ft_printf("%d\n", win->e_coords->y);
|
||||
// ft_printf("%d\n", win->p_coords->x);
|
||||
// ft_printf("%d\n", win->p_coords->y);
|
||||
return (0);
|
||||
}
|
||||
|
Binary file not shown.
17
get_map.c
17
get_map.c
@ -6,19 +6,23 @@
|
||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/08 13:13:18 by adjoly #+# #+# */
|
||||
/* Updated: 2024/01/19 14:30:23 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/01/19 17:14:11 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "so_long.h"
|
||||
|
||||
size_t ft_countline_fd(int fd)
|
||||
size_t ft_countline_fd(char *file_name)
|
||||
{
|
||||
size_t line_count;
|
||||
ssize_t rd;
|
||||
char *buf;
|
||||
size_t i;
|
||||
int fd;
|
||||
|
||||
fd = open(file_name, O_RDONLY);
|
||||
if (fd < 1)
|
||||
return (0);
|
||||
i = 0;
|
||||
line_count = 0;
|
||||
buf = ft_calloc(1, 1);
|
||||
@ -31,6 +35,7 @@ size_t ft_countline_fd(int fd)
|
||||
line_count++;
|
||||
i++;
|
||||
}
|
||||
close(fd);
|
||||
free(buf);
|
||||
return (line_count);
|
||||
}
|
||||
@ -43,13 +48,9 @@ char **ft_read_map(char *file_name)
|
||||
size_t ln_count;
|
||||
|
||||
i = 0;
|
||||
fd = open(file_name, O_RDONLY);
|
||||
if (fd < 1)
|
||||
{
|
||||
ln_count = ft_countline_fd(file_name);
|
||||
if (ln_count == 0)
|
||||
return (NULL);
|
||||
}
|
||||
ln_count = ft_countline_fd(fd);
|
||||
close(fd);
|
||||
fd = open(file_name, O_RDONLY);
|
||||
map_read = ft_calloc(sizeof(char *), ln_count);
|
||||
if (!map_read)
|
||||
|
Binary file not shown.
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/01 17:12:00 by adjoly #+# #+# */
|
||||
/* Updated: 2024/01/10 15:03:00 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/01/22 13:40:26 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,7 +17,7 @@
|
||||
# include <stdlib.h>
|
||||
|
||||
# ifndef BUFFER_SIZE
|
||||
# define BUFFER_SIZE 5
|
||||
# define BUFFER_SIZE 1
|
||||
# endif
|
||||
|
||||
char *get_next_line(int fd);
|
||||
|
Binary file not shown.
109
main.c
109
main.c
@ -6,126 +6,101 @@
|
||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/06 16:18:56 by adjoly #+# #+# */
|
||||
/* Updated: 2024/01/19 14:44:15 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/01/22 13:46:27 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "MacroLibX/includes/mlx.h"
|
||||
#include "printf/ft_printf.h"
|
||||
#include "so_long.h"
|
||||
|
||||
void ft_freeimg(t_window *win)
|
||||
{
|
||||
while (*win->img)
|
||||
{
|
||||
mlx_destroy_image(win, *win->img);
|
||||
win->img++;
|
||||
}
|
||||
free(win->img);
|
||||
return ;
|
||||
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);
|
||||
}
|
||||
|
||||
size_t ft_sizemap(char **map)
|
||||
|
||||
void ft_freemap(char **map)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
i = 0;
|
||||
while (*map)
|
||||
while (map[i] && i < ULONG_MAX)
|
||||
{
|
||||
map++;
|
||||
// ft_printf("freed : %s\n", map[i]);
|
||||
free(map[i]);
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
void ft_freemap(char **map)
|
||||
{
|
||||
while (*map)
|
||||
{
|
||||
free(*map);
|
||||
map++;
|
||||
}
|
||||
free(map);
|
||||
// free(map);
|
||||
}
|
||||
|
||||
int win_close(int event, void *param)
|
||||
{
|
||||
t_window *win;
|
||||
|
||||
win = (t_window *)param;
|
||||
if (event == 0)
|
||||
mlx_loop_end(param);
|
||||
ft_exit(win);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void ft_exit(t_window *win)
|
||||
{
|
||||
ft_freeimg(win);
|
||||
mlx_destroy_window(win->mlx, win->win);
|
||||
mlx_destroy_display(win->mlx);
|
||||
ft_freemap(win->map);
|
||||
ft_freeimg(win);
|
||||
free(win->p_coords);
|
||||
free(win->e_coords);
|
||||
free(win);
|
||||
exit (0);
|
||||
// free(win->p_coords);
|
||||
// free(win->e_coords);
|
||||
// free(win);
|
||||
exit (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_window *win;
|
||||
char **map;
|
||||
void **img;
|
||||
t_coords *map_size;
|
||||
|
||||
(void) ac;
|
||||
(void) av;
|
||||
map = NULL;
|
||||
if (ac != 2)
|
||||
ft_send_error("Invalid number of argument\n", map);
|
||||
if (ft_valid_file_ext(av[1]) == 1)
|
||||
ft_send_error("Invalid map file extension (not .ber)\n", map);
|
||||
if (ft_check_file(av[1]) == 1)
|
||||
ft_send_error("File cannot be opened or doesn't exist\n", map);
|
||||
map = ft_read_map(av[1]);
|
||||
if (!map)
|
||||
return (0);
|
||||
exit(EXIT_SUCCESS);
|
||||
if (!map[0])
|
||||
ft_send_error("Map is empty", map);
|
||||
ft_check_map_error(map);
|
||||
map_size = malloc(sizeof(t_coords));
|
||||
if (!map_size)
|
||||
{
|
||||
ft_freemap(map);
|
||||
return (0);
|
||||
}
|
||||
map_size->x = ft_strlen(map[0]);
|
||||
map_size->y = ft_sizemap(map);
|
||||
ft_printf("x: %d\n", map_size->x);
|
||||
ft_printf("y: %d\n", map_size->y);
|
||||
img = malloc((map_size->x * map_size->y) * sizeof(void *));
|
||||
if (!img)
|
||||
{
|
||||
free(map_size);
|
||||
ft_freemap(map);
|
||||
return (0);
|
||||
}
|
||||
win = malloc(sizeof(map) + sizeof(t_window) + sizeof(img));
|
||||
win->img = img;
|
||||
win = malloc(sizeof(map) + sizeof(t_window));
|
||||
if (!win)
|
||||
{
|
||||
ft_freemap(map);
|
||||
return (0);
|
||||
}
|
||||
win->mlx = mlx_init();
|
||||
if (!win->mlx)
|
||||
{
|
||||
// free map img win;
|
||||
return (0);
|
||||
}
|
||||
win->win = mlx_new_window(win->mlx, 1600, 900, "so_fluffy");
|
||||
if (!win->win)
|
||||
{
|
||||
// free map img win ; destroy mlx
|
||||
return (0);
|
||||
}
|
||||
win->map = map;
|
||||
win->map_size = map_size;
|
||||
// protect all three
|
||||
win->p_coords = malloc(sizeof(t_coords));
|
||||
win->e_coords = malloc(sizeof(t_coords));
|
||||
win->img = malloc(sizeof(t_img));
|
||||
win->mlx = mlx_init();
|
||||
if (!win->mlx)
|
||||
// free map img win;
|
||||
return (0);
|
||||
win->win = mlx_new_window(win->mlx, 1600, 900, "so_fluffy");
|
||||
if (!win->win)
|
||||
// free map img win ; destroy mlx
|
||||
return (0);
|
||||
win->map = map;
|
||||
win->mov_count = 0;
|
||||
mlx_on_event(win->mlx, win->win, MLX_WINDOW_EVENT, win_close, win->mlx);
|
||||
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);
|
||||
|
4
map.ber
4
map.ber
@ -1,5 +1,5 @@
|
||||
1111111111111
|
||||
100100C0000C1
|
||||
10010000000C1
|
||||
1000011111001
|
||||
1P0011E00C001
|
||||
1P0011E000001
|
||||
1111111111111
|
||||
|
77
print_map.c
77
print_map.c
@ -6,34 +6,52 @@
|
||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/09 15:30:06 by adjoly #+# #+# */
|
||||
/* Updated: 2024/01/19 14:43:12 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/01/21 14:51:46 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "MacroLibX/includes/mlx.h"
|
||||
#include "so_long.h"
|
||||
|
||||
void ft_putplayer(t_window *win)
|
||||
{
|
||||
ft_putimg(win->p_coords->x, win->p_coords->y, win, P_PNG);
|
||||
}
|
||||
|
||||
void ft_putimg(size_t x, size_t y, t_window *win, char *file_path)
|
||||
void ft_alloc_img(t_window *win)
|
||||
{
|
||||
int img_x;
|
||||
int img_y;
|
||||
size_t img_xy;
|
||||
|
||||
img_x = 0;
|
||||
img_y = 0;
|
||||
img_xy = x + (y * win->map_size->x);
|
||||
ft_printf("%d\n", img_xy);
|
||||
if (win->img[img_xy])
|
||||
mlx_destroy_image(win->mlx, win->img[img_xy]);
|
||||
ft_putstr_fd(file_path, 1);
|
||||
ft_putchar_fd('\n', 1);
|
||||
win->img[img_xy] = mlx_png_file_to_image(win->mlx, file_path, &img_x, &img_y);
|
||||
mlx_put_image_to_window(win->mlx, win->win, win->img[img_xy], x * img_x, y * img_y);
|
||||
img_x = 0;
|
||||
win->img->collectible = mlx_png_file_to_image(win->mlx,
|
||||
C_PNG, &img_x, &img_y);
|
||||
win->img->exit = mlx_png_file_to_image(win->mlx, E_PNG, &img_x, &img_y);
|
||||
win->img->ground = mlx_png_file_to_image(win->mlx, G_PNG, &img_x, &img_y);
|
||||
win->img->player = mlx_png_file_to_image(win->mlx, P_PNG, &img_x, &img_y);
|
||||
win->img->wall = mlx_png_file_to_image(win->mlx, W_PNG, &img_x, &img_y);
|
||||
}
|
||||
|
||||
void ft_putimg(size_t x, size_t y, t_window *win, char c)
|
||||
{
|
||||
if (c == 'E')
|
||||
{
|
||||
win->e_coords->x = x;
|
||||
win->e_coords->y = y;
|
||||
mlx_put_image_to_window(win->mlx, win->win,
|
||||
win->img->exit, x * T_SIZE, y * T_SIZE);
|
||||
}
|
||||
else if (c == 'C')
|
||||
{
|
||||
mlx_put_image_to_window(win->mlx, win->win,
|
||||
win->img->collectible, x * T_SIZE, y * T_SIZE);
|
||||
win->c_count++;
|
||||
}
|
||||
else if (c == '0')
|
||||
mlx_put_image_to_window(win->mlx, win->win,
|
||||
win->img->ground, x * T_SIZE, y * T_SIZE);
|
||||
else if (c == 'P')
|
||||
{
|
||||
win->p_coords->y = y;
|
||||
win->p_coords->x = x;
|
||||
mlx_put_image_to_window(win->mlx, win->win,
|
||||
win->img->player, x * T_SIZE, y * T_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
void ft_printmap(char **map, t_window *win)
|
||||
@ -49,27 +67,16 @@ void ft_printmap(char **map, t_window *win)
|
||||
while (map[y][x])
|
||||
{
|
||||
if (map[y][x] == '1')
|
||||
ft_putimg(x, y, win, W_PNG);
|
||||
mlx_put_image_to_window(win->mlx, win->win,
|
||||
win->img->wall, x * T_SIZE, y * T_SIZE);
|
||||
else if (map[y][x] == '0')
|
||||
ft_putimg(x, y, win, G_PNG);
|
||||
ft_putimg(x, y, win, map[y][x]);
|
||||
else if (map[y][x] == 'C')
|
||||
{
|
||||
ft_putimg(x, y, win, C_PNG);
|
||||
win->c_count++;
|
||||
}
|
||||
ft_putimg(x, y, win, map[y][x]);
|
||||
else if (map[y][x] == 'E')
|
||||
{
|
||||
win->e_coords->x = x;
|
||||
win->e_coords->y = y;
|
||||
ft_putimg(x, y, win, E_PNG);
|
||||
}
|
||||
ft_putimg(x, y, win, map[y][x]);
|
||||
else if (map[y][x] == 'P')
|
||||
{
|
||||
win->p_coords->y = y;
|
||||
win->p_coords->x = x;
|
||||
ft_putimg(x, y, win, P_PNG);
|
||||
}
|
||||
|
||||
ft_putimg(x, y, win, map[y][x]);
|
||||
x++;
|
||||
}
|
||||
y++;
|
||||
|
BIN
print_map.o
BIN
print_map.o
Binary file not shown.
23
so_long.h
23
so_long.h
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/01/06 16:19:42 by adjoly #+# #+# */
|
||||
/* Updated: 2024/01/19 14:34:31 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/01/21 15:42:52 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -27,6 +27,7 @@
|
||||
# define E_PNG "assets/exit.png"
|
||||
# define W_PNG "assets/wall.png"
|
||||
# define C_PNG "assets/collectible.png"
|
||||
# define T_SIZE 64
|
||||
|
||||
typedef struct s_coords
|
||||
{
|
||||
@ -34,30 +35,42 @@ typedef struct s_coords
|
||||
int y;
|
||||
} t_coords;
|
||||
|
||||
typedef struct s_img
|
||||
{
|
||||
void *player;
|
||||
void *collectible;
|
||||
void *exit;
|
||||
void *wall;
|
||||
void *ground;
|
||||
} t_img;
|
||||
|
||||
typedef struct s_window
|
||||
{
|
||||
void *mlx;
|
||||
void *win;
|
||||
void **img;
|
||||
char **map;
|
||||
t_coords *p_coords;
|
||||
t_coords *e_coords;
|
||||
size_t c_count;
|
||||
size_t mov_count;
|
||||
t_coords *map_size;
|
||||
t_img *img;
|
||||
} t_window;
|
||||
|
||||
int ft_key_event(int key, void *param);
|
||||
|
||||
void ft_freemap(char **map);
|
||||
int check_wall(char **map, t_coords *player);
|
||||
size_t ft_countline_fd(int fd);
|
||||
size_t ft_countline_fd(char *file_name);
|
||||
char **ft_read_map(char *file_name);
|
||||
|
||||
char ft_check_file(char *file_name);
|
||||
void ft_exit(t_window *win);
|
||||
|
||||
void ft_printmap(char **map, t_window *win);
|
||||
void ft_putimg(size_t x, size_t y, t_window *win, char *file_path);
|
||||
void ft_putimg(size_t x, size_t y, t_window *win, char c);
|
||||
void ft_check_map_error(char **map);
|
||||
|
||||
void ft_alloc_img(t_window *win);
|
||||
char ft_valid_file_ext(char *file_name);
|
||||
void ft_send_error(char *msg, char **map);
|
||||
void ft_check_map_error(char **map);
|
||||
|
Reference in New Issue
Block a user