galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
Mouse.cpp
Go to the documentation of this file.
1
7
8#define SDL_STBIMG_SDL3
9
10#include <SDL_stbimage.h>
11
14
15#include "Mouse.hpp"
16
17namespace galaxy
18{
19 Mouse::Mouse() noexcept
20 : m_cursor {nullptr}
21 {
22 }
23
24 Mouse::~Mouse() noexcept
25 {
27 }
28
29 bool Mouse::has_mouse() const noexcept
30 {
31 return SDL_HasMouse();
32 }
33
34 void Mouse::set_mouse_grab(const bool grabbed) const noexcept
35 {
36 SDL_SetWindowMouseGrab(m_window, grabbed);
37 }
38
39 void Mouse::warp_mouse(const float x, const float y) const noexcept
40 {
41 SDL_WarpMouseInWindow(m_window, x, y);
42 }
43
44 void Mouse::show_cursor() const noexcept
45 {
46 SDL_ShowCursor();
47 }
48
49 void Mouse::hide_cursor() const noexcept
50 {
51 SDL_HideCursor();
52 }
53
54 void Mouse::set_cursor_system(const SDL_SystemCursor cursor) noexcept
55 {
56 destroy_cursor();
57
58 m_cursor = SDL_CreateSystemCursor(cursor);
59 if (m_cursor)
60 {
61 SDL_SetCursor(m_cursor);
62 }
63 else
64 {
65 GALAXY_LOG(GALAXY_ERROR, "Failed to create system cursor: '{0}'.", SDL_GetError());
66 }
67 }
68
69 void Mouse::set_cursor_custom(const std::string& cursor, const glm::ivec2& hotspot) noexcept
70 {
71 if (!cursor.empty())
72 {
73 destroy_cursor();
74
75 auto& fs = entt::locator<VirtualFileSystem>::value();
76
77 auto data = fs.read_binary(cursor);
78 if (!data.empty())
79 {
80 auto surface = STBIMG_LoadFromMemory(data.data(), static_cast<int>(data.size()));
81 if (surface)
82 {
83 m_cursor = SDL_CreateColorCursor(surface, hotspot.x, hotspot.y);
84 if (m_cursor)
85 {
86 SDL_SetCursor(m_cursor);
87 }
88 else
89 {
90 GALAXY_LOG(GALAXY_ERROR, "Failed to create custom cursor: '{0}'.", SDL_GetError());
91 }
92 }
93 else
94 {
95 GALAXY_LOG(GALAXY_ERROR, SDL_GetError());
96 }
97
98 SDL_DestroySurface(surface);
99 }
100 else
101 {
102 GALAXY_LOG(GALAXY_ERROR, "Failed to read '{0}' from the vfs.", cursor);
103 }
104 }
105 }
106
107 void Mouse::restore_cursor() noexcept
108 {
110 SDL_SetCursor(SDL_GetDefaultCursor());
111 }
112
113 void Mouse::destroy_cursor() noexcept
114 {
115 if (m_cursor)
116 {
117 SDL_DestroyCursor(m_cursor);
118 m_cursor = nullptr;
119 }
120 }
121} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:28
#define GALAXY_ERROR
Definition Log.hpp:24
void set_cursor_system(const SDL_SystemCursor cursor) noexcept
Set cursor to a system cursor.
Definition Mouse.cpp:54
Mouse() noexcept
Constructor.
Definition Mouse.cpp:19
~Mouse() noexcept
Destructor.
Definition Mouse.cpp:24
void destroy_cursor() noexcept
Destroy any existing cursor.
Definition Mouse.cpp:113
void hide_cursor() const noexcept
Hide cursor.
Definition Mouse.cpp:49
void restore_cursor() noexcept
Set the cursor back to default.
Definition Mouse.cpp:107
void set_mouse_grab(const bool grabbed) const noexcept
Toggle mouse grab.
Definition Mouse.cpp:34
void show_cursor() const noexcept
Show cursor.
Definition Mouse.cpp:44
SDL_Cursor * m_cursor
Holds data for a custom cursor.
Definition Mouse.hpp:121
bool has_mouse() const noexcept
Check if there is a mouse connected.
Definition Mouse.cpp:29
void set_cursor_custom(const std::string &cursor, const glm::ivec2 &hotspot) noexcept
Set custom cursor texture.
Definition Mouse.cpp:69
void warp_mouse(const float x, const float y) const noexcept
Move the mouse cursor to the given position within the window.
Definition Mouse.cpp:39
Animated.cpp galaxy.
Definition Animated.cpp:16