From ddd3f84ad7cd82763b6e8028d00421b2c57021ee Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Mon, 20 Aug 2012 18:53:06 -0300 Subject: [PATCH] Many fixes in win32 platform --- CMakeLists.txt | 2 +- modules/client/client.lua | 1 + src/framework/core/adaptativeframecounter.h | 1 + src/framework/core/graphicalapplication.cpp | 3 +- src/framework/platform/platformwindow.h | 2 + src/framework/platform/win32window.cpp | 128 +++++++++++++------- src/framework/platform/win32window.h | 6 +- src/framework/platform/x11window.cpp | 3 + 8 files changed, 95 insertions(+), 51 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e82647f..6fcf6661 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,7 +25,7 @@ if(WIN32) -I${CMAKE_CURRENT_SOURCE_DIR}/src -i${CMAKE_CURRENT_SOURCE_DIR}/src/otcicon.rc -o ${CMAKE_CURRENT_BINARY_DIR}/otcicon.o) - SET(executable_SOURCES ${executable_SOURCES} otcicon.o) + set(executable_SOURCES ${executable_SOURCES} otcicon.o) endif() # add otclient executable diff --git a/modules/client/client.lua b/modules/client/client.lua index 962a6f42..bccc12db 100644 --- a/modules/client/client.lua +++ b/modules/client/client.lua @@ -35,6 +35,7 @@ end function Client.init() g_window.setMinimumSize({ width = 600, height = 480 }) + g_sounds.preload("startup.ogg") -- initialize in fullscreen mode on mobile devices if g_window.getPlatformType() == "X11-EGL" then diff --git a/src/framework/core/adaptativeframecounter.h b/src/framework/core/adaptativeframecounter.h index 553d7a62..0527d08f 100644 --- a/src/framework/core/adaptativeframecounter.h +++ b/src/framework/core/adaptativeframecounter.h @@ -52,6 +52,7 @@ public: int getLastFps() { return m_lastFps; } int getPartialFps() { return (int)m_partialFps; } int getMaxFps() { return m_maxFps; } + int getFrames() { return m_frames; } float getMediumFrameDelay() { return m_mediumFrameDelay; } private: diff --git a/src/framework/core/graphicalapplication.cpp b/src/framework/core/graphicalapplication.cpp index bb9f7500..c1bc0e82 100644 --- a/src/framework/core/graphicalapplication.cpp +++ b/src/framework/core/graphicalapplication.cpp @@ -108,8 +108,7 @@ void GraphicalApplication::run() g_lua.callGlobalField("g_app", "onRun"); - // show the application only after we draw some frames - g_dispatcher.scheduleEvent([] { g_window.show(); }, 10); + g_window.show(); while(!m_stopping) { // poll all events before rendering diff --git a/src/framework/platform/platformwindow.h b/src/framework/platform/platformwindow.h index 989af504..065b2323 100644 --- a/src/framework/platform/platformwindow.h +++ b/src/framework/platform/platformwindow.h @@ -70,6 +70,7 @@ public: Size getUnmaximizedSize() { return m_unmaximizedSize; } Size getSize() { return m_size; } + Size getMinimumSize() { return m_minimumSize; } int getWidth() { return m_size.width(); } int getHeight() { return m_size.height(); } Point getUnmaximizedPos() { return m_unmaximizedPos; } @@ -105,6 +106,7 @@ protected: Timer m_keyPressTimer; Size m_size; + Size m_minimumSize; Point m_position; Size m_unmaximizedSize; Point m_unmaximizedPos; diff --git a/src/framework/platform/win32window.cpp b/src/framework/platform/win32window.cpp index 4491ef5a..02e1967e 100644 --- a/src/framework/platform/win32window.cpp +++ b/src/framework/platform/win32window.cpp @@ -37,6 +37,8 @@ WIN32Window::WIN32Window() m_deviceContext = 0; m_cursor = 0; m_minimumSize = Size(600,480); + m_size = Size(600,480); + m_hidden = true; #ifdef OPENGL_ES m_eglConfig = 0; @@ -264,21 +266,19 @@ void WIN32Window::internalCreateWindow() DWORD dwStyle = WS_OVERLAPPEDWINDOW; // initialize in the center of the screen - m_size = m_minimumSize; m_position = ((getDisplaySize() - m_size) / 2).toPoint(); - RECT windowRect = {m_position.x, m_position.y, m_position.x + m_size.width(), m_position.y + m_size.height()}; - AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle); + Rect screenRect = adjustWindowRect(Rect(m_position, m_size)); updateUnmaximizedCoords(); m_window = CreateWindowExA(dwExStyle, g_app.getCompactName().c_str(), NULL, dwStyle, - windowRect.left, - windowRect.top, - windowRect.right - windowRect.left, - windowRect.bottom - windowRect.top, + screenRect.left(), + screenRect.top(), + screenRect.width(), + screenRect.height(), NULL, NULL, m_instance, @@ -444,50 +444,45 @@ void *WIN32Window::getExtensionProcAddress(const char *ext) void WIN32Window::move(const Point& pos) { - RECT clientRect; - GetClientRect(m_window, &clientRect); - - RECT windowRect = {pos.x, pos.y, clientRect.right - clientRect.left, clientRect.bottom - clientRect.top}; - AdjustWindowRectEx(&windowRect, WS_OVERLAPPEDWINDOW, FALSE, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE); - - int x = windowRect.left; - int y = windowRect.top; - GetWindowRect(m_window, &windowRect); - int width = windowRect.right - windowRect.left; - int height = windowRect.bottom - windowRect.top; - - MoveWindow(m_window, x, y, width, height, TRUE); + Rect clientRect(pos, getClientRect().size()); + Rect windowRect = adjustWindowRect(clientRect); + MoveWindow(m_window, windowRect.x(), windowRect.y(), windowRect.width(), windowRect.height(), TRUE); + if(m_hidden) + ShowWindow(m_window, SW_HIDE); } void WIN32Window::resize(const Size& size) { - RECT windowRect; - RECT clientRect; - - GetWindowRect(m_window, &windowRect); - GetClientRect(m_window, &clientRect); - - int x = windowRect.left; - int y = windowRect.top; - int width = size.width() + ((windowRect.right - windowRect.left) - (clientRect.right - clientRect.left)); - int height = size.height() + ((windowRect.bottom - windowRect.top) - (clientRect.bottom - clientRect.top)); - - MoveWindow(m_window, x, y, width, height, TRUE); + if(size.width() < m_minimumSize.width() || size.height() < m_minimumSize.height()) + return; + Rect clientRect(getClientRect().topLeft(), size); + Rect windowRect = adjustWindowRect(clientRect); + MoveWindow(m_window, windowRect.x(), windowRect.y(), windowRect.width(), windowRect.height(), TRUE); + if(m_hidden) + ShowWindow(m_window, SW_HIDE); } void WIN32Window::show() { - ShowWindow(m_window, SW_SHOW); + m_hidden = false; + if(m_maximized) + ShowWindow(m_window, SW_MAXIMIZE); + else + ShowWindow(m_window, SW_SHOW); } void WIN32Window::hide() { + m_hidden = true; ShowWindow(m_window, SW_HIDE); } void WIN32Window::maximize() { - ShowWindow(m_window, SW_MAXIMIZE); + if(!m_hidden) + ShowWindow(m_window, SW_MAXIMIZE); + else + m_maximized = true; } void WIN32Window::poll() @@ -697,8 +692,9 @@ LRESULT WIN32Window::windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPar } case WM_GETMINMAXINFO: { LPMINMAXINFO pMMI = (LPMINMAXINFO)lParam; - pMMI->ptMinTrackSize.x = m_minimumSize.width(); - pMMI->ptMinTrackSize.y = m_minimumSize.height(); + Rect adjustedRect = adjustWindowRect(Rect(getWindowRect().topLeft(), m_minimumSize)); + pMMI->ptMinTrackSize.x = adjustedRect.width(); + pMMI->ptMinTrackSize.y = adjustedRect.height(); break; } case WM_SIZE: { @@ -722,9 +718,9 @@ LRESULT WIN32Window::windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lPar if(m_visible && m_deviceContext) internalRestoreGLContext(); - Size size; - size.setWidth(std::max(std::min((int)LOWORD(lParam), 7680), m_minimumSize.width())); - size.setHeight(std::max(std::min((int)HIWORD(lParam), 4320), m_minimumSize.height())); + Size size = Size(LOWORD(lParam), HIWORD(lParam)); + size.setWidth(std::max(std::min(size.width(), 7680), 32)); + size.setHeight(std::max(std::min(size.height(), 4320), 32)); if(m_visible && (forceResize || m_size != size)) { m_size = size; @@ -837,23 +833,22 @@ void WIN32Window::setFullscreen(bool fullscreen) if(m_fullscreen == fullscreen) return; + m_fullscreen = fullscreen; + DWORD dwStyle = GetWindowLong(m_window, GWL_STYLE); static WINDOWPLACEMENT wpPrev; wpPrev.length = sizeof(wpPrev); if(fullscreen) { + Size size = getDisplaySize(); GetWindowPlacement(m_window, &wpPrev); - SetWindowLong(m_window, GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW); - SetWindowPos(m_window, HWND_TOP, 0, 0, getDisplayWidth(), getDisplayHeight(), - SWP_NOOWNERZORDER | SWP_FRAMECHANGED); + SetWindowLong(m_window, GWL_STYLE, (dwStyle & ~WS_OVERLAPPEDWINDOW) | WS_POPUP | WS_EX_TOPMOST); + SetWindowPos(m_window, HWND_TOPMOST, 0, 0, size.width(), size.height(), SWP_FRAMECHANGED); } else { - SetWindowLong(m_window, GWL_STYLE, dwStyle | WS_OVERLAPPEDWINDOW); + SetWindowLong(m_window, GWL_STYLE, (dwStyle & ~(WS_POPUP | WS_EX_TOPMOST)) | WS_OVERLAPPEDWINDOW); SetWindowPlacement(m_window, &wpPrev); - SetWindowPos(m_window, NULL, 0, 0, 0, 0, - SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED); + SetWindowPos(m_window, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED); } - - m_fullscreen = fullscreen; } void WIN32Window::setVerticalSync(bool enable) @@ -968,4 +963,43 @@ std::string WIN32Window::getPlatformType() #endif } + +Rect WIN32Window::getClientRect() +{ + RECT clientRect = {0,0,0,0}; + int ret = GetClientRect(m_window, &clientRect); + assert(ret != 0); + return Rect(Point(clientRect.left, clientRect.top), Point(clientRect.right, clientRect.bottom)); +} + +Rect WIN32Window::getWindowRect() +{ + RECT windowRect = {0,0,0,0}; + int ret = GetWindowRect(m_window, &windowRect); + assert(ret != 0); + return Rect(Point(windowRect.left, windowRect.top), Point(windowRect.right, windowRect.bottom)); +} + +Rect WIN32Window::adjustWindowRect(const Rect& clientRect) +{ + Rect rect; + DWORD dwStyle; + DWORD dwExStyle; + RECT windowRect = { clientRect.left(), clientRect.top(), clientRect.right(), clientRect.bottom() }; + if(m_window) { + dwStyle = GetWindowLong(m_window, GWL_STYLE); + dwExStyle = GetWindowLong(m_window, GWL_EXSTYLE); + } else { + dwStyle = WS_OVERLAPPEDWINDOW; + dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; + } + if(AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle) != 0) { + rect = Rect(Point(windowRect.left, windowRect.top), Point(windowRect.right, windowRect.bottom)); + } else { + g_logger.traceError("AdjustWindowRectEx failed"); + rect = Rect(0,0, m_minimumSize); + } + return rect; +} + #endif diff --git a/src/framework/platform/win32window.h b/src/framework/platform/win32window.h index 9453561d..815355f2 100644 --- a/src/framework/platform/win32window.h +++ b/src/framework/platform/win32window.h @@ -79,12 +79,16 @@ public: std::string getPlatformType(); private: + Rect getClientRect(); + Rect getWindowRect(); + Rect adjustWindowRect(const Rect& rect); + HWND m_window; HINSTANCE m_instance; HDC m_deviceContext; HCURSOR m_cursor; HCURSOR m_defaultCursor; - Size m_minimumSize; + bool m_hidden; #ifdef OPENGL_ES EGLConfig m_eglConfig; diff --git a/src/framework/platform/x11window.cpp b/src/framework/platform/x11window.cpp index 5724fbc5..1cbacc00 100644 --- a/src/framework/platform/x11window.cpp +++ b/src/framework/platform/x11window.cpp @@ -40,6 +40,7 @@ X11Window::X11Window() m_xic = 0; m_screen = 0; m_wmDelete = 0; + m_minimumSize = Size(600,480); m_size = Size(600,480); #ifdef OPENGL_ES @@ -520,6 +521,8 @@ void X11Window::move(const Point& pos) void X11Window::resize(const Size& size) { + if(size.width() < m_minimumSize.width() || size.height() < m_minimumSize.height()) + return; XResizeWindow(m_display, m_window, size.width(), size.height()); }