| 2203 | } |
| 2204 | |
| 2205 | static void CalcTimeStep(HEngine engine, float& step_dt, uint32_t& num_steps) |
| 2206 | { |
| 2207 | uint64_t time = dmTime::GetMonotonicTime(); |
| 2208 | uint64_t frame_time = time - engine->m_PreviousFrameTime; // The actual time between two engine frames |
| 2209 | engine->m_PreviousFrameTime = time; |
| 2210 | |
| 2211 | float frame_dt = (float)(frame_time / 1000000.0); |
| 2212 | |
| 2213 | // Never allow for large hitches |
| 2214 | if (frame_dt > engine->m_MaxTimeStep) { |
| 2215 | frame_dt = engine->m_MaxTimeStep; |
| 2216 | } |
| 2217 | |
| 2218 | // Variable frame rate |
| 2219 | if (engine->m_UpdateFrequency == 0) |
| 2220 | { |
| 2221 | step_dt = frame_dt; |
| 2222 | num_steps = 1; |
| 2223 | return; |
| 2224 | } |
| 2225 | |
| 2226 | // Fixed frame rate |
| 2227 | float fixed_dt = 1.0f / (float)engine->m_UpdateFrequency; |
| 2228 | |
| 2229 | // We don't allow having a higher framerate than the actual variable frame rate |
| 2230 | // since the update+render is currently coupled together and also Flip() would be called more than once. |
| 2231 | // E.g. if the fixed_dt == 1/120 and the frame_dt == 1/60 |
| 2232 | if (fixed_dt < frame_dt) |
| 2233 | { |
| 2234 | fixed_dt = frame_dt; |
| 2235 | } |
| 2236 | |
| 2237 | engine->m_AccumFrameTime += frame_dt; |
| 2238 | |
| 2239 | float num_steps_f = engine->m_AccumFrameTime / fixed_dt; |
| 2240 | |
| 2241 | num_steps = (uint32_t)num_steps_f; |
| 2242 | step_dt = fixed_dt; |
| 2243 | |
| 2244 | engine->m_AccumFrameTime = engine->m_AccumFrameTime - num_steps * fixed_dt; |
| 2245 | } |
| 2246 | |
| 2247 | void Step(HEngine engine) |
| 2248 | { |
no test coverage detected