From a58537ebd1e4e5bafcac46a8f27efa343d382818 Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Tue, 15 May 2012 22:56:01 -0700 Subject: [PATCH] Preserve aspect ratio when scaling window. --- src/bgfx.cpp | 7 +++++++ src/bgfx_p.h | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 147fc6ab8..a770458f2 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -631,6 +631,13 @@ namespace bgfx write(&_value, sizeof(void*) ); } +#if BX_PLATFORM_WINDOWS + LRESULT CALLBACK Context::Window::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam) + { + return s_ctx.m_window.process(_hwnd, _id, _wparam, _lparam); + } +#endif // BX_PLATFORM_WINDOWS + void Context::init(bool _createRenderThread) { BX_TRACE("init"); diff --git a/src/bgfx_p.h b/src/bgfx_p.h index cace8998f..b9e45673a 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -56,7 +56,7 @@ extern void dbgPrintfData(const void* _data, uint32_t _size, const char* _format # include extern HWND g_bgfxHwnd; #elif BX_PLATFORM_XBOX360 -# include +# include # include #endif // BX_PLATFORM_WINDOWS @@ -2478,7 +2478,7 @@ namespace bgfx } } - static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam) + LRESULT process(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam) { switch (_id) { @@ -2486,6 +2486,46 @@ namespace bgfx TerminateProcess(GetCurrentProcess(), 0); break; + case WM_SIZING: + { + RECT& rect = *(RECT*)_lparam; + uint32_t width = rect.right-rect.left; + uint32_t height = rect.bottom-rect.top; + + switch (_wparam) + { + case WMSZ_LEFT: + case WMSZ_RIGHT: + { + float aspectRatio = 1.0f/m_aspectRatio; + width = bx::uint32_max(BGFX_DEFAULT_WIDTH/4, width); + height = uint32_t(float(width)*aspectRatio); + } + break; + + default: + { + float aspectRatio = m_aspectRatio; + height = bx::uint32_max(BGFX_DEFAULT_HEIGHT/4, height); + width = uint32_t(float(height)*aspectRatio); + } + break; + } + + rect.right = rect.left + width; + rect.bottom = rect.top + height; + + SetWindowPos(_hwnd + , HWND_TOP + , rect.left + , rect.top + , (rect.right-rect.left) + , (rect.bottom-rect.top) + , SWP_SHOWWINDOW + ); + } + return 0; + default: break; } @@ -2509,7 +2549,6 @@ namespace bgfx void adjust(uint32_t _width, uint32_t _height, bool _windowFrame) { - ShowWindow(g_bgfxHwnd, SW_SHOWNORMAL); RECT rect; RECT newrect = {0, 0, (LONG)_width, (LONG)_height}; @@ -2563,11 +2602,17 @@ namespace bgfx ShowWindow(g_bgfxHwnd, SW_RESTORE); + m_aspectRatio = float(_width)/float(_height); + m_frame = _windowFrame; } + private: + static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam); + RECT m_rect; DWORD m_style; + float m_aspectRatio; bool m_frame; bool m_update; };