Utility/Window: Replace NAZARA_UTILITY_THREADED_WINDOW by a runtime flag (WIP)

This commit is contained in:
Jérôme Leclercq
2016-11-07 02:01:09 +01:00
parent 2f11529669
commit cf2bf52701
10 changed files with 176 additions and 186 deletions

View File

@@ -83,6 +83,7 @@ namespace Nz
bool WindowImpl::Create(const VideoMode& mode, const String& title, UInt32 style)
{
bool async = (style & WindowStyle_Threaded) != 0;
bool fullscreen = (style & WindowStyle_Fullscreen) != 0;
DWORD win32Style, win32StyleEx;
unsigned int x, y;
@@ -147,19 +148,20 @@ namespace Nz
m_callback = 0;
#if NAZARA_UTILITY_THREADED_WINDOW
Mutex mutex;
ConditionVariable condition;
m_threadActive = true;
if (async)
{
Mutex mutex;
ConditionVariable condition;
m_threadActive = true;
// On attend que la fenêtre soit créée
mutex.Lock();
m_thread = Thread(WindowThread, &m_handle, win32StyleEx, title, win32Style, x, y, width, height, this, &mutex, &condition);
condition.Wait(&mutex);
mutex.Unlock();
#else
m_handle = CreateWindowExW(win32StyleEx, className, title.GetWideString().data(), win32Style, x, y, width, height, nullptr, nullptr, GetModuleHandle(nullptr), this);
#endif
// On attend que la fenêtre soit créée
mutex.Lock();
m_thread = Thread(WindowThread, &m_handle, win32StyleEx, title, win32Style, x, y, width, height, this, &mutex, &condition);
condition.Wait(&mutex);
mutex.Unlock();
}
else
m_handle = CreateWindowExW(win32StyleEx, className, title.GetWideString().data(), win32Style, x, y, width, height, nullptr, nullptr, GetModuleHandle(nullptr), this);
if (!m_handle)
{
@@ -175,9 +177,7 @@ namespace Nz
m_eventListener = true;
m_ownsWindow = true;
#if !NAZARA_UTILITY_THREADED_WINDOW
m_sizemove = false;
#endif
m_style = style;
// Récupération de la position/taille de la fenêtre (Après sa création)
@@ -203,9 +203,7 @@ namespace Nz
m_eventListener = false;
m_ownsWindow = false;
#if !NAZARA_UTILITY_THREADED_WINDOW
m_sizemove = false;
#endif
m_style = RetrieveStyle(m_handle);
RECT clientRect, windowRect;
@@ -222,18 +220,21 @@ namespace Nz
{
if (m_ownsWindow)
{
#if NAZARA_UTILITY_THREADED_WINDOW
if (m_thread.IsJoinable())
if (m_style & WindowStyle_Threaded)
{
m_threadActive = false;
PostMessageW(m_handle, WM_NULL, 0, 0); // Pour réveiller le thread
if (m_thread.IsJoinable())
{
m_threadActive = false;
PostMessageW(m_handle, WM_NULL, 0, 0); // Wake up our thread
m_thread.Join();
m_thread.Join();
}
}
else
{
if (m_handle)
DestroyWindow(m_handle);
}
#else
if (m_handle)
DestroyWindow(m_handle);
#endif
}
else
SetEventListener(false);
@@ -280,7 +281,7 @@ namespace Nz
if (titleSize == 0)
return String();
titleSize++; // Caractère nul
titleSize++; // \0
std::unique_ptr<wchar_t[]> wTitle(new wchar_t[titleSize]);
GetWindowTextW(m_handle, wTitle.get(), titleSize);
@@ -320,7 +321,11 @@ namespace Nz
if (m_ownsWindow)
{
if (block)
{
NazaraNotice("WaitMessage");
WaitMessage();
NazaraNotice("~WaitMessage");
}
MSG message;
while (PeekMessageW(&message, nullptr, 0, 0, PM_REMOVE))
@@ -525,7 +530,6 @@ namespace Nz
return true; // Afin que Windows ne ferme pas la fenêtre automatiquement
}
#if !NAZARA_UTILITY_THREADED_WINDOW
case WM_ENTERSIZEMOVE:
{
m_sizemove = true;
@@ -536,6 +540,10 @@ namespace Nz
{
m_sizemove = false;
// In case of threaded window, size and move events are not blocked
if (m_style & WindowStyle_Threaded)
break;
// On vérifie ce qui a changé
RECT clientRect, windowRect;
GetClientRect(m_handle, &clientRect);
@@ -565,7 +573,6 @@ namespace Nz
m_parent->PushEvent(event);
}
}
#endif
case WM_KEYDOWN:
case WM_SYSKEYDOWN:
@@ -789,10 +796,8 @@ namespace Nz
case WM_MOVE:
{
#if !NAZARA_UTILITY_THREADED_WINDOW
if (m_sizemove)
if (m_sizemove && (m_style & WindowStyle_Threaded) == 0)
break;
#endif
RECT windowRect;
GetWindowRect(m_handle, &windowRect);
@@ -862,27 +867,26 @@ namespace Nz
case WM_SIZE:
{
#if NAZARA_UTILITY_THREADED_WINDOW
if (wParam != SIZE_MINIMIZED)
#else
if (!m_sizemove && wParam != SIZE_MINIMIZED)
#endif
{
RECT rect;
GetClientRect(m_handle, &rect);
if (m_sizemove && (m_style & WindowStyle_Threaded) == 0)
break;
Vector2ui size(rect.right-rect.left, rect.bottom-rect.top); // On récupère uniquement la taille de la zone client
if (m_size == size)
break;
if (wParam == SIZE_MINIMIZED)
break;
m_size = size;
RECT rect;
GetClientRect(m_handle, &rect);
WindowEvent event;
event.type = WindowEventType_Resized;
event.size.width = size.x;
event.size.height = size.y;
m_parent->PushEvent(event);
}
Vector2ui size(rect.right-rect.left, rect.bottom-rect.top); // On récupère uniquement la taille de la zone client
if (m_size == size)
break;
m_size = size;
WindowEvent event;
event.type = WindowEventType_Resized;
event.size.width = size.x;
event.size.height = size.y;
m_parent->PushEvent(event);
break;
}
@@ -1187,7 +1191,6 @@ namespace Nz
return style;
}
#if NAZARA_UTILITY_THREADED_WINDOW
void WindowImpl::WindowThread(HWND* handle, DWORD styleEx, const String& title, DWORD style, unsigned int x, unsigned int y, unsigned int width, unsigned int height, WindowImpl* window, Mutex* mutex, ConditionVariable* condition)
{
HWND& winHandle = *handle;
@@ -1195,15 +1198,16 @@ namespace Nz
mutex->Lock();
condition->Signal();
mutex->Unlock(); // mutex et condition sont considérés invalides à partir d'ici
mutex->Unlock(); // mutex and condition may be destroyed after this line
if (!winHandle)
return;
NazaraNotice("In");
while (window->m_threadActive)
window->ProcessEvents(true);
NazaraNotice("Out");
DestroyWindow(winHandle);
}
#endif
}

View File

@@ -22,10 +22,8 @@
namespace Nz
{
#if NAZARA_UTILITY_THREADED_WINDOW
class ConditionVariable;
class Mutex;
#endif
class Window;
#undef IsMinimized // Conflit avec la méthode du même nom
@@ -88,9 +86,7 @@ namespace Nz
static Keyboard::Key ConvertVirtualKey(WPARAM key, LPARAM flags);
static LRESULT CALLBACK MessageHandler(HWND window, UINT message, WPARAM wParam, LPARAM lParam);
static UInt32 RetrieveStyle(HWND window);
#if NAZARA_UTILITY_THREADED_WINDOW
static void WindowThread(HWND* handle, DWORD styleEx, const String& title, DWORD style, unsigned int x, unsigned int y, unsigned int width, unsigned int height, WindowImpl* window, Mutex* mutex, ConditionVariable* condition);
#endif
HCURSOR m_cursor;
HWND m_handle;
@@ -101,21 +97,15 @@ namespace Nz
Vector2i m_mousePos;
Vector2i m_position;
Vector2ui m_size;
#if NAZARA_UTILITY_THREADED_WINDOW
Thread m_thread;
#endif
Window* m_parent;
bool m_eventListener;
bool m_keyRepeat;
bool m_mouseInside;
bool m_ownsWindow;
#if !NAZARA_UTILITY_THREADED_WINDOW
bool m_sizemove;
#endif
bool m_smoothScrolling;
#if NAZARA_UTILITY_THREADED_WINDOW
bool m_threadActive;
#endif
short m_scrolling;
};
}