From 2c934fd5a3acfb712ebbd38188bf71111cefd113 Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 18 May 2020 18:29:37 +0200 Subject: [PATCH] Cursor is now changeable --- SDLU/graphics/RenderWindow.cpp | 37 ++++++++++++++++++++++++++++++ SDLU/graphics/RenderWindow.hpp | 42 +++++++++++++++++++++++++++++++++- SDLU_Example/main.cpp | 3 ++- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/SDLU/graphics/RenderWindow.cpp b/SDLU/graphics/RenderWindow.cpp index db386f0..c05e5cf 100644 --- a/SDLU/graphics/RenderWindow.cpp +++ b/SDLU/graphics/RenderWindow.cpp @@ -245,4 +245,41 @@ namespace sdlu { m_oFramerate = max; } + + void RenderWindow::SetMouseCursor(SDL_Cursor* cursor) + { + SDL_SetCursor(cursor); + } + + void RenderWindow::SetMouseCursor(SDL_SystemCursor cursor) + { + SDL_Cursor* _cursor = SDL_CreateSystemCursor(cursor); + SDL_SetCursor(_cursor); + } + + void RenderWindow::SetMouseCursor(SDL_Surface* surface, Vector2u clickspot) + { + SDL_Cursor* _cursor = SDL_CreateColorCursor(surface, clickspot.x, clickspot.y); + SDL_SetCursor(_cursor); + } + + void RenderWindow::SetMouseCursor(const Uint8* pixels, Vector2u size, Vector2u clickspot) + { + size_t _size = static_cast(size.x) * static_cast(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 RenderWindow::SetMouseCursor(const Uint32* pixels, Vector2u size, Vector2u clickspot) + { + size_t _size = static_cast(size.x) * static_cast(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); + } } \ No newline at end of file diff --git a/SDLU/graphics/RenderWindow.hpp b/SDLU/graphics/RenderWindow.hpp index 0534efa..eee6972 100644 --- a/SDLU/graphics/RenderWindow.hpp +++ b/SDLU/graphics/RenderWindow.hpp @@ -233,7 +233,47 @@ namespace sdlu * * @param[in] max The new maximum framerate */ - void SetMaxFramerate(Uint32 max); + void SetMaxFramerate(Uint32 max); + + /** + * @brief Changes the mouse cursor + * + * @param[in] cursor A pointer to a SDL_Cursor containing cursor data + */ + void SetMouseCursor(SDL_Cursor* cursor); + + /** + * @brief Changes the mouse cursor + * + * @param[in] cursor An enum for a system cursor + */ + void SetMouseCursor(SDL_SystemCursor cursor); + + /** + * @brief Changes the mouse cursor + * + * @param[in] surface A pointer to a SDL_Surface containing sprite data + * @param[in] clickspot The effective position of the cursor relative to the top left of the sprite + */ + void SetMouseCursor(SDL_Surface* surface, Vector2u clickspot); + + /** + * @brief Changes the mouse cursor + * + * @param[in] pixels An array of color data (RGBA as seperate 8-bit values) + * @param[in] size Size of the cursor + * @param[in] clickspot The effective position of the cursor relative to the top left of the sprite + */ + void SetMouseCursor(const Uint8* pixels, Vector2u size, Vector2u clickspot); + + /** + * @brief Changes the mouse cursor + * + * @param[in] pixels An array of color data (RGBA as one 32-bit value) + * @param[in] size Size of the cursor + * @param[in] clickspot The effective position of the cursor relative to the top left of the sprite + */ + void SetMouseCursor(const Uint32* pixels, Vector2u size, Vector2u clickspot); protected: SDL_Window* m_pWindow; ///< A pointer to the window object diff --git a/SDLU_Example/main.cpp b/SDLU_Example/main.cpp index 7783776..6815c5d 100644 --- a/SDLU_Example/main.cpp +++ b/SDLU_Example/main.cpp @@ -22,6 +22,7 @@ int main(int argc, char** argv) SDL_SetWindowTitle(window.GetWindow(), "New Title"); window.SetIcon(64, 64, icon_data); + window.SetMouseCursor(SDL_SYSTEM_CURSOR_CROSSHAIR); window.SetMaxFramerate(144); SDL_Event event; @@ -45,7 +46,7 @@ int main(int argc, char** argv) window.Clear(sdlu::Color::FromHSV(floor(t), 100, 100)); window.Display(); - t += 0.01; + t += 0.08; diff = std::chrono::duration_cast (std::chrono::steady_clock::now() - start).count();