-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
61 lines (39 loc) · 1.15 KB
/
main.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
#include <stdlib.h>
#include "SDL.h"
#include "game.h"
const int TARGET_FPS = 60;
const int DELAY_TIME = 1000 / TARGET_FPS;
int main() {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
printf("SDL could not be initialized!\n");
printf("SDL_Error: %s\n", SDL_GetError());
return 1;
}
Game * game = malloc(sizeof (Game));
int gameInit = initializeGame(game);
if(gameInit != 0){
printf("Game could not be initialized!");
return 1;
}
SDL_TimerID spawnApples = SDL_AddTimer(1000, spawnApplesTimer, game);
SDL_Event e;
while (game->isRunning){
Uint32 frameStart = SDL_GetTicks();
while(SDL_PollEvent(&e) != 0){
if(e.type == SDL_QUIT){
game->isRunning = false;
}
handleGameEvents(game,e);
}
updateGame(game);
renderGame(game);
Uint32 frameTime = SDL_GetTicks() - frameStart;
if(frameTime < DELAY_TIME){
SDL_Delay(DELAY_TIME - frameTime);
}
}
//pthread_join(thread, NULL);
SDL_RemoveTimer(spawnApples);
cleanupGame(game);
SDL_Quit();
}