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 <display.h>
11#include <draw.h>
12#include <input.h>
13#include <SDL_stbimage.h>
14
17
18#include "Mouse.hpp"
19
20namespace galaxy
21{
22 Mouse::Mouse() noexcept
24 , m_cursor {nullptr}
25 , m_cursor_bitmap {nullptr}
26 {
27 }
28
29 Mouse::~Mouse() noexcept
30 {
32 {
33 sl::destroy_bitmap(m_cursor_bitmap);
34 m_cursor_bitmap = nullptr;
35 }
36
38 }
39
40 bool Mouse::mouse_connected() const noexcept
41 {
42 return SDL_HasMouse();
43 }
44
45 void Mouse::set_mouse_grab(const bool grabbed) const noexcept
46 {
47 SDL_SetWindowMouseGrab(m_window, grabbed);
48 }
49
50 void Mouse::warp_mouse(const float x, const float y) const noexcept
51 {
52 SDL_WarpMouseInWindow(m_window, x, y);
53 }
54
56 {
57 const auto sdlrect = rect.to_sdl();
58 return SDL_SetWindowMouseRect(m_window, &sdlrect);
59 }
60
61 void Mouse::show_cursor() const noexcept
62 {
63 sl::show_mouse();
64 }
65
66 void Mouse::hide_cursor() const noexcept
67 {
68 sl::hide_mouse();
69 }
70
71 void Mouse::set_cursor_system(const SDL_SystemCursor cursor) noexcept
72 {
74
75 m_cursor = SDL_CreateSystemCursor(cursor);
76 if (m_cursor)
77 {
78 SDL_SetCursor(m_cursor);
79 }
80 else
81 {
82 GALAXY_LOG(GALAXY_ERROR, "Failed to create system cursor: '{0}'.", SDL_GetError());
83 }
84 }
85
86 void Mouse::use_custom_mouse(const std::string& cursor, const glm::ivec2& offset)
87 {
88 if (!cursor.empty())
89 {
91
92 auto& fs = entt::locator<VirtualFileSystem>::value();
93
94 auto data = fs.read_binary(cursor);
95 if (!data.empty())
96 {
97 auto surface = STBIMG_LoadFromMemory(data.data(), static_cast<int>(data.size()));
98 if (surface)
99 {
100 if (m_cursor_bitmap)
101 {
102 sl::destroy_bitmap(m_cursor_bitmap);
103 }
104
105 m_cursor_bitmap = sl::load_bitmap_from_surface(surface);
106 sl::set_mouse_bitmap(m_cursor_bitmap, offset.x, offset.y);
107 }
108 else
109 {
110 GALAXY_LOG(GALAXY_ERROR, SDL_GetError());
111 }
112
113 SDL_DestroySurface(surface);
114 }
115 else
116 {
117 GALAXY_LOG(GALAXY_ERROR, "Failed to read '{0}' from the vfs.", cursor);
118 }
119 }
120 }
121
122 void Mouse::restore_cursor() noexcept
123 {
125 SDL_SetCursor(SDL_GetDefaultCursor());
126 }
127
128 void Mouse::destroy_cursor() noexcept
129 {
130 if (m_cursor)
131 {
132 SDL_DestroyCursor(m_cursor);
133 m_cursor = nullptr;
134 }
135 }
136
137 bool Mouse::is_mouse_down(const MouseButton btn) const noexcept
138 {
139 const auto buttons = SDL_GetMouseState(nullptr, nullptr);
140 return buttons & SDL_BUTTON_MASK(static_cast<SDL_MouseButtonFlags>(btn));
141 }
142
143 int Mouse::mouse_x() const noexcept
144 {
145 return sl::mouse_x();
146 }
147
148 int Mouse::mouse_y() const noexcept
149 {
150 return sl::mouse_y();
151 }
152
153 void Mouse::set_mouse_mapping(const int x, const int y, const float scale_x, const float scale_y) const noexcept
154 {
155 return sl::set_mouse_mapping(x, y, scale_x, scale_y);
156 }
157
158 void Mouse::clear_mouse_mapping() const noexcept
159 {
160 sl::clear_mouse_mapping();
161 }
162
163 void Mouse::set_relative_mouse_mode(const bool enabled) const noexcept
164 {
165 sl::set_relative_mouse_mode(m_window, enabled);
166 }
167
168 bool Mouse::relative_mouse_mode() const noexcept
169 {
170 return sl::relative_mouse_mode(m_window);
171 }
172
173 glm::ivec2 Mouse::mouse_delta(int* dx, int* dy) noexcept
174 {
175 glm::ivec2 vec2;
176 sl::mouse_delta(&vec2.x, &vec2.y);
177
178 return vec2;
179 }
180} // namespace galaxy
#define GALAXY_LOG(level, msg,...)
Definition Log.hpp:29
#define GALAXY_ERROR
Definition Log.hpp:25
bool set_window_mouse_rect(iRect &rect) noexcept
Confines the cursor to the specified area of a window.
Definition Mouse.cpp:55
glm::ivec2 mouse_delta(int *dx, int *dy) noexcept
Retrieve relative mouse movement (delta) since the last call.
Definition Mouse.cpp:173
sl::Bitmap * m_cursor_bitmap
Mouse custom bitmap.
Definition Mouse.hpp:215
void set_mouse_mapping(const int x, const int y, const float scale_x, const float scale_y) const noexcept
Set mouse relative mapping to another coord space.
Definition Mouse.cpp:153
bool is_mouse_down(const MouseButton btn) const noexcept
Check if a mouse button was pressed.
Definition Mouse.cpp:137
void set_cursor_system(const SDL_SystemCursor cursor) noexcept
Set cursor to a system cursor.
Definition Mouse.cpp:71
Mouse() noexcept
Constructor.
Definition Mouse.cpp:22
~Mouse() noexcept
Destructor.
Definition Mouse.cpp:29
void clear_mouse_mapping() const noexcept
Return mouse_x()/mouse_y() to plain screen coordinates.
Definition Mouse.cpp:158
void destroy_cursor() noexcept
Destroy any existing cursor.
Definition Mouse.cpp:128
bool relative_mouse_mode() const noexcept
Return whether relative mouse mode is currently enabled.
Definition Mouse.cpp:168
void hide_cursor() const noexcept
Hide cursor.
Definition Mouse.cpp:66
void set_relative_mouse_mode(const bool enabled) const noexcept
Enable or disable relative mouse mode (locks cursor to window, hides it, and reports relative motion)...
Definition Mouse.cpp:163
void restore_cursor() noexcept
Set the cursor back to default.
Definition Mouse.cpp:122
void set_mouse_grab(const bool grabbed) const noexcept
Toggle mouse grab.
Definition Mouse.cpp:45
int mouse_y() const noexcept
Get current cursor y position.
Definition Mouse.cpp:148
void show_cursor() const noexcept
Show cursor.
Definition Mouse.cpp:61
SDL_Cursor * m_cursor
Holds data for a custom cursor.
Definition Mouse.hpp:210
int mouse_x() const noexcept
Get current cursor x position.
Definition Mouse.cpp:143
bool mouse_connected() const noexcept
Check if there is a mouse connected.
Definition Mouse.cpp:40
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:50
void use_custom_mouse(const std::string &cursor, const glm::ivec2 &offset=glm::ivec2(0, 0))
Replaces the system cursor with a bitmap.
Definition Mouse.cpp:86
WindowBindable() noexcept
Constructor.
SDL_Window * m_window
Pointer to SDL_Window handle.
Tag.cpp galaxy.
Definition Tag.cpp:13
Rect< int > iRect
Type definition for an int rectangle.
Definition Rect.hpp:190
MouseButton
Enum class representing mouse buttons.