-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.c
113 lines (104 loc) · 3.16 KB
/
setup.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
103
104
105
106
107
108
109
110
111
112
113
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* setup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hbaddrul <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 18:30:11 by hbaddrul #+# #+# */
/* Updated: 2021/11/21 19:20:32 by hbaddrul ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include <stdlib.h>
#include "so_long.h"
#include "minilibx_opengl_20191021/mlx.h"
static int update_p_or_v(t_game *g, int i, int j, int k)
{
if (k >= 0)
{
g->enemies[k].actual.x = j;
g->enemies[k].actual.y = i;
}
else
{
g->player.attempt.x = j;
g->player.attempt.y = i;
g->player.actual.x = j;
g->player.actual.y = i;
}
return (1);
}
void init_g(t_game *g)
{
g->player.c = 0;
g->player.moves = 0;
g->map.c = 0;
g->map.e = 0;
g->map.p = 0;
g->map.v = 0;
g->map.diff_cols = 0;
g->map.px = 32;
}
void init_lines_and_images(char *file, t_game *g)
{
int i;
char *line;
char **ret;
const int fd = open(file, O_RDONLY);
ret = malloc(sizeof(char *) * (g->map.rows + 1));
if (!ret)
return ;
i = 0;
line = get_next_line(fd);
while (line)
{
ret[i++] = line;
line = get_next_line(fd);
}
ret[i] = 0;
g->lines = ret;
g->o = mlx_png_file_to_image(g->mlx, "png/o.png", &g->map.px, &g->map.px);
g->c = mlx_png_file_to_image(g->mlx, "png/c1.png", &g->map.px, &g->map.px);
g->c2 = mlx_png_file_to_image(g->mlx, "png/c2.png", &g->map.px, &g->map.px);
g->e = mlx_png_file_to_image(g->mlx, "png/e.png", &g->map.px, &g->map.px);
g->p2 = mlx_png_file_to_image(g->mlx, "png/p1.png", &g->map.px, &g->map.px);
g->p3 = mlx_png_file_to_image(g->mlx, "png/p2.png", &g->map.px, &g->map.px);
g->p = g->p2;
g->v = mlx_png_file_to_image(g->mlx, "png/v.png", &g->map.px, &g->map.px);
}
void init_enemies(t_game *g)
{
t_enemy *enemies;
enemies = malloc(sizeof(t_enemy) * g->map.v);
if (!enemies)
return ;
g->enemies = enemies;
}
void put_images(t_game *g)
{
int i;
int j;
int k;
int (*f)(void *, void *, void *, int x, int y);
f = mlx_put_image_to_window;
i = -1;
k = -1;
while (++i < g->map.rows)
{
j = -1;
while (++j < g->map.cols)
{
if (g->lines[i][j] == '1')
f(g->mlx, g->win, g->o, j * g->map.px, i * g->map.px);
else if (g->lines[i][j] == 'C')
f(g->mlx, g->win, g->c, j * g->map.px, i * g->map.px);
else if (g->lines[i][j] == 'E')
f(g->mlx, g->win, g->e, j * g->map.px, i * g->map.px);
else if (g->lines[i][j] == 'P' && update_p_or_v(g, i, j, -1))
f(g->mlx, g->win, g->p, j * g->map.px, i * g->map.px);
else if (g->lines[i][j] == 'V' && update_p_or_v(g, i, j, ++k))
f(g->mlx, g->win, g->v, j * g->map.px, i * g->map.px);
}
}
}