From 7659fdf4608bd75de5eebf63156fc734d20a9c9d Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 25 May 2012 12:23:04 +0200 Subject: [PATCH] Window's subsystem is now initialized by NzUtility Optimized Image --- include/Nazara/Utility/Window.hpp | 5 ++ src/Nazara/Utility/Image.cpp | 8 +-- src/Nazara/Utility/PixelFormat.cpp | 10 ++-- src/Nazara/Utility/Utility.cpp | 11 ++++ src/Nazara/Utility/Win32/WindowImpl.cpp | 68 +++++++++---------------- src/Nazara/Utility/Win32/WindowImpl.hpp | 6 ++- src/Nazara/Utility/Window.cpp | 10 ++++ 7 files changed, 64 insertions(+), 54 deletions(-) diff --git a/include/Nazara/Utility/Window.hpp b/include/Nazara/Utility/Window.hpp index fd9f98c2e..e6b202df3 100644 --- a/include/Nazara/Utility/Window.hpp +++ b/include/Nazara/Utility/Window.hpp @@ -24,10 +24,12 @@ #include #endif +class NzUtility; class NzWindowImpl; class NAZARA_API NzWindow : NzNonCopyable { + friend class NzUtility; friend class NzWindowImpl; public: @@ -99,6 +101,9 @@ class NAZARA_API NzWindow : NzNonCopyable private: void PushEvent(const NzEvent& event); + static bool Initialize(); + static void Uninitialize(); + std::queue m_events; #if NAZARA_UTILITY_THREADED_WINDOW NzMutex m_eventMutex; diff --git a/src/Nazara/Utility/Image.cpp b/src/Nazara/Utility/Image.cpp index 6d85cc3c0..96d037cce 100644 --- a/src/Nazara/Utility/Image.cpp +++ b/src/Nazara/Utility/Image.cpp @@ -298,7 +298,7 @@ bool NzImage::Update(const nzUInt8* pixels) return false; } - if (IsCubemap()) + if (m_sharedImage->type == nzImageType_Cubemap) { NazaraError("Update is not designed for cubemaps, use UpdateFace instead"); return false; @@ -327,7 +327,7 @@ bool NzImage::Update(const nzUInt8* pixels, const NzRectui& rect) return false; } - if (IsCubemap()) + if (m_sharedImage->type == nzImageType_Cubemap) { NazaraError("Update is not designed for cubemaps, use UpdateFace instead"); return false; @@ -380,7 +380,7 @@ bool NzImage::UpdateFace(nzCubemapFace face, const nzUInt8* pixels) return false; } - if (!IsCubemap()) + if (m_sharedImage->type != nzImageType_Cubemap) { NazaraError("Update is only designed for cubemaps, use Update instead"); return false; @@ -410,7 +410,7 @@ bool NzImage::UpdateFace(nzCubemapFace face, const nzUInt8* pixels, const NzRect return false; } - if (!IsCubemap()) + if (m_sharedImage->type != nzImageType_Cubemap) { NazaraError("Update is only designed for cubemaps, use Update instead"); return false; diff --git a/src/Nazara/Utility/PixelFormat.cpp b/src/Nazara/Utility/PixelFormat.cpp index 35dc5612f..8dff3adcb 100644 --- a/src/Nazara/Utility/PixelFormat.cpp +++ b/src/Nazara/Utility/PixelFormat.cpp @@ -14,22 +14,22 @@ namespace { inline nzUInt8 c4to8(nzUInt8 c) { - return c * 255.f/15.f; + return c * (255.f/15.f); } inline nzUInt8 c5to8(nzUInt8 c) { - return c * 255.f/31.f; + return c * (255.f/31.f); } inline nzUInt8 c8to4(nzUInt8 c) { - return c * 15.f/255.f; + return c * (15.f/255.f); } inline nzUInt8 c8to5(nzUInt8 c) { - return c * 31.f/255.f; + return c * (31.f/255.f); } template @@ -215,7 +215,7 @@ namespace *ptr++ = (static_cast(c8to5(start[2])) << 11) | (static_cast(c8to5(start[1])) << 6) | (static_cast(c8to5(start[0])) << 1) | - ((start[3] == 0xFF) ? 1 : 0); + ((start[3] == 0xFF) ? 1 : 0); start += 4; } diff --git a/src/Nazara/Utility/Utility.cpp b/src/Nazara/Utility/Utility.cpp index 64de352dc..3d1bfe716 100644 --- a/src/Nazara/Utility/Utility.cpp +++ b/src/Nazara/Utility/Utility.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include NzUtility::NzUtility() @@ -36,6 +37,14 @@ bool NzUtility::Initialize() return false; } + if (!NzWindow::Initialize()) + { + NazaraError("Failed to initialize window's system"); + NzPixelFormat::Uninitialize(); + + return false; + } + // Loaders spécialisés NzLoaders_PCX_Register(); // Loader de fichiers .PCX (1, 4, 8, 24) @@ -60,6 +69,8 @@ void NzUtility::Uninitialize() NzLoaders_STB_Unregister(); NzLoaders_PCX_Unregister(); + NzWindow::Uninitialize(); + NzPixelFormat::Uninitialize(); s_initialized = false; diff --git a/src/Nazara/Utility/Win32/WindowImpl.cpp b/src/Nazara/Utility/Win32/WindowImpl.cpp index 937b1c311..f020cbdfa 100644 --- a/src/Nazara/Utility/Win32/WindowImpl.cpp +++ b/src/Nazara/Utility/Win32/WindowImpl.cpp @@ -25,7 +25,6 @@ namespace { const wchar_t* className = L"Nazara Window"; NzWindowImpl* fullscreenWindow = nullptr; - unsigned int windowCount = 0; } NzWindowImpl::NzWindowImpl(NzWindow* parent) : @@ -62,19 +61,10 @@ void NzWindowImpl::Close() } else SetEventListener(false); - - if (--windowCount == 0) - Uninitialize(); } bool NzWindowImpl::Create(NzVideoMode mode, const NzString& title, nzUInt32 style) { - if (windowCount++ == 0 && !Initialize()) - { - NazaraError("Unable to initialize window implementation"); - return false; - } - bool fullscreen = (style & NzWindow::Fullscreen) != 0; DWORD win32Style, win32StyleEx; unsigned int x, y; @@ -165,15 +155,7 @@ bool NzWindowImpl::Create(NzVideoMode mode, const NzString& title, nzUInt32 styl m_eventListener = true; m_ownsWindow = true; - if (m_handle != nullptr) - return true; - else - { - if (--windowCount == 0) - Uninitialize(); - - return false; - } + return m_handle != nullptr; } bool NzWindowImpl::Create(NzWindowHandle handle) @@ -776,6 +758,30 @@ bool NzWindowImpl::HandleMessage(HWND window, UINT message, WPARAM wParam, LPARA return false; } +bool NzWindowImpl::Initialize() +{ + // Nous devons faire un type Unicode pour que la fenêtre le soit également + // http://msdn.microsoft.com/en-us/library/windows/desktop/ms633574(v=vs.85).aspx + WNDCLASSW windowClass; + windowClass.cbClsExtra = 0; + windowClass.cbWndExtra = 0; + windowClass.hbrBackground = nullptr; + windowClass.hCursor = nullptr; // Le curseur est définit dynamiquement + windowClass.hIcon = nullptr; // L'icône est définie dynamiquement + windowClass.hInstance = GetModuleHandle(nullptr); + windowClass.lpfnWndProc = MessageHandler; + windowClass.lpszClassName = className; + windowClass.lpszMenuName = nullptr; + windowClass.style = CS_DBLCLKS; // Gestion du double-clic + + return RegisterClassW(&windowClass); +} + +void NzWindowImpl::Uninitialize() +{ + UnregisterClassW(className, GetModuleHandle(nullptr)); +} + LRESULT CALLBACK NzWindowImpl::MessageHandler(HWND window, UINT message, WPARAM wParam, LPARAM lParam) { NzWindowImpl* me; @@ -798,25 +804,6 @@ LRESULT CALLBACK NzWindowImpl::MessageHandler(HWND window, UINT message, WPARAM return DefWindowProcW(window, message, wParam, lParam); } -bool NzWindowImpl::Initialize() -{ - // Nous devons faire un type Unicode pour que la fenêtre le soit également - // http://msdn.microsoft.com/en-us/library/windows/desktop/ms633574(v=vs.85).aspx - WNDCLASSW windowClass; - windowClass.cbClsExtra = 0; - windowClass.cbWndExtra = 0; - windowClass.hbrBackground = nullptr; - windowClass.hCursor = nullptr; // Le curseur est définit dynamiquement - windowClass.hIcon = nullptr; // L'icône est définie dynamiquement - windowClass.hInstance = GetModuleHandle(nullptr); - windowClass.lpfnWndProc = MessageHandler; - windowClass.lpszClassName = className; - windowClass.lpszMenuName = nullptr; - windowClass.style = CS_DBLCLKS; // Gestion du double-clic - - return RegisterClassW(&windowClass); -} - NzKeyboard::Key NzWindowImpl::ConvertVirtualKey(WPARAM key, LPARAM flags) { switch (key) @@ -949,11 +936,6 @@ NzKeyboard::Key NzWindowImpl::ConvertVirtualKey(WPARAM key, LPARAM flags) } } -void NzWindowImpl::Uninitialize() -{ - UnregisterClassW(className, GetModuleHandle(nullptr)); -} - #if NAZARA_UTILITY_THREADED_WINDOW void NzWindowImpl::WindowThread(HWND* handle, DWORD styleEx, const wchar_t* title, DWORD style, unsigned int x, unsigned int y, unsigned int width, unsigned int height, NzWindowImpl* window, NzMutex* mutex, NzThreadCondition* condition) { diff --git a/src/Nazara/Utility/Win32/WindowImpl.hpp b/src/Nazara/Utility/Win32/WindowImpl.hpp index caf84d163..48791c68c 100644 --- a/src/Nazara/Utility/Win32/WindowImpl.hpp +++ b/src/Nazara/Utility/Win32/WindowImpl.hpp @@ -24,6 +24,7 @@ class NzMutex; class NzThread; class NzThreadCondition; #endif +class NzWindow; #undef IsMinimized // Conflit avec la méthode du même nom @@ -67,13 +68,14 @@ class NzWindowImpl : NzNonCopyable void ShowMouseCursor(bool show); void StayOnTop(bool stayOnTop); + static bool Initialize(); + static void Uninitialize(); + private: bool HandleMessage(HWND window, UINT message, WPARAM wParam, LPARAM lParam); - static bool Initialize(); static LRESULT CALLBACK MessageHandler(HWND window, UINT message, WPARAM wParam, LPARAM lParam); static NzKeyboard::Key ConvertVirtualKey(WPARAM key, LPARAM flags); - static void Uninitialize(); #if NAZARA_UTILITY_THREADED_WINDOW static void WindowThread(HWND* handle, DWORD styleEx, const wchar_t* title, DWORD style, unsigned int x, unsigned int y, unsigned int width, unsigned int height, NzWindowImpl* window, NzMutex* mutex, NzThreadCondition* condition); #endif diff --git a/src/Nazara/Utility/Window.cpp b/src/Nazara/Utility/Window.cpp index 384549143..b8425bd7e 100644 --- a/src/Nazara/Utility/Window.cpp +++ b/src/Nazara/Utility/Window.cpp @@ -473,3 +473,13 @@ void NzWindow::PushEvent(const NzEvent& event) } #endif } + +bool NzWindow::Initialize() +{ + return NzWindowImpl::Initialize(); +} + +void NzWindow::Uninitialize() +{ + NzWindowImpl::Uninitialize(); +}