From b13bbdaef969633735c659ad412068e493357c28 Mon Sep 17 00:00:00 2001 From: Xottab-DUTY Date: Sat, 16 Dec 2023 13:49:34 +0500 Subject: [PATCH] Moved application cycle processing to CApplication --- src/xrEngine/device.cpp | 155 +++++++++++++--------------------------- src/xrEngine/device.h | 12 ++-- src/xrEngine/x_ray.cpp | 70 +++++++++++++++++- 3 files changed, 127 insertions(+), 110 deletions(-) diff --git a/src/xrEngine/device.cpp b/src/xrEngine/device.cpp index 321ec8dda78..8261cb97ee0 100644 --- a/src/xrEngine/device.cpp +++ b/src/xrEngine/device.cpp @@ -23,8 +23,6 @@ ENGINE_API bool g_bRendering = false; int ps_fps_limit = 501; int ps_fps_limit_in_menu = 60; -constexpr size_t MAX_WINDOW_EVENTS = 32; - bool g_bLoaded = false; ref_light precache_light = 0; @@ -298,128 +296,77 @@ void CRenderDevice::ProcessFrame() Sleep(1); } -void CRenderDevice::message_loop() +void CRenderDevice::ProcessEvent(const SDL_Event& event) { - while (!SDL_QuitRequested()) // SDL_PumpEvents is here + switch (event.type) { - bool canCallActivate = false; - bool shouldActivate = false; - - SDL_Event events[MAX_WINDOW_EVENTS]; - const int count = SDL_PeepEvents(events, MAX_WINDOW_EVENTS, - SDL_GETEVENT, SDL_WINDOWEVENT, SDL_WINDOWEVENT); - - for (int i = 0; i < count; ++i) - { - const SDL_Event event = events[i]; - - switch (event.type) - { #if SDL_VERSION_ATLEAST(2, 0, 9) - case SDL_DISPLAYEVENT: - { - switch (event.display.type) - { - case SDL_DISPLAYEVENT_ORIENTATION: + case SDL_DISPLAYEVENT: + { + switch (event.display.type) + { + case SDL_DISPLAYEVENT_ORIENTATION: #if SDL_VERSION_ATLEAST(2, 0, 14) - case SDL_DISPLAYEVENT_CONNECTED: - case SDL_DISPLAYEVENT_DISCONNECTED: + case SDL_DISPLAYEVENT_CONNECTED: + case SDL_DISPLAYEVENT_DISCONNECTED: #endif - CleanupVideoModes(); - FillVideoModes(); + CleanupVideoModes(); + FillVideoModes(); #if SDL_VERSION_ATLEAST(2, 0, 14) - if (event.display.display == psDeviceMode.Monitor && event.display.type != SDL_DISPLAYEVENT_CONNECTED) + if (event.display.display == psDeviceMode.Monitor && event.display.type != SDL_DISPLAYEVENT_CONNECTED) #else if (event.display.display == psDeviceMode.Monitor) #endif - Reset(); - else - UpdateWindowProps(); - break; - } // switch (event.display.type) - break; - } + Reset(); + else + UpdateWindowProps(); + break; + } // switch (event.display.type) + break; + } #endif - case SDL_WINDOWEVENT: - { - switch (event.window.event) - { - case SDL_WINDOWEVENT_MOVED: - { - UpdateWindowRects(); + case SDL_WINDOWEVENT: + { + switch (event.window.event) + { + case SDL_WINDOWEVENT_MOVED: + { + UpdateWindowRects(); #if !SDL_VERSION_ATLEAST(2, 0, 18) // without SDL_WINDOWEVENT_DISPLAY_CHANGED, let's detect monitor change ourselves const int display = SDL_GetWindowDisplayIndex(m_sdlWnd); if (display != -1) psDeviceMode.Monitor = display; #endif - break; - } + break; + } #if SDL_VERSION_ATLEAST(2, 0, 18) - case SDL_WINDOWEVENT_DISPLAY_CHANGED: - psDeviceMode.Monitor = event.window.data1; - break; + case SDL_WINDOWEVENT_DISPLAY_CHANGED: + psDeviceMode.Monitor = event.window.data1; + break; #endif - case SDL_WINDOWEVENT_SIZE_CHANGED: - { - if (psDeviceMode.WindowStyle != rsFullscreen) - { - if (static_cast(psDeviceMode.Width) == event.window.data1 && - static_cast(psDeviceMode.Height) == event.window.data2) - break; // we don't need to reset device if resolution wasn't really changed - - psDeviceMode.Width = event.window.data1; - psDeviceMode.Height = event.window.data2; - - Reset(); - } - else - UpdateWindowRects(); - - break; - } - - case SDL_WINDOWEVENT_SHOWN: - case SDL_WINDOWEVENT_FOCUS_GAINED: - case SDL_WINDOWEVENT_RESTORED: - case SDL_WINDOWEVENT_MAXIMIZED: - canCallActivate = true; - shouldActivate = true; - break; - - case SDL_WINDOWEVENT_HIDDEN: - case SDL_WINDOWEVENT_FOCUS_LOST: - case SDL_WINDOWEVENT_MINIMIZED: - canCallActivate = true; - shouldActivate = false; - break; - - case SDL_WINDOWEVENT_ENTER: - SDL_ShowCursor(SDL_FALSE); - break; - - case SDL_WINDOWEVENT_LEAVE: - SDL_ShowCursor(SDL_TRUE); - break; - - case SDL_WINDOWEVENT_CLOSE: - Engine.Event.Defer("KERNEL:disconnect"); - Engine.Event.Defer("KERNEL:quit"); - break; - } // switch (event.window.event) + case SDL_WINDOWEVENT_SIZE_CHANGED: + { + if (psDeviceMode.WindowStyle != rsFullscreen) + { + if (static_cast(psDeviceMode.Width) == event.window.data1 && + static_cast(psDeviceMode.Height) == event.window.data2) + break; // we don't need to reset device if resolution wasn't really changed + + psDeviceMode.Width = event.window.data1; + psDeviceMode.Height = event.window.data2; + + Reset(); } - } // switch (event.type) - } // for (int i = 0; i < count; ++i) + else + UpdateWindowRects(); - // Workaround for screen blinking when there's too much timeouts - if (canCallActivate) - { - OnWindowActivate(shouldActivate); + break; } - - ProcessFrame(); + } // switch (event.window.event) } + } // switch (event.type) } void CRenderDevice::Run() @@ -448,10 +395,10 @@ void CRenderDevice::Run() SDL_RaiseWindow(m_sdlWnd); if (GEnv.isDedicatedServer || strstr(Core.Params, "-center_screen")) SDL_SetWindowPosition(m_sdlWnd, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); +} - // Message cycle - message_loop(); - +void CRenderDevice::Shutdown() +{ // Stop Balance-Thread mt_bMustExit = true; diff --git a/src/xrEngine/device.h b/src/xrEngine/device.h index 09cc982b13b..76433627257 100644 --- a/src/xrEngine/device.h +++ b/src/xrEngine/device.h @@ -191,10 +191,16 @@ class ENGINE_API CRenderDevice : public IWindowHandler public: // Creation & Destroying void Create(); - void Run(); void Destroy(); + void Reset(bool precache = true); + void Run(); + void Shutdown(); + + void ProcessEvent(const SDL_Event& event); + void OnWindowActivate(bool activated); + void UpdateWindowProps(); void UpdateWindowRects(); void SelectResolution(bool windowed); @@ -258,10 +264,6 @@ class ENGINE_API CRenderDevice : public IWindowHandler private: void CalcFrameStats(); - void OnWindowActivate(bool activated); - - void message_loop(); - public: [[nodiscard]] auto& editor() { return m_editor; } diff --git a/src/xrEngine/x_ray.cpp b/src/xrEngine/x_ray.cpp index 9e3d284a9a0..b66f03311e2 100644 --- a/src/xrEngine/x_ray.cpp +++ b/src/xrEngine/x_ray.cpp @@ -37,6 +37,8 @@ // global variables constexpr u32 SPLASH_FRAMERATE = 30; +constexpr size_t MAX_WINDOW_EVENTS = 32; + ENGINE_API CInifile* pGameIni = nullptr; ENGINE_API bool CallOfPripyatMode = false; ENGINE_API bool ClearSkyMode = false; @@ -357,8 +359,74 @@ int CApplication::Run() #endif // Main cycle - HideSplash(); Device.Run(); + HideSplash(); + + while (!SDL_QuitRequested()) // SDL_PumpEvents is here + { + bool canCallActivate = false; + bool shouldActivate = false; + + SDL_Event events[MAX_WINDOW_EVENTS]; + const int count = SDL_PeepEvents(events, MAX_WINDOW_EVENTS, + SDL_GETEVENT, SDL_WINDOWEVENT, SDL_WINDOWEVENT); + + for (int i = 0; i < count; ++i) + { + const SDL_Event event = events[i]; + + switch (event.type) + { + case SDL_WINDOWEVENT: + { + switch (event.window.event) + { + case SDL_WINDOWEVENT_SHOWN: + case SDL_WINDOWEVENT_FOCUS_GAINED: + case SDL_WINDOWEVENT_RESTORED: + case SDL_WINDOWEVENT_MAXIMIZED: + canCallActivate = true; + shouldActivate = true; + continue; + + case SDL_WINDOWEVENT_HIDDEN: + case SDL_WINDOWEVENT_FOCUS_LOST: + case SDL_WINDOWEVENT_MINIMIZED: + canCallActivate = true; + shouldActivate = false; + continue; + + case SDL_WINDOWEVENT_ENTER: + SDL_ShowCursor(SDL_FALSE); + continue; + + case SDL_WINDOWEVENT_LEAVE: + SDL_ShowCursor(SDL_TRUE); + continue; + + case SDL_WINDOWEVENT_CLOSE: + Engine.Event.Defer("KERNEL:disconnect"); + Engine.Event.Defer("KERNEL:quit"); + continue; + } // switch (event.window.event) + } + } // switch (event.type) + + // Only process event in Device + // if it wasn't processed in the switch above + Device.ProcessEvent(event); + } // for (int i = 0; i < count; ++i) + + // Workaround for screen blinking when there's too much timeouts + if (canCallActivate) + { + Device.OnWindowActivate(shouldActivate); + } + + Device.ProcessFrame(); + } // while (!SDL_QuitRequested()) + + Device.Shutdown(); return 0; }