Processes the next full system frame, including rendering. Returns false only if the main engine loop should stop.
| 250 | // Processes the next full system frame, including rendering. Returns false only |
| 251 | // if the main engine loop should stop. |
| 252 | SystemStatus Engine::Frame() { |
| 253 | // Calculate new frame time |
| 254 | const auto end = std::chrono::high_resolution_clock::now(); |
| 255 | //const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - _stats.prevFrameStart).count(); |
| 256 | const auto elapsed = end - stats_.prevFrameStart; |
| 257 | const double durationMsec = std::chrono::duration<double, std::micro>(elapsed).count(); |
| 258 | const auto requestedFrameTimingMsec = 1000000.0 / double(_params.maxFrameRate); |
| 259 | // Make sure we haven't exceeded the max frame rate |
| 260 | //if (frameRate > _params.maxFrameRate) { |
| 261 | if (durationMsec < requestedFrameTimingMsec) { |
| 262 | //std::this_thread::sleep_for(std::chrono::nanoseconds(1)); |
| 263 | return SystemStatus::SYSTEM_CONTINUE; |
| 264 | } |
| 265 | |
| 266 | //STRATUS_LOG << durationMsec << " " << requestedFrameTimingMsec << std::endl; |
| 267 | |
| 268 | // Validate |
| 269 | CHECK_IS_APPLICATION_THREAD(); |
| 270 | |
| 271 | if (IsInitializing()) return SystemStatus::SYSTEM_CONTINUE; |
| 272 | if (IsShuttingDown()) return SystemStatus::SYSTEM_SHUTDOWN; |
| 273 | |
| 274 | //std::shared_lock<std::shared_mutex> sl(_mainLoop); |
| 275 | |
| 276 | const double duration = std::chrono::duration<double, std::milli>(elapsed).count(); |
| 277 | const double deltaSeconds = duration / 1000.0; |
| 278 | //const double frameRate = 1.0 / deltaSeconds; |
| 279 | |
| 280 | //sl.unlock(); |
| 281 | |
| 282 | //std::unique_lock<std::shared_mutex> ul(_mainLoop); |
| 283 | |
| 284 | // Frame counter should always be +1 for each valid frame |
| 285 | ++stats_.currentFrame; |
| 286 | |
| 287 | // Update prev frame start to be the beginning of this current frame |
| 288 | stats_.prevFrameStart = end; |
| 289 | |
| 290 | //ul.unlock(); |
| 291 | |
| 292 | SystemStatus status; |
| 293 | #define UPDATE_MODULE(name) \ |
| 294 | status = name::Instance()->Update(deltaSeconds); \ |
| 295 | if (status != SystemStatus::SYSTEM_CONTINUE) return status; |
| 296 | |
| 297 | // Update core modules |
| 298 | UPDATE_MODULE(Log) |
| 299 | UPDATE_MODULE(InputManager) |
| 300 | UPDATE_MODULE(EntityManager) |
| 301 | UPDATE_MODULE(TaskSystem) |
| 302 | UPDATE_MODULE(MaterialManager) |
| 303 | UPDATE_MODULE(ResourceManager) |
| 304 | UPDATE_MODULE(Window) |
| 305 | UPDATE_MODULE(RendererFrontend) |
| 306 | |
| 307 | // Finish with update to application |
| 308 | return Application::Instance()->Update(deltaSeconds); |
| 309 |