From 5529556f934ead15a3bbe25e2c50b15da602d0a7 Mon Sep 17 00:00:00 2001 From: Xottab-DUTY Date: Mon, 16 Jul 2018 08:29:57 +0500 Subject: [PATCH] Moved part of the window management code from renderers to xrEngine --- src/Layers/xrRender/D3DXRenderBase.h | 2 +- src/Layers/xrRender/HW.cpp | 60 ++-------------------- src/Layers/xrRender/HW.h | 8 +-- src/Layers/xrRender/r__dsgraph_build.cpp | 8 +-- src/Layers/xrRenderDX10/dx10HW.cpp | 59 ++------------------- src/Layers/xrRenderGL/glHW.cpp | 65 ++---------------------- src/xrEngine/Device_Initialize.cpp | 4 +- src/xrEngine/Device_create.cpp | 39 +++++++++++++- src/xrEngine/Device_destroy.cpp | 7 +++ src/xrEngine/Render.h | 2 +- src/xrEngine/device.cpp | 8 +-- src/xrEngine/device.h | 2 + 12 files changed, 67 insertions(+), 197 deletions(-) diff --git a/src/Layers/xrRender/D3DXRenderBase.h b/src/Layers/xrRender/D3DXRenderBase.h index 9d6ff18e243..53c14675e2f 100644 --- a/src/Layers/xrRender/D3DXRenderBase.h +++ b/src/Layers/xrRender/D3DXRenderBase.h @@ -190,7 +190,7 @@ class D3DXRenderBase : public IRender, public pureFrame // Init virtual void SetupStates() override; virtual void OnDeviceCreate(const char* shName) override; - virtual void Create(SDL_Window* hWnd, u32& dwWidth, u32& dwHeight, float& fWidth_2, float& fHeight_2, bool) override; + virtual void Create(SDL_Window* hWnd, u32& dwWidth, u32& dwHeight, float& fWidth_2, float& fHeight_2) override; virtual void SetupGPU(bool bForceGPU_SW, bool bForceGPU_NonPure, bool bForceGPU_REF) override; // Overdraw virtual void overdrawBegin() override; diff --git a/src/Layers/xrRender/HW.cpp b/src/Layers/xrRender/HW.cpp index 206a5707fd8..1027a4a8ad8 100644 --- a/src/Layers/xrRender/HW.cpp +++ b/src/Layers/xrRender/HW.cpp @@ -36,13 +36,12 @@ void CHW::DestroyD3D() _RELEASE(pD3D); } -void CHW::CreateDevice(SDL_Window* m_sdlWnd, bool move_window) +void CHW::CreateDevice(SDL_Window* m_sdlWnd) { - m_move_window = move_window; CreateD3D(); bool bWindowed = !psDeviceFlags.is(rsFullscreen); - if (GEnv.isDedicatedServer) + if (GEnv.isDedicatedServer || Device.editor()) bWindowed = true; m_DriverType = Caps.bForceGPU_REF ? D3DDEVTYPE_REF : D3DDEVTYPE_HAL; @@ -214,7 +213,6 @@ void CHW::CreateDevice(SDL_Window* m_sdlWnd, bool move_window) Msg("* Texture memory: %d M", memory / (1024 * 1024)); Msg("* DDI-level: %2.1f", float(D3DXGetDriverLevel(pDevice)) / 100.f); - updateWindowProps(m_sdlWnd); fill_vid_mode_list(this); } @@ -240,7 +238,7 @@ void CHW::DestroyDevice() ////////////////////////////////////////////////////////////////////// // Resetting device ////////////////////////////////////////////////////////////////////// -void CHW::Reset(SDL_Window* m_sdlWnd) +void CHW::Reset() { #ifdef DEBUG _RELEASE(dwDebugSB); @@ -284,9 +282,6 @@ void CHW::Reset(SDL_Window* m_sdlWnd) #ifdef DEBUG R_CHK(pDevice->CreateStateBlock(D3DSBT_ALL, &dwDebugSB)); #endif - - updateWindowProps(m_sdlWnd); - SDL_ShowWindow(m_sdlWnd); } D3DFORMAT CHW::selectDepthStencil(D3DFORMAT fTarget) @@ -466,55 +461,6 @@ BOOL CHW::support(D3DFORMAT fmt, DWORD type, DWORD usage) return TRUE; } -void CHW::updateWindowProps(SDL_Window *m_sdlWnd) -{ - bool bWindowed = !psDeviceFlags.is(rsFullscreen); - - if (GEnv.isDedicatedServer) - bWindowed = true; - - u32 dwWindowStyle = 0; - // Set window properties depending on what mode were in. - if (bWindowed) - { - if (m_move_window) - { - if(NULL != strstr(Core.Params, "-draw_borders")) - SDL_SetWindowBordered(m_sdlWnd, SDL_TRUE); - // When moving from fullscreen to windowed mode, it is important to - // adjust the window size after recreating the device rather than - // beforehand to ensure that you get the window size you want. For - // example, when switching from 640x480 fullscreen to windowed with - // a 1000x600 window on a 1024x768 desktop, it is impossible to set - // the window size to 1000x600 until after the display mode has - // changed to 1024x768, because windows cannot be larger than the - // desktop. - - bool centerScreen = false; - if (GEnv.isDedicatedServer || strstr(Core.Params, "-center_screen")) - centerScreen = true; - - SDL_SetWindowSize(m_sdlWnd, DevPP.BackBufferWidth, DevPP.BackBufferHeight); - - if (centerScreen) - { - SDL_SetWindowPosition(m_sdlWnd, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); - } - else - { - SDL_SetWindowPosition(m_sdlWnd, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED); - } - } - } - else - { - SDL_ShowWindow(m_sdlWnd); - } - - if (!GEnv.isDedicatedServer) - SDL_SetWindowGrab(m_sdlWnd, SDL_TRUE); -} - struct uniqueRenderingMode { uniqueRenderingMode(pcstr v) : value(v) {} diff --git a/src/Layers/xrRender/HW.h b/src/Layers/xrRender/HW.h index 02d3ca0431f..f6f4198a166 100644 --- a/src/Layers/xrRender/HW.h +++ b/src/Layers/xrRender/HW.h @@ -25,11 +25,11 @@ class CHW void DestroyD3D(); #endif // !USE_OGL - void CreateDevice(SDL_Window* m_sdlWnd, bool move_window); + void CreateDevice(SDL_Window* m_sdlWnd); void DestroyDevice(); - void Reset(SDL_Window* m_sdlWnd); + void Reset(); #ifndef USE_OGL void selectResolution(u32& dwWidth, u32& dwHeight, BOOL bWindowed); @@ -40,7 +40,6 @@ class CHW BOOL support(D3DFORMAT fmt, DWORD type, DWORD usage); #endif // !USE_OGL - void updateWindowProps(SDL_Window* m_sdlWnd); #ifdef DEBUG #if defined(USE_DX10) || defined(USE_DX11) || defined(USE_OGL) void Validate(void){}; @@ -145,9 +144,6 @@ class CHW #endif // USE_OGL int maxRefreshRate = 200; // ECO_RENDER add - -private: - bool m_move_window = true; }; extern ECORE_API CHW HW; diff --git a/src/Layers/xrRender/r__dsgraph_build.cpp b/src/Layers/xrRender/r__dsgraph_build.cpp index 1d4bbdbd920..ef1231d73de 100644 --- a/src/Layers/xrRender/r__dsgraph_build.cpp +++ b/src/Layers/xrRender/r__dsgraph_build.cpp @@ -804,7 +804,7 @@ void D3DXRenderBase::DestroyHW() HW.DestroyDevice(); } -void D3DXRenderBase::Reset(SDL_Window *hWnd, u32& dwWidth, u32& dwHeight, float& fWidth_2, float& fHeight_2) +void D3DXRenderBase::Reset(SDL_Window* hWnd, u32& dwWidth, u32& dwHeight, float& fWidth_2, float& fHeight_2) { #if defined(DEBUG) && !defined(USE_OGL) _SHOW_REF("*ref -CRenderDevice::ResetTotal: DeviceREF:", HW.pDevice); @@ -812,7 +812,7 @@ void D3DXRenderBase::Reset(SDL_Window *hWnd, u32& dwWidth, u32& dwHeight, float& Resources->reset_begin(); Memory.mem_compact(); - HW.Reset(hWnd); + HW.Reset(); #if defined(USE_OGL) dwWidth = psCurrentVidMode[0]; @@ -900,9 +900,9 @@ void D3DXRenderBase::OnDeviceCreate(const char* shName) } } -void D3DXRenderBase::Create(SDL_Window* hWnd, u32& dwWidth, u32& dwHeight, float& fWidth_2, float& fHeight_2, bool move_window) +void D3DXRenderBase::Create(SDL_Window* hWnd, u32& dwWidth, u32& dwHeight, float& fWidth_2, float& fHeight_2) { - HW.CreateDevice(hWnd, move_window); + HW.CreateDevice(hWnd); #if defined(USE_OGL) dwWidth = psCurrentVidMode[0]; dwHeight = psCurrentVidMode[1]; diff --git a/src/Layers/xrRenderDX10/dx10HW.cpp b/src/Layers/xrRenderDX10/dx10HW.cpp index f3849f2742e..f050fc9e225 100644 --- a/src/Layers/xrRenderDX10/dx10HW.cpp +++ b/src/Layers/xrRenderDX10/dx10HW.cpp @@ -64,12 +64,11 @@ void CHW::DestroyD3D() _RELEASE(m_pFactory); } -void CHW::CreateDevice(SDL_Window* m_sdlWnd, bool move_window) +void CHW::CreateDevice(SDL_Window* m_sdlWnd) { - m_move_window = move_window; CreateD3D(); - bool bWindowed = !psDeviceFlags.is(rsFullscreen); + bool bWindowed = !psDeviceFlags.is(rsFullscreen) || Device.editor(); m_DriverType = Caps.bForceGPU_REF ? D3D_DRIVER_TYPE_REFERENCE : D3D_DRIVER_TYPE_HARDWARE; @@ -196,10 +195,7 @@ void CHW::CreateDevice(SDL_Window* m_sdlWnd, bool move_window) const auto memory = Desc.DedicatedVideoMemory; Msg("* Texture memory: %d M", memory / (1024 * 1024)); //Msg("* DDI-level: %2.1f", float(D3DXGetDriverLevel(pDevice)) / 100.f); -#ifndef _EDITOR - updateWindowProps(m_sdlWnd); fill_vid_mode_list(this); -#endif } void CHW::DestroyDevice() @@ -241,7 +237,7 @@ void CHW::DestroyDevice() ////////////////////////////////////////////////////////////////////// // Resetting device ////////////////////////////////////////////////////////////////////// -void CHW::Reset(SDL_Window* m_sdlWnd) +void CHW::Reset() { DXGI_SWAP_CHAIN_DESC& cd = m_ChainDesc; BOOL bWindowed = !psDeviceFlags.is(rsFullscreen); @@ -265,9 +261,6 @@ void CHW::Reset(SDL_Window* m_sdlWnd) CHK_DX(m_pSwapChain->ResizeBuffers( cd.BufferCount, desc.Width, desc.Height, desc.Format, DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH)); UpdateViews(); - - updateWindowProps(m_sdlWnd); - SDL_ShowWindow(m_sdlWnd); } D3DFORMAT CHW::selectDepthStencil(D3DFORMAT /*fTarget*/) @@ -353,52 +346,6 @@ BOOL CHW::support(D3DFORMAT fmt, DWORD type, DWORD usage) return TRUE; } -void CHW::updateWindowProps(SDL_Window* m_sdlWnd) -{ - bool bWindowed = !psDeviceFlags.is(rsFullscreen); - - u32 dwWindowStyle = 0; - // Set window properties depending on what mode were in. - if (bWindowed) - { - if (m_move_window) - { - if (NULL != strstr(Core.Params, "-draw_borders")) - SDL_SetWindowBordered(m_sdlWnd, SDL_TRUE); - // When moving from fullscreen to windowed mode, it is important to - // adjust the window size after recreating the device rather than - // beforehand to ensure that you get the window size you want. For - // example, when switching from 640x480 fullscreen to windowed with - // a 1000x600 window on a 1024x768 desktop, it is impossible to set - // the window size to 1000x600 until after the display mode has - // changed to 1024x768, because windows cannot be larger than the - // desktop. - - bool centerScreen = false; - if (GEnv.isDedicatedServer || strstr(Core.Params, "-center_screen")) - centerScreen = true; - - SDL_SetWindowSize(m_sdlWnd, m_ChainDesc.BufferDesc.Width, m_ChainDesc.BufferDesc.Height); - - if (centerScreen) - { - SDL_SetWindowPosition(m_sdlWnd, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); - } - else - { - SDL_SetWindowPosition(m_sdlWnd, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED); - } - } - } - else - { - SDL_ShowWindow(m_sdlWnd); - } - - if (!GEnv.isDedicatedServer) - SDL_SetWindowGrab(m_sdlWnd, SDL_TRUE); -} - struct uniqueRenderingMode { uniqueRenderingMode(pcstr v) : value(v) {} diff --git a/src/Layers/xrRenderGL/glHW.cpp b/src/Layers/xrRenderGL/glHW.cpp index 9c5f215a529..23f20ebfc74 100644 --- a/src/Layers/xrRenderGL/glHW.cpp +++ b/src/Layers/xrRenderGL/glHW.cpp @@ -34,17 +34,15 @@ CHW::CHW() : pFB(0), m_hWnd(nullptr), m_hDC(nullptr), - m_hRC(nullptr), - m_move_window(true) {} + m_hRC(nullptr) {} CHW::~CHW() {} ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// -void CHW::CreateDevice(SDL_Window *hWnd, bool move_window) +void CHW::CreateDevice(SDL_Window* hWnd) { m_hWnd = hWnd; - m_move_window = move_window; R_ASSERT(m_hWnd); @@ -94,11 +92,7 @@ void CHW::CreateDevice(SDL_Window *hWnd, bool move_window) // Create render target and depth-stencil views here UpdateViews(); - -#ifndef _EDITOR - updateWindowProps(m_hWnd); fill_vid_mode_list(this); -#endif } void CHW::DestroyDevice() @@ -119,10 +113,8 @@ void CHW::DestroyDevice() ////////////////////////////////////////////////////////////////////// // Resetting device ////////////////////////////////////////////////////////////////////// -void CHW::Reset(SDL_Window* hwnd) +void CHW::Reset() { - BOOL bWindowed = !psDeviceFlags.is(rsFullscreen); - CHK_GL(glDeleteProgramPipelines(1, &pPP)); CHK_GL(glDeleteFramebuffers(1, &pFB)); CHK_GL(glDeleteFramebuffers(1, &pCFB)); @@ -131,57 +123,6 @@ void CHW::Reset(SDL_Window* hwnd) CHK_GL(glDeleteTextures(1, &pBaseZB)); UpdateViews(); - - updateWindowProps(hwnd); - SDL_ShowWindow(hwnd); -} - -void CHW::updateWindowProps(SDL_Window* m_sdlWnd) -{ - bool bWindowed = !psDeviceFlags.is(rsFullscreen); - - u32 dwWindowStyle = 0; - // Set window properties depending on what mode were in. - if (bWindowed) - { - if (m_move_window) - { - if (NULL != strstr(Core.Params, "-draw_borders")) - SDL_SetWindowBordered(m_sdlWnd, SDL_TRUE); - // When moving from fullscreen to windowed mode, it is important to - // adjust the window size after recreating the device rather than - // beforehand to ensure that you get the window size you want. For - // example, when switching from 640x480 fullscreen to windowed with - // a 1000x600 window on a 1024x768 desktop, it is impossible to set - // the window size to 1000x600 until after the display mode has - // changed to 1024x768, because windows cannot be larger than the - // desktop. - - bool centerScreen = false; - if (GEnv.isDedicatedServer || strstr(Core.Params, "-center_screen")) - centerScreen = true; - - SDL_SetWindowSize(m_sdlWnd, psCurrentVidMode[0], psCurrentVidMode[1]); - - if (centerScreen) - { - SDL_SetWindowPosition(m_sdlWnd, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); - } - else - { - SDL_SetWindowPosition(m_sdlWnd, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED); - } - } - } - else - { - SDL_SetWindowPosition(m_sdlWnd, 0, 0); - SDL_SetWindowSize(m_sdlWnd, psCurrentVidMode[0], psCurrentVidMode[1]); - SDL_ShowWindow(m_sdlWnd); - } - - if (!GEnv.isDedicatedServer) - SDL_SetWindowGrab(m_sdlWnd, SDL_TRUE); } struct uniqueRenderingMode diff --git a/src/xrEngine/Device_Initialize.cpp b/src/xrEngine/Device_Initialize.cpp index 6fa1b0a1f2d..3f1d3d93647 100644 --- a/src/xrEngine/Device_Initialize.cpp +++ b/src/xrEngine/Device_Initialize.cpp @@ -46,12 +46,12 @@ void CRenderDevice::Initialize() if (!m_sdlWnd) { - Uint32 flags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_OPENGL; + Uint32 flags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL; #if SDL_VERSION_ATLEAST(2, 0, 5) flags |= SDL_WINDOW_ALWAYS_ON_TOP; #endif - m_sdlWnd = SDL_CreateWindow("S.T.A.L.K.E.R.: Call of Pripyat", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, flags); + m_sdlWnd = SDL_CreateWindow("S.T.A.L.K.E.R.: Call of Pripyat", 0, 0, 640, 480, flags); R_ASSERT3(m_sdlWnd, "Unable to create SDL window", SDL_GetError()); } diff --git a/src/xrEngine/Device_create.cpp b/src/xrEngine/Device_create.cpp index c771474b83b..03b7ddaf007 100644 --- a/src/xrEngine/Device_create.cpp +++ b/src/xrEngine/Device_create.cpp @@ -37,8 +37,8 @@ void CRenderDevice::Create() #endif fFOV = 90.f; fASPECT = 1.f; - const bool noEd = !editor(); - GEnv.Render->Create(m_sdlWnd, dwWidth, dwHeight, fWidth_2, fHeight_2, noEd); + GEnv.Render->Create(m_sdlWnd, dwWidth, dwHeight, fWidth_2, fHeight_2); + UpdateWindowProps(); SDL_GetWindowPosition(m_sdlWnd, &m_rcWindowClient.x, &m_rcWindowClient.y); int w = 0, h = 0; SDL_GetWindowSize(m_sdlWnd, &w, &h); @@ -54,3 +54,38 @@ void CRenderDevice::Create() dwFrame = 0; PreCache(0, false, false); } + +void CRenderDevice::UpdateWindowProps() +{ + const bool windowed = !psDeviceFlags.is(rsFullscreen) || editor(); + + // Set window properties depending on what mode were in. + if (windowed) + { + const bool drawBorders = strstr(Core.Params, "-draw_borders"); + if (drawBorders) + SDL_SetWindowBordered(m_sdlWnd, SDL_TRUE); + + SDL_SetWindowSize(m_sdlWnd, psCurrentVidMode[0], psCurrentVidMode[1]); + + if (GEnv.isDedicatedServer || strstr(Core.Params, "-center_screen")) + SDL_SetWindowPosition(m_sdlWnd, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED); + else + { + int top = 0, left = 0, right = 0, bottom = 0; + //SDL_GetWindowBordersSize(m_sdlWnd, &top, &left, &bottom, &right); +#ifdef WINDOWS + // XXX: Currently SDL_GetWindowBordersSize is supported only on X11 + // For now we must use method below. + if (drawBorders) + top = GetSystemMetrics(SM_CYCAPTION); // size of the window title bar +#else +#pragma TODO("Implement for other platforms") +#endif + SDL_SetWindowPosition(m_sdlWnd, left, top); + } + } + + if (!GEnv.isDedicatedServer) + SDL_SetWindowGrab(m_sdlWnd, SDL_TRUE); +} diff --git a/src/xrEngine/Device_destroy.cpp b/src/xrEngine/Device_destroy.cpp index 37b5e2c6b64..f133fe03cd6 100644 --- a/src/xrEngine/Device_destroy.cpp +++ b/src/xrEngine/Device_destroy.cpp @@ -46,6 +46,13 @@ void CRenderDevice::Reset(bool precache) const auto tm_start = TimerAsync(); GEnv.Render->Reset(m_sdlWnd, dwWidth, dwHeight, fWidth_2, fHeight_2); + UpdateWindowProps(); + + SDL_GetWindowPosition(m_sdlWnd, &m_rcWindowClient.x, &m_rcWindowClient.y); + int w = 0, h = 0; + SDL_GetWindowSize(m_sdlWnd, &w, &h); + m_rcWindowClient.w = m_rcWindowClient.x + w; + m_rcWindowClient.h = m_rcWindowClient.y + h; if (g_pGamePersistent) g_pGamePersistent->Environment().bNeed_re_create_env = true; diff --git a/src/xrEngine/Render.h b/src/xrEngine/Render.h index 7d8be3680bc..9881a2481a6 100644 --- a/src/xrEngine/Render.h +++ b/src/xrEngine/Render.h @@ -402,7 +402,7 @@ class ENGINE_API IRender // Init virtual void SetupStates() = 0; virtual void OnDeviceCreate(LPCSTR shName) = 0; - virtual void Create(SDL_Window* hWnd, u32& dwWidth, u32& dwHeight, float& fWidth_2, float& fHeight_2, bool) = 0; + virtual void Create(SDL_Window* hWnd, u32& dwWidth, u32& dwHeight, float& fWidth_2, float& fHeight_2) = 0; virtual void SetupGPU(bool bForceGPU_SW, bool bForceGPU_NonPure, bool bForceGPU_REF) = 0; // Overdraw virtual void overdrawBegin() = 0; diff --git a/src/xrEngine/device.cpp b/src/xrEngine/device.cpp index 044c6f58892..c3939466793 100644 --- a/src/xrEngine/device.cpp +++ b/src/xrEngine/device.cpp @@ -355,12 +355,6 @@ void CRenderDevice::message_loop() case SDL_WINDOWEVENT_CLOSE: event.type = SDL_QUIT; break; case SDL_WINDOWEVENT_SHOWN: SDL_Log("Window %d has been shown", event.window.windowID); - - if (editor()) - { - Device.b_is_Active = TRUE; - break; - } OnWM_Activate(1, event.window.data2); break; case SDL_WINDOWEVENT_HIDDEN: @@ -409,7 +403,9 @@ void CRenderDevice::Run() seqAppStart.Process(); GEnv.Render->ClearTarget(); splash::hide(); + SDL_FlushEvents(SDL_FIRSTEVENT, SDL_SYSWMEVENT); SDL_ShowWindow(m_sdlWnd); + SDL_RaiseWindow(m_sdlWnd); pInput->ClipCursor(true); message_loop(); seqAppEnd.Process(); diff --git a/src/xrEngine/device.h b/src/xrEngine/device.h index 7413fa280f7..fa1e8fec67f 100644 --- a/src/xrEngine/device.h +++ b/src/xrEngine/device.h @@ -249,6 +249,8 @@ class ENGINE_API CRenderDevice : public CRenderDeviceBase void Destroy(void); void Reset(bool precache = true); + void UpdateWindowProps(); + void Initialize(void); void ShutDown(void); virtual const RenderDeviceStatictics& GetStats() const override { return stats; }