cpp-sdl2
C++ header-only SDL2 wrapper
mouse.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "exception.hpp"
4 #include "surface.hpp"
5 #include "window.hpp"
6 #include <SDL.h>
7 
8 namespace sdl::Mouse
9 {
12 inline void set_relative(bool enabled)
13 {
14  if (SDL_SetRelativeMouseMode(enabled ? SDL_TRUE : SDL_FALSE) < 0)
15  throw Exception("SDL_SetRelativeMouseMode");
16 }
17 
19 inline bool get_relative()
20 {
21  return SDL_GetRelativeMouseMode() == SDL_TRUE;
22 }
23 
25 inline void warp_in_window(Window const& window, Vec2i const& position)
26 {
27  SDL_WarpMouseInWindow(window.ptr(), position.x, position.y);
28 }
29 
31 inline void warp_global(Vec2i const& position)
32 {
33  SDL_WarpMouseGlobal(position.x, position.y);
34 }
35 
37 class Cursor
38 {
40  SDL_Cursor* cursor_ = nullptr;
41 
42 public:
44  Cursor(SDL_SystemCursor id) : cursor_(SDL_CreateSystemCursor(id))
45  {
46  if (!cursor_)
47  {
48  throw Exception("SDL_CreateSystemCursor");
49  }
50  }
51 
53  Cursor(const uint8_t* data, const uint8_t* mask, Vec2i const& size, Vec2i const& hot)
54  : cursor_(SDL_CreateCursor(data, mask, size.x, size.y, hot.x, hot.y))
55  {
56  if (!cursor_)
57  {
58  throw Exception("SDL_CreateCursor");
59  }
60  }
61 
63  Cursor(Surface const& surface, Vec2i const& hot)
64  : cursor_(SDL_CreateColorCursor(surface.ptr(), hot.x, hot.y))
65  {
66  if (!cursor_)
67  {
68  throw Exception("SDL_CreateColorCursor");
69  }
70  }
71 
74  {
75  if (cursor_) SDL_FreeCursor(cursor_);
76  }
77 
78  Cursor& operator=(Cursor&& other) noexcept
79  {
80  if (cursor_ != other.cursor_)
81  {
82  cursor_ = other.cursor_;
83  other.cursor_ = nullptr;
84  }
85  return *this;
86  }
87 
88  Cursor(Cursor&& other) noexcept { *this = std::move(other); }
89 
91  void set() const
92  {
93  if (cursor_) SDL_SetCursor(cursor_);
94  }
95 
97  static void show()
98  {
99  const auto value = SDL_ShowCursor(SDL_ENABLE);
100 
101  if (value != SDL_ENABLE)
102  {
103  throw Exception("SDL_ShowCursor");
104  }
105  }
106 
108  static void hide()
109  {
110  const auto value = SDL_ShowCursor(SDL_DISABLE);
111 
112  if (value != SDL_DISABLE)
113  {
114  throw Exception("SDL_ShowCursor");
115  }
116  }
117 
119  static bool visible()
120  {
121  const auto value = SDL_ShowCursor(SDL_QUERY);
122 
123  if (value < 0)
124  {
125  throw Exception("SDL_ShowCursor");
126  }
127 
128  return value == SDL_ENABLE;
129  }
130 };
131 } // namespace sdl::Mouse
Cursor(SDL_SystemCursor id)
Construct a system cursor.
Definition: mouse.hpp:44
void warp_global(Vec2i const &position)
Wrap at this global and absolute position.
Definition: mouse.hpp:31
Generic templated 2D vector class.
Definition: vec2.hpp:24
Cursor(const uint8_t *data, const uint8_t *mask, Vec2i const &size, Vec2i const &hot)
Construct a cursor from MSB bitmap data.
Definition: mouse.hpp:53
static void hide()
Set the cursor as invisible.
Definition: mouse.hpp:108
Represent an SDL window.
Definition: window.hpp:19
SDL_Cursor * cursor_
SDL pointer.
Definition: mouse.hpp:40
bool get_relative()
Get if the mouse is in relative mode.
Definition: mouse.hpp:19
Cursor object.
Definition: mouse.hpp:37
~Cursor()
Free the cursor.
Definition: mouse.hpp:73
Cursor(Cursor &&other) noexcept
Definition: mouse.hpp:88
Cursor(Surface const &surface, Vec2i const &hot)
Create cursor from a SDL_Surface.
Definition: mouse.hpp:63
Cursor & operator=(Cursor &&other) noexcept
Definition: mouse.hpp:78
Represent an SDL_Surface.
Definition: surface.hpp:21
SDL_Window * ptr() const
Getter for the raw SDL2 window pointer.
Definition: window.hpp:61
static bool visible()
Return true or false if the cursor is curently visible or not.
Definition: mouse.hpp:119
void set_relative(bool enabled)
Namespace containing everything that has to do with mouse management Set the mose in relative mode...
Definition: mouse.hpp:12
static void show()
Set the cursor as visible.
Definition: mouse.hpp:97
Define to deactivate exception support.
Definition: exception.hpp:21
void warp_in_window(Window const &window, Vec2i const &position)
Wrap inside the given windows.
Definition: mouse.hpp:25