From d6e4a57cf2e86a9c281717d34dd9488cd0eff8cb Mon Sep 17 00:00:00 2001 From: bkaradzic Date: Sun, 13 Jan 2013 10:47:30 -0800 Subject: [PATCH] Moving window creation to examples/common. --- examples/00-helloworld/helloworld.cpp | 3 +- examples/01-cubes/cubes.cpp | 3 +- examples/02-metaballs/metaballs.cpp | 3 +- examples/03-raymarch/raymarch.cpp | 3 +- examples/04-mesh/mesh.cpp | 3 +- examples/05-instancing/instancing.cpp | 3 +- examples/06-bump/bump.cpp | 3 +- examples/08-update/update.cpp | 3 +- examples/common/entry_nacl.cpp | 11 +- examples/common/entry_windows.cpp | 337 +++++++++++++++++++++++++- include/bgfxplatform.h | 31 +++ src/bgfx.cpp | 18 +- src/bgfx_p.h | 169 ------------- src/vertexdecl.h | 5 + 14 files changed, 398 insertions(+), 197 deletions(-) create mode 100644 include/bgfxplatform.h diff --git a/examples/00-helloworld/helloworld.cpp b/examples/00-helloworld/helloworld.cpp index 7311a178..3b560bc0 100644 --- a/examples/00-helloworld/helloworld.cpp +++ b/examples/00-helloworld/helloworld.cpp @@ -5,6 +5,7 @@ #include #include +#include "../common/entry.h" #include "../common/dbg.h" int _main_(int _argc, char** _argv) @@ -26,7 +27,7 @@ int _main_(int _argc, char** _argv) , 0 ); - while (true) + while (entry::Event::Exit != entry::poll() ) { // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. diff --git a/examples/01-cubes/cubes.cpp b/examples/01-cubes/cubes.cpp index be105d9e..6520be8e 100644 --- a/examples/01-cubes/cubes.cpp +++ b/examples/01-cubes/cubes.cpp @@ -6,6 +6,7 @@ #include #include #include +#include "../common/entry.h" #include "../common/dbg.h" #include "../common/math.h" @@ -168,7 +169,7 @@ int _main_(int _argc, char** _argv) bgfx::destroyVertexShader(vsh); bgfx::destroyFragmentShader(fsh); - while (true) + while (entry::Event::Exit != entry::poll() ) { // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. diff --git a/examples/02-metaballs/metaballs.cpp b/examples/02-metaballs/metaballs.cpp index addc50aa..b16826d1 100644 --- a/examples/02-metaballs/metaballs.cpp +++ b/examples/02-metaballs/metaballs.cpp @@ -6,6 +6,7 @@ #include #include #include +#include "../common/entry.h" #include "../common/dbg.h" #include "../common/math.h" @@ -568,7 +569,7 @@ int _main_(int _argc, char** _argv) const uint32_t zpitch = DIMS*DIMS; const float invdim = 1.0f/float(DIMS-1); - while (true) + while (entry::Event::Exit != entry::poll() ) { // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. diff --git a/examples/03-raymarch/raymarch.cpp b/examples/03-raymarch/raymarch.cpp index 4bb9de81..631f5957 100644 --- a/examples/03-raymarch/raymarch.cpp +++ b/examples/03-raymarch/raymarch.cpp @@ -6,6 +6,7 @@ #include #include #include +#include "../common/entry.h" #include "../common/dbg.h" #include "../common/math.h" @@ -231,7 +232,7 @@ int _main_(int _argc, char** _argv) bgfx::ProgramHandle raymarching = loadProgram("vs_raymarching", "fs_raymarching"); - while (true) + while (entry::Event::Exit != entry::poll() ) { // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to viewZ 0. diff --git a/examples/04-mesh/mesh.cpp b/examples/04-mesh/mesh.cpp index debc3ec1..cdd41156 100644 --- a/examples/04-mesh/mesh.cpp +++ b/examples/04-mesh/mesh.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "../common/entry.h" #include "../common/dbg.h" #include "../common/math.h" @@ -326,7 +327,7 @@ int _main_(int _argc, char** _argv) Mesh mesh; mesh.load("meshes/bunny.bin"); - while (true) + while (entry::Event::Exit != entry::poll() ) { // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. diff --git a/examples/05-instancing/instancing.cpp b/examples/05-instancing/instancing.cpp index ae01513c..c1192006 100644 --- a/examples/05-instancing/instancing.cpp +++ b/examples/05-instancing/instancing.cpp @@ -6,6 +6,7 @@ #include #include #include +#include "../common/entry.h" #include "../common/dbg.h" #include "../common/math.h" @@ -168,7 +169,7 @@ int _main_(int _argc, char** _argv) bgfx::destroyVertexShader(vsh); bgfx::destroyFragmentShader(fsh); - while (true) + while (entry::Event::Exit != entry::poll() ) { // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. diff --git a/examples/06-bump/bump.cpp b/examples/06-bump/bump.cpp index 0cd8ef86..4fbf7a6b 100644 --- a/examples/06-bump/bump.cpp +++ b/examples/06-bump/bump.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "../common/entry.h" #include "../common/dbg.h" #include "../common/math.h" @@ -344,7 +345,7 @@ int _main_(int _argc, char** _argv) mem = loadTexture("fieldstone-n.dds"); bgfx::TextureHandle textureNormal = bgfx::createTexture(mem); - while (true) + while (entry::Event::Exit != entry::poll() ) { // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. diff --git a/examples/08-update/update.cpp b/examples/08-update/update.cpp index 14e04ae6..0bf60d76 100644 --- a/examples/08-update/update.cpp +++ b/examples/08-update/update.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "../common/entry.h" #include "../common/dbg.h" #include "../common/math.h" @@ -224,7 +225,7 @@ int _main_(int _argc, char** _argv) int64_t updateTime = 0; - while (true) + while (entry::Event::Exit != entry::poll() ) { // This dummy draw call is here to make sure that view 0 is cleared // if no other draw calls are submitted to view 0. diff --git a/examples/common/entry_nacl.cpp b/examples/common/entry_nacl.cpp index 3e91580a..3c3cec13 100644 --- a/examples/common/entry_nacl.cpp +++ b/examples/common/entry_nacl.cpp @@ -3,13 +3,11 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ -#include #include -#include "dbg.h" -#include #if BX_PLATFORM_NACL +#include #include #include @@ -22,11 +20,8 @@ #include #include -namespace bgfx -{ - typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height); - extern void naclSetIntefraces(PP_Instance, const PPB_Instance*, const PPB_Graphics3D*, PostSwapBuffersFn); -} +#include +#include "dbg.h" extern int _main_(int _argc, char** _argv); diff --git a/examples/common/entry_windows.cpp b/examples/common/entry_windows.cpp index 226bee29..553d1b12 100644 --- a/examples/common/entry_windows.cpp +++ b/examples/common/entry_windows.cpp @@ -7,11 +7,346 @@ #if BX_PLATFORM_WINDOWS +#include +#include +#include + +#include "entry.h" +#include "dbg.h" + +#define DEFAULT_WIDTH 1280 +#define DEFAULT_HEIGHT 720 + extern int _main_(int _argc, char** _argv); +namespace entry +{ + struct MainThreadEntry + { + int m_argc; + char** m_argv; + + static int32_t threadFunc(void* _userData) + { + MainThreadEntry* self = (MainThreadEntry*)_userData; + return _main_(self->m_argc, self->m_argv); + } + }; + + struct Context + { + Context() + : m_frame(true) + , m_exit(false) + { + } + + int32_t main(int _argc, char** _argv) + { + HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL); + + WNDCLASSEX wnd; + memset(&wnd, 0, sizeof(wnd) ); + wnd.cbSize = sizeof(wnd); + wnd.lpfnWndProc = DefWindowProc; + wnd.hInstance = instance; + wnd.hIcon = LoadIcon(instance, IDI_APPLICATION); + wnd.hCursor = LoadCursor(instance, IDC_ARROW); + wnd.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wnd.lpszClassName = "bgfx_letterbox"; + wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION); + RegisterClassExA(&wnd); + + memset(&wnd, 0, sizeof(wnd) ); + wnd.cbSize = sizeof(wnd); + wnd.style = CS_HREDRAW | CS_VREDRAW; + wnd.lpfnWndProc = wndProc; + wnd.hInstance = instance; + wnd.hIcon = LoadIcon(instance, IDI_APPLICATION); + wnd.hCursor = LoadCursor(instance, IDC_ARROW); + wnd.lpszClassName = "bgfx"; + wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION); + RegisterClassExA(&wnd); + + HWND hwnd = CreateWindowA("bgfx_letterbox" + , "BGFX" + , WS_POPUP|WS_SYSMENU + , -32000 + , -32000 + , 0 + , 0 + , NULL + , NULL + , instance + , 0 + ); + + m_hwnd = CreateWindowA("bgfx" + , "BGFX" + , WS_OVERLAPPEDWINDOW|WS_VISIBLE + , 0 + , 0 + , DEFAULT_WIDTH + , DEFAULT_HEIGHT + , hwnd + , NULL + , instance + , 0 + ); + + bgfx::setHwnd(m_hwnd); + + adjust(DEFAULT_WIDTH, DEFAULT_HEIGHT, true); + + MainThreadEntry mte; + mte.m_argc = _argc; + mte.m_argv = _argv; + + bx::Thread thread; + thread.init(mte.threadFunc, &mte); + + MSG msg; + msg.message = WM_NULL; + + while (!m_exit) + { + WaitMessage(); + + while (0 != PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) ) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + } + + thread.shutdown(); + + return 0; + } + + LRESULT process(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam) + { + switch (_id) + { + case WM_CLOSE: + TerminateProcess(GetCurrentProcess(), 0); + break; + + case WM_SIZING: + { + RECT clientRect; + GetClientRect(_hwnd, &clientRect); + uint32_t width = clientRect.right-clientRect.left; + uint32_t height = clientRect.bottom-clientRect.top; + + RECT& rect = *(RECT*)_lparam; + uint32_t frameWidth = rect.right-rect.left - width; + uint32_t frameHeight = rect.bottom-rect.top - height; + + switch (_wparam) + { + case WMSZ_LEFT: + case WMSZ_RIGHT: + { + float aspectRatio = 1.0f/m_aspectRatio; + width = bx::uint32_max(DEFAULT_WIDTH/4, width); + height = uint32_t(float(width)*aspectRatio); + } + break; + + default: + { + float aspectRatio = m_aspectRatio; + height = bx::uint32_max(DEFAULT_HEIGHT/4, height); + width = uint32_t(float(height)*aspectRatio); + } + break; + } + + rect.right = rect.left + width + frameWidth; + rect.bottom = rect.top + height + frameHeight; + + SetWindowPos(_hwnd + , HWND_TOP + , rect.left + , rect.top + , (rect.right-rect.left) + , (rect.bottom-rect.top) + , SWP_SHOWWINDOW + ); + } + return 0; + + case WM_SYSCOMMAND: + switch (_wparam) + { + case SC_MINIMIZE: + case SC_RESTORE: + { + HWND parent = GetWindow(_hwnd, GW_OWNER); + if (NULL != parent) + { + PostMessage(parent, _id, _wparam, _lparam); + } + } + } + break; + + case WM_KEYDOWN: + case WM_SYSKEYDOWN: + if ((WM_KEYDOWN == _id && VK_F11 == _wparam) + || (WM_SYSKEYDOWN == _id && VK_RETURN == _wparam) ) + { + toggleWindowFrame(); + } + break; + + default: + break; + } + + return DefWindowProc(_hwnd, _id, _wparam, _lparam); + } + + void adjust(uint32_t _width, uint32_t _height, bool _windowFrame) + { + m_width = _width; + m_height = _height; + m_aspectRatio = float(_width)/float(_height); + + ShowWindow(m_hwnd, SW_SHOWNORMAL); + RECT rect; + RECT newrect = {0, 0, (LONG)_width, (LONG)_height}; + DWORD style = WS_POPUP|WS_SYSMENU; + + if (m_frame) + { + GetWindowRect(m_hwnd, &m_rect); + m_style = GetWindowLong(m_hwnd, GWL_STYLE); + } + + if (_windowFrame) + { + rect = m_rect; + style = m_style; + } + else + { +#if defined(__MINGW32__) + rect = m_rect; + style = m_style; +#else + HMONITOR monitor = MonitorFromWindow(m_hwnd, MONITOR_DEFAULTTONEAREST); + MONITORINFO mi; + mi.cbSize = sizeof(mi); + GetMonitorInfo(monitor, &mi); + newrect = mi.rcMonitor; + rect = mi.rcMonitor; +#endif // !defined(__MINGW__) + } + + SetWindowLong(m_hwnd, GWL_STYLE, style); + AdjustWindowRect(&newrect, style, FALSE); + UpdateWindow(m_hwnd); + + if (rect.left == -32000 + || rect.top == -32000) + { + rect.left = 0; + rect.top = 0; + } + + int32_t left = rect.left; + int32_t top = rect.top; + int32_t width = (newrect.right-newrect.left); + int32_t height = (newrect.bottom-newrect.top); + + if (!_windowFrame) + { + float aspectRatio = 1.0f/m_aspectRatio; + width = bx::uint32_max(DEFAULT_WIDTH/4, width); + height = uint32_t(float(width)*aspectRatio); + + left = newrect.left+(newrect.right-newrect.left-width)/2; + top = newrect.top+(newrect.bottom-newrect.top-height)/2; + } + + HWND parent = GetWindow(m_hwnd, GW_OWNER); + if (NULL != parent) + { + if (_windowFrame) + { + SetWindowPos(parent + , HWND_TOP + , -32000 + , -32000 + , 0 + , 0 + , SWP_SHOWWINDOW + ); + } + else + { + SetWindowPos(parent + , HWND_TOP + , newrect.left + , newrect.top + , newrect.right-newrect.left + , newrect.bottom-newrect.top + , SWP_SHOWWINDOW + ); + } + } + + SetWindowPos(m_hwnd + , HWND_TOP + , left + , top + , width + , height + , SWP_SHOWWINDOW + ); + + ShowWindow(m_hwnd, SW_RESTORE); + + m_frame = _windowFrame; + } + + static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam); + + void toggleWindowFrame() + { + adjust(m_width, m_height, !m_frame); + } + + HWND m_hwnd; + RECT m_rect; + DWORD m_style; + uint32_t m_width; + uint32_t m_height; + float m_aspectRatio; + bool m_frame; + bool m_exit; + }; + + static Context s_ctx; + + LRESULT CALLBACK Context::wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam) + { + return s_ctx.process(_hwnd, _id, _wparam, _lparam); + } + + Event::Enum poll() + { + return Event::Nop; + } + +} // namespace entry + int main(int _argc, char** _argv) { - return _main_(_argc, _argv); + using namespace entry; + return s_ctx.main(_argc, _argv); } #endif // BX_PLATFORM_WINDOWS diff --git a/include/bgfxplatform.h b/include/bgfxplatform.h new file mode 100644 index 00000000..e154a85e --- /dev/null +++ b/include/bgfxplatform.h @@ -0,0 +1,31 @@ +/* + * Copyright 2011-2012 Branimir Karadzic. All rights reserved. + * License: http://www.opensource.org/licenses/BSD-2-Clause + */ + +#ifndef __BGFXPLATFORM_H__ +#define __BGFXPLATFORM_H__ + +#include + +#if BX_PLATFORM_NACL +# include +# include + +namespace bgfx +{ + typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height); + void naclSetIntefraces(PP_Instance, const PPB_Instance*, const PPB_Graphics3D*, PostSwapBuffersFn); +} // namespace bgfx + +#elif BX_PLATFORM_WINDOWS +# include + +namespace bgfx +{ + void setHwnd(HWND _hwnd); +} // namespace bgfx + +#endif // BX_PLATFORM_ + +#endif // __BGFXPLATFORM_H__ diff --git a/src/bgfx.cpp b/src/bgfx.cpp index 04f3801b..82f46134 100644 --- a/src/bgfx.cpp +++ b/src/bgfx.cpp @@ -37,6 +37,13 @@ namespace bgfx # define BGFX_CHECK_RENDER_THREAD() #endif // BGFX_CONFIG_MULTITHREADED +#if BX_PLATFORM_WINDOWS + void setHwnd(HWND _hwnd) + { + g_bgfxHwnd = _hwnd; + } +#endif // BX_PLATFORM_WINDOWS + struct CallbackStub : public CallbackI { virtual ~CallbackStub() @@ -643,13 +650,6 @@ 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"); @@ -657,10 +657,6 @@ namespace bgfx m_submit->create(); m_render->create(); -#if BX_PLATFORM_WINDOWS - m_window.init(); -#endif // BX_PLATFORM_ - #if BGFX_CONFIG_MULTITHREADED if (_createRenderThread) { diff --git a/src/bgfx_p.h b/src/bgfx_p.h index d7ee4e57..7498c63b 100644 --- a/src/bgfx_p.h +++ b/src/bgfx_p.h @@ -1443,10 +1443,6 @@ namespace bgfx void frame() { -#if BX_PLATFORM_WINDOWS - m_window.update(); -#endif // BX_PLATFORM_WINDOWS - // wait for render thread to finish renderSemWait(); @@ -2846,167 +2842,9 @@ namespace bgfx { Window() : m_frame(true) - , m_update(false) { } - void init() - { - if (NULL == g_bgfxHwnd) - { - HINSTANCE instance = (HINSTANCE)GetModuleHandle(NULL); - - WNDCLASSEX wnd; - memset(&wnd, 0, sizeof(wnd) ); - wnd.cbSize = sizeof(wnd); - wnd.lpfnWndProc = DefWindowProc; - wnd.hInstance = instance; - wnd.hIcon = LoadIcon(instance, IDI_APPLICATION); - wnd.hCursor = LoadCursor(instance, IDC_ARROW); - wnd.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); - wnd.lpszClassName = "bgfx_letterbox"; - wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION); - RegisterClassExA(&wnd); - - memset(&wnd, 0, sizeof(wnd) ); - wnd.cbSize = sizeof(wnd); - wnd.style = CS_HREDRAW | CS_VREDRAW; - wnd.lpfnWndProc = wndProc; - wnd.hInstance = instance; - wnd.hIcon = LoadIcon(instance, IDI_APPLICATION); - wnd.hCursor = LoadCursor(instance, IDC_ARROW); - wnd.lpszClassName = "bgfx"; - wnd.hIconSm = LoadIcon(instance, IDI_APPLICATION); - RegisterClassExA(&wnd); - - HWND hwnd = CreateWindowA("bgfx_letterbox" - , "BGFX" - , WS_POPUP|WS_SYSMENU - , -32000 - , -32000 - , 0 - , 0 - , NULL - , NULL - , instance - , 0 - ); - - g_bgfxHwnd = CreateWindowA("bgfx" - , "BGFX" - , WS_OVERLAPPEDWINDOW|WS_VISIBLE - , 0 - , 0 - , BGFX_DEFAULT_WIDTH - , BGFX_DEFAULT_HEIGHT - , hwnd - , NULL - , instance - , 0 - ); - - m_update = true; - } - } - - LRESULT process(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam) - { - switch (_id) - { - case WM_CLOSE: - TerminateProcess(GetCurrentProcess(), 0); - break; - - case WM_SIZING: - { - RECT clientRect; - GetClientRect(_hwnd, &clientRect); - uint32_t width = clientRect.right-clientRect.left; - uint32_t height = clientRect.bottom-clientRect.top; - - RECT& rect = *(RECT*)_lparam; - uint32_t frameWidth = rect.right-rect.left - width; - uint32_t frameHeight = rect.bottom-rect.top - height; - - 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 + frameWidth; - rect.bottom = rect.top + height + frameHeight; - - SetWindowPos(_hwnd - , HWND_TOP - , rect.left - , rect.top - , (rect.right-rect.left) - , (rect.bottom-rect.top) - , SWP_SHOWWINDOW - ); - } - return 0; - - case WM_SYSCOMMAND: - switch (_wparam) - { - case SC_MINIMIZE: - case SC_RESTORE: - { - HWND parent = GetWindow(_hwnd, GW_OWNER); - if (NULL != parent) - { - PostMessage(parent, _id, _wparam, _lparam); - } - } - } - break; - - case WM_KEYDOWN: - case WM_SYSKEYDOWN: - if ((WM_KEYDOWN == _id && VK_F11 == _wparam) - || (WM_SYSKEYDOWN == _id && VK_RETURN == _wparam) ) - { - toggleWindowFrame(); - } - break; - - default: - break; - } - - return DefWindowProc(_hwnd, _id, _wparam, _lparam); - } - - void update() - { - if (m_update) - { - MSG msg; - msg.message = WM_NULL; - if (0 != PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) ) - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - } - } - void adjust(uint32_t _width, uint32_t _height, bool _windowFrame) { m_width = _width; @@ -3112,13 +2950,6 @@ namespace bgfx } private: - static LRESULT CALLBACK wndProc(HWND _hwnd, UINT _id, WPARAM _wparam, LPARAM _lparam); - - void toggleWindowFrame() - { - adjust(m_width, m_height, !m_frame); - } - RECT m_rect; DWORD m_style; uint32_t m_width; diff --git a/src/vertexdecl.h b/src/vertexdecl.h index e3ec8903..b14c16b8 100644 --- a/src/vertexdecl.h +++ b/src/vertexdecl.h @@ -3,6 +3,9 @@ * License: http://www.opensource.org/licenses/BSD-2-Clause */ +#ifndef __VERTEXDECL_H__ +#define __VERTEXDECL_H__ + #include namespace bgfx @@ -14,3 +17,5 @@ namespace bgfx void dump(const VertexDecl& _decl); } // namespace bgfx + +#endif // __VERTEXDECL_H__