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

View File

@ -20,7 +20,7 @@ namespace Nz
Create(mode, title, style);
}
inline Window::Window(WindowHandle handle) :
inline Window::Window(void* handle) :
Window()
{
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>

View File

@ -8,11 +8,44 @@
#define NAZARA_WINDOWHANDLE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Utility/Config.hpp>
namespace Nz
{
// Real type is SDL_Window
using WindowHandle = void*;
enum class WindowManager
{
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

View File

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

View File

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

View File

@ -29,7 +29,7 @@ namespace Nz
public:
RenderWindow() = default;
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(RenderWindow&&) = delete; ///TODO
virtual ~RenderWindow();
@ -38,7 +38,7 @@ namespace Nz
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(WindowHandle handle, const ContextParameters& parameters = ContextParameters());
bool Create(void* handle, const ContextParameters& parameters = ContextParameters());
void Display();

View File

@ -6,6 +6,7 @@
#include <Nazara/Platform/Debug.hpp>
#include <Nazara/Platform/SDL2/InputImpl.hpp>
#include <Nazara/Platform/SDL2/SDLHelper.hpp>
#include <Nazara/Platform/SDL2/WindowImpl.hpp>
#include <Nazara/Platform/Window.hpp>
#include <SDL2/SDL_keyboard.h>
#include <SDL2/SDL_keycode.h>
@ -45,21 +46,10 @@ namespace Nz
Vector2i EventImpl::GetMousePosition(const Window& relativeTo)
{
auto handle = relativeTo.GetHandle();
if (handle)
{
auto windowPos = relativeTo.GetPosition();
auto mousePos = GetMousePosition();
auto windowPos = relativeTo.GetPosition();
auto mousePos = GetMousePosition();
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);
}
return mousePos - windowPos;
}
bool EventImpl::IsKeyPressed(Keyboard::Scancode key)
@ -98,7 +88,7 @@ namespace Nz
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)
SDL_WarpMouseInWindow(handle, x, y);
else

View File

@ -6,6 +6,7 @@
#include <memory>
#include <Nazara/Core/ConditionVariable.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Core/Mutex.hpp>
#include <Nazara/Core/Thread.hpp>
#include <Nazara/Platform/Config.hpp>
@ -17,6 +18,7 @@
#include <Nazara/Platform/SDL2/WindowImpl.hpp>
#include <Nazara/Utility/Image.hpp>
#include <SDL2/SDL.h>
#include <SDL2/SDL_syswm.h>
namespace Nz
{
@ -124,10 +126,9 @@ namespace Nz
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))
{
NazaraError("Invalid handle");
@ -171,7 +172,7 @@ namespace Nz
m_smoothScrolling = enable;
}
WindowHandle WindowImpl::GetHandle() const
SDL_Window* WindowImpl::GetHandle() const
{
return m_handle;
}
@ -190,6 +191,56 @@ namespace Nz
{
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
{

View File

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

View File

@ -124,7 +124,7 @@ namespace Nz
return true;
}
bool Window::Create(WindowHandle handle)
bool Window::Create(void* handle)
{
Destroy();
@ -197,19 +197,6 @@ namespace Nz
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
{
#if NAZARA_PLATFORM_SAFE
@ -249,6 +236,19 @@ namespace Nz
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
{
#if NAZARA_PLATFORM_SAFE
@ -613,6 +613,11 @@ namespace Nz
return *this;
}
void* Window::GetHandle()
{
return (m_impl) ? m_impl->GetHandle() : nullptr;
}
bool Window::OnWindowCreated()
{
return true;

View File

@ -20,7 +20,7 @@ namespace Nz
Create(mode, title, style, parameters);
}
RenderWindow::RenderWindow(WindowHandle handle, const ContextParameters& parameters) :
RenderWindow::RenderWindow(void* handle, const ContextParameters& parameters) :
RenderTarget(), Window()
{
ErrorFlags flags(ErrorFlag_ThrowException, true);
@ -125,7 +125,7 @@ namespace Nz
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;
return Window::Create(handle);