iMac : cc -lmlx -framework OpenGL -framework AppKit *.c
MacBook (M2) : cc -L./mlx -lmlx -framework OpenGL -framework AppKit *.c
subject의 minilibx_opengl.tgz
폴더 다운로드
과제 폴더에 mlx
이름으로 변경하여 추가
Makefile 수정
%.o:%.c so_long.h
cc -Wall -Wextra -Werror -c $< -o $@
$(NAME) : $(OBJS)
make -C ./mlx/
cc -o so_long $(OBJS) -L./mlx -lmlx -framework OpenGL -framework AppKit
#include "./mlx/mlx.h"
int main(void)
{
void *mlx;
void *win;
mlx = mlx_init();
win = mlx_new_window(mlx, 500, 500, "so_long");
mlx_loop(mlx);
}
#include "so_long.h"
void init(t_param *p)
{
p->play_x = 3;
p->play_y = 4;
p->mlx = mlx_init();
p->win = mlx_new_window(p->mlx, 500, 500, "so_long");
p->img_coin = mlx_xpm_file_to_image(p->mlx, "./imgs/coin.xpm", &p->img_x, &p->img_y);
p->img_exit = mlx_xpm_file_to_image(p->mlx, "./imgs/exit.xpm", &p->img_x, &p->img_y);
p->img_map = mlx_xpm_file_to_image(p->mlx, "./imgs/map.xpm", &p->img_x, &p->img_y);
p->img_mario = mlx_xpm_file_to_image(p->mlx, "./imgs/mario.xpm", &p->img_x, &p->img_y);
p->img_wall = mlx_xpm_file_to_image(p->mlx, "./imgs/wall.xpm", &p->img_x, &p->img_y);
}
int main(void)
{
t_param p;
init(&p);
mlx_put_image_to_window(p.mlx, p.win, p.img_wall, 0, 0);
mlx_put_image_to_window(p.mlx, p.win, p.img_map, 64, 0);
mlx_put_image_to_window(p.mlx, p.win, p.img_coin, 128, 0);
mlx_put_image_to_window(p.mlx, p.win, p.img_exit, 0, 128);
mlx_put_image_to_window(p.mlx, p.win, p.img_mario, 0, 64);
mlx_loop(p.mlx);
}
int map_strjoin(t_param *p, char *str)
{
t_list *cur;
t_list *new;
cur = p->map;
new = malloc(sizeof(t_list));
if (!new) {
map_free(p);
return (1);
}
new->data = ft_strdup(str);
new->next = NULL;
if (cur == NULL)
p->map = new;
else {
while (cur->next != NULL)
cur = cur->next;
cur->next = new;
}
return (0);
}
int map_read(t_param *p, char *file)
{
int fd;
char *line;
fd = open(file, O_RDONLY);
if (fd <= 0)
return (1);
line = get_next_line(fd);
ft_printf("line = %s\n", line);
while (line != NULL)
{
if (map_strjoin(p, line))
return (1);
free(line);
line = get_next_line(fd);
ft_printf("line = %s\n", line);
}
free(line);
return (0);
}
void map_free(t_param *p)
{
t_list *cur;
t_list *new;
cur = p->map;
while (cur)
{
new = cur->next;
free (cur->data);
free (cur);
cur = new;
}
free (cur);
}
int main(void)
{
t_param p;
init(&p);
map_read(&p, "map/map.ber");
mlx_put_image_to_window(p.mlx, p.win, p.img_wall, 0, 0);
mlx_put_image_to_window(p.mlx, p.win, p.img_map, 64, 0);
mlx_put_image_to_window(p.mlx, p.win, p.img_coin, 128, 0);
mlx_put_image_to_window(p.mlx, p.win, p.img_exit, 0, 128);
mlx_put_image_to_window(p.mlx, p.win, p.img_mario, 0, 64);
mlx_loop(p.mlx);
}
void map_set(t_param *p)
{
t_list *cur;
char *line;
int x;
int y;
x = 0;
y = 0;
cur = p->map;
init_player(p);
mlx_clear_window(p->mlx, p->win);
while (cur)
{
line = cur->data;
while (*line)
{
map_imgs(p, *line, x, y);
line++;
x += 64;
}
x = 0;
y += 64;
cur = cur->next;
}
}
void map_imgs(t_param *p, char c, int x, int y)
{
mlx_put_image_to_window(p->mlx, p->win, p->img_map, x, y);
if (c == '1')
mlx_put_image_to_window(p->mlx, p->win, p->img_wall, x, y);
else if (c == '0')
mlx_put_image_to_window(p->mlx, p->win, p->img_map, x, y);
else if (c == 'C')
mlx_put_image_to_window(p->mlx, p->win, p->img_coin, x, y);
else if (c == 'E')
mlx_put_image_to_window(p->mlx, p->win, p->img_exit, x, y);
else if (c == 'P')
mlx_put_image_to_window(p->mlx, p->win, p->img_mario, x, y);
}
void init_player(t_param *p)
{
t_list *cur;
char *line;
cur = p->map;
while (cur)
{
line = cur->data;
while (*line)
{
if (*line++ == 'P')
return ;
p->play_x++;
}
p->play_x = 0;
p->play_y++;
cur = cur->next;
}
}
(수정) 뒤에 map 배경 모두 설정한 후 이미지 출력
int main(void)
{
t_param p;
init(&p);
if (map_read(&p, "map/map.ber"))
{
ft_printf("Error : Map Read Fail");
map_free(&p);
exit(0);
return (0);
}
map_check(&p);
p.win = mlx_new_window(p.mlx, p.win_x, p.win_y, "so_long");
map_set(&p);
mlx_key_hook(p.win, &key_press, &p);
mlx_hook(p.win, RED, 0, &map_red, &p);
mlx_loop(p.mlx);
return (0);
}
ESC
및 좌측 빨간 버튼
: 종료#include "./mlx/mlx.h"
#include "./ft_printf/ft_printf.h"
# define KEY_PRESS 2
# define KEY_RELEASE 3
# define KEY_W 13
# define KEY_A 0
# define KEY_S 1
# define KEY_D 2
# define KEY_ESC 53
# define KEY_RED 17
typedef struct s_param
{
int x;
int y;
} t_param;
void init(t_param *p)
{
p->x = 3;
p->y = 4;
}
int key_press(int key, t_param *p)
{
if (key == KEY_W)
key_W(p);
else if (key == KEY_A)
key_A(p);
else if (key == KEY_S)
key_S(p);
else if (key == KEY_D)
key_D(p);
else if (key == KEY_ESC)
exit(0);
else if (key == KEY_RED)
exit(0);
ft_printf("x : %d, y : %d\n", p->x, p->y);
return (0);
}
void key_W(t_param *p)
{
p->y++;
}
void key_A(t_param *p)
{
p->x--;
}
void key_S(t_param *p)
{
p->y--;
}
void key_D(t_param *p)
{
p->x++;
}
int main(void)
{
void *mlx;
void *win;
t_param p;
init(&p);
mlx = mlx_init();
win = mlx_new_window(mlx, 500, 500, "so_long");
mlx_hook(win, KEY_RELEASE, 0, &key_press, &p);
mlx_loop(mlx);
if (win == 0)
return(0);
}
// 마리오(P)가 지나가면 벽(1), 코인(C), 출구(E) 모두 맵(0)으로 바꿈
#include "so_long.h"
int key_press(int key, t_param *p)
{
if (key == W)
key_W(p);
else if (key == A)
key_A(p);
else if (key == S)
key_S(p);
else if (key == D)
key_D(p);
else if (key == ESC)
map_esc(p);
ft_printf("move = %d\n", p->move);
map_set(p);
return (0);
}
void key_A(t_param *p)
{
int y;
char *line;
t_list *cur;
cur = p->map;
y = p->play_y;
while (y--)
cur = cur->next;
line = cur->data;
line[p->play_x - 1] = 'P';
line[p->play_x] = '0';
init_player(p);
p->move++;
}
int key_press(int key, t_param *p)
{
if (key == W)
key_up(p);
else if (key == A)
key_left(p);
else if (key == S)
key_down(p);
else if (key == D)
key_right(p);
else if (key == ESC)
map_esc(p);
else
return (1);
ft_printf("move = %d\n", p->move);
map_set(p);
return (0);
}
void key_up(t_param *p)
{
int y;
char *line;
t_list *cur;
char *cur_prev;
cur = p->map;
y = p->play_y;
while (y-- != 1)
cur = cur->next;
cur_prev = cur->data;
cur = cur->next;
line = cur->data;
if (cur_prev[p->play_x] == '1')
return ;
else if (cur_prev[p->play_x] == 'E')
if (map_exit(p))
return ;
if (cur_prev[p->play_x] == 'C')
p->coin--;
cur_prev[p->play_x] = 'P';
line[p->play_x] = '0';
init_player(p);
p->move++;
}
void key_left(t_param *p)
{
int y;
char *line;
t_list *cur;
cur = p->map;
y = p->play_y;
while (y--)
cur = cur->next;
line = cur->data;
if (line[p->play_x - 1] == '1')
return ;
else if (line[p->play_x - 1] == 'E')
if (map_exit(p))
return ;
if (line[p->play_x - 1] == 'C')
p->coin--;
line[p->play_x - 1] = 'P';
line[p->play_x] = '0';
init_player(p);
p->move++;
}
void key_down(t_param *p)
{
int y;
char *line;
t_list *cur;
char *cur_next;
cur = p->map;
y = p->play_y;
while (y--)
cur = cur->next;
line = cur->data;
cur = cur->next;
cur_next = cur->data;
if (cur_next[p->play_x] == '1')
return ;
else if (cur_next[p->play_x] == 'E')
if (map_exit(p))
return ;
if (cur_next[p->play_x] == 'C')
p->coin--;
line[p->play_x] = '0';
cur_next[p->play_x] = 'P';
init_player(p);
p->move++;
}
void key_right(t_param *p)
{
int y;
char *line;
t_list *cur;
cur = p->map;
y = p->play_y;
while (y--)
cur = cur->next;
line = cur->data;
if (line[p->play_x + 1] == '1')
return ;
else if (line[p->play_x + 1] == 'E')
if (map_exit(p))
return ;
if (line[p->play_x + 1] == 'C')
p->coin--;
line[p->play_x + 1] = 'P';
line[p->play_x] = '0';
init_player(p);
p->move++;
}
// RED 버튼 누르면 segmentation fault 오류 발생
mlx_hook(p.win, RED, 0, &map_exit, &p);
int map_exit(int key, t_param *p)
{
if (key == ESC)
ft_printf("Exit : ESC");
else
ft_printf("Exit : Red Button");
map_free(p);
exit(0);
return (0);
}
// 수정 완료
mlx_hook(p.win, RED, 0, &map_red, &p);
int map_esc(t_param *p)
{
ft_printf("Exit : ESC");
map_free(p);
exit(0);
return (0);
}
int map_red(t_param *p)
{
ft_printf("Exit : Red Button");
map_free(p);
exit(0);
return (0);
}
int map_check(t_param *p)
{
t_list *cur;
int count;
cur = p->map;
count = map_check_line(p);
if (count == -1)
return (1);
p->win_x = p->img_x * (ft_strlen(cur->data) - 1);
p->win_y = p->img_y * count;
if (map_check_wall(p, count) == -1)
{
ft_printf("Error : (No Wall Enclosed) ");
return (1);
}
if (map_check_count(p) == -1)
{
ft_printf("Error : (Invalid Start / Exit / Collect) ");
return (1);
}
return (0);
}
int map_check_line(t_param *p)
{
t_list *cur;
int len;
int len_next;
int count;
cur = p->map;
len = ft_strlen(cur->data);
count = 0;
while (cur)
{
len_next = ft_strlen(cur->data);
if (len != len_next)
{
ft_printf("Error : (Different Line Lengths) ");
return (-1);
}
cur = cur->next;
count++;
}
if (count < 3)
{
ft_printf("Error : (Less than 3 Lines) ");
return (-1);
}
return (count);
}
int map_check_wall(t_param *p, int end)
{
int count;
t_list *cur;
count = 0;
cur = p->map;
while (cur)
{
if (check_wall(cur->data, count, end) == -1)
return (-1);
cur = cur->next;
count++;
}
return (0);
}
int check_wall(char *str, int count, int end)
{
char c;
if (count == 0 || count == end - 1)
{
while (*str != '\n')
{
if (*str != '1')
return (-1);
str++;
}
}
else
{
if (*str != '1')
return (-1);
while (*str != '\n')
{
c = *str;
str++;
}
if (c != '1')
return (-1);
}
return (0);
}
int map_check_count(t_param *p)
{
t_list *cur;
char *line;
cur = p->map;
while (cur)
{
line = cur->data;
while (*line)
{
if (*line == 'C')
p->coin++;
else if (*line == 'E')
p->exit++;
else if (*line == 'P')
p->player++;
line++;
}
cur = cur->next;
}
if (p->player != 1 || p->exit != 1 || p->coin < 1)
return (-1);
return (0);
}
// map_imgs 에서 확인 후 에러 처리
void map_imgs(t_param *p, char c)
{
if (c != '1' && c != '0' && c != 'C' && c != 'E' && c != 'P' && c != '\n' && c != '\0')
{
printf("%c\n", c);
ft_printf("Error : Invalid Char\n");
map_free(p);
exit(0);
}
...
}
int map_read(t_param *p, char *file)
{
...
fd = open(file, O_RDONLY);
if (fd < 0)
return (1);
line = get_next_line(fd);
if (line == NULL) // 비어있어서 line = 0일 경우 에러 발생
{
free(line);
ft_printf("Empty Map\n");
return (1);
}
}
int map_dfs(t_param *p, int x, int y, char visit[2000][2000])
{
...
else if (visit[y][x] == 'E') // 출구 만나면 재귀 빠져나오게 변경
{
p->exit++;
return (0);
}
...
}
int file_name(char *file)
{
int i;
char *ext;
i = 0;
ext = ".ber";
while (*(file + i) != '.')
i++;
while (*(file + i) || *ext)
{
if (*(file + i) != *ext)
{
ft_printf_s("Error : Invalid File name or File Extenstion\n");
return (1);
}
ext++;
i++;
}
return (0);
}
int dfs(t_param *p, int coin, int exit)
{
t_list *cur;
char *line;
char d[2000][2000];
int x;
int y;
cur = p->map;
y = 0;
while (cur)
{
x = 0;
line = cur->data;
while (*line)
d[y][x++] = *line++;
y++;
cur = cur->next;
}
map_dfs(p, p->play_x, p->play_y, d);
if (p->exit != 2 * exit || p->coin != 2 * coin)
{
ft_printf("Error : (No Map Route) ");
return (1);
}
p->coin = coin;
return (0);
}
int map_dfs(t_param *p, int x, int y, char visit[2000][2000])
{
if (x < 0 || x >= p->win_x || y < 0 || y >= p->win_y)
return (0);
if (visit[y][x] == '1')
return (0);
if (visit[y][x] != 'V')
{
if (visit[y][x] == 'E')
p->exit++;
if (visit[y][x] == 'C')
p->coin++;
visit[y][x] = 'V';
map_dfs(p, x - 1, y, visit);
map_dfs(p, x + 1, y, visit);
map_dfs(p, x, y - 1, visit);
map_dfs(p, x, y + 1, visit);
return (1);
}
return (0);
}
#include "so_long.h"
int init(t_param *p)
{
p->mlx = mlx_init();
p->img_coin = mlx_xpm_file_to_image(\
p->mlx, "imgs/coin.xpm", &p->x, &p->y);
p->img_exit = mlx_xpm_file_to_image(\
p->mlx, "imgs/exit.xpm", &p->x, &p->y);
p->img_map = mlx_xpm_file_to_image(\
p->mlx, "imgs/map.xpm", &p->x, &p->y);
p->img_mario = mlx_xpm_file_to_image(\
p->mlx, "imgs/mario.xpm", &p->x, &p->y);
p->img_wall = mlx_xpm_file_to_image(\
p->mlx, "imgs/wall.xpm", &p->x, &p->y);
p->map = NULL;
p->move = 1;
p->count = 0;
p->coin = 0;
p->coin_check = 0;
p->exit = 0;
p->player = 0;
return (1);
}
void init_player(t_param *p)
{
t_list *cur;
char *line;
cur = p->map;
p->play_x = 0;
p->play_y = 0;
while (cur)
{
line = cur->data;
while (*line)
{
if (*line++ == 'P')
return ;
p->play_x++;
}
p->play_x = 0;
p->play_y++;
cur = cur->next;
}
}
int map_exit(t_param *p)
{
if (p->coin == 0)
{
p->move++;
ft_printf_d(p->move);
ft_printf_s("Exit : GAME SUCCESS\n");
map_free(p);
exit(0);
return (0);
}
return (1);
}
int map_red_exit(t_param *p)
{
ft_printf_s("Exit : Red Button");
map_free(p);
exit(0);
return (1);
}
int main(int argc, char **argv)
{
t_param p;
if (argc != 2 || file_name(argv[1]))
{
ft_printf_s("Error : Few argument or Invalid File Extenstion\n");
return (-1);
}
if (init(&p) && map_read(&p, argv[1]))
return (-1);
if (map_check(&p))
{
map_free(&p);
return (-1);
}
p.win = mlx_new_window(p.mlx, p.win_x, p.win_y, "so_long");
map_set(&p);
if (dfs(&p))
{
map_free(&p);
return (-1);
}
mlx_key_hook(p.win, &move, &p);
mlx_hook(p.win, RED, 0, &map_red_exit, &p);
mlx_loop(p.mlx);
return (0);
}
#include "so_long.h"
int map_read(t_param *p, char *file)
{
int fd;
char *line;
fd = open(file, O_RDONLY);
if (fd < 0)
{
ft_printf_s("Error : Read Fail or Invalid File name\n");
return (1);
}
line = get_next_line(fd);
if (line == NULL)
{
free(line);
ft_printf_s("Error : Invalid Map (Empty Map)\n");
return (1);
}
while (line != NULL)
{
map_strjoin(p, line);
free(line);
line = get_next_line(fd);
}
free(line);
return (0);
}
int map_strjoin(t_param *p, char *line)
{
t_list *cur;
t_list *new;
cur = p->map;
new = malloc(sizeof(t_list));
if (!new)
{
map_free(p);
return (0);
}
new->data = ft_strdup(line);
new->next = NULL;
if (cur == NULL)
p->map = new;
else
{
while (cur->next != NULL)
cur = cur->next;
cur->next = new;
}
return (1);
}
void map_set(t_param *p)
{
t_list *cur;
char *line;
p->x = 0;
p->y = 0;
cur = p->map;
init_player(p);
mlx_clear_window(p->mlx, p->win);
while (cur)
{
line = cur->data;
while (*line)
{
map_imgs(p, *line);
line++;
p->x += 64;
}
p->x = 0;
p->y += 64;
cur = cur->next;
}
}
void map_imgs(t_param *p, char c)
{
if (c != '1' && c != '0' && c != 'C' && c != 'E' && c != 'P' \
&& c != '\n' && c != '\0')
{
ft_printf_s("Error : Invalid Char\n");
map_free(p);
exit(0);
}
mlx_put_image_to_window(\
p->mlx, p->win, p->img_map, p->x, p->y);
if (c == '1')
mlx_put_image_to_window(\
p->mlx, p->win, p->img_wall, p->x, p->y);
else if (c == '0')
mlx_put_image_to_window(\
p->mlx, p->win, p->img_map, p->x, p->y);
else if (c == 'C')
mlx_put_image_to_window(\
p->mlx, p->win, p->img_coin, p->x, p->y);
else if (c == 'E')
mlx_put_image_to_window(\
p->mlx, p->win, p->img_exit, p->x, p->y);
else if (c == 'P')
mlx_put_image_to_window(\
p->mlx, p->win, p->img_mario, p->x, p->y);
}
void map_free(t_param *p)
{
t_list *cur;
t_list *new;
cur = p->map;
while (cur)
{
new = cur->next;
free (cur->data);
free (cur);
cur = new;
}
free (cur);
exit(0);
}
#include "so_long.h"
int move(int key, t_param *p)
{
if (key == W)
move_up(p);
else if (key == A)
move_left(p);
else if (key == S)
move_down(p);
else if (key == D)
move_right(p);
else if (key == ESC)
{
ft_printf_s("Exit : ESC");
map_free(p);
exit(0);
}
else
return (1);
map_set(p);
return (0);
}
void move_up(t_param *p)
{
int y;
char *line;
t_list *cur;
char *cur_prev;
cur = p->map;
y = p->play_y;
while (y-- != 1)
cur = cur->next;
cur_prev = cur->data;
cur = cur->next;
line = cur->data;
if (cur_prev[p->play_x] == '1')
return ;
else if (cur_prev[p->play_x] == 'E')
if (map_exit(p))
return ;
if (cur_prev[p->play_x] == 'C')
p->coin--;
cur_prev[p->play_x] = 'P';
line[p->play_x] = '0';
init_player(p);
ft_printf_d(p->move);
p->move++;
}
void move_left(t_param *p)
{
int y;
char *line;
t_list *cur;
cur = p->map;
y = p->play_y;
while (y--)
cur = cur->next;
line = cur->data;
if (line[p->play_x - 1] == '1')
return ;
else if (line[p->play_x - 1] == 'E')
if (map_exit(p))
return ;
if (line[p->play_x - 1] == 'C')
p->coin--;
line[p->play_x - 1] = 'P';
line[p->play_x] = '0';
init_player(p);
ft_printf_d(p->move);
p->move++;
}
void move_down(t_param *p)
{
int y;
char *line;
t_list *cur;
char *cur_next;
cur = p->map;
y = p->play_y;
while (y--)
cur = cur->next;
line = cur->data;
cur = cur->next;
cur_next = cur->data;
if (cur_next[p->play_x] == '1')
return ;
else if (cur_next[p->play_x] == 'E')
if (map_exit(p))
return ;
if (cur_next[p->play_x] == 'C')
p->coin--;
line[p->play_x] = '0';
cur_next[p->play_x] = 'P';
init_player(p);
ft_printf_d(p->move);
p->move++;
}
void move_right(t_param *p)
{
int y;
char *line;
t_list *cur;
cur = p->map;
y = p->play_y;
while (y--)
cur = cur->next;
line = cur->data;
if (line[p->play_x + 1] == '1')
return ;
else if (line[p->play_x + 1] == 'E')
if (map_exit(p))
return ;
if (line[p->play_x + 1] == 'C')
p->coin--;
line[p->play_x + 1] = 'P';
line[p->play_x] = '0';
init_player(p);
ft_printf_d(p->move);
p->move++;
}
#include "so_long.h"
int map_check(t_param *p)
{
t_list *cur;
cur = p->map;
if (map_check_line(p) == -1)
return (1);
p->win_x = p->x * (ft_strlen(cur->data) - 1);
p->win_y = p->y * p->count;
if (map_check_wall(p, p->count) == -1)
{
ft_printf_s("Error : Invalid Map (No Wall Enclosed) \n");
return (1);
}
if (map_check_count(p) == -1)
{
ft_printf_s("Error : Invalid Map (Invalid Start / Exit / Collect) \n");
return (1);
}
return (0);
}
int map_check_line(t_param *p)
{
t_list *cur;
int len;
int len_next;
cur = p->map;
len = ft_strlen(cur->data);
while (cur)
{
len_next = ft_strlen(cur->data);
if (len != len_next)
{
ft_printf_s("Error : Invalid Map (Different Line Lengths) \n");
return (-1);
}
cur = cur->next;
p->count++;
}
if (p->count < 3)
{
ft_printf_s("Error : Invalid Map (Less than 3 Lines) \n");
return (-1);
}
return (1);
}
int map_check_wall(t_param *p, int end)
{
int count;
t_list *cur;
count = 0;
cur = p->map;
while (cur)
{
if (check_wall(cur->data, count, end) == -1)
return (-1);
cur = cur->next;
count++;
}
return (0);
}
int check_wall(char *str, int count, int end)
{
char c;
if (count == 0 || count == end - 1)
{
while (*str != '\n')
if (*str++ != '1')
return (-1);
}
else
{
if (*str != '1')
return (-1);
while (*str != '\n')
c = *str++;
if (c != '1')
return (-1);
}
return (0);
}
int map_check_count(t_param *p)
{
t_list *cur;
char *line;
cur = p->map;
while (cur)
{
line = cur->data;
while (*line)
{
if (*line == 'C')
p->coin++;
else if (*line == 'E')
p->exit++;
else if (*line == 'P')
p->player++;
line++;
}
cur = cur->next;
}
if (p->player != 1 || p->exit != 1 || p->coin < 1)
return (-1);
return (0);
}
#include "so_long.h"
int dfs(t_param *p)
{
t_list *cur;
char *line;
char **visit;
cur = p->map;
p->y = 0;
visit = (char **)malloc(sizeof(char *) * (p->count + 1));
while (cur)
{
p->x = 0;
line = cur->data;
visit[p->y] = (char *)malloc(sizeof(char) * (ft_strlen(line) + 1));
while (*line)
visit[p->y][p->x++] = *line++;
p->y++;
cur = cur->next;
}
dfs_recur(p, p->play_x, p->play_y, visit);
if (p->coin != p->coin_check)
{
ft_printf_s("Error : Invalid Map Route\n");
return (1);
}
free(visit);
return (0);
}
int dfs_recur(t_param *p, int x, int y, char **visit)
{
if (x < 0 || x >= p->win_x || y < 0 || y >= p->win_y)
return (0);
if (visit[y][x] == '1')
return (0);
else if (visit[y][x] == 'E')
return (0);
else if (visit[y][x] != 'V')
{
if (visit[y][x] == 'C')
p->coin_check++;
visit[y][x] = 'V';
dfs_recur(p, x - 1, y, visit);
dfs_recur(p, x + 1, y, visit);
dfs_recur(p, x, y - 1, visit);
dfs_recur(p, x, y + 1, visit);
return (1);
}
return (0);
}
int file_name(char *file)
{
int i;
char *ext;
i = 0;
ext = ".ber";
while (*(file + i) != '.')
i++;
while (*(file + i) || *ext)
{
if (*(file + i) != *ext)
return (1);
ext++;
i++;
}
return (0);
}
void ft_printf_s(char *s)
{
size_t i;
char c;
i = 0;
if (!s)
{
if (write(1, "(null)", 6) == -1)
return ;
}
while (*(s + i))
{
c = *(s + i);
write(1, &c, 1);
i++;
}
}
void ft_printf_d(long long n)
{
int i;
char c[12];
i = 0;
if (n < 0)
{
c[0] = '-';
write(1, &c, 1);
n *= -1;
}
while (n >= 10)
{
c[i] = (n % 10) + 48;
n /= 10;
i++;
}
c[i] = (n % 10) + 48;
while (i >= 0)
{
write(1, &c[i], 1);
i--;
}
c[0] = '\n';
write(1, &c, 1);
}
#ifndef SO_LONG_H
# define SO_LONG_H
# define W 13
# define A 0
# define S 1
# define D 2
# define ESC 53
# define RED 17
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <fcntl.h>
# include "mlx/mlx.h"
# include "libft/get_next_line.h"
typedef struct s_list
{
char *data;
struct s_list *next;
} t_list;
typedef struct s_param
{
void *mlx;
void *win;
void *img_mario;
void *img_map;
void *img_coin;
void *img_wall;
void *img_exit;
t_list *map;
int win_x;
int win_y;
int x;
int y;
int play_x;
int play_y;
int move;
int count;
int coin;
int coin_check;
int exit;
int player;
} t_param;
void ft_printf_s(char *s);
void ft_printf_d(long long n);
int file_name(char *file);
int init(t_param *p);
void init_player(t_param *p);
int move(int key, t_param *p);
void move_up(t_param *p);
void move_left(t_param *p);
void move_down(t_param *p);
void move_right(t_param *p);
int map_read(t_param *p, char *file);
int map_strjoin(t_param *p, char *line);
void map_set(t_param *p);
void map_imgs(t_param *p, char c);
void map_free(t_param *p);
int map_exit(t_param *p);
int map_red_exit(t_param *p);
int map_check(t_param *p);
int map_check_line(t_param *p);
int map_check_wall(t_param *p, int end);
int map_check_count(t_param *p);
int check_wall(char *str, int count, int end);
int dfs(t_param *p);
int dfs_recur(t_param *p, int x, int y, char **visit);
#endif
NAME = so_long
SRCS = libft/get_next_line.c\
libft/get_next_line_utils.c\
so_long.c\
so_long_move.c\
so_long_map.c\
so_long_check.c\
so_long_dfs.c
OBJS = $(SRCS:.c=.o)
%.o:%.c so_long.h
cc -Wall -Wextra -Werror -c $< -o $@
$(NAME) : $(OBJS)
make -C ./mlx/
cc -o so_long $(OBJS) -L./mlx -lmlx -framework OpenGL -framework AppKit
all : $(NAME)
clean :
make -C ./mlx clean
rm -rf $(OBJS)
fclean : clean
rm -rf $(NAME)
re :
make fclean
make all
.PHONY : all clean fclean re