-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_helper.c
More file actions
133 lines (125 loc) · 4.22 KB
/
main_helper.c
File metadata and controls
133 lines (125 loc) · 4.22 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main_helper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aylaaouf <aylaaouf@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/15 03:54:40 by aylaaouf #+# #+# */
/* Updated: 2025/03/20 05:46:52 by aylaaouf ### ########.fr */
/* */
/* ************************************************************************** */
#include "so_long.h"
void mlx_images(t_game *game)
{
int width;
int height;
game->coin = mlx_xpm_file_to_image(game->mlx, "./textures/coin.xpm", &width,
&height);
game->player = mlx_xpm_file_to_image(game->mlx, "./textures/player.xpm",
&width, &height);
game->wall = mlx_xpm_file_to_image(game->mlx, "./textures/wall.xpm", &width,
&height);
game->empty_space = mlx_xpm_file_to_image(game->mlx,
"./textures/empty_space.xpm", &width, &height);
game->gate = mlx_xpm_file_to_image(game->mlx, "./textures/gate.xpm", &width,
&height);
game->animation = mlx_xpm_file_to_image(game->mlx,
"./textures/animation.xpm", &width, &height);
game->killer = mlx_xpm_file_to_image(game->mlx, "./textures/killer.xpm",
&width, &height);
if (!game->coin || !game->player || !game->wall || !game->empty_space
|| !game->gate)
{
printf("Error: Failed to load one or more images\n");
free_images(game);
clean_game(game);
exit(1);
}
}
void draw_map_helper(t_game *game, char place, int x, int y)
{
if (place == 'E')
{
if (no_collectible_more(game))
mlx_put_image_to_window(game->mlx, game->win, game->animation, x,
y);
else
mlx_put_image_to_window(game->mlx, game->win, game->gate, x, y);
}
else if (place == 'C')
mlx_put_image_to_window(game->mlx, game->win, game->coin, x, y);
else if (place == 'P')
mlx_put_image_to_window(game->mlx, game->win, game->player, x, y);
else if (place == '0')
mlx_put_image_to_window(game->mlx, game->win, game->empty_space, x, y);
else if (place == '1')
mlx_put_image_to_window(game->mlx, game->win, game->wall, x, y);
else if (place == 'X')
mlx_put_image_to_window(game->mlx, game->win, game->killer, x, y);
}
void draw_map(t_game *game)
{
int i;
int j;
mlx_clear_window(game->mlx, game->win);
i = 0;
while (i < game->map->height)
{
j = 0;
while (j < game->map->width)
{
draw_map_helper(game, game->map->map[i][j], j * TILE_SIZE, i
* TILE_SIZE);
j++;
}
i++;
}
if (game->moves_count_str != NULL)
{
mlx_string_put(game->mlx, game->win, 64, 64, 0xFFFFFF, "Moves:");
mlx_string_put(game->mlx, game->win, 128, 64, 0xFFFFFF,
game->moves_count_str);
}
}
void move_player(int new_x, int new_y, t_game *game)
{
if (new_x < 0 || new_x >= game->map->width || new_y < 0
|| new_y >= game->map->height)
return ;
if (game->map->map[new_y][new_x] == 'X')
close_window(game);
if (game->map->map[new_y][new_x] == 'E' && no_collectible_more(game))
{
write(1, "You Won!\n", 9);
close_window(game);
}
if (game->map->map[new_y][new_x] != '1'
&& game->map->map[new_y][new_x] != 'E')
{
game->map->map[game->player_y][game->player_x] = '0';
game->player_x = new_x;
game->player_y = new_y;
game->map->map[game->player_y][game->player_x] = 'P';
}
else if (game->map->map[new_y][new_x] == 'X')
helper(game);
}
void handle_movement(int key, t_pos *pos, t_game *game)
{
if (key == UP_KEY && game->map->map[pos->y - 1][pos->x] != '1')
pos->y--;
else if (key == DOWN_KEY && game->map->map[pos->y + 1][pos->x] != '1')
pos->y++;
else if (key == LEFT_KEY && game->map->map[pos->y][pos->x - 1] != '1')
pos->x--;
else if (key == RIGHT_KEY && game->map->map[pos->y][pos->x + 1] != '1')
pos->x++;
else
return ;
if (game->map->map[pos->y][pos->x] != 'E')
game->moves_count++;
if (game->moves_count_str != NULL)
free(game->moves_count_str);
game->moves_count_str = ft_itoa(game->moves_count);
}