Added cursor support

This commit is contained in:
Robert 2021-04-23 16:29:26 +02:00
parent c66cae17f2
commit 1ec25d036c
6 changed files with 110 additions and 45 deletions

62
src/structures/Cursor.cpp Normal file
View file

@ -0,0 +1,62 @@
#include "structures/Cursor.hpp"
#include <SDL2/SDL_mouse.h>
SDLU_BEGIN
Cursor::Cursor() :
cursor(SDL_CreateSystemCursor(static_cast<SDL_SystemCursor>(Type::Arrow)))
{
}
Cursor::Cursor(Type type) :
cursor(SDL_CreateSystemCursor(static_cast<SDL_SystemCursor>(type)))
{
}
Cursor::Cursor(Cursor&& other) noexcept
{
this->cursor = other.cursor;
other.cursor = nullptr;
}
Cursor::~Cursor()
{
SDL_FreeCursor(cursor);
}
bool Cursor::LoadFromPixels(const Uint8* pixels, Vector2u size, Vector2u hotspot)
{
SDL_FreeCursor(cursor);
cursor = SDL_CreateCursor(pixels, nullptr, size.x, size.y, hotspot.x, hotspot.y);
if (IS_NULLPTR(cursor))
return false;
return true;
}
bool Cursor::LoadFromSurface(SDL_Surface* surface, Vector2u hotspot)
{
SDL_FreeCursor(cursor);
cursor = SDL_CreateColorCursor(surface, hotspot.x, hotspot.y);
if (IS_NULLPTR(cursor))
return false;
return true;
}
bool Cursor::LoadFromSystem(Type type)
{
SDL_FreeCursor(cursor);
cursor = SDL_CreateSystemCursor(static_cast<SDL_SystemCursor>(type));
if (IS_NULLPTR(cursor))
return false;
return true;
}
SDLU_END

View file

@ -3,6 +3,8 @@
#include <SDL.h>
#include <cstring>
#include "structures/Cursor.hpp"
SDLU_BEGIN
Window::Window() :
window(nullptr)
@ -204,30 +206,9 @@ void Window::SetMouseCursor(SDL_Cursor* cursor)
SDL_SetCursor(cursor);
}
void Window::SetMouseCursor(SDL_Surface* surface, Vector2u clickspot)
void Window::SetMouseCursor(const Cursor& cursor)
{
SDL_Cursor* _cursor = SDL_CreateColorCursor(surface, clickspot.x, clickspot.y);
SDL_SetCursor(_cursor);
}
void Window::SetMouseCursor(const Uint8* pixels, Vector2u size, Vector2u clickspot)
{
size_t _size = static_cast<size_t>(size.x) * static_cast<size_t>(size.y) * 4;
void* _pixels = malloc(_size);
memcpy(_pixels, pixels, _size);
SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(_pixels,
size.x, size.y, 32, 8 * size.x, SDL_PIXELFORMAT_RGBA8888);
this->SetMouseCursor(surface, clickspot);
}
void Window::SetMouseCursor(const Uint32* pixels, Vector2u size, Vector2u clickspot)
{
size_t _size = static_cast<size_t>(size.x) * static_cast<size_t>(size.y) * 4;
void* _pixels = malloc(_size);
memcpy(_pixels, pixels, _size);
SDL_Surface* surface = SDL_CreateRGBSurfaceWithFormatFrom(_pixels,
size.x, size.y, 32, 8 * size.x, SDL_PIXELFORMAT_RGBA32);
this->SetMouseCursor(surface, clickspot);
SDL_SetCursor(cursor.cursor);
}
void Window::OnCreate()