rolmodl
C++ API for the Simple Directmedia Library 2 (SDL2)
Win.cpp
1 #include "hpp/Win.hpp"
2 
3 #include <utility>
4 #include <cassert>
5 
6 #include "hpp/Base.hpp"
7 
8 namespace rolmodl {
9  using detail::throwOnErr;
10 
12  top_(0),
13  l_(0),
14  bot_(0),
15  r_(0)
16  {
17  throwOnErr(SDL_GetWindowBordersSize(w.unsafeRaw(), &top_, &l_, &bot_, &r_));
18  }
19 
20  int BorderSizes::top() const noexcept {
21  return top_;
22  }
23  int BorderSizes::l() const noexcept {
24  return l_;
25  }
26  int BorderSizes::bot() const noexcept {
27  return bot_;
28  }
29  int BorderSizes::r() const noexcept {
30  return r_;
31  }
32 
33 
34  Win_Base::Win_Base() noexcept :
35  h_(nullptr)
36  {}
37  Win_Base::Win_Base(const char* title, const geom::Pos p, const geom::Size s, const win::Flags flags) :
38  h_(SDL_CreateWindow(title, p.x, p.y, s.w, s.h, flags.raw()))
39  {
40  assert(0 <= s.w); assert(s.w <= 16384);
41  assert(0 <= s.h); assert(s.h <= 16384);
42  if (h_ == nullptr)
43  throw sdlexception();
44  }
45  Win_Base::Win_Base(const char* title, const geom::Size s, const win::Flags flags) :
46  Win_Base(title, geom::Pos{SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED}, s, flags) {}
47  Win_Base::Win_Base(const char* title, const geom::Pos p, const geom::Size s) :
48  Win_Base(title, p, s, win::Flags()) {}
49  Win_Base::Win_Base(const char* title, const geom::Size s) :
50  Win_Base(title, s, win::Flags()) {}
51 
52  Win_Base::~Win_Base() noexcept {
53  if (h_ != nullptr)
54  SDL_DestroyWindow(h_);
55  h_ = nullptr;
56  }
57 
58  // Win_Base::Win_Base(const Win_Base& that) = delete;
59  Win_Base::Win_Base(Win_Base&& that) noexcept :
60  Win_Base()
61  {
62  swap(*this, that);
63  }
64 
65  // Win_Base& Win_Base::operator=(const Win_Base& that) = delete;
67  swap(*this, that);
68  return *this;
69  }
70 
71  void swap(Win_Base& a, Win_Base& b) noexcept {
72  using std::swap;
73  swap(a.h_, b.h_);
74  }
75 
76  // h_ can be nullptr from
77  // the private default constructor
78  // => its a rolmodl bug
79  // SDL
80  // => user should have handled the exception and not tried to use the window
81  SDL_Window* Win_Base::unsafeRaw() noexcept {
82  assert(h_ != nullptr);
83  return h_;
84  }
85  const SDL_Window* Win_Base::unsafeRaw() const noexcept {
86  assert(h_ != nullptr);
87  return h_;
88  }
89 
90  uint32_t Win_Base::unsafeId() {
91  const uint32_t res = SDL_GetWindowID(unsafeRaw());
92  if (res == 0)
93  throw sdlexception();
94  return res;
95  }
96  pixelfmt::Id Win_Base::pixelFmt() {
97  const uint32_t f = SDL_GetWindowPixelFormat(unsafeRaw());
98  if (f == SDL_PIXELFORMAT_UNKNOWN)
99  throw sdlexception();
100  return pixelfmt::id::unsafe::fromSDLEnum(f);
101  }
102  void Win_Base::moveMouseIn(const geom::Pos p) noexcept {
103  SDL_WarpMouseInWindow(unsafeRaw(), p.x, p.y);
104  }
105 
106 
107  void Win_Base::hide() noexcept {
108  SDL_HideWindow(unsafeRaw());
109  }
110  void Win_Base::show() noexcept {
111  SDL_ShowWindow(unsafeRaw());
112  }
113  void Win_Base::raise() noexcept {
114  SDL_RaiseWindow(unsafeRaw());
115  }
116 
117  void Win_Base::maximize() noexcept {
118  SDL_MaximizeWindow(unsafeRaw());
119  }
120  void Win_Base::minimize() noexcept {
121  SDL_MinimizeWindow(unsafeRaw());
122  }
123  void Win_Base::restore() noexcept {
124  SDL_RestoreWindow(unsafeRaw());
125  }
126 
128  return static_cast<unsigned int>(throwOnErr(SDL_GetWindowDisplayIndex(unsafeRaw())));
129  }
132  }
133 
135  float res = 0;
136  throwOnErr(SDL_GetWindowOpacity(unsafeRaw(), &res));
137  return res;
138  }
139  void Win_Base::setOpacity(const float v) {
140  throwOnErr(SDL_SetWindowOpacity(unsafeRaw(), v));
141  }
142 
144  geom::Pos res{};
145  SDL_GetWindowPosition(unsafeRaw(), &res.x, &res.y);
146  return res;
147  }
148  void Win_Base::setPos(const geom::Pos p) noexcept {
149  SDL_SetWindowPosition(unsafeRaw(), p.x, p.y);
150  }
151 
152  const char* Win_Base::title() noexcept {
153  return SDL_GetWindowTitle(unsafeRaw());
154  }
155  void Win_Base::setTitle(const char* str) noexcept {
156  SDL_SetWindowTitle(unsafeRaw(), str);
157  }
158 
159 
161  geom::Size res{};
162  SDL_GetWindowSize(unsafeRaw(), &res.w, &res.h);
163  return res;
164  }
165  void Win_Base::setSize(const geom::Size s) noexcept {
166  SDL_SetWindowSize(unsafeRaw(), s.w, s.h);
167  }
168 
170  geom::Size res{};
171  SDL_GetWindowMaximumSize(unsafeRaw(), &res.w, &res.h);
172  return res;
173  }
174  void Win_Base::setMaxSize(const geom::Size s) noexcept {
175  SDL_SetWindowMaximumSize(unsafeRaw(), s.w, s.h);
176  }
177 
179  geom::Size res{};
180  SDL_GetWindowMinimumSize(unsafeRaw(), &res.w, &res.h);
181  return res;
182  }
183  void Win_Base::setMinSize(const geom::Size s) noexcept {
184  SDL_SetWindowMinimumSize(unsafeRaw(), s.w, s.h);
185  }
186 }
rolmodl::Win_Base::maxSize
geom::Size maxSize() noexcept
Get the current maximum window size.
Definition: Win.cpp:169
rolmodl::detail::throwOnErr
int throwOnErr(const int code)
Throw a rolmodl::sdlexception if code < 0.
Definition: Base.cpp:370
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
utility
rolmodl::Win_Base::hide
void hide() noexcept
Hide the window.
Definition: Win.cpp:107
rolmodl::Win_Base::size
geom::Size size() noexcept
Get the current window size.
Definition: Win.cpp:160
rolmodl::sys::Display
Display information snapshot.
Definition: Base.hpp:258
rolmodl::geom::Size
int dimensions data type. The value is in pixels. Semantically different from rolmodl::geom::Pos.
Definition: Geom.hpp:31
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_Base::raise
void raise() noexcept
Place this window above all other windows and grab input focus.
Definition: Win.cpp:113
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_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
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_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
std::swap
T swap(T... args)
rolmodl::Win_Base::setOpacity
void setOpacity(const float v)
Set the window opacity.
Definition: Win.cpp:139
rolmodl::sdlexception
Exception type containing an error code and the last SDL error at the moment of creation.
Definition: Base.hpp:399
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_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::geom::Pos
int point data type. The value is in pixels. Semantically different from rolmodl::geom::Size.
Definition: Geom.hpp:18
cassert
Win.hpp
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_Base::setSize
void setSize(const geom::Size s) noexcept
Set the window size.
Definition: Win.cpp:165
rolmodl::BorderSizes::bot
int bot() const noexcept
Get the recorded bottom border size.
Definition: Win.cpp:26
rolmodl::sys::display::unsafe::byIndex
Display byIndex(unsigned int i)
Get display information for the ith display. Unsafe because there is no bounds checking on the index.
Definition: Base.cpp:210
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_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
Base.hpp
rolmodl::BorderSizes::l
int l() const noexcept
Get the recorded left border size.
Definition: Win.cpp:23
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