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
21 , m_cursor {nullptr}
22 {
23 }
24
25 Mouse::~Mouse() noexcept
26 {
28 }
29
30 bool Mouse::has_mouse() const noexcept
31 {
32 return SDL_HasMouse();
33 }
34
35 void Mouse::set_mouse_grab(const bool grabbed) const noexcept
36 {
37 SDL_SetWindowMouseGrab(m_window, grabbed);
38 }
39
40 void Mouse::warp_mouse(const float x, const float y) const noexcept
41 {
42 SDL_WarpMouseInWindow(m_window, x, y);
43 }
44
45 void Mouse::show_cursor() const noexcept
46 {
47 SDL_ShowCursor();
48 }
49
50 void Mouse::hide_cursor() const noexcept
51 {
52 SDL_HideCursor();
53 }
54
55 void Mouse::set_cursor_system(const SDL_SystemCursor cursor) noexcept
56 {
57 destroy_cursor();
58
59 m_cursor = SDL_CreateSystemCursor(cursor);
60 if (m_cursor)
61 {
62 SDL_SetCursor(m_cursor);
63 }
64 else
65 {
66 GALAXY_LOG(GALAXY_ERROR, "Failed to create system cursor: '{0}'.", SDL_GetError());
67 }
68 }
69
70 void Mouse::set_cursor_custom(const std::string& cursor, const glm::ivec2& hotspot) noexcept
71 {
72 if (!cursor.empty())
73 {
74 destroy_cursor();
75
76 auto& fs = entt::locator<VirtualFileSystem>::value();
77
78 auto data = fs.read_binary(cursor);
79 if (!data.empty())
80 {
81 auto surface = STBIMG_LoadFromMemory(data.data(), static_cast<int>(data.size()));
82 if (surface)
83 {
84 m_cursor = SDL_CreateColorCursor(surface, hotspot.x, hotspot.y);
85 if (m_cursor)
86 {
87 SDL_SetCursor(m_cursor);
88 }
89 else
90 {
91 GALAXY_LOG(GALAXY_ERROR, "Failed to create custom cursor: '{0}'.", SDL_GetError());
92 }
93 }
94 else
95 {
96 GALAXY_LOG(GALAXY_ERROR, SDL_GetError());
97 }
98
99 SDL_DestroySurface(surface);
100 }
101 else
102 {
103 GALAXY_LOG(GALAXY_ERROR, "Failed to read '{0}' from the vfs.", cursor);
104 }
105 }
106 }
107
108 void Mouse::restore_cursor() noexcept
109 {
111 SDL_SetCursor(SDL_GetDefaultCursor());
112 }
113
114 void Mouse::destroy_cursor() noexcept
115 {
116 if (m_cursor)
117 {
118 SDL_DestroyCursor(m_cursor);
119 m_cursor = nullptr;
120 }
121 }
122} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
void set_cursor_system(const SDL_SystemCursor cursor) noexcept
Set cursor to a system cursor.
Definition Mouse.cpp:55
Mouse() noexcept
Constructor.
Definition Mouse.cpp:19
~Mouse() noexcept
Destructor.
Definition Mouse.cpp:25
void destroy_cursor() noexcept
Destroy any existing cursor.
Definition Mouse.cpp:114
void hide_cursor() const noexcept
Hide cursor.
Definition Mouse.cpp:50
void restore_cursor() noexcept
Set the cursor back to default.
Definition Mouse.cpp:108
void set_mouse_grab(const bool grabbed) const noexcept
Toggle mouse grab.
Definition Mouse.cpp:35
void show_cursor() const noexcept
Show cursor.
Definition Mouse.cpp:45
SDL_Cursor * m_cursor
Holds data for a custom cursor.
Definition Mouse.hpp:119
bool has_mouse() const noexcept
Check if there is a mouse connected.
Definition Mouse.cpp:30
void set_cursor_custom(const std::string &cursor, const glm::ivec2 &hotspot) noexcept
Set custom cursor texture.
Definition Mouse.cpp:70
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:40
Allows you to bind a base class to the window.
Application.hpp galaxy.