-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
113 lines (99 loc) · 2.83 KB
/
main.c
File metadata and controls
113 lines (99 loc) · 2.83 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
/*
** EPITECH PROJECT, 2024
** B-MUL-100-LYN-1-1-myhunter-aurelien.demeusy
** File description:
** main.c
*/
#include <SFML/Graphics.h>
#include <SFML/Window.h>
#include <stdlib.h>
#include "game/structs.h"
#include "include/my.h"
#include <time.h>
static void play_gun_sound(struct gun *gun)
{
if (gun && gun->sound) {
sfSound_play(gun->sound);
}
}
int evt(sfRenderWindow *wd, struct dk_n **dk_l, struct sc *sc, struct gun *gun)
{
sfEvent event;
sfVector2i mouse_pos;
while (sfRenderWindow_pollEvent(wd, &event)) {
if (event.type == sfEvtClosed)
return 0;
if (event.type == sfEvtMouseButtonPressed) {
mouse_pos = sfMouse_getPositionRenderWindow(wd);
play_gun_sound(gun);
click(mouse_pos, dk_l, sc);
}
}
return 1;
}
static void cleanup_game(struct game *game)
{
sfRenderWindow_destroy(game->window);
sfSprite_destroy(game->bg_sprite);
sfTexture_destroy(game->bg_texture);
free(game);
}
static void render_background(struct game *game)
{
sfRenderWindow_drawSprite(game->window, game->bg_sprite, NULL);
}
static void cleanup_clock(sfClock *clock)
{
sfClock_destroy(clock);
}
void render_ducks_list(struct dk_n *duck_list, sfRenderWindow *window)
{
render_ducks(duck_list, window);
}
void rd_gm(struct game *gm, struct sc *sc, struct hp *hp, struct dk_n *dk_l)
{
sfRenderWindow_clear(gm->window, sfBlack);
render_background(gm);
render_ducks_list(dk_l, gm->window);
render_score(sc, gm->window);
render_hp(hp, gm->window);
sfRenderWindow_display(gm->window);
}
void cln(struct sc *sc, struct hp *hp, struct dk_n *dk_l, sfClock *ck)
{
cleanup_score(sc);
cleanup_hp(hp);
cleanup_duck_list(dk_l);
cleanup_clock(ck);
}
static void game_loop(struct game_state *state)
{
float spawn_timer = 0.0f;
float delta_time;
while (sfRenderWindow_isOpen(state->gm->window)) {
if (!evt(state->gm->window, state->dk_l, state->sc, state->playerGun))
break;
delta_time = sfTime_asSeconds(sfClock_restart(state->ck));
spawn_timer += delta_time;
spawn_duck(state->dk_l, &spawn_timer);
updt_dk_l(state->dk_l, delta_time, state->hp, state->gm);
rd_gm(state->gm, state->sc, state->hp, *state->dk_l);
sfRenderWindow_setFramerateLimit(state->gm->window, 30);
}
}
int main(int argc, char **argv)
{
struct game *game = init_game();
struct sc *score = init_score();
struct hp *hp = init_hp();
struct dk_n *duck_list = NULL;
sfClock *clock = sfClock_create();
struct gun playerGun;
struct game_state st = { game, score, hp, &duck_list, &playerGun, clock };
h_option(argc, argv);
init_gun(&playerGun, "sound/bang_sound.mp3");
game_loop(&st);
cleanup_game(game);
cln(score, hp, duck_list, clock);
return 0;
}