-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap0.c
102 lines (90 loc) · 2.35 KB
/
map0.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map0.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joleksia <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/11 09:02:14 by joleksia #+# #+# */
/* Updated: 2025/01/11 09:02:15 by joleksia ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
static t_image *__sl_map_getimg(t_game *game, t_vec2i p);
static int __sl_map_dcell(t_game *game, t_vec2i p);
int sl_readmap(t_game *game, const char *path)
{
char *fstr;
if (!ft_strchr(path, '.') || ft_memcmp(ft_strchr(path, '.'), ".ber", 5))
{
ft_printf("Error\nType: Invalid filepath: %s\n", path);
game->s_map.data = NULL;
return (0);
}
fstr = sl_readfile(path);
if (!fstr)
{
ft_printf("Error\nType:File processing error\n");
game->s_map.data = NULL;
return (0);
}
game->s_map.data = ft_split(fstr, '\n');
free(fstr);
game->s_map.s = 32;
game->s_map.spots[0] = 0;
game->s_map.spots[1] = 0;
game->s_map.spots[2] = 0;
return (1);
}
int sl_dmap(t_game *game)
{
t_vec2i iter;
iter[1] = -1;
while (++iter[1] < game->s_map.h)
{
iter[0] = -1;
while (++iter[0] < game->s_map.w)
{
__sl_map_dcell(game, iter);
}
}
return (1);
}
int sl_unload_map(t_game *game)
{
size_t i;
if (!game->s_map.data)
return (0);
i = -1;
while (game->s_map.data[++i])
free(game->s_map.data[i]);
free(game->s_map.data);
return (1);
}
static t_image *__sl_map_getimg(t_game *game, t_vec2i p)
{
char c;
c = game->s_map.data[p[1]][p[0]];
if (c == '1')
return (&game->s_assets.imgs[SO_LONG_SPRITE_WALL]);
else if (c == 'C' || c == 'c')
return (&game->s_assets.imgs[SO_LONG_SPRITE_COLL]);
else if (c == 'E')
return (&game->s_assets.imgs[SO_LONG_SPRITE_EXIT]);
else if (c == 'P')
return (&game->s_assets.imgs[SO_LONG_SPRITE_PLAY]);
else
return (&game->s_assets.imgs[SO_LONG_SPRITE_EMPT]);
}
static int __sl_map_dcell(t_game *game, t_vec2i p)
{
t_vec2i pos;
t_vec2i siz;
t_image *img;
img = __sl_map_getimg(game, p);
siz[0] = game->s_map.s;
siz[1] = game->s_map.s;
pos[0] = p[0] * game->s_map.s;
pos[1] = p[1] * game->s_map.s;
return (sl_dimg(game, *img, pos, siz));
}