rolmodl
C++ API for the Simple Directmedia Library 2 (SDL2)
Base.hpp
Go to the documentation of this file.
1 #pragma once
2 
11 
12 #include "forwarddecl/Base.hpp"
13 
14 #include "forwarddecl/Event.hpp"
15 
16 #include "Geom.hpp"
17 #include "PixelFmt.hpp"
18 
20 namespace rolmodl {
22  namespace sys {
28  enum class Id {
29  AIX,
30  android,
31  BSDI,
32  dreamcast,
33  emscripten,
34  freeBSD,
35  haiku,
36  HPUX,
37  irix,
38  linux,
39  MiNT,
40  macOSClassic,
41  macOSX,
42  NaCl,
43  netBSD,
44  openBSD,
45  OS2,
46  OSF,
47  QNXNeutrino,
48  RISCOS,
49  solaris,
50  windows,
51  winRT,
52  tvOS,
53  iOS,
54  PSP,
55  unknown
56  };
57 
60  const char* name() noexcept;
62  Id id() noexcept;
63 
66  unsigned int ram() noexcept;
69  unsigned int logicalCores() noexcept;
71  namespace cpu {
74  unsigned int l1Size() noexcept;
75 
76  // todo: put in a namespace/rename?
81  bool has3DNow() noexcept;
86  bool hasAVX() noexcept;
91  bool hasAVX2() noexcept;
96  bool hasAltiVec() noexcept;
101  bool hasMMX() noexcept;
106  bool hasRDTSC() noexcept;
111  bool hasSSE() noexcept;
116  bool hasSSE2() noexcept;
121  bool hasSSE3() noexcept;
126  bool hasSSE41() noexcept;
131  bool hasSSE42() noexcept;
132  }
133 
135  namespace pwr {
138  enum class State {
139  onBattery,
140  noBattery,
141  charging,
142  charged,
143  unknown
144  };
145 
151  struct Status {
152  public:
154  Status() noexcept;
155 
157  State state() const noexcept;
159  std::optional<int> time() const noexcept;
161  std::optional<int> percentage() const noexcept;
162 
163  private:
164  State state_;
165  std::optional<int> time_, percentage_;
166  };
168  Status status() noexcept;
169  }
170 
172  namespace clipboard {
175  bool hasText() noexcept;
178  SDLString getText();
181  void setText(const char* x);
182  }
183 
185  namespace screensaver {
188  void enable() noexcept;
191  void disable() noexcept;
194  bool enabled() noexcept;
195  }
196 
198  namespace driver {
201  unsigned int count();
207  const char* name(const unsigned int i);
210  std::optional<const char*> current() noexcept;
211  }
212 
216  namespace display {
218  namespace unsafe {}
219  }
220  namespace display::unsafe {
222  Display byIndex(unsigned int i);
223 
226  unsigned int count();
233  const char* name(const unsigned int i);
234 
241  geom::RectWH bounds(const unsigned int i);
248  geom::RectWH usableBounds(const unsigned int i);
249  }
250 
258  struct Display {
259  public:
263  const char* name() const noexcept;
264 
267  geom::RectWH bounds() const noexcept;
270  geom::RectWH usableBounds() const noexcept;
271 
274  float ddpi() const noexcept;
277  float hdpi() const noexcept;
280  float vdpi() const noexcept;
281 
282  private:
283  explicit Display(const unsigned int i);
284 
285  const char* name_;
286  geom::RectWH bounds_, usableBounds_;
287 
288  float ddpi_, hdpi_, vdpi_;
289 
291  friend Display display::unsafe::byIndex(unsigned int i);
292  };
293 
297  namespace display {
301  namespace mode {
303  namespace unsafe {}
304  }
305  }
306  namespace display::mode::unsafe {
312  unsigned int countForDisplayN(unsigned int n);
315  DisplayMode desktopForDisplayN(unsigned int n);
318  DisplayMode currentForDisplayN(unsigned int n);
321  DisplayMode forDisplayNByIndexI(unsigned int n, unsigned int i);
322 
331  DisplayMode closestForDisplayN(unsigned int n, const DisplayMode ideal);
332  }
333 
336  struct DisplayMode {
337  public:
339  pixelfmt::Id fmt;
345  void* unsafeDriverData = nullptr;
346  private:
347  };
348  }
349 
355  struct RGB {
356  public:
357  uint8_t r, g, b;
358 
360  constexpr RGB() :
361  r(0), g(0), b(0)
362  {}
364  constexpr RGB(const uint8_t arg_r, const uint8_t arg_g, const uint8_t arg_b) :
365  r(arg_r), g(arg_g), b(arg_b)
366  {}
367 
368  private:
369  };
370 
377  struct RGBA : public RGB {
378  public:
379  uint8_t a;
380 
382  constexpr RGBA() :
383  RGB(), a(255)
384  {}
386  constexpr RGBA(const uint8_t arg_r, const uint8_t arg_g, const uint8_t arg_b) :
387  RGB(arg_r, arg_g, arg_b), a(255)
388  {}
390  constexpr RGBA(const uint8_t arg_r, const uint8_t arg_g, const uint8_t arg_b, const uint8_t arg_a) :
391  RGB(arg_r, arg_g, arg_b), a(arg_a)
392  {}
393 
394  private:
395  };
396 
399  struct sdlexception : public std::exception {
400  public:
403  explicit sdlexception(const int code) noexcept;
406  sdlexception() noexcept;
408  int code() const noexcept;
411  const char* what() const noexcept override;
412 
413  private:
414  int code_;
415  const char* msg_;
416  };
417 
419  namespace detail {
421  int throwOnErr(const int code);
422  }
423 
425  class SDLString {
426  public:
428  ~SDLString() noexcept;
429 
431  SDLString(const SDLString& that);
433  SDLString(SDLString&& that) noexcept;
434 
436  SDLString& operator=(const SDLString& that);
438  SDLString& operator=(SDLString&& that) noexcept;
439 
443  friend std::optional<Event> event::poll() noexcept;
445  friend void swap(SDLString& a, SDLString& b) noexcept;
446 
448  char* unsafeRaw() noexcept;
450  const char* unsafeRaw() const noexcept;
451 
452  private:
453  SDLString() noexcept;
454  explicit SDLString(char* x) noexcept;
455 
456  char* raw_;
457  };
458 }
rolmodl::sys::DisplayMode::refreshRate
std::optional< unsigned int > refreshRate
Display mode refresh rate, might be unspecified.
Definition: Base.hpp:343
rolmodl::sys::DisplayMode::size
geom::Size size
Display mode size.
Definition: Base.hpp:341
rolmodl::sys::DisplayMode::fmt
pixelfmt::Id fmt
Display mode format.
Definition: Base.hpp:339
rolmodl::detail::throwOnErr
int throwOnErr(const int code)
Throw a rolmodl::sdlexception if code < 0.
Definition: Base.cpp:370
rolmodl::RGBA::RGBA
constexpr RGBA(const uint8_t arg_r, const uint8_t arg_g, const uint8_t arg_b, const uint8_t arg_a)
Initialize with the given red, green, blue, and alpha values.
Definition: Base.hpp:390
PixelFmt.hpp
std::exception
rolmodl::RGB::RGB
constexpr RGB(const uint8_t arg_r, const uint8_t arg_g, const uint8_t arg_b)
Initialize with the given red, green, and blue values.
Definition: Base.hpp:364
rolmodl::sys::name
const char * name() noexcept
Human-readable OS name. Use rolmodl::sys::id() for OS identification purposes instead of this method.
Definition: Base.cpp:9
rolmodl::sys::logicalCores
unsigned int logicalCores() noexcept
Get the number of available logical CPU cores.
Definition: Base.cpp:76
rolmodl::RGBA
RGBA color type. Has an alpha component.
Definition: Base.hpp:377
rolmodl::sys::Display::name
const char * name() const noexcept
Get recorded display name.
Definition: Base.cpp:261
rolmodl::sys::Display
Display information snapshot.
Definition: Base.hpp:258
rolmodl::sys::display::unsafe::name
const char * name(const unsigned int i)
Get the name of the ith display. Sample output: XF250Q for my Acer XF250Q. Unsafe because there is no...
Definition: Base.cpp:221
rolmodl::sys::Display::ddpi
float ddpi() const noexcept
Get recorded display diagonal DPI (dots per inch).
Definition: Base.cpp:272
rolmodl::geom::Size
int dimensions data type. The value is in pixels. Semantically different from rolmodl::geom::Pos.
Definition: Geom.hpp:31
Geom.hpp
rolmodl::SDLString
Container for SDL-owned strings that must be freed with SDL_free.
Definition: Base.hpp:425
rolmodl::RGB
RGB color type. No alpha component.
Definition: Base.hpp:355
rolmodl::sys::Display::vdpi
float vdpi() const noexcept
Get recorded display vertical DPI (dots per inch).
Definition: Base.cpp:278
rolmodl::sys::clipboard::getText
SDLString getText()
Get clipboard text contents.
Definition: Base.cpp:164
rolmodl::sys::display::mode::unsafe::currentForDisplayN
DisplayMode currentForDisplayN(unsigned int n)
Get the current display mode for the nth display. Unsafe because there is no bounds checking on the i...
Definition: Base.cpp:313
rolmodl::sys::DisplayMode
Display mode parameter plain old struct.
Definition: Base.hpp:336
rolmodl::sys::Display::usableBounds
geom::RectWH usableBounds() const noexcept
Get recorded display usable bounds. Excludes system decorations such as the menu bar and the dock.
Definition: Base.cpp:268
rolmodl::sys::ram
unsigned int ram() noexcept
Get the amount of system RAM in MB.
Definition: Base.cpp:73
rolmodl::sys::display::unsafe::count
unsigned int count()
Get the number of displays. Unsafe because the return value is unreliable and may lead to out-of-boun...
Definition: Base.cpp:214
rolmodl::geom::RectWH
Rectangle represented by its top left corner coordinates, width, and height.
Definition: Geom.hpp:59
rolmodl::sys::display::unsafe::bounds
geom::RectWH bounds(const unsigned int i)
Get the resolution of the ith display. Unsafe because there is no bounds checking on the index.
Definition: Base.cpp:227
rolmodl::RGBA::RGBA
constexpr RGBA()
Initialize as the default pure opaque black color: RGBA(0, 0, 0, 255) = #000000ff.
Definition: Base.hpp:382
rolmodl::sys::display::unsafe::usableBounds
geom::RectWH usableBounds(const unsigned int i)
Get the bounds of the usable space of the ith display. Excludes system decorations such as the menu b...
Definition: Base.cpp:236
rolmodl::sys::display::mode::unsafe::desktopForDisplayN
DisplayMode desktopForDisplayN(unsigned int n)
Get the display mode used by the system desktop for the nth display. Unsafe because there is no bound...
Definition: Base.cpp:304
rolmodl::sys::pwr::Status
System power status snapshot.
Definition: Base.hpp:151
std::nullopt
T nullopt
rolmodl::sys::pwr::State
State
Indicates battery status a.k.a. the system power state.
Definition: Base.hpp:138
rolmodl::sys::DisplayMode::unsafeDriverData
void * unsafeDriverData
Unspecified driver-specific data.
Definition: Base.hpp:345
rolmodl
Main namespace.
Definition: Base.cpp:7
rolmodl::sys::display::mode::unsafe::countForDisplayN
unsigned int countForDisplayN(unsigned int n)
Get the amount of display modes available for the nth display.
Definition: Base.cpp:284
rolmodl::sdlexception
Exception type containing an error code and the last SDL error at the moment of creation.
Definition: Base.hpp:399
rolmodl::RGBA::RGBA
constexpr RGBA(const uint8_t arg_r, const uint8_t arg_g, const uint8_t arg_b)
Initialize with the given red, green, blue values and the default alpha of 255.
Definition: Base.hpp:386
rolmodl::sys::Id
Id
OS type enum.
Definition: Base.hpp:28
std::optional< int >
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::sys::display::mode::unsafe::closestForDisplayN
DisplayMode closestForDisplayN(unsigned int n, const DisplayMode ideal)
Get the display mode closest to the one specified for the nth display. Unsafe because there is no bou...
Definition: Base.cpp:335
rolmodl::sys::Display::bounds
geom::RectWH bounds() const noexcept
Get recorded display resolution.
Definition: Base.cpp:265
rolmodl::RGB::RGB
constexpr RGB()
Initialize as the default pure black color: RGB(0, 0, 0) = #000000.
Definition: Base.hpp:360
rolmodl::sys::Display::hdpi
float hdpi() const noexcept
Get recorded display horizontal DPI (dots per inch).
Definition: Base.cpp:275
rolmodl::sys::display::mode::unsafe::forDisplayNByIndexI
DisplayMode forDisplayNByIndexI(unsigned int n, unsigned int i)
Get the ith display mode for the nth display. Unsafe because there is no bounds checking on either in...
Definition: Base.cpp:322