rolmodl
C++ API for the Simple Directmedia Library 2 (SDL2)
Win.hpp
Go to the documentation of this file.
1 #pragma once
2 
7 
8 #include "forwarddecl/Win.hpp"
9 
10 #include "forwarddecl/Geom.hpp"
11 #include "forwarddecl/PixelFmt.hpp"
12 #include "forwarddecl/Base.hpp"
13 
15 namespace rolmodl {
17  namespace win {
25  struct Flags {
26  public:
28  constexpr Flags() noexcept :
29  data_(0)
30  {}
31 
32  // test
34  constexpr bool isFullscreen() const noexcept {
35  return (data_ & static_cast<uint32_t>(SDL_WINDOW_FULLSCREEN)) != 0;
36  }
38  constexpr bool isFullscreenDesktop() const noexcept {
39  return (data_ & static_cast<uint32_t>(SDL_WINDOW_FULLSCREEN_DESKTOP)) != 0;
40  }
41 
43  constexpr bool isMinimized() const noexcept {
44  return (data_ & static_cast<uint32_t>(SDL_WINDOW_MINIMIZED)) != 0;
45  }
47  constexpr bool isMaximized() const noexcept {
48  return (data_ & static_cast<uint32_t>(SDL_WINDOW_MAXIMIZED)) != 0;
49  }
50 
53  constexpr bool isOpengl() const noexcept {
54  return (data_ & static_cast<uint32_t>(SDL_WINDOW_OPENGL)) != 0;
55  }
57  constexpr bool isHidden() const noexcept {
58  return (data_ & static_cast<uint32_t>(SDL_WINDOW_HIDDEN)) != 0;
59  }
61  constexpr bool isBorderless() const noexcept {
62  return (data_ & static_cast<uint32_t>(SDL_WINDOW_BORDERLESS)) != 0;
63  }
65  constexpr bool isResizable() const noexcept {
66  return (data_ & static_cast<uint32_t>(SDL_WINDOW_RESIZABLE)) != 0;
67  }
69  constexpr bool isInputGrabbed() const noexcept {
70  return (data_ & static_cast<uint32_t>(SDL_WINDOW_INPUT_GRABBED)) != 0;
71  }
73  constexpr bool isHighDPI() const noexcept {
74  return (data_ & static_cast<uint32_t>(SDL_WINDOW_ALLOW_HIGHDPI)) != 0;
75  }
76 
77  // set
79  constexpr Flags withFullscreen() const noexcept {
80  return Flags(data_ | static_cast<uint32_t>(SDL_WINDOW_FULLSCREEN));
81  }
83  constexpr Flags withFullscreenDesktop() const noexcept {
84  return Flags(data_ | static_cast<uint32_t>(SDL_WINDOW_FULLSCREEN_DESKTOP));
85  }
86 
88  constexpr Flags withMinimized() const noexcept {
89  return Flags(data_ | static_cast<uint32_t>(SDL_WINDOW_MINIMIZED));
90  }
92  constexpr Flags withMaximized() const noexcept {
93  return Flags(data_ | static_cast<uint32_t>(SDL_WINDOW_MAXIMIZED));
94  }
95 
98  constexpr Flags withOpengl() const noexcept {
99  return Flags(data_ | static_cast<uint32_t>(SDL_WINDOW_OPENGL));
100  }
102  constexpr Flags withHidden() const noexcept {
103  return Flags(data_ | static_cast<uint32_t>(SDL_WINDOW_HIDDEN));
104  }
106  constexpr Flags withBorderless() const noexcept {
107  return Flags(data_ | static_cast<uint32_t>(SDL_WINDOW_BORDERLESS));
108  }
110  constexpr Flags withResizable() const noexcept {
111  return Flags(data_ | static_cast<uint32_t>(SDL_WINDOW_RESIZABLE));
112  }
114  constexpr Flags withInputGrabbed() const noexcept {
115  return Flags(data_ | static_cast<uint32_t>(SDL_WINDOW_INPUT_GRABBED));
116  }
118  constexpr Flags withHighDPI() const noexcept {
119  return Flags(data_ | static_cast<uint32_t>(SDL_WINDOW_ALLOW_HIGHDPI));
120  }
121 
122  // unset
124  constexpr Flags withoutFullscreen() const noexcept {
125  return Flags(data_ & ~static_cast<uint32_t>(SDL_WINDOW_FULLSCREEN));
126  }
128  constexpr Flags withoutFullscreenDesktop() const noexcept {
129  return Flags(data_ & ~static_cast<uint32_t>(SDL_WINDOW_FULLSCREEN_DESKTOP));
130  }
131 
133  constexpr Flags withoutMinimized() const noexcept {
134  return Flags(data_ & ~static_cast<uint32_t>(SDL_WINDOW_MINIMIZED));
135  }
137  constexpr Flags withoutMaximized() const noexcept {
138  return Flags(data_ & ~static_cast<uint32_t>(SDL_WINDOW_MAXIMIZED));
139  }
140 
142  constexpr Flags withoutOpengl() const noexcept {
143  return Flags(data_ & ~static_cast<uint32_t>(SDL_WINDOW_OPENGL));
144  }
146  constexpr Flags withoutHidden() const noexcept {
147  return Flags(data_ & ~static_cast<uint32_t>(SDL_WINDOW_HIDDEN));
148  }
150  constexpr Flags withoutBorderless() const noexcept {
151  return Flags(data_ & ~static_cast<uint32_t>(SDL_WINDOW_BORDERLESS));
152  }
154  constexpr Flags withoutResizable() const noexcept {
155  return Flags(data_ & ~static_cast<uint32_t>(SDL_WINDOW_RESIZABLE));
156  }
158  constexpr Flags withoutInputGrabbed() const noexcept {
159  return Flags(data_ & ~static_cast<uint32_t>(SDL_WINDOW_INPUT_GRABBED));
160  }
162  constexpr Flags withoutHighDPI() const noexcept {
163  return Flags(data_ & ~static_cast<uint32_t>(SDL_WINDOW_ALLOW_HIGHDPI));
164  }
165 
167  constexpr uint32_t raw() const noexcept {
168  return data_;
169  }
170  // fixme: missing unsafeFromRaw
171  private:
172  explicit constexpr Flags(const uint32_t data) noexcept :
173  data_(data)
174  {}
175 
176  uint32_t data_;
177  };
178 
180  namespace unsafe {
181  // Win_Base fromId();
182  }
183  }
184 
187  struct BorderSizes {
188  public:
190  explicit BorderSizes(Win_Base& w);
191 
193  int top() const noexcept;
195  int l() const noexcept;
197  int bot() const noexcept;
199  int r() const noexcept;
200 
201  private:
202  int top_, l_, bot_, r_;
203  };
204 
212  class Win_Base {
213  public:
216  Win_Base(const char* title, const geom::Pos p, const geom::Size s, const win::Flags flags);
219  Win_Base(const char* title, const geom::Size s, const win::Flags flags);
222  Win_Base(const char* title, const geom::Pos p, const geom::Size s);
225  Win_Base(const char* title, const geom::Size s);
228  ~Win_Base() noexcept;
229 
231  Win_Base(const Win_Base& that) = delete;
233  Win_Base(Win_Base&& that) noexcept;
234 
236  Win_Base& operator=(const Win_Base& that) = delete;
238  Win_Base& operator=(Win_Base&& that) noexcept;
239 
241  friend void swap(Win_Base& a, Win_Base& b) noexcept;
242 
244  SDL_Window* unsafeRaw() noexcept;
246  const SDL_Window* unsafeRaw() const noexcept;
247 
248 
251  void hide() noexcept;
254  void show() noexcept;
257  void raise() noexcept;
258 
261  void maximize() noexcept;
264  void minimize() noexcept;
267  void restore() noexcept;
268 
271  unsigned int unsafeDisplayIndex();
275 
278  float opacity();
281  void setOpacity(const float v);
282 
285  geom::Pos pos() noexcept;
288  void setPos(const geom::Pos p) noexcept;
289 
292  const char* title() noexcept;
295  void setTitle(const char* str) noexcept;
296 
297 
300  geom::Size size() noexcept;
303  void setSize(const geom::Size s) noexcept;
304 
307  geom::Size maxSize() noexcept;
310  void setMaxSize(const geom::Size s) noexcept;
311 
314  geom::Size minSize() noexcept;
317  void setMinSize(const geom::Size s) noexcept;
318 
319 
322  uint32_t unsafeId();
325  pixelfmt::Id pixelFmt();
328  void moveMouseIn(const geom::Pos p) noexcept;
329 
330  private:
331  Win_Base() noexcept;
332 
333  SDL_Window* h_;
334  };
335 
337  class Win : public Win_Base {
338  public:
339  using Win_Base::Win_Base;
340  };
341 }
rolmodl::win::Flags::isHighDPI
constexpr bool isHighDPI() const noexcept
Test whether this configuration corresponds to a window with preference for high DPI display....
Definition: Win.hpp:73
rolmodl::Win_Base::maxSize
geom::Size maxSize() noexcept
Get the current maximum window size.
Definition: Win.cpp:169
rolmodl::Win_Base::setMaxSize
void setMaxSize(const geom::Size s) noexcept
Set the maximum window size.
Definition: Win.cpp:174
rolmodl::Win_Base::show
void show() noexcept
Show the window.
Definition: Win.cpp:110
rolmodl::Win_Base::hide
void hide() noexcept
Hide the window.
Definition: Win.cpp:107
rolmodl::BorderSizes
Window decoration (border) sizes snapshot.
Definition: Win.hpp:187
rolmodl::win::Flags::isHidden
constexpr bool isHidden() const noexcept
Test whether this configuration corresponds to a hidden window. The SDL flag equivalent is SDL_WINDOW...
Definition: Win.hpp:57
rolmodl::Win_Base::size
geom::Size size() noexcept
Get the current window size.
Definition: Win.cpp:160
rolmodl::win::Flags::withoutFullscreen
constexpr Flags withoutFullscreen() const noexcept
Create a version of this configuration that corresponds to a window that is not fullscreen....
Definition: Win.hpp:124
rolmodl::win::Flags::withMaximized
constexpr Flags withMaximized() const noexcept
Create a version of this configuration that corresponds to a maximized window. The SDL flag equivalen...
Definition: Win.hpp:92
rolmodl::win::Flags::isOpengl
constexpr bool isOpengl() const noexcept
Test whether this configuration corresponds to a window usable with OpenGL. The SDL flag equivalent i...
Definition: Win.hpp:53
rolmodl::sys::Display
Display information snapshot.
Definition: Base.hpp:258
rolmodl::win::Flags::isBorderless
constexpr bool isBorderless() const noexcept
Test whether this configuration corresponds to a borderless window. The SDL flag equivalent is SDL_WI...
Definition: Win.hpp:61
rolmodl::geom::Size
int dimensions data type. The value is in pixels. Semantically different from rolmodl::geom::Pos.
Definition: Geom.hpp:31
rolmodl::win::Flags::withoutInputGrabbed
constexpr Flags withoutInputGrabbed() const noexcept
Create a version of this configuration that corresponds to a window that has not grabbed input focus....
Definition: Win.hpp:158
rolmodl::Win_Base::maximize
void maximize() noexcept
Maximize the window setting its size to the largest that will fit on screen.
Definition: Win.cpp:117
rolmodl::Win_Base::unsafeRaw
SDL_Window * unsafeRaw() noexcept
Get the underlying SDL_Window*. Unsafe because this value might be nullptr and using it with some SDL...
Definition: Win.cpp:81
rolmodl::win::Flags::withoutHighDPI
constexpr Flags withoutHighDPI() const noexcept
Create a version of this configuration that corresponds to a window with no preference for high DPI d...
Definition: Win.hpp:162
rolmodl::Win
Window class for use with accelerated rendering (rolmodl::Ren).
Definition: Win.hpp:337
rolmodl::Win_Base::unsafeId
uint32_t unsafeId()
Get the underlying SDL window id. Unsafe because this can be used to make a pointer to the underlying...
Definition: Win.cpp:90
rolmodl::win::Flags::withFullscreenDesktop
constexpr Flags withFullscreenDesktop() const noexcept
Create a version of this configuration that corresponds to a desktop-resolution fullscreen window....
Definition: Win.hpp:83
rolmodl::Win_Base::setPos
void setPos(const geom::Pos p) noexcept
Set the window screen position.
Definition: Win.cpp:148
rolmodl::Win_Base::display
sys::Display display()
Get the display information snapshot for the display that contains this window's center.
Definition: Win.cpp:130
rolmodl::BorderSizes::BorderSizes
BorderSizes(Win_Base &w)
Initialize from the current decoration (border) sizes for window w.
Definition: Win.cpp:11
rolmodl::win::Flags::withoutResizable
constexpr Flags withoutResizable() const noexcept
Create a version of this configuration that corresponds to a window that is not resizable....
Definition: Win.hpp:154
rolmodl::win::Flags::withResizable
constexpr Flags withResizable() const noexcept
Create a version of this configuration that corresponds to a resizable window. The SDL flag equivalen...
Definition: Win.hpp:110
rolmodl::win::Flags
Window configuration (flags) container.
Definition: Win.hpp:25
rolmodl::Win_Base::pixelFmt
pixelfmt::Id pixelFmt()
Get the window pixel format.
Definition: Win.cpp:96
rolmodl::Win_Base::operator=
Win_Base & operator=(const Win_Base &that)=delete
Copying rolmodl windows is not allowed because their lifetime is tied to the underlying SDL_Window's....
rolmodl::Win_Base::~Win_Base
~Win_Base() noexcept
Free the underlying SDL_Window.
Definition: Win.cpp:52
rolmodl::BorderSizes::r
int r() const noexcept
Get the recorded right border size.
Definition: Win.cpp:29
rolmodl::win::Flags::withOpengl
constexpr Flags withOpengl() const noexcept
Create a version of this configuration that corresponds to a window usable with OpenGL....
Definition: Win.hpp:98
rolmodl::Win_Base::minSize
geom::Size minSize() noexcept
Get the current minimum window size.
Definition: Win.cpp:178
rolmodl
Main namespace.
Definition: Base.cpp:7
rolmodl::BorderSizes::top
int top() const noexcept
Get the recorded top border size.
Definition: Win.cpp:20
rolmodl::win::Flags::withMinimized
constexpr Flags withMinimized() const noexcept
Create a version of this configuration that corresponds to a minimized window. The SDL flag equivalen...
Definition: Win.hpp:88
rolmodl::win::Flags::Flags
constexpr Flags() noexcept
Create a configuration corresponding to a default window.
Definition: Win.hpp:28
rolmodl::win::Flags::withHighDPI
constexpr Flags withHighDPI() const noexcept
Create a version of this configuration that corresponds to a window with preference for high DPI disp...
Definition: Win.hpp:118
rolmodl::Win_Base::setOpacity
void setOpacity(const float v)
Set the window opacity.
Definition: Win.cpp:139
rolmodl::Win_Base::opacity
float opacity()
Get the window opacity.
Definition: Win.cpp:134
rolmodl::Win_Base::moveMouseIn
void moveMouseIn(const geom::Pos p) noexcept
Move the mouse pointer to a position relative to this window.
Definition: Win.cpp:102
rolmodl::Win_Base::pos
geom::Pos pos() noexcept
Get the window screen position.
Definition: Win.cpp:143
rolmodl::win::Flags::withoutMinimized
constexpr Flags withoutMinimized() const noexcept
Create a version of this configuration that corresponds to a window that is not minimized....
Definition: Win.hpp:133
rolmodl::Win_Base::setMinSize
void setMinSize(const geom::Size s) noexcept
Set the minimum window size.
Definition: Win.cpp:183
rolmodl::Win_Base::minimize
void minimize() noexcept
Minimize the window hiding it in the start menu or dock or an equivalent.
Definition: Win.cpp:120
rolmodl::win::Flags::withoutOpengl
constexpr Flags withoutOpengl() const noexcept
Create a version of this configuration that corresponds to a window that is not usable with OpenGL....
Definition: Win.hpp:142
rolmodl::geom::Pos
int point data type. The value is in pixels. Semantically different from rolmodl::geom::Size.
Definition: Geom.hpp:18
rolmodl::win::Flags::withHidden
constexpr Flags withHidden() const noexcept
Create a version of this configuration that corresponds to a hidden window. The SDL flag equivalent i...
Definition: Win.hpp:102
rolmodl::win::Flags::withoutBorderless
constexpr Flags withoutBorderless() const noexcept
Create a version of this configuration that corresponds to a window that is not borderless....
Definition: Win.hpp:150
rolmodl::win::Flags::isResizable
constexpr bool isResizable() const noexcept
Test whether this configuration corresponds to a resizable window. The SDL flag equivalent is SDL_WIN...
Definition: Win.hpp:65
rolmodl::win::Flags::withoutMaximized
constexpr Flags withoutMaximized() const noexcept
Create a version of this configuration that corresponds to a window that is not maximized....
Definition: Win.hpp:137
rolmodl::Win_Base::unsafeDisplayIndex
unsigned int unsafeDisplayIndex()
Get the index of the display that contains this window's center. Unsafe because storing this index ca...
Definition: Win.cpp:127
rolmodl::Win_Base
Common window class. Use rolmodl::Win for use with accelerated rendering (rolmodl::Ren) and rolmodl::...
Definition: Win.hpp:212
rolmodl::win::Flags::withInputGrabbed
constexpr Flags withInputGrabbed() const noexcept
Create a version of this configuration that corresponds to a window that has grabbed input focus....
Definition: Win.hpp:114
rolmodl::Win_Base::setSize
void setSize(const geom::Size s) noexcept
Set the window size.
Definition: Win.cpp:165
rolmodl::win::Flags::isFullscreen
constexpr bool isFullscreen() const noexcept
Test whether this configuration corresponds to a fullscreen window. The SDL flag equivalent is SDL_WI...
Definition: Win.hpp:34
rolmodl::BorderSizes::bot
int bot() const noexcept
Get the recorded bottom border size.
Definition: Win.cpp:26
rolmodl::Win_Base::setTitle
void setTitle(const char *str) noexcept
Set the window title. str is in UTF-8.
Definition: Win.cpp:155
rolmodl::win::Flags::isInputGrabbed
constexpr bool isInputGrabbed() const noexcept
Test whether this configuration corresponds to a window that has grabbed input focus....
Definition: Win.hpp:69
rolmodl::Win_Base::swap
friend void swap(Win_Base &a, Win_Base &b) noexcept
Exchange the underlying SDL_Window with that.
Definition: Win.cpp:71
rolmodl::win::Flags::withFullscreen
constexpr Flags withFullscreen() const noexcept
Create a version of this configuration that corresponds to a fullscreen window. The SDL flag equivale...
Definition: Win.hpp:79
rolmodl::win::Flags::withoutHidden
constexpr Flags withoutHidden() const noexcept
Create a version of this configuration that corresponds to a window that is not hidden....
Definition: Win.hpp:146
rolmodl::Win_Base::title
const char * title() noexcept
Get the window title in UTF-8. Will be an empty string if there is no title.
Definition: Win.cpp:152
rolmodl::win::Flags::withBorderless
constexpr Flags withBorderless() const noexcept
Create a version of this configuration that corresponds to a borderless window. The SDL flag equivale...
Definition: Win.hpp:106
rolmodl::win::Flags::withoutFullscreenDesktop
constexpr Flags withoutFullscreenDesktop() const noexcept
Create a version of this configuration that corresponds to a window that is not display-resolution fu...
Definition: Win.hpp:128
rolmodl::win::Flags::isMaximized
constexpr bool isMaximized() const noexcept
Test whether this configuration corresponds to a maximized window. The SDL flag equivalent is SDL_WIN...
Definition: Win.hpp:47
rolmodl::win::Flags::isMinimized
constexpr bool isMinimized() const noexcept
Test whether this configuration corresponds to a minimized window. The SDL flag equivalent is SDL_WIN...
Definition: Win.hpp:43
rolmodl::win::Flags::isFullscreenDesktop
constexpr bool isFullscreenDesktop() const noexcept
Test whether this configuration corresponds to a desktop-resolution fullscreen window....
Definition: Win.hpp:38
rolmodl::BorderSizes::l
int l() const noexcept
Get the recorded left border size.
Definition: Win.cpp:23
rolmodl::win::Flags::raw
constexpr uint32_t raw() const noexcept
Return the underlying bitfield representation of this configuration.
Definition: Win.hpp:167
rolmodl::Win_Base::restore
void restore() noexcept
Restore the window to the size and screen position it had before being maximized or minimized.
Definition: Win.cpp:123