-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
126 lines (105 loc) · 2.28 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
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
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_net.h>
#include <stdlib.h>
#include "globals.h"
#include "game.h"
#include "fsm.h"
s_Game g_game;
s_StateMachine g_stateMachine;
int initSDL(const char* title, const int x, const int y, const int w, const int h);
void initMainMenu();
void handleEvents();
void update();
void render();
void clean();
int main() {
const unsigned int SCREEN_FPS = 60;
const unsigned int SCREEN_TICKS_PER_FRAME = 1000 / SCREEN_FPS;
initSDL("Floodit", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
game_init(&g_game);
fsm_init(&g_game);
Uint32 nextFrame;
nextFrame = SDL_GetTicks() + SCREEN_TICKS_PER_FRAME;
while (fsm_isRunning()) {
handleEvents();
update();
render();
Uint32 now;
now = SDL_GetTicks();
if (nextFrame > now) {
SDL_Delay(nextFrame - now);
}
nextFrame += SCREEN_TICKS_PER_FRAME;
}
clean();
printf("Exited cleanly\n");
return 0;
}
int initSDL(const char* title, const int x, const int y, const int w, const int h) {
char l_bReturn = 1;
unsigned int flags;
if (IS_GCW) {
flags = SDL_WINDOW_FULLSCREEN;
}
else {
flags = 0;
}
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL Init failed\n");
l_bReturn = 0;
}
else {
g_game.window = SDL_CreateWindow(title, x, y, w, h, flags);
if (g_game.window == 0) {
printf("Window creation failed\n");
l_bReturn = 0;
}
else {
g_game.renderer = SDL_CreateRenderer(g_game.window, -1, 0);
if (g_game.renderer == 0) {
printf("Renderer creation failed\n");
l_bReturn = 0;
}
}
}
if (TTF_Init() == -1) {
printf("TTF_Init: %s\n", TTF_GetError());
l_bReturn = 0;
}
if (SDLNet_Init() == -1) {
printf("SDLNet_Init: %s\n", SDLNet_GetError());
l_bReturn = 0;
}
return l_bReturn;
}
void handleEvents() {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
fsm_setState(&g_game, end);
break;
case SDL_KEYDOWN:
fsm_handleEvent(&g_game, event.key.keysym.sym);
break;
default:
continue;
}
}
}
void update() {
fsm_update(&g_game);
}
void render() {
SDL_SetRenderDrawColor(g_game.renderer, 0, 0, 0, 255);
SDL_RenderClear(g_game.renderer);
fsm_render(&g_game);
SDL_RenderPresent(g_game.renderer);
}
void clean() {
game_clean(&g_game);
TTF_Quit();
SDLNet_Quit();
SDL_Quit();
}