-
Notifications
You must be signed in to change notification settings - Fork 3
/
decouple_framedrawing.patch
162 lines (147 loc) · 5.21 KB
/
decouple_framedrawing.patch
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
Index: src/engine/main.cpp
===================================================================
--- src/engine/main.cpp (revision 6490)
+++ src/engine/main.cpp (working copy)
@@ -72,7 +72,7 @@
exit(EXIT_FAILURE);
}
-int curtime = 0, lastmillis = 1, elapsedtime = 0, totalmillis = 1;
+int curtime = 0, lastmillis = 1, elapsedtime = 0, totalmillis = 1, curframetime = 0;
dynent *player = NULL;
@@ -1034,28 +1034,44 @@
VARP(menufps, 0, 60, 1000);
VARP(maxfps, 0, 200, 1000);
+MOD(VARFP, maxtps, 0, 0, 1000, { if(maxtps && maxtps<60) {conoutf("can't set maxtps < 60"); maxtps = 60;} });
-void limitfps(int &millis, int curmillis)
+void ratelimit(int &millis, int lastdrawmillis, bool &draw)
{
- int limit = (mainmenu || minimized) && menufps ? (maxfps ? min(maxfps, menufps) : menufps) : maxfps;
- if(!limit) return;
- static int fpserror = 0;
- int delay = 1000/limit - (millis-curmillis);
- if(delay < 0) fpserror = 0;
- else
- {
- fpserror += 1000%limit;
- if(fpserror >= limit)
- {
- ++delay;
- fpserror -= limit;
- }
- if(delay > 0)
- {
- SDL_Delay(delay);
- millis += delay;
- }
+ int fpslimit = (mainmenu || minimized) && menufps ? (maxfps ? min(maxfps, menufps) : menufps) : maxfps;
+ if(!fpslimit) draw = true;
+ int tpslimit = minimized ? 100 : (maxtps ? max(maxtps, fpslimit) : 0);
+ if(!fpslimit && !tpslimit) return;
+ int delay = 1;
+ if(tpslimit) delay = max(1000/tpslimit - (millis-totalmillis), 0);
+ // should we draw?
+ int fpsdelay = INT_MAX;
+ if(!minimized && fpslimit)
+ {
+ static int fpserror = 0;
+ fpsdelay = 1000/fpslimit - (millis-lastdrawmillis);
+ if(fpsdelay < 0)
+ {
+ draw = true;
+ fpserror = 0;
+ }
+ else
+ {
+ if(fpserror >= fpslimit) fpsdelay++;
+ if(fpsdelay <= delay)
+ {
+ draw = true;
+ if(fpserror >= fpslimit) fpserror -= fpslimit;
+ fpserror += 1000%fpslimit;
+ }
+ }
+ }
+ delay = min(delay, fpsdelay);
+ if(delay > 0)
+ {
+ SDL_Delay(delay);
+ millis += delay;
}
}
#if defined(WIN32) && !defined(_DEBUG) && !defined(__GNUC__)
@@ -1346,9 +1362,10 @@
for(;;)
{
- static int frames = 0;
+ static int frames = 0, lastdrawmillis = 0;
int millis = getclockmillis();
- limitfps(millis, totalmillis);
+ bool draw = false;
+ ratelimit(millis, lastdrawmillis, draw);
elapsedtime = millis - totalmillis;
static int timeerr = 0;
int scaledtime = game::scaletime(elapsedtime) + timeerr;
@@ -1370,9 +1387,6 @@
serverslice(false, 0);
- if(frames) updatefpshistory(elapsedtime);
- frames++;
-
// miscellaneous general game effects
recomputecamera();
updateparticles();
@@ -1380,11 +1394,24 @@
if(minimized) continue;
- inbetweenframes = false;
- if(mainmenu) gl_drawmainmenu();
- else gl_drawframe();
- swapbuffers();
- renderedframe = inbetweenframes = true;
+ if(draw)
+ {
+ int frametime = millis - lastdrawmillis;
+ static int frametimeerr = 0;
+ int scaledframetime = game::scaletime(frametime) + frametimeerr;
+ curframetime = scaledframetime/100;
+ frametimeerr = scaledframetime%100;
+
+ if(frames) updatefpshistory(frametime);
+ frames++;
+
+ inbetweenframes = false;
+ if(mainmenu) gl_drawmainmenu();
+ else gl_drawframe();
+ swapbuffers();
+ renderedframe = inbetweenframes = true;
+ lastdrawmillis = millis;
+ }
}
ASSERT(0);
Index: src/engine/rendergl.cpp
===================================================================
--- src/engine/rendergl.cpp (revision 6490)
+++ src/engine/rendergl.cpp (working copy)
@@ -2025,7 +2025,7 @@
gle::attrib(m.transform(vec2(0, 0)));
// fade in log space so short blips don't disappear too quickly
- scale -= float(curtime)/damagecompassfade;
+ scale -= float(curframetime)/damagecompassfade;
damagedirs[i] = scale > 0 ? (pow(logscale, scale) - 1) / (logscale - 1) : 0;
}
if(dirs) gle::end();
Index: src/shared/iengine.h
===================================================================
--- src/shared/iengine.h (revision 6490)
+++ src/shared/iengine.h (working copy)
@@ -1,9 +1,10 @@
// the interface the game uses to access the engine
-extern int curtime; // current frame time
-extern int lastmillis; // last time
-extern int elapsedtime; // elapsed frame time
-extern int totalmillis; // total elapsed time
+extern int curtime; // scaled duration since last step
+extern int lastmillis; // totalmillis value at last step
+extern int elapsedtime; // wall clock duration since last step
+extern int totalmillis; // total elapsed wall clock duration since engine start
+extern int curframetime; // scaled duration since last frame
extern uint totalsecs;
extern int gamespeed, paused;