diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-11-28 19:28:35 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-11-28 19:28:35 +0800 |
| commit | e4fd76c113e43aa0fa00cf05a14cb12db080ba78 (patch) | |
| tree | 4174347e22625aa4e0151ece714708d46c10bfd1 /src/platform/gui/sdl/Cursor.cpp | |
| parent | fa9e88a3a753de0f7a33efcf6a2671c28f099ba3 (diff) | |
| download | cru-e4fd76c113e43aa0fa00cf05a14cb12db080ba78.tar.gz cru-e4fd76c113e43aa0fa00cf05a14cb12db080ba78.tar.bz2 cru-e4fd76c113e43aa0fa00cf05a14cb12db080ba78.zip | |
Impl cursor on sdl.
Diffstat (limited to 'src/platform/gui/sdl/Cursor.cpp')
| -rw-r--r-- | src/platform/gui/sdl/Cursor.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/platform/gui/sdl/Cursor.cpp b/src/platform/gui/sdl/Cursor.cpp new file mode 100644 index 00000000..901c2bff --- /dev/null +++ b/src/platform/gui/sdl/Cursor.cpp @@ -0,0 +1,48 @@ +#include "cru/platform/gui/sdl/Cursor.h" +#include "cru/base/Base.h" +#include "cru/platform/gui/Cursor.h" +#include "cru/platform/gui/sdl/Base.h" + +#include <memory> + +namespace cru::platform::gui::sdl { +std::shared_ptr<SdlCursor> SdlCursor::CreateSystem(SDL_SystemCursor cursor) { + auto c = SDL_CreateSystemCursor(cursor); + if (!c) { + throw SdlException("Failed to create system cursor."); + } + return std::make_shared<SdlCursor>(c, true); +} + +SdlCursor::SdlCursor(SDL_Cursor* cursor, bool auto_destroy) + : cursor_(cursor), auto_destroy_(auto_destroy) {} + +SdlCursor::~SdlCursor() { + if (auto_destroy_) { + SDL_DestroyCursor(cursor_); + } +} + +SDL_Cursor* SdlCursor::GetSdlCursor() { return cursor_; } + +SdlCursorManager::SdlCursorManager() { + arrow_cursor_ = SdlCursor::CreateSystem(SDL_SYSTEM_CURSOR_DEFAULT); + hand_cursor_ = SdlCursor::CreateSystem(SDL_SYSTEM_CURSOR_POINTER); + ibeam_cursor_ = SdlCursor::CreateSystem(SDL_SYSTEM_CURSOR_TEXT); +} + +std::shared_ptr<ICursor> SdlCursorManager::GetSystemCursor( + SystemCursorType type) { + switch (type) { + case SystemCursorType::Arrow: + return std::static_pointer_cast<ICursor>(arrow_cursor_); + case SystemCursorType::Hand: + return std::static_pointer_cast<ICursor>(hand_cursor_); + case SystemCursorType::IBeam: + return std::static_pointer_cast<ICursor>(ibeam_cursor_); + default: + UnreachableCode(); + } +} + +} // namespace cru::platform::gui::sdl |
