Fix WindowHandles

This commit is contained in:
Lynix 2020-05-27 18:55:03 +02:00
parent 65ee3c5c2a
commit 2d189dc85e
11 changed files with 143 additions and 49 deletions

View File

@ -32,13 +32,14 @@ namespace Nz
class NAZARA_PLATFORM_API Window class NAZARA_PLATFORM_API Window
{ {
friend WindowImpl; friend WindowImpl;
friend class EventImpl;
friend class Mouse; friend class Mouse;
friend class Platform; friend class Platform;
public: public:
Window(); Window();
inline Window(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default); inline Window(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default);
inline explicit Window(WindowHandle handle); inline explicit Window(void* handle);
Window(const Window&) = delete; Window(const Window&) = delete;
Window(Window&& window); Window(Window&& window);
virtual ~Window(); virtual ~Window();
@ -46,7 +47,7 @@ namespace Nz
inline void Close(); inline void Close();
bool Create(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default); bool Create(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default);
bool Create(WindowHandle handle); bool Create(void* handle);
void Destroy(); void Destroy();
@ -61,10 +62,10 @@ namespace Nz
inline const CursorRef& GetCursor() const; inline const CursorRef& GetCursor() const;
inline CursorController& GetCursorController(); inline CursorController& GetCursorController();
inline EventHandler& GetEventHandler(); inline EventHandler& GetEventHandler();
WindowHandle GetHandle() const;
Vector2i GetPosition() const; Vector2i GetPosition() const;
Vector2ui GetSize() const; Vector2ui GetSize() const;
WindowStyleFlags GetStyle() const; WindowStyleFlags GetStyle() const;
WindowHandle GetSystemHandle() const;
String GetTitle() const; String GetTitle() const;
bool HasFocus() const; bool HasFocus() const;
@ -106,6 +107,8 @@ namespace Nz
Window& operator=(Window&& window); Window& operator=(Window&& window);
protected: protected:
void* GetHandle();
virtual bool OnWindowCreated(); virtual bool OnWindowCreated();
virtual void OnWindowDestroy(); virtual void OnWindowDestroy();
virtual void OnWindowResized(); virtual void OnWindowResized();
@ -116,6 +119,9 @@ namespace Nz
void ConnectSlots(); void ConnectSlots();
void DisconnectSlots(); void DisconnectSlots();
inline WindowImpl* GetImpl();
inline const WindowImpl* GetImpl() const;
void IgnoreNextMouseEvent(int mouseX, int mouseY) const; void IgnoreNextMouseEvent(int mouseX, int mouseY) const;
void HandleEvent(const WindowEvent& event); void HandleEvent(const WindowEvent& event);

View File

@ -20,7 +20,7 @@ namespace Nz
Create(mode, title, style); Create(mode, title, style);
} }
inline Window::Window(WindowHandle handle) : inline Window::Window(void* handle) :
Window() Window()
{ {
ErrorFlags flags(ErrorFlag_ThrowException, true); ErrorFlags flags(ErrorFlag_ThrowException, true);
@ -111,6 +111,16 @@ namespace Nz
} }
} }
} }
inline WindowImpl* Window::GetImpl()
{
return m_impl;
}
inline const WindowImpl* Window::GetImpl() const
{
return m_impl;
}
} }
#include <Nazara/Platform/DebugOff.hpp> #include <Nazara/Platform/DebugOff.hpp>

View File

@ -8,11 +8,44 @@
#define NAZARA_WINDOWHANDLE_HPP #define NAZARA_WINDOWHANDLE_HPP
#include <Nazara/Prerequisites.hpp> #include <Nazara/Prerequisites.hpp>
#include <Nazara/Utility/Config.hpp>
namespace Nz namespace Nz
{ {
// Real type is SDL_Window enum class WindowManager
using WindowHandle = void*; {
None,
X11,
Wayland,
Windows
};
struct WindowHandle
{
WindowManager type = WindowManager::None;
union
{
struct
{
void* display; //< Display*
void* window; //< Window
} x11;
struct
{
void* display; //< wl_display*
void* surface; //< wl_surface*
void* shellSurface; //< wl_shell_surface*
} wayland;
struct
{
void* window; //< HWND
} windows;
};
};
} }
#endif // NAZARA_WINDOWHANDLE_HPP #endif // NAZARA_WINDOWHANDLE_HPP

View File

@ -122,14 +122,12 @@
#define NAZARA_EXPORT __attribute__((visibility ("default"))) #define NAZARA_EXPORT __attribute__((visibility ("default")))
#define NAZARA_IMPORT __attribute__((visibility ("default"))) #define NAZARA_IMPORT __attribute__((visibility ("default")))
/*#elif defined(__APPLE__) && defined(__MACH__) /*#elif defined(__APPLE__) && defined(__MACH__)
#define NAZARA_CORE_API
#define NAZARA_PLATFORM_MACOSX #define NAZARA_PLATFORM_MACOSX
#define NAZARA_PLATFORM_POSIX*/ #define NAZARA_PLATFORM_POSIX*/
#else #else
#error This operating system is not fully supported by the Nazara Engine #error This operating system is not fully supported by the Nazara Engine
#define NAZARA_PLATFORM_UNKNOWN #define NAZARA_PLATFORM_UNKNOWN
#define NAZARA_CORE_API
#endif #endif
// Detect 64 bits // Detect 64 bits

View File

@ -26,7 +26,7 @@ namespace Nz
minorVersion(defaultMinorVersion), minorVersion(defaultMinorVersion),
stencilBits(parameters.stencilBits), stencilBits(parameters.stencilBits),
shareContext(defaultShareContext), shareContext(defaultShareContext),
window(0), window(nullptr),
compatibilityProfile(defaultCompatibilityProfile), compatibilityProfile(defaultCompatibilityProfile),
debugMode(defaultDebugMode), debugMode(defaultDebugMode),
doubleBuffered(defaultDoubleBuffered), doubleBuffered(defaultDoubleBuffered),
@ -41,7 +41,7 @@ namespace Nz
UInt8 minorVersion; UInt8 minorVersion;
UInt8 stencilBits; UInt8 stencilBits;
const Context* shareContext; const Context* shareContext;
WindowHandle window; void* window;
bool compatibilityProfile; bool compatibilityProfile;
bool debugMode; bool debugMode;
bool doubleBuffered; bool doubleBuffered;

View File

@ -29,7 +29,7 @@ namespace Nz
public: public:
RenderWindow() = default; RenderWindow() = default;
RenderWindow(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default, const ContextParameters& parameters = ContextParameters()); RenderWindow(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default, const ContextParameters& parameters = ContextParameters());
explicit RenderWindow(WindowHandle handle, const ContextParameters& parameters = ContextParameters()); explicit RenderWindow(void* handle, const ContextParameters& parameters = ContextParameters());
RenderWindow(const RenderWindow&) = delete; RenderWindow(const RenderWindow&) = delete;
RenderWindow(RenderWindow&&) = delete; ///TODO RenderWindow(RenderWindow&&) = delete; ///TODO
virtual ~RenderWindow(); virtual ~RenderWindow();
@ -38,7 +38,7 @@ namespace Nz
bool CopyToImage(AbstractImage* image, const Rectui& rect, const Vector3ui& dstPos = Vector3ui(0U)) const; bool CopyToImage(AbstractImage* image, const Rectui& rect, const Vector3ui& dstPos = Vector3ui(0U)) const;
bool Create(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default, const ContextParameters& parameters = ContextParameters()); bool Create(VideoMode mode, const String& title, WindowStyleFlags style = WindowStyle_Default, const ContextParameters& parameters = ContextParameters());
bool Create(WindowHandle handle, const ContextParameters& parameters = ContextParameters()); bool Create(void* handle, const ContextParameters& parameters = ContextParameters());
void Display(); void Display();

View File

@ -6,6 +6,7 @@
#include <Nazara/Platform/Debug.hpp> #include <Nazara/Platform/Debug.hpp>
#include <Nazara/Platform/SDL2/InputImpl.hpp> #include <Nazara/Platform/SDL2/InputImpl.hpp>
#include <Nazara/Platform/SDL2/SDLHelper.hpp> #include <Nazara/Platform/SDL2/SDLHelper.hpp>
#include <Nazara/Platform/SDL2/WindowImpl.hpp>
#include <Nazara/Platform/Window.hpp> #include <Nazara/Platform/Window.hpp>
#include <SDL2/SDL_keyboard.h> #include <SDL2/SDL_keyboard.h>
#include <SDL2/SDL_keycode.h> #include <SDL2/SDL_keycode.h>
@ -45,21 +46,10 @@ namespace Nz
Vector2i EventImpl::GetMousePosition(const Window& relativeTo) Vector2i EventImpl::GetMousePosition(const Window& relativeTo)
{ {
auto handle = relativeTo.GetHandle(); auto windowPos = relativeTo.GetPosition();
if (handle) auto mousePos = GetMousePosition();
{
auto windowPos = relativeTo.GetPosition();
auto mousePos = GetMousePosition();
return mousePos - windowPos; return mousePos - windowPos;
}
else
{
NazaraError("Invalid window handle");
// Attention que (-1, -1) est une position tout à fait valide et ne doit pas servir de test
return Vector2i(-1, -1);
}
} }
bool EventImpl::IsKeyPressed(Keyboard::Scancode key) bool EventImpl::IsKeyPressed(Keyboard::Scancode key)
@ -98,7 +88,7 @@ namespace Nz
void EventImpl::SetMousePosition(int x, int y, const Window& relativeTo) void EventImpl::SetMousePosition(int x, int y, const Window& relativeTo)
{ {
auto handle = static_cast<SDL_Window*>(relativeTo.GetHandle()); SDL_Window* handle = static_cast<const WindowImpl*>(relativeTo.GetImpl())->GetHandle();
if (handle) if (handle)
SDL_WarpMouseInWindow(handle, x, y); SDL_WarpMouseInWindow(handle, x, y);
else else

View File

@ -6,6 +6,7 @@
#include <memory> #include <memory>
#include <Nazara/Core/ConditionVariable.hpp> #include <Nazara/Core/ConditionVariable.hpp>
#include <Nazara/Core/Error.hpp> #include <Nazara/Core/Error.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Core/Mutex.hpp> #include <Nazara/Core/Mutex.hpp>
#include <Nazara/Core/Thread.hpp> #include <Nazara/Core/Thread.hpp>
#include <Nazara/Platform/Config.hpp> #include <Nazara/Platform/Config.hpp>
@ -17,6 +18,7 @@
#include <Nazara/Platform/SDL2/WindowImpl.hpp> #include <Nazara/Platform/SDL2/WindowImpl.hpp>
#include <Nazara/Utility/Image.hpp> #include <Nazara/Utility/Image.hpp>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
namespace Nz namespace Nz
{ {
@ -124,10 +126,9 @@ namespace Nz
return true; return true;
} }
bool WindowImpl::Create(WindowHandle handle) bool WindowImpl::Create(void* handle)
{ {
m_handle = static_cast<SDL_Window*>(handle); m_handle = SDL_CreateWindowFrom(handle);
if (!m_handle || !SDL_GetWindowID(m_handle)) if (!m_handle || !SDL_GetWindowID(m_handle))
{ {
NazaraError("Invalid handle"); NazaraError("Invalid handle");
@ -171,7 +172,7 @@ namespace Nz
m_smoothScrolling = enable; m_smoothScrolling = enable;
} }
WindowHandle WindowImpl::GetHandle() const SDL_Window* WindowImpl::GetHandle() const
{ {
return m_handle; return m_handle;
} }
@ -190,6 +191,56 @@ namespace Nz
{ {
return m_style; return m_style;
} }
WindowHandle WindowImpl::GetSystemHandle() const
{
SDL_SysWMinfo wmInfo;
if (SDL_GetWindowWMInfo(m_handle, &wmInfo) != SDL_TRUE)
{
ErrorFlags flags(ErrorFlag_ThrowException, true);
NazaraError(std::string("failed to retrieve window manager info: ") + SDL_GetError());
}
WindowHandle handle;
switch (wmInfo.subsystem)
{
#if defined(SDL_VIDEO_DRIVER_X11)
case SDL_SYSWM_X11:
{
handle.type = WindowManager::X11;
handle.x11.display = wmInfo.info.x11.display;
handle.x11.window = wmInfo.info.x11.window;
break;
}
#endif
#if defined(SDL_VIDEO_DRIVER_WAYLAND)
case SDL_SYSWM_WAYLAND:
{
handle.type = WindowManager::Wayland;
handle.wayland.display = wmInfo.info.wl.display;
handle.wayland.surface = wmInfo.info.wl.surface;
handle.wayland.shellSurface = wmInfo.info.wl.shell_surface;
break;
}
#endif
#if defined(SDL_VIDEO_DRIVER_WINDOWS)
case SDL_SYSWM_WINDOWS:
{
handle.type = WindowManager::Windows;
handle.windows.window = wmInfo.info.win.window;
break;
}
#endif
default:
{
ErrorFlags flags(ErrorFlag_ThrowException, true);
NazaraError("unhandled window subsystem");
}
}
return handle;
}
String WindowImpl::GetTitle() const String WindowImpl::GetTitle() const
{ {

View File

@ -38,17 +38,18 @@ namespace Nz
~WindowImpl() = default; ~WindowImpl() = default;
bool Create(const VideoMode& mode, const String& title, WindowStyleFlags style); bool Create(const VideoMode& mode, const String& title, WindowStyleFlags style);
bool Create(WindowHandle handle); bool Create(void* handle);
void Destroy(); void Destroy();
void EnableKeyRepeat(bool enable); void EnableKeyRepeat(bool enable);
void EnableSmoothScrolling(bool enable); void EnableSmoothScrolling(bool enable);
WindowHandle GetHandle() const; SDL_Window* GetHandle() const;
Vector2i GetPosition() const; Vector2i GetPosition() const;
Vector2ui GetSize() const; Vector2ui GetSize() const;
WindowStyleFlags GetStyle() const; WindowStyleFlags GetStyle() const;
WindowHandle GetSystemHandle() const;
String GetTitle() const; String GetTitle() const;
bool HasFocus() const; bool HasFocus() const;

View File

@ -124,7 +124,7 @@ namespace Nz
return true; return true;
} }
bool Window::Create(WindowHandle handle) bool Window::Create(void* handle)
{ {
Destroy(); Destroy();
@ -197,19 +197,6 @@ namespace Nz
m_impl->EnableSmoothScrolling(enable); m_impl->EnableSmoothScrolling(enable);
} }
WindowHandle Window::GetHandle() const
{
#if NAZARA_PLATFORM_SAFE
if (!m_impl)
{
NazaraError("Window not created");
return static_cast<WindowHandle>(0);
}
#endif
return m_impl->GetHandle();
}
Vector2i Window::GetPosition() const Vector2i Window::GetPosition() const
{ {
#if NAZARA_PLATFORM_SAFE #if NAZARA_PLATFORM_SAFE
@ -249,6 +236,19 @@ namespace Nz
return m_impl->GetStyle(); return m_impl->GetStyle();
} }
WindowHandle Window::GetSystemHandle() const
{
#if NAZARA_PLATFORM_SAFE
if (!m_impl)
{
NazaraError("Window not created");
return {};
}
#endif
return m_impl->GetSystemHandle();
}
String Window::GetTitle() const String Window::GetTitle() const
{ {
#if NAZARA_PLATFORM_SAFE #if NAZARA_PLATFORM_SAFE
@ -613,6 +613,11 @@ namespace Nz
return *this; return *this;
} }
void* Window::GetHandle()
{
return (m_impl) ? m_impl->GetHandle() : nullptr;
}
bool Window::OnWindowCreated() bool Window::OnWindowCreated()
{ {
return true; return true;

View File

@ -20,7 +20,7 @@ namespace Nz
Create(mode, title, style, parameters); Create(mode, title, style, parameters);
} }
RenderWindow::RenderWindow(WindowHandle handle, const ContextParameters& parameters) : RenderWindow::RenderWindow(void* handle, const ContextParameters& parameters) :
RenderTarget(), Window() RenderTarget(), Window()
{ {
ErrorFlags flags(ErrorFlag_ThrowException, true); ErrorFlags flags(ErrorFlag_ThrowException, true);
@ -125,7 +125,7 @@ namespace Nz
return Window::Create(mode, title, style); return Window::Create(mode, title, style);
} }
bool RenderWindow::Create(WindowHandle handle, const ContextParameters& parameters) bool RenderWindow::Create(void* handle, const ContextParameters& parameters)
{ {
m_parameters = parameters; m_parameters = parameters;
return Window::Create(handle); return Window::Create(handle);