forked from munificent/game-programming-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame-loop.h
More file actions
144 lines (125 loc) · 2.08 KB
/
Copy pathgame-loop.h
File metadata and controls
144 lines (125 loc) · 2.08 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
134
135
136
137
138
139
140
141
142
143
144
void processInput() {}
void update() {}
void update(double elapsed) {}
void render() {}
void render(double elapsed) {}
double getCurrentTime() { return 0; }
void sleep(double time) {}
int FPS = 60;
int MS_PER_FRAME = 1000 / FPS;
int MS_PER_TICK = 1000 / FPS;
namespace Repl
{
char* readCommand() { return NULL; }
void handleCommand(char* command) {}
void loop()
{
//^1
while (true)
{
char* command = readCommand();
handleCommand(command);
}
//^1
}
}
namespace EventLoop
{
class Event {};
Event* waitForEvent() { return NULL; }
bool dispatchEvent(Event* event) { return false; }
void loop()
{
//^2
while (true)
{
Event* event = waitForEvent();
dispatchEvent(event);
}
//^2
}
}
namespace FastAsPossible
{
void runGame()
{
//^3
while (true)
{
processInput();
update();
render();
}
//^3
}
}
namespace FixedFramerate
{
void runGame()
{
//^4
while (true)
{
double start = getCurrentTime();
processInput();
update();
render();
sleep(start + MS_PER_FRAME - getCurrentTime());
}
//^4
}
}
namespace FluidFramerate
{
void runGame()
{
//^5
double lastTime = getCurrentTime();
while (true)
{
double current = getCurrentTime();
double elapsed = current - lastTime;
processInput();
update(elapsed);
render();
lastTime = current;
}
//^5
}
}
namespace FixedUpdateFramerate
{
void runGame()
{
const double MS_PER_UPDATE = 8;
//^6
double previous = getCurrentTime();
double lag = 0.0;
while (true)
{
double current = getCurrentTime();
double elapsed = current - previous;
previous = current;
lag += elapsed;
processInput();
while (lag >= MS_PER_UPDATE)
{
update();
lag -= MS_PER_UPDATE;
}
render();
}
//^6
}
}
namespace Interpolate
{
void runGame()
{
const double MS_PER_UPDATE = 8;
double lag = 0;
//^7
render(lag / MS_PER_UPDATE);
//^7
}
}