Upgrade Platform
This commit is contained in:
@@ -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 <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Platform/Debug.hpp>
|
||||
#include <Nazara/Platform/SDL2/CursorImpl.hpp>
|
||||
#include <Nazara/Utility/Image.hpp>
|
||||
#include <Nazara/Core/ErrorFlags.hpp>
|
||||
#include <Nazara/Utility/PixelFormat.hpp>
|
||||
#include <array>
|
||||
#include <Nazara/Platform/Debug.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
bool CursorImpl::Create(const Image& cursor, int hotSpotX, int hotSpotY)
|
||||
namespace
|
||||
{
|
||||
m_iconImage = cursor;
|
||||
if (!m_iconImage.Convert(PixelFormat_BGRA8))
|
||||
std::array<SDL_SystemCursor, SystemCursorCount> 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<SDL_SystemCursor, SystemCursor_Max + 1> 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");
|
||||
}
|
||||
|
||||
@@ -7,38 +7,33 @@
|
||||
#ifndef NAZARA_CURSORIMPL_HPP
|
||||
#define NAZARA_CURSORIMPL_HPP
|
||||
|
||||
#include <array>
|
||||
#include <Nazara/Platform/Enums.hpp>
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/MovablePtr.hpp>
|
||||
#include <Nazara/Math/Vector2.hpp>
|
||||
#include <Nazara/Platform/Enums.hpp>
|
||||
#include <Nazara/Utility/Image.hpp>
|
||||
|
||||
#include <SDL2/SDL_mouse.h>
|
||||
|
||||
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<SDL_SystemCursor, SystemCursor_Max + 1> s_systemCursorIds;
|
||||
MovablePtr<SDL_Cursor> m_cursor;
|
||||
MovablePtr<SDL_Surface> m_surface;
|
||||
Image m_cursorImage;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -3,20 +3,20 @@
|
||||
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||
|
||||
#include <Nazara/Platform/Debug.hpp>
|
||||
#include <Nazara/Core/ErrorFlags.hpp>
|
||||
#include <Nazara/Platform/SDL2/IconImpl.hpp>
|
||||
#include <Nazara/Utility/Image.hpp>
|
||||
#include <Nazara/Utility/PixelFormat.hpp>
|
||||
|
||||
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()
|
||||
|
||||
@@ -8,25 +8,28 @@
|
||||
#define NAZARA_ICONIMPL_HPP
|
||||
|
||||
#include <Nazara/Prerequisites.hpp>
|
||||
#include <Nazara/Core/MovablePtr.hpp>
|
||||
#include <Nazara/Utility/Image.hpp>
|
||||
#include <SDL2/SDL_surface.h>
|
||||
|
||||
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<SDL_Surface> m_icon;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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<WindowImpl*>(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<unsigned int>(std::max(0, event->window.data1));
|
||||
evt.size.height = static_cast<unsigned int>(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<const char*> 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)
|
||||
|
||||
Reference in New Issue
Block a user