-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
65 lines (53 loc) · 1.19 KB
/
main.cpp
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
#include "scene_manager.h"
#include "game_scene.h"
#include "menu_scene.h"
#include "window.h"
Window *window;
Scene* menu_scene = nullptr;
Scene* game_scene = nullptr;
SceneManager scene_manager;
void Init() {
//SDL初始化
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
SDL_Log("Init failed, %s", SDL_GetError());
return;
}
//SDL_IMG初始化
if (!IMG_Init(IMG_INIT_PNG)) {
SDL_Log("Cannot init img,%s", SDL_GetError());
return;
}
//SDL_TTF 初始化
TTF_Init();
}
void Quit() {
IMG_Quit();
TTF_Quit();
SDL_Quit();
}
int main(int, char**) {
Init();
menu_scene = new MenuScene();
game_scene = new GameScene();
scene_manager.set_current_scene(menu_scene);
while (scene_manager.is_running())
{
//控制帧率
uint32_t begin = SDL_GetTicks();//获取开始的时间
scene_manager.on_input();
scene_manager.on_update();
scene_manager.on_draw();
long current = SDL_GetTicks();//结束时间
long cost = begin - current;//前面耗费的时间
long frame = 1000 / FRAMERATE;//每帧时间
long delay = frame - cost;//需要延迟的时间
if (delay > 0) {
SDL_Delay(delay);
}
}
//释放
menu_scene->on_destroy();
game_scene->on_destroy();
Quit();
return 0;
}