Commit 7758a17
committed
[libretro] Reverting back to the use of clock()
clock() was previously replaced by frame time callback (1c8eb84 #2589)
It introduced a few bugs that I tried to fix (#2829)
But there is a [benchmark cartridge](https://tic80.com/play?cart=153)
that calls time() to mesure itra-frame timing.
This is how Gemini explains it:
The situation involves two different timing requirements:
Inter-Frame Timing (Game Speed): This is about ensuring the game
logic advances correctly from one frame to the next. If 1/60th of a
second has passed in the real world, the game should also advance its
state by 1/60th of a second. The original fix (`state->frameTime +=
usec;`) solved this by correctly accumulating the time between frames.
Intra-Frame Timing (Benchmark Measurement): This is what the
benchmark cartridge needs. It uses TIC-80's `time()` function to measure
how long an operation takes within a single frame. It records the start
time, does a lot of drawing, records the end time, and calculates the
difference.
The original fix failed the benchmark test because the
`state->frameTime` value is only updated once per frame. For the entire
duration of the `MAINTIC()` function, the value returned by `time()`
would be constant. Therefore, `time() - stime` would always be zero.
--
The clock() solution is superior because it provides a high-resolution,
continuously updating timer directly from the system's C library.
clock(): This standard function returns the amount of processor time
used by the program. Crucially, this value increases during the
execution of a single frame.
CLOCKS_PER_SEC: This constant provides the frequency of the clock()
timer.
When the benchmark calls time() (`clock() / CLOCKS_PER_SEC`), it gets a
precise timestamp. After it finishes its work and calls time() again,
the value from clock() will have increased, yielding a correct, non-zero
`runningTime`. This solves the intra-frame timing problem.
Simultaneously, because clock() is a consistently increasing counter,
the TIC-80 engine can use it to perfectly measure the duration between
frames, solving the inter-frame timing problem as well.1 parent 2cc226b commit 7758a17
1 file changed
+2
-28
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
80 | | - | |
81 | 80 | | |
82 | 81 | | |
83 | 82 | | |
| |||
86 | 85 | | |
87 | 86 | | |
88 | 87 | | |
89 | | - | |
90 | | - | |
91 | | - | |
92 | | - | |
93 | | - | |
| 88 | + | |
94 | 89 | | |
95 | 90 | | |
96 | 91 | | |
97 | 92 | | |
98 | 93 | | |
99 | 94 | | |
100 | 95 | | |
101 | | - | |
| 96 | + | |
102 | 97 | | |
103 | 98 | | |
104 | 99 | | |
| |||
155 | 150 | | |
156 | 151 | | |
157 | 152 | | |
158 | | - | |
159 | | - | |
160 | | - | |
161 | | - | |
162 | | - | |
163 | | - | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | 153 | | |
170 | 154 | | |
171 | 155 | | |
| |||
1116 | 1100 | | |
1117 | 1101 | | |
1118 | 1102 | | |
1119 | | - | |
1120 | | - | |
1121 | | - | |
1122 | | - | |
1123 | | - | |
1124 | | - | |
1125 | | - | |
1126 | | - | |
1127 | | - | |
1128 | | - | |
1129 | 1103 | | |
1130 | 1104 | | |
1131 | 1105 | | |
| |||
0 commit comments