| 165 | } |
| 166 | |
| 167 | void Engine::OnLoop() |
| 168 | { |
| 169 | // Reduce CPU usage by introducing idle time if the engine is running very fast and has enough time to spend |
| 170 | const bool useSleep = !PLATFORM_WEB; // TODO: this should probably be a platform setting |
| 171 | if ((useSleep && Time::UpdateFPS > ZeroTolerance) || !Platform::GetHasFocus()) |
| 172 | { |
| 173 | double nextTick = Time::GetNextTick(); |
| 174 | double timeToTick = nextTick - Platform::GetTimeSeconds(); |
| 175 | |
| 176 | // Sleep less than needed, some platforms may sleep slightly more than requested |
| 177 | if (timeToTick > 0.002) |
| 178 | { |
| 179 | PROFILE_CPU_NAMED("Idle"); |
| 180 | auto sleepStart = Platform::GetTimeSeconds(); |
| 181 | Platform::Sleep(1); |
| 182 | auto sleepEnd = Platform::GetTimeSeconds(); |
| 183 | EngineIdleTime += sleepEnd - sleepStart; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // App paused logic |
| 188 | if (Platform::GetIsPaused()) |
| 189 | { |
| 190 | OnPause(); |
| 191 | do |
| 192 | { |
| 193 | Platform::Sleep(10); |
| 194 | Platform::Tick(); |
| 195 | } while (Platform::GetIsPaused() && !ShouldExit()); |
| 196 | if (ShouldExit()) |
| 197 | return; |
| 198 | OnUnpause(); |
| 199 | } |
| 200 | |
| 201 | // Use the same time for all ticks to improve synchronization |
| 202 | const double time = Platform::GetTimeSeconds(); |
| 203 | |
| 204 | // Update application (will gather data and other platform related events) |
| 205 | { |
| 206 | PROFILE_CPU_NAMED("Platform.Tick"); |
| 207 | Platform::Tick(); |
| 208 | #if COMPILE_WITH_PROFILER |
| 209 | extern void TickProfilerMemory(); |
| 210 | TickProfilerMemory(); |
| 211 | #endif |
| 212 | } |
| 213 | |
| 214 | // Update game logic |
| 215 | if (Time::OnBeginUpdate(time)) |
| 216 | { |
| 217 | OnUpdate(); |
| 218 | OnLateUpdate(); |
| 219 | Time::OnEndUpdate(); |
| 220 | EngineIdleTime = 0; |
| 221 | } |
| 222 | |
| 223 | // Start physics simulation |
| 224 | if (Time::OnBeginPhysics(time)) |
nothing calls this directly
no test coverage detected