| 16 | Engine * Engine::instance_ = nullptr; |
| 17 | |
| 18 | bool Engine::EngineMain(Application * app, const int numArgs, const char ** args) { |
| 19 | static std::mutex preventMultipleMainCalls; |
| 20 | std::unique_lock<std::mutex> ul(preventMultipleMainCalls, std::defer_lock); |
| 21 | if (!ul.try_lock()) { |
| 22 | throw std::runtime_error("EngineMain already running"); |
| 23 | } |
| 24 | |
| 25 | // Set up engine params and create global Engine instance |
| 26 | EngineInitParams params; |
| 27 | params.numCmdArgs = numArgs; |
| 28 | params.cmdArgs = args; |
| 29 | Application::Instance_() = app; |
| 30 | |
| 31 | // Delete the instance in case it's left over from a previous run |
| 32 | delete Engine::instance_; |
| 33 | Engine::instance_ = new Engine(params); |
| 34 | |
| 35 | // Pre-initialize to set up things like the ApplicationThread |
| 36 | Engine::Instance()->PreInitialize(); |
| 37 | ApplicationThread::Instance()->Queue([]() { |
| 38 | Engine::Instance()->Initialize(); |
| 39 | }); |
| 40 | ApplicationThread::Instance()->DispatchAndSynchronize_(); |
| 41 | |
| 42 | // Set up shut down sequence |
| 43 | const auto shutdown = []() { |
| 44 | Engine::Instance()->BeginShutDown(); |
| 45 | Engine::Instance()->ShutDown(); |
| 46 | }; |
| 47 | |
| 48 | // Enter main loop |
| 49 | std::atomic<SystemStatus> status(SystemStatus::SYSTEM_CONTINUE); |
| 50 | const auto runFrame = [&status]() { |
| 51 | status.store(Engine::Instance()->Frame()); |
| 52 | }; |
| 53 | |
| 54 | volatile bool shouldRestart = false; |
| 55 | volatile bool running = true; |
| 56 | while (running) { |
| 57 | ApplicationThread::Instance()->Queue(runFrame); |
| 58 | // No need to synchronize since ApplicationThread uses this thread's context |
| 59 | ApplicationThread::Instance()->Dispatch_(); |
| 60 | |
| 61 | // Check the system status message and decide what to do next |
| 62 | switch (status.load()) { |
| 63 | case SystemStatus::SYSTEM_RESTART: |
| 64 | shouldRestart = true; |
| 65 | // fall down to next |
| 66 | case SystemStatus::SYSTEM_SHUTDOWN: |
| 67 | running = false; |
| 68 | break; |
| 69 | case SystemStatus::SYSTEM_PANIC: |
| 70 | std::cerr << "Critical error - exiting immediately" << std::endl; |
| 71 | exit(-1); |
| 72 | default: |
| 73 | continue; |
| 74 | } |
| 75 | } |
nothing calls this directly
no test coverage detected