diff --git a/include/Nazara/Platform/Cursor.hpp b/include/Nazara/Platform/Cursor.hpp index 52438123f..bf800dcd9 100644 --- a/include/Nazara/Platform/Cursor.hpp +++ b/include/Nazara/Platform/Cursor.hpp @@ -8,7 +8,6 @@ #define NAZARA_CURSOR_HPP #include -#include #include #include #include @@ -19,51 +18,42 @@ namespace Nz { class CursorImpl; - class Cursor; - - using CursorConstRef = ObjectRef; - using CursorRef = ObjectRef; - - class NAZARA_PLATFORM_API Cursor : public RefCounted + class NAZARA_PLATFORM_API Cursor { friend class Platform; friend class WindowImpl; public: - inline Cursor(); - inline Cursor(const Image& cursor, const Vector2i& hotSpot, SystemCursor placeholder); + Cursor(); + Cursor(const Image& cursor, const Vector2i& hotSpot, SystemCursor placeholder); Cursor(const Cursor&) = delete; - Cursor(Cursor&&) = delete; - inline ~Cursor(); + Cursor(Cursor&&) noexcept; + ~Cursor(); bool Create(const Image& cursor, const Vector2i& hotSpot, SystemCursor placeholder); - void Destroy(); - inline const Image& GetImage() const; inline SystemCursor GetSystemCursor() const; inline bool IsValid() const; Cursor& operator=(const Cursor&) = delete; - Cursor& operator=(Cursor&&) = delete; + Cursor& operator=(Cursor&&) noexcept; - static inline Cursor* Get(SystemCursor cursor); - template static CursorRef New(Args&&... args); + static inline std::shared_ptr& Get(SystemCursor cursor); private: - inline explicit Cursor(SystemCursor systemCursor); + explicit Cursor(SystemCursor systemCursor); bool Create(SystemCursor cursor); static bool Initialize(); static void Uninitialize(); - Image m_cursorImage; SystemCursor m_systemCursor; - CursorImpl* m_impl; + std::unique_ptr m_impl; - static std::array s_systemCursors; + static std::array, SystemCursorCount> s_systemCursors; }; } diff --git a/include/Nazara/Platform/Cursor.inl b/include/Nazara/Platform/Cursor.inl index 12aceb908..26bdf9070 100644 --- a/include/Nazara/Platform/Cursor.inl +++ b/include/Nazara/Platform/Cursor.inl @@ -2,47 +2,11 @@ // This file is part of the "Nazara Engine - Platform module" // For conditions of distribution and use, see copyright notice in Config.hpp -#include +#include #include namespace Nz { - inline Cursor::Cursor() : - m_impl(nullptr) - { - } - - inline Cursor::Cursor(const Image& cursor, const Vector2i& hotSpot, SystemCursor placeholder) - { - ErrorFlags flags(ErrorFlag_ThrowException, true); - Create(cursor, hotSpot, placeholder); - } - - inline Cursor* Cursor::Get(SystemCursor cursor) - { - return &s_systemCursors[cursor]; - } - - inline Cursor::Cursor(SystemCursor systemCursor) : - Cursor() - { - ErrorFlags flags(ErrorFlag_ThrowException, true); - Create(systemCursor); - } - - inline Cursor::~Cursor() - { - Destroy(); - } - - inline const Image& Cursor::GetImage() const - { - NazaraAssert(IsValid(), "Invalid cursor"); - NazaraAssert(m_cursorImage.IsValid(), "System cursors have no image"); - - return m_cursorImage; - } - inline SystemCursor Cursor::GetSystemCursor() const { NazaraAssert(IsValid(), "Invalid cursor"); @@ -55,13 +19,9 @@ namespace Nz return m_impl != nullptr; } - template - CursorRef Cursor::New(Args&&... args) + inline std::shared_ptr& Cursor::Get(SystemCursor cursor) { - std::unique_ptr object(new Cursor(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); + return s_systemCursors[UnderlyingCast(cursor)]; } } diff --git a/include/Nazara/Platform/CursorController.hpp b/include/Nazara/Platform/CursorController.hpp index 2fc68d623..efb519e2d 100644 --- a/include/Nazara/Platform/CursorController.hpp +++ b/include/Nazara/Platform/CursorController.hpp @@ -28,12 +28,12 @@ namespace Nz CursorController(CursorController&&) noexcept = default; ~CursorController() = default; - inline void UpdateCursor(const CursorRef& cursor); + inline void UpdateCursor(const std::shared_ptr& cursor); CursorController& operator=(const CursorController&) = delete; CursorController& operator=(CursorController&&) noexcept = default; - NazaraSignal(OnCursorUpdated, const CursorController* /*cursorController*/, const CursorRef& /*cursor*/); + NazaraSignal(OnCursorUpdated, const CursorController* /*cursorController*/, const std::shared_ptr& /*cursor*/); }; } diff --git a/include/Nazara/Platform/CursorController.inl b/include/Nazara/Platform/CursorController.inl index 45b9393e1..e9cde185b 100644 --- a/include/Nazara/Platform/CursorController.inl +++ b/include/Nazara/Platform/CursorController.inl @@ -7,7 +7,7 @@ namespace Nz { - inline void CursorController::UpdateCursor(const CursorRef& cursor) + inline void CursorController::UpdateCursor(const std::shared_ptr& cursor) { OnCursorUpdated(this, cursor); } diff --git a/include/Nazara/Platform/Enums.hpp b/include/Nazara/Platform/Enums.hpp index 1f8390ae6..f874a0947 100644 --- a/include/Nazara/Platform/Enums.hpp +++ b/include/Nazara/Platform/Enums.hpp @@ -11,75 +11,81 @@ namespace Nz { - enum SystemCursor + enum class SystemCursor { - SystemCursor_Crosshair, - SystemCursor_Default, - SystemCursor_Hand, - SystemCursor_Help, - SystemCursor_Move, - SystemCursor_None, - SystemCursor_Pointer, - SystemCursor_Progress, - SystemCursor_ResizeE, - SystemCursor_ResizeN, - SystemCursor_ResizeNE, - SystemCursor_ResizeNW, - SystemCursor_ResizeS, - SystemCursor_ResizeSE, - SystemCursor_ResizeSW, - SystemCursor_ResizeW, - SystemCursor_Text, - SystemCursor_Wait, + Crosshair, + Default, + Hand, + Help, + Move, + None, + Pointer, + Progress, + ResizeE, + ResizeN, + ResizeNE, + ResizeNW, + ResizeS, + ResizeSE, + ResizeSW, + ResizeW, + Text, + Wait, - SystemCursor_Max = SystemCursor_Wait + Max = Wait }; - enum WindowEventType - { - WindowEventType_GainedFocus, - WindowEventType_LostFocus, - WindowEventType_KeyPressed, - WindowEventType_KeyReleased, - WindowEventType_MouseButtonDoubleClicked, - WindowEventType_MouseButtonPressed, - WindowEventType_MouseButtonReleased, - WindowEventType_MouseEntered, - WindowEventType_MouseLeft, - WindowEventType_MouseMoved, - WindowEventType_MouseWheelMoved, - WindowEventType_Moved, - WindowEventType_Quit, - WindowEventType_Resized, - WindowEventType_TextEdited, - WindowEventType_TextEntered, + constexpr std::size_t SystemCursorCount = static_cast(SystemCursor::Max) + 1; - WindowEventType_Max = WindowEventType_TextEntered + enum class WindowEventType + { + GainedFocus, + LostFocus, + KeyPressed, + KeyReleased, + MouseButtonDoubleClicked, + MouseButtonPressed, + MouseButtonReleased, + MouseEntered, + MouseLeft, + MouseMoved, + MouseWheelMoved, + Moved, + Quit, + Resized, + TextEdited, + TextEntered, + + Max = TextEntered }; - enum WindowStyle + constexpr std::size_t WindowEventTypeCount = static_cast(WindowEventType::Max) + 1; + + enum class WindowStyle { - WindowStyle_None, ///< Window has no border nor titlebar. - WindowStyle_Fullscreen, ///< At the window creation, the OS tries to set it in fullscreen. + None, ///< Window has no border nor titlebar. + Fullscreen, ///< At the window creation, the OS tries to set it in fullscreen. - WindowStyle_Closable, ///< Allows the window to be closed by a button in the titlebar, generating a Quit event. - WindowStyle_Resizable, ///< Allows the window to be resized by dragging its corners or by a button of the titlebar. - WindowStyle_Titlebar, ///< Adds a titlebar to the window, this option is automatically enabled if buttons of the titlebar are enabled. + Closable, ///< Allows the window to be closed by a button in the titlebar, generating a Quit event. + Resizable, ///< Allows the window to be resized by dragging its corners or by a button of the titlebar. + Titlebar, ///< Adds a titlebar to the window, this option is automatically enabled if buttons of the titlebar are enabled. - WindowStyle_Threaded, ///< Runs the window into a thread, allowing the application to keep updating while resizing/dragging the window. + Threaded, ///< Runs the window into a thread, allowing the application to keep updating while resizing/dragging the window. - WindowStyle_Max = WindowStyle_Threaded + Max = Threaded }; + constexpr std::size_t WindowStyleCount = static_cast(WindowStyle::Max) + 1; + template<> struct EnumAsFlags { - static constexpr WindowStyle max = WindowStyle_Max; + static constexpr WindowStyle max = WindowStyle::Max; }; using WindowStyleFlags = Flags; - constexpr WindowStyleFlags WindowStyle_Default = WindowStyle_Closable | WindowStyle_Resizable | WindowStyle_Titlebar; + constexpr WindowStyleFlags WindowStyle_Default = WindowStyle::Closable | WindowStyle::Resizable | WindowStyle::Titlebar; } #endif // NAZARA_ENUMS_PLATFORM_HPP diff --git a/include/Nazara/Platform/Event.hpp b/include/Nazara/Platform/Event.hpp index cfa73efd9..625905a28 100644 --- a/include/Nazara/Platform/Event.hpp +++ b/include/Nazara/Platform/Event.hpp @@ -20,8 +20,8 @@ namespace Nz struct WindowEvent { // Used by: - // -WindowEventType_KeyPressed - // -WindowEventType_KeyReleased + // -WindowEventType::KeyPressed + // -WindowEventType::KeyReleased struct KeyEvent { Keyboard::Scancode scancode; @@ -34,8 +34,8 @@ namespace Nz }; // Used by: - // -WindowEventType_MouseButtonDoubleClicked - // -WindowEventType_MouseButtonPressed + // -WindowEventType::MouseButtonDoubleClicked + // -WindowEventType::MouseButtonPressed struct MouseButtonEvent { Mouse::Button button; @@ -44,7 +44,7 @@ namespace Nz }; // Used by: - // -WindowEventType_MouseMoved + // -WindowEventType::MouseMoved struct MouseMoveEvent { int deltaX; @@ -54,7 +54,7 @@ namespace Nz }; // Used by: - // -WindowEventType_MouseWheelMoved + // -WindowEventType::MouseWheelMoved struct MouseWheelEvent { float delta; @@ -63,7 +63,7 @@ namespace Nz }; // Used by: - // -WindowEventType_Moved + // -WindowEventType::Moved struct PositionEvent { int x; @@ -71,7 +71,7 @@ namespace Nz }; // Used by: - // -WindowEventType_Resized + // -WindowEventType::Resized struct SizeEvent { unsigned int height; @@ -79,7 +79,7 @@ namespace Nz }; // Used by: - // -WindowEventType_TextEntered + // -WindowEventType::TextEntered struct TextEvent { bool repeated; @@ -87,7 +87,7 @@ namespace Nz }; // Used by: - // -WindowEventType_TextEdited + // -WindowEventType::TextEdited struct EditEvent { int length; @@ -99,37 +99,37 @@ namespace Nz union { // Used by: - // -WindowEventType_KeyPressed - // -WindowEventType_KeyReleased + // -WindowEventType::KeyPressed + // -WindowEventType::KeyReleased KeyEvent key; // Used by: - // -WindowEventType_MouseButtonDoubleClicked - // -WindowEventType_MouseButtonPressed + // -WindowEventType::MouseButtonDoubleClicked + // -WindowEventType::MouseButtonPressed MouseButtonEvent mouseButton; // Used by: - // -WindowEventType_MouseMoved + // -WindowEventType::MouseMoved MouseMoveEvent mouseMove; // Used by: - // -WindowEventType_MouseWheelMoved + // -WindowEventType::MouseWheelMoved MouseWheelEvent mouseWheel; // Used by: - // -WindowEventType_Moved + // -WindowEventType::Moved PositionEvent position; // Used by: - // -WindowEventType_Resized + // -WindowEventType::Resized SizeEvent size; // Used by: - // -WindowEventType_TextEntered + // -WindowEventType::TextEntered TextEvent text; // Used by: - // -WindowEventType_TextEntered + // -WindowEventType::TextEntered EditEvent edit; }; }; diff --git a/include/Nazara/Platform/EventHandler.inl b/include/Nazara/Platform/EventHandler.inl index 7b083285f..f062891e0 100644 --- a/include/Nazara/Platform/EventHandler.inl +++ b/include/Nazara/Platform/EventHandler.inl @@ -19,67 +19,67 @@ namespace Nz switch (event.type) { - case WindowEventType_GainedFocus: + case WindowEventType::GainedFocus: OnGainedFocus(this); break; - case WindowEventType_KeyPressed: + case WindowEventType::KeyPressed: OnKeyPressed(this, event.key); break; - case WindowEventType_KeyReleased: + case WindowEventType::KeyReleased: OnKeyReleased(this, event.key); break; - case WindowEventType_LostFocus: + case WindowEventType::LostFocus: OnLostFocus(this); break; - case WindowEventType_MouseButtonDoubleClicked: + case WindowEventType::MouseButtonDoubleClicked: OnMouseButtonDoubleClicked(this, event.mouseButton); break; - case WindowEventType_MouseButtonPressed: + case WindowEventType::MouseButtonPressed: OnMouseButtonPressed(this, event.mouseButton); break; - case WindowEventType_MouseButtonReleased: + case WindowEventType::MouseButtonReleased: OnMouseButtonReleased(this, event.mouseButton); break; - case WindowEventType_MouseEntered: + case WindowEventType::MouseEntered: OnMouseEntered(this); break; - case WindowEventType_MouseLeft: + case WindowEventType::MouseLeft: OnMouseLeft(this); break; - case WindowEventType_MouseMoved: + case WindowEventType::MouseMoved: OnMouseMoved(this, event.mouseMove); break; - case WindowEventType_MouseWheelMoved: + case WindowEventType::MouseWheelMoved: OnMouseWheelMoved(this, event.mouseWheel); break; - case WindowEventType_Moved: + case WindowEventType::Moved: OnMoved(this, event.position); break; - case WindowEventType_Quit: + case WindowEventType::Quit: OnQuit(this); break; - case WindowEventType_Resized: + case WindowEventType::Resized: OnResized(this, event.size); break; - case WindowEventType_TextEntered: + case WindowEventType::TextEntered: OnTextEntered(this, event.text); break; - case WindowEventType_TextEdited: + case WindowEventType::TextEdited: OnTextEdited(this, event.edit); break; } diff --git a/include/Nazara/Platform/Icon.hpp b/include/Nazara/Platform/Icon.hpp index 080a42171..9e5258ed3 100644 --- a/include/Nazara/Platform/Icon.hpp +++ b/include/Nazara/Platform/Icon.hpp @@ -8,36 +8,35 @@ #define NAZARA_ICON_HPP #include -#include #include +#include namespace Nz { class Image; class IconImpl; - class Icon; - - using IconRef = ObjectRef; - - class NAZARA_PLATFORM_API Icon : public RefCounted + class NAZARA_PLATFORM_API Icon { friend class WindowImpl; public: - inline Icon(); - inline explicit Icon(const Image& icon); - inline ~Icon(); + Icon(); + explicit Icon(const Image& icon); + Icon(const Icon&) = delete; + Icon(Icon&&) noexcept; + ~Icon(); bool Create(const Image& icon); void Destroy(); inline bool IsValid() const; - template static IconRef New(Args&&... args); + Icon& operator=(const Icon&) = delete; + Icon& operator=(Icon&&) noexcept; private: - IconImpl* m_impl; + std::unique_ptr m_impl; }; } diff --git a/include/Nazara/Platform/Icon.inl b/include/Nazara/Platform/Icon.inl index 9dc84932c..53436de52 100644 --- a/include/Nazara/Platform/Icon.inl +++ b/include/Nazara/Platform/Icon.inl @@ -7,35 +7,10 @@ namespace Nz { - Icon::Icon() : - m_impl(nullptr) - { - } - - inline Icon::Icon(const Image& icon) - { - ErrorFlags flags(ErrorFlag_ThrowException, true); - Create(icon); - } - - Icon::~Icon() - { - Destroy(); - } - bool Icon::IsValid() const { return m_impl != nullptr; } - - template - IconRef Icon::New(Args&&... args) - { - std::unique_ptr object(new Icon(std::forward(args)...)); - object->SetPersistent(false); - - return object.release(); - } } #include diff --git a/include/Nazara/Platform/Window.hpp b/include/Nazara/Platform/Window.hpp index eebbabe0c..99c9c288d 100644 --- a/include/Nazara/Platform/Window.hpp +++ b/include/Nazara/Platform/Window.hpp @@ -58,7 +58,7 @@ namespace Nz void EnableKeyRepeat(bool enable); void EnableSmoothScrolling(bool enable); - inline const CursorRef& GetCursor() const; + inline const std::shared_ptr& GetCursor() const; inline CursorController& GetCursorController(); inline EventHandler& GetEventHandler(); Vector2i GetPosition() const; @@ -82,11 +82,11 @@ namespace Nz void ProcessEvents(bool block = false); - void SetCursor(CursorRef cursor); + void SetCursor(std::shared_ptr cursor); inline void SetCursor(SystemCursor systemCursor); void SetEventListener(bool listener); void SetFocus(); - void SetIcon(IconRef icon); + void SetIcon(std::shared_ptr icon); void SetMaximumSize(const Vector2i& maxSize); void SetMaximumSize(int width, int height); void SetMinimumSize(const Vector2i& minSize); @@ -133,9 +133,9 @@ namespace Nz std::vector m_pendingEvents; std::condition_variable m_eventCondition; CursorController m_cursorController; - CursorRef m_cursor; + std::shared_ptr m_cursor; EventHandler m_eventHandler; - IconRef m_icon; + std::shared_ptr m_icon; std::mutex m_eventMutex; std::mutex m_eventConditionMutex; bool m_asyncWindow; diff --git a/include/Nazara/Platform/Window.inl b/include/Nazara/Platform/Window.inl index d6ab61579..4668313e8 100644 --- a/include/Nazara/Platform/Window.inl +++ b/include/Nazara/Platform/Window.inl @@ -46,7 +46,7 @@ namespace Nz } } - inline const CursorRef& Window::GetCursor() const + inline const std::shared_ptr& Window::GetCursor() const { return m_cursor; } diff --git a/src/Nazara/Platform/Cursor.cpp b/src/Nazara/Platform/Cursor.cpp index 95bb6f755..da52a62d9 100644 --- a/src/Nazara/Platform/Cursor.cpp +++ b/src/Nazara/Platform/Cursor.cpp @@ -3,24 +3,43 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include namespace Nz { + Cursor::Cursor() = default; + + Cursor::Cursor(const Image& cursor, const Vector2i& hotSpot, SystemCursor placeholder) + { + ErrorFlags flags(ErrorFlag_ThrowException, true); + Create(cursor, hotSpot, placeholder); + } + + Cursor::Cursor(SystemCursor systemCursor) + { + ErrorFlags flags(ErrorFlag_ThrowException, true); + Create(systemCursor); + } + + Cursor::Cursor(Cursor&&) noexcept = default; + Cursor::~Cursor() = default; + bool Cursor::Create(const Image& cursor, const Vector2i& hotSpot, SystemCursor placeholder) { Destroy(); - std::unique_ptr impl(new CursorImpl); - if (!impl->Create(cursor, hotSpot.x, hotSpot.y)) + try { - NazaraError("Failed to create cursor implementation"); + m_impl = std::make_unique(cursor, hotSpot); + } + catch (const std::exception& e) + { + NazaraError(e.what()); return false; } - m_cursorImage = cursor; - m_impl = impl.release(); m_systemCursor = placeholder; return true; @@ -28,52 +47,46 @@ namespace Nz void Cursor::Destroy() { - m_cursorImage.Destroy(); - - if (m_impl) - { - m_impl->Destroy(); - - delete m_impl; - m_impl = nullptr; - } + m_impl.reset(); } bool Cursor::Create(SystemCursor cursor) { Destroy(); - std::unique_ptr impl(new CursorImpl); - if (!impl->Create(cursor)) + try { - NazaraError("Failed to create cursor implementation"); + m_impl = std::make_unique(cursor); + } + catch (const std::exception& e) + { + NazaraError(e.what()); return false; } - m_impl = impl.release(); m_systemCursor = cursor; return true; } + Cursor& Cursor::operator=(Cursor&&) noexcept = default; + bool Cursor::Initialize() { - if (!CursorImpl::Initialize()) - return false; - - for (std::size_t i = 0; i <= SystemCursor_Max; ++i) - s_systemCursors[i].Create(static_cast(i)); + for (std::size_t i = 0; i < SystemCursorCount; ++i) + { + s_systemCursors[i] = std::make_shared(); + s_systemCursors[i]->Create(static_cast(i)); + } return true; } void Cursor::Uninitialize() { - for (Cursor& cursor : s_systemCursors) - cursor.Destroy(); - - CursorImpl::Uninitialize(); + for (std::shared_ptr& cursor : s_systemCursors) + cursor.reset(); } - std::array Cursor::s_systemCursors; + std::array, SystemCursorCount> Cursor::s_systemCursors; } diff --git a/src/Nazara/Platform/Icon.cpp b/src/Nazara/Platform/Icon.cpp index bc2cb5820..72cf081f3 100644 --- a/src/Nazara/Platform/Icon.cpp +++ b/src/Nazara/Platform/Icon.cpp @@ -8,30 +8,38 @@ namespace Nz { + Icon::Icon() = default; + + Icon::Icon(const Image& icon) + { + ErrorFlags flags(ErrorFlag_ThrowException, true); + Create(icon); + } + + Icon::Icon(Icon&&) noexcept = default; + Icon::~Icon() = default; + bool Icon::Create(const Image& icon) { Destroy(); - std::unique_ptr impl(new IconImpl); - if (!impl->Create(icon)) + try { - NazaraError("Failed to create icon implementation"); + m_impl = std::make_unique(icon); + } + catch (const std::exception& e) + { + NazaraError(e.what()); return false; } - m_impl = impl.release(); - return true; } void Icon::Destroy() { - if (m_impl) - { - m_impl->Destroy(); - - delete m_impl; - m_impl = nullptr; - } + m_impl.reset(); } + + Icon& Icon::operator=(Icon&&) noexcept = default; } diff --git a/src/Nazara/Platform/SDL2/CursorImpl.cpp b/src/Nazara/Platform/SDL2/CursorImpl.cpp index 306f8a23d..a6b07f7c4 100644 --- a/src/Nazara/Platform/SDL2/CursorImpl.cpp +++ b/src/Nazara/Platform/SDL2/CursorImpl.cpp @@ -2,67 +2,87 @@ // This file is part of the "Nazara Engine - Platform module" // For conditions of distribution and use, see copyright notice in Config.hpp -#include -#include #include -#include +#include #include +#include +#include namespace Nz { - bool CursorImpl::Create(const Image& cursor, int hotSpotX, int hotSpotY) + namespace { - m_iconImage = cursor; - if (!m_iconImage.Convert(PixelFormat_BGRA8)) + std::array s_systemCursorIds = { - NazaraError("Failed to convert icon to BGRA8"); - return false; - } + SDL_SYSTEM_CURSOR_CROSSHAIR, // SystemCursor::Crosshair + SDL_SYSTEM_CURSOR_ARROW, // SystemCursor::Default + SDL_SYSTEM_CURSOR_HAND, // SystemCursor::Hand + SDL_SYSTEM_CURSOR_ARROW, // SystemCursor::Help + SDL_SYSTEM_CURSOR_SIZEALL, // SystemCursor::Move + SDL_NUM_SYSTEM_CURSORS, // SystemCursor::None + SDL_SYSTEM_CURSOR_HAND, // SystemCursor::Pointer + SDL_SYSTEM_CURSOR_WAITARROW, // SystemCursor::Progress + SDL_SYSTEM_CURSOR_SIZEWE, // SystemCursor::ResizeE + SDL_SYSTEM_CURSOR_SIZENS, // SystemCursor::ResizeN + SDL_SYSTEM_CURSOR_SIZENESW, // SystemCursor::ResizeNE + SDL_SYSTEM_CURSOR_SIZENWSE, // SystemCursor::ResizeNW + SDL_SYSTEM_CURSOR_SIZENS, // SystemCursor::ResizeS + SDL_SYSTEM_CURSOR_SIZENWSE, // SystemCursor::ResizeSE + SDL_SYSTEM_CURSOR_SIZENESW, // SystemCursor::ResizeSW + SDL_SYSTEM_CURSOR_SIZEWE, // SystemCursor::ResizeW + SDL_SYSTEM_CURSOR_IBEAM, // SystemCursor::Text + SDL_SYSTEM_CURSOR_WAIT // SystemCursor::Wait + }; - m_icon = SDL_CreateRGBSurfaceWithFormatFrom( - m_iconImage.GetPixels(), - m_iconImage.GetWidth(), - m_iconImage.GetHeight(), + static_assert(SystemCursorCount == 18, "System cursor array is incomplete"); + } + + CursorImpl::CursorImpl(const Image& cursor, const Vector2i& hotSpot) + { + ErrorFlags errFlags(ErrorFlag_ThrowException); + + m_cursorImage = cursor; + if (!m_cursorImage.Convert(PixelFormat::BGRA8)) + NazaraError("Failed to convert icon to BGRA8"); + + m_surface = SDL_CreateRGBSurfaceWithFormatFrom( + m_cursorImage.GetPixels(), + m_cursorImage.GetWidth(), + m_cursorImage.GetHeight(), 32, - 4 * m_iconImage.GetWidth(), + 4 * m_cursorImage.GetWidth(), SDL_PIXELFORMAT_BGRA32 ); - if (!m_icon) - { - NazaraError(SDL_GetError()); - - return false; - } - - m_cursor = SDL_CreateColorCursor(m_icon, hotSpotX, hotSpotY); + if (!m_surface) + NazaraError("failed to create SDL Surface for cursor: " + std::string(SDL_GetError())); + m_cursor = SDL_CreateColorCursor(m_surface, hotSpot.x, hotSpot.y); if (!m_cursor) { - NazaraError(SDL_GetError()); + if (m_surface) //< Just in case exceptions were disabled + SDL_FreeSurface(m_surface); - return false; + NazaraError("failed to create SDL cursor: " + std::string(SDL_GetError())); } - - return true; } - bool CursorImpl::Create(SystemCursor cursor) + CursorImpl::CursorImpl(SystemCursor cursor) { - if (cursor != SystemCursor_None) - m_cursor = SDL_CreateSystemCursor(s_systemCursorIds[cursor]); - else - m_cursor = nullptr; + ErrorFlags errFlags(ErrorFlag_ThrowException); - m_icon = nullptr; - - return true; + if (cursor != SystemCursor::None) + { + m_cursor = SDL_CreateSystemCursor(s_systemCursorIds[UnderlyingCast(cursor)]); + if (!m_cursor) + NazaraError("failed to create SDL cursor: " + std::string(SDL_GetError())); + } } - void CursorImpl::Destroy() + CursorImpl::~CursorImpl() { - if (m_icon) - SDL_FreeSurface(m_icon); + if (m_surface) + SDL_FreeSurface(m_surface); if (m_cursor) SDL_FreeCursor(m_cursor); @@ -72,37 +92,4 @@ namespace Nz { return m_cursor; } - - bool CursorImpl::Initialize() - { - return true; - } - - void CursorImpl::Uninitialize() - { - } - - std::array CursorImpl::s_systemCursorIds = - { - SDL_SYSTEM_CURSOR_CROSSHAIR, // SystemCursor_Crosshair - SDL_SYSTEM_CURSOR_ARROW, // SystemCursor_Default - SDL_SYSTEM_CURSOR_HAND, // SystemCursor_Hand - SDL_SYSTEM_CURSOR_ARROW, // SystemCursor_Help - SDL_SYSTEM_CURSOR_SIZEALL, // SystemCursor_Move - SDL_NUM_SYSTEM_CURSORS, // SystemCursor_None - SDL_SYSTEM_CURSOR_HAND, // SystemCursor_Pointer - SDL_SYSTEM_CURSOR_WAITARROW, // SystemCursor_Progress - SDL_SYSTEM_CURSOR_SIZEWE, // SystemCursor_ResizeE - SDL_SYSTEM_CURSOR_SIZENS, // SystemCursor_ResizeN - SDL_SYSTEM_CURSOR_SIZENESW, // SystemCursor_ResizeNE - SDL_SYSTEM_CURSOR_SIZENWSE, // SystemCursor_ResizeNW - SDL_SYSTEM_CURSOR_SIZENS, // SystemCursor_ResizeS - SDL_SYSTEM_CURSOR_SIZENWSE, // SystemCursor_ResizeSE - SDL_SYSTEM_CURSOR_SIZENESW, // SystemCursor_ResizeSW - SDL_SYSTEM_CURSOR_SIZEWE, // SystemCursor_ResizeW - SDL_SYSTEM_CURSOR_IBEAM, // SystemCursor_Text - SDL_SYSTEM_CURSOR_WAIT // SystemCursor_Wait - }; - - static_assert(SystemCursor_Max + 1 == 18, "System cursor array is incomplete"); } diff --git a/src/Nazara/Platform/SDL2/CursorImpl.hpp b/src/Nazara/Platform/SDL2/CursorImpl.hpp index 6e637055a..c93ae17ae 100644 --- a/src/Nazara/Platform/SDL2/CursorImpl.hpp +++ b/src/Nazara/Platform/SDL2/CursorImpl.hpp @@ -7,38 +7,33 @@ #ifndef NAZARA_CURSORIMPL_HPP #define NAZARA_CURSORIMPL_HPP -#include -#include #include +#include +#include +#include #include - #include namespace Nz { - class Image; - class CursorImpl { - friend class Cursor; - public: - bool Create(const Image& image, int hotSpotX, int hotSpotY); - bool Create(SystemCursor cursor); - - void Destroy(); + CursorImpl(const Image& image, const Vector2i& hotSpot); + CursorImpl(SystemCursor cursor); + CursorImpl(const CursorImpl&) = delete; + CursorImpl(CursorImpl&&) noexcept = default; + ~CursorImpl(); SDL_Cursor* GetCursor(); + CursorImpl& operator=(const CursorImpl&) = delete; + CursorImpl& operator=(CursorImpl&&) noexcept = default; + private: - static bool Initialize(); - static void Uninitialize(); - - SDL_Cursor* m_cursor = nullptr; - SDL_Surface* m_icon = nullptr; - Image m_iconImage; - - static std::array s_systemCursorIds; + MovablePtr m_cursor; + MovablePtr m_surface; + Image m_cursorImage; }; } diff --git a/src/Nazara/Platform/SDL2/IconImpl.cpp b/src/Nazara/Platform/SDL2/IconImpl.cpp index 1ef8c4fd8..290276de8 100644 --- a/src/Nazara/Platform/SDL2/IconImpl.cpp +++ b/src/Nazara/Platform/SDL2/IconImpl.cpp @@ -3,20 +3,20 @@ // For conditions of distribution and use, see copyright notice in Config.hpp #include +#include #include #include #include namespace Nz { - bool IconImpl::Create(const Image& icon) + IconImpl::IconImpl(const Image& icon) { + ErrorFlags errFlags(ErrorFlag_ThrowException); + m_iconImage = icon; - if (!m_iconImage.Convert(PixelFormat_BGRA8)) - { + if (!m_iconImage.Convert(PixelFormat::BGRA8)) NazaraError("Failed to convert icon to BGRA8"); - return false; - } m_icon = SDL_CreateRGBSurfaceWithFormatFrom( m_iconImage.GetPixels(), @@ -25,21 +25,16 @@ namespace Nz 32, 32 * m_iconImage.GetWidth(), SDL_PIXELFORMAT_BGRA8888 - ); + ); if (!m_icon) - { - NazaraError(SDL_GetError()); - return false; - } - - return true; + NazaraError("failed to create SDL Surface for icon: " + std::string(SDL_GetError())); } - void IconImpl::Destroy() + IconImpl::~IconImpl() { - SDL_FreeSurface(m_icon); - m_iconImage.Destroy(); + if (m_icon) + SDL_FreeSurface(m_icon); } SDL_Surface* IconImpl::GetIcon() diff --git a/src/Nazara/Platform/SDL2/IconImpl.hpp b/src/Nazara/Platform/SDL2/IconImpl.hpp index 35389f950..600ba357a 100644 --- a/src/Nazara/Platform/SDL2/IconImpl.hpp +++ b/src/Nazara/Platform/SDL2/IconImpl.hpp @@ -8,25 +8,28 @@ #define NAZARA_ICONIMPL_HPP #include +#include #include #include namespace Nz { - class Image; - class IconImpl { public: - bool Create(const Image& image); - void Destroy(); + IconImpl(const Image& image); + IconImpl(const IconImpl&) = delete; + IconImpl(IconImpl&&) noexcept = default; + ~IconImpl(); SDL_Surface* GetIcon(); - private: + IconImpl& operator=(const IconImpl&) = default; + IconImpl& operator=(IconImpl&&) noexcept = default; - SDL_Surface* m_icon = nullptr; + private: Image m_iconImage; + MovablePtr m_icon; }; } diff --git a/src/Nazara/Platform/SDL2/WindowImpl.cpp b/src/Nazara/Platform/SDL2/WindowImpl.cpp index 74cb9b162..080405bd8 100644 --- a/src/Nazara/Platform/SDL2/WindowImpl.cpp +++ b/src/Nazara/Platform/SDL2/WindowImpl.cpp @@ -65,7 +65,7 @@ namespace Nz bool WindowImpl::Create(const VideoMode& mode, const std::string& title, WindowStyleFlags style) { - bool async = (style & WindowStyle_Threaded) != 0; + bool async = (style & WindowStyle::Threaded) != 0; if (async) { NazaraError("SDL2 backend doesn't support asyn window for now"); @@ -74,7 +74,7 @@ namespace Nz } - bool fullscreen = (style & WindowStyle_Fullscreen) != 0; + bool fullscreen = (style & WindowStyle::Fullscreen) != 0; Uint32 winStyle = 0; @@ -94,16 +94,16 @@ namespace Nz } else { - if (!(style & WindowStyle_Titlebar)) + if (!(style & WindowStyle::Titlebar)) winStyle |= SDL_WINDOW_BORDERLESS; x = SDL_WINDOWPOS_CENTERED; y = SDL_WINDOWPOS_CENTERED; } - if (style & WindowStyle_Resizable) + if (style & WindowStyle::Resizable) winStyle |= SDL_WINDOW_RESIZABLE; - if (style & WindowStyle_Max) + if (style & WindowStyle::Max) winStyle |= SDL_WINDOW_MAXIMIZED; m_eventListener = true; @@ -313,7 +313,7 @@ namespace Nz auto window = static_cast(userdata); WindowEvent evt; - evt.type = WindowEventType::WindowEventType_Max; + evt.type = WindowEventType::Max; switch (event->type) { @@ -324,10 +324,10 @@ namespace Nz switch (event->window.event) { case SDL_WINDOWEVENT_CLOSE: - evt.type = Nz::WindowEventType::WindowEventType_Quit; + evt.type = WindowEventType::Quit; break; case SDL_WINDOWEVENT_RESIZED: - evt.type = Nz::WindowEventType::WindowEventType_Resized; + evt.type = WindowEventType::Resized; evt.size.width = static_cast(std::max(0, event->window.data1)); evt.size.height = static_cast(std::max(0, event->window.data2)); @@ -336,7 +336,7 @@ namespace Nz break; case SDL_WINDOWEVENT_MOVED: - evt.type = Nz::WindowEventType::WindowEventType_Moved; + evt.type = WindowEventType::Moved; evt.position.x = event->window.data1; evt.position.y = event->window.data2; @@ -345,19 +345,19 @@ namespace Nz break; case SDL_WINDOWEVENT_FOCUS_GAINED: - evt.type = Nz::WindowEventType::WindowEventType_GainedFocus; + evt.type = WindowEventType::GainedFocus; break; case SDL_WINDOWEVENT_FOCUS_LOST: - evt.type = Nz::WindowEventType::WindowEventType_LostFocus; + evt.type = WindowEventType::LostFocus; break; case SDL_WINDOWEVENT_ENTER: - evt.type = Nz::WindowEventType::WindowEventType_MouseEntered; + evt.type = WindowEventType::MouseEntered; break; case SDL_WINDOWEVENT_LEAVE: - evt.type = Nz::WindowEventType::WindowEventType_MouseLeft; + evt.type = WindowEventType::MouseLeft; break; } @@ -374,7 +374,7 @@ namespace Nz return 0; } - evt.type = Nz::WindowEventType::WindowEventType_MouseMoved; + evt.type = WindowEventType::MouseMoved; evt.mouseMove.x = event->motion.x; evt.mouseMove.y = event->motion.y; @@ -393,12 +393,12 @@ namespace Nz if (event->button.clicks % 2 == 0) { - evt.type = Nz::WindowEventType::WindowEventType_MouseButtonDoubleClicked; + evt.type = WindowEventType::MouseButtonDoubleClicked; window->m_parent->PushEvent(evt); } - evt.type = Nz::WindowEventType::WindowEventType_MouseButtonPressed; + evt.type = WindowEventType::MouseButtonPressed; break; @@ -410,7 +410,7 @@ namespace Nz evt.mouseButton.x = event->button.x; evt.mouseButton.y = event->button.y; - evt.type = Nz::WindowEventType::WindowEventType_MouseButtonReleased; + evt.type = WindowEventType::MouseButtonReleased; break; @@ -418,7 +418,7 @@ namespace Nz if (SDL_GetWindowID(window->m_handle) != event->wheel.windowID) return 0; - evt.type = Nz::WindowEventType::WindowEventType_MouseWheelMoved; + evt.type = WindowEventType::MouseWheelMoved; evt.mouseWheel.delta = event->wheel.y; @@ -428,7 +428,7 @@ namespace Nz if (SDL_GetWindowID(window->m_handle) != event->key.windowID) return 0; - evt.type = WindowEventType_KeyPressed; + evt.type = WindowEventType::KeyPressed; evt.key.scancode = SDLHelper::FromSDL(event->key.keysym.scancode); evt.key.virtualKey = SDLHelper::FromSDL(event->key.keysym.sym); @@ -446,7 +446,7 @@ namespace Nz break; window->m_parent->PushEvent(evt); - evt.type = WindowEventType_TextEntered; + evt.type = WindowEventType::TextEntered; evt.text.character = U'\n'; evt.text.repeated = event->key.repeat != 0; @@ -457,7 +457,7 @@ namespace Nz case Nz::Keyboard::VKey::Backspace: window->m_parent->PushEvent(evt); - evt.type = WindowEventType_TextEntered; + evt.type = WindowEventType::TextEntered; evt.text.character = U'\b'; evt.text.repeated = event->key.repeat != 0; @@ -475,7 +475,7 @@ namespace Nz if (SDL_GetWindowID(window->m_handle) != event->key.windowID) return 0; - evt.type = WindowEventType_KeyReleased; + evt.type = WindowEventType::KeyReleased; evt.key.scancode = SDLHelper::FromSDL(event->key.keysym.scancode); evt.key.virtualKey = SDLHelper::FromSDL(event->key.keysym.sym); @@ -492,7 +492,7 @@ namespace Nz if (SDL_GetWindowID(window->m_handle) != event->text.windowID) return 0; - evt.type = WindowEventType_TextEntered; + evt.type = WindowEventType::TextEntered; evt.text.repeated = false; utf8::unchecked::iterator it(event->text.text); @@ -504,7 +504,7 @@ namespace Nz } while (*it++); // prevent post switch event - evt.type = WindowEventType::WindowEventType_Max; + evt.type = WindowEventType::Max; break; } @@ -513,7 +513,7 @@ namespace Nz if (SDL_GetWindowID(window->m_handle) != event->edit.windowID) return 0; - evt.type = WindowEventType_TextEdited; + evt.type = WindowEventType::TextEdited; evt.edit.length = event->edit.length; window->m_lastEditEventLength = evt.edit.length; @@ -525,7 +525,7 @@ namespace Nz break; } - if (evt.type != WindowEventType::WindowEventType_Max) + if (evt.type != WindowEventType::Max) window->m_parent->PushEvent(evt); } catch (std::exception e) diff --git a/src/Nazara/Platform/Window.cpp b/src/Nazara/Platform/Window.cpp index 733a459c3..dca2d7639 100644 --- a/src/Nazara/Platform/Window.cpp +++ b/src/Nazara/Platform/Window.cpp @@ -61,12 +61,12 @@ namespace Nz Destroy(); // Inspired by the code of the SFML by Laurent Gomila (and its team) - if (style & WindowStyle_Fullscreen) + if (style & WindowStyle::Fullscreen) { if (fullscreenWindow) { NazaraError("Window " + PointerToString(fullscreenWindow) + " already in fullscreen mode"); - style &= ~WindowStyle_Fullscreen; + style &= ~WindowStyle::Fullscreen; } else { @@ -79,10 +79,10 @@ namespace Nz fullscreenWindow = this; } } - else if (style & WindowStyle_Closable || style & WindowStyle_Resizable) - style |= WindowStyle_Titlebar; + else if (style & WindowStyle::Closable || style & WindowStyle::Resizable) + style |= WindowStyle::Titlebar; - m_asyncWindow = (style & WindowStyle_Threaded) != 0; + m_asyncWindow = (style & WindowStyle::Threaded) != 0; std::unique_ptr impl = std::make_unique(this); if (!impl->Create(mode, title, style)) @@ -152,7 +152,7 @@ namespace Nz void Window::Destroy() { - m_cursor.Reset(); + m_cursor.reset(); if (m_impl) { @@ -341,7 +341,7 @@ namespace Nz } } - void Window::SetCursor(CursorRef cursor) + void Window::SetCursor(std::shared_ptr cursor) { NazaraAssert(m_impl, "Window not created"); NazaraAssert(cursor && cursor->IsValid(), "Invalid cursor"); @@ -382,10 +382,10 @@ namespace Nz m_impl->SetFocus(); } - void Window::SetIcon(IconRef icon) + void Window::SetIcon(std::shared_ptr icon) { NazaraAssert(m_impl, "Window not created"); - NazaraAssert(icon && icon.IsValid(), "Invalid icon"); + NazaraAssert(icon, "Invalid icon"); m_icon = std::move(icon); m_impl->SetIcon(*m_icon); @@ -629,7 +629,7 @@ namespace Nz void Window::ConnectSlots() { - m_cursorUpdateSlot.Connect(m_cursorController.OnCursorUpdated, [this](const CursorController*, const CursorRef& cursor) + m_cursorUpdateSlot.Connect(m_cursorController.OnCursorUpdated, [this](const CursorController*, const std::shared_ptr& cursor) { if (IsValid()) SetCursor(cursor); @@ -663,15 +663,15 @@ namespace Nz switch (event.type) { - case WindowEventType_MouseEntered: + case WindowEventType::MouseEntered: m_impl->RefreshCursor(); break; - case WindowEventType_Resized: + case WindowEventType::Resized: OnWindowResized(); break; - case WindowEventType_Quit: + case WindowEventType::Quit: if (m_closeOnQuit) Close(); diff --git a/tests/Engine/Core/ObjectRef.cpp b/tests/Engine/Core/ObjectRef.cpp index e44e5f1ff..87f1e3870 100644 --- a/tests/Engine/Core/ObjectRef.cpp +++ b/tests/Engine/Core/ObjectRef.cpp @@ -1,20 +1,22 @@ #include #include -#include +class Test : public Nz::RefCounted +{ +}; SCENARIO("ObjectRef", "[CORE][OBJECTREF]") { GIVEN("A ObjectRef") { - Nz::ObjectRef objectRef; + Nz::ObjectRef objectRef; WHEN("We have two objectRef handling the same object") { - Nz::Font font; + Test test; - objectRef = &font; - Nz::ObjectRef otherRef(&font); + objectRef = &test; + Nz::ObjectRef otherRef(&test); THEN("Pointers the same") { @@ -27,11 +29,11 @@ SCENARIO("ObjectRef", "[CORE][OBJECTREF]") WHEN("We assign it to a simple font") { - Nz::Font font; + Test test; THEN("Release suppress the reference to the object") { - objectRef.Reset(&font); + objectRef.Reset(&test); objectRef.Release(); REQUIRE(!objectRef.IsValid());