GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
Display.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_GRAPHICS_DISPLAY_HPP
5#define GREM_GRAPHICS_DISPLAY_HPP
6
8
13#include <GREM/core/extents.hpp>
15
16#include <iterator> // std::forward_iterator_tag
17
18namespace grem::graphics {
19
20class Window; // Forward declaration, to avoid a circular include of Window.hpp.
21class Display; // Forward declaration.
22
26class DisplayMode {
27public:
34 [[nodiscard]] Extent2D getSize() const noexcept {
35 return size;
36 }
37
45 [[nodiscard]] float getPixelDensity() const noexcept {
46 return pixelDensity;
47 }
48
55 [[nodiscard]] float getRefreshRate() const noexcept {
56 return refreshRate;
57 }
58
65 [[nodiscard]] GREM_API(graphics) int getBitsPerPixel() const noexcept;
66
67private:
68 friend Display;
69 friend Window;
70
71 constexpr DisplayMode(uint32_t displayID, uint32_t format, Extent2D size, float pixelDensity, float refreshRate, int refreshRateNumerator, int refreshRateDenominator,
72 void* internal) noexcept
73 : displayID(displayID)
74 , format(format)
75 , size(size)
76 , pixelDensity(pixelDensity)
77 , refreshRate(refreshRate)
78 , refreshRateNumerator(refreshRateNumerator)
79 , refreshRateDenominator(refreshRateDenominator)
80 , internal(internal) {}
81
82 uint32_t displayID;
83 uint32_t format;
84 Extent2D size;
85 float pixelDensity;
86 float refreshRate;
87 int refreshRateNumerator;
88 int refreshRateDenominator;
89 void* internal;
90};
91
95class Display {
96private:
97 struct DisplaySentinel;
98 class DisplayIterator;
99 class DisplayRange;
100 struct DisplayModeSentinel;
102 class DisplayModeRange;
103
104public:
105 enum class Orientation : int32_t { // NOLINT(performance-enum-size)
111 };
112
121 [[nodiscard]] GREM_API(graphics) static DisplayRange getAll();
122
131 [[nodiscard]] GREM_API(graphics) static Display getPrimary();
132
141 [[nodiscard]] GREM_API(graphics) static Optional<Display> findByID(uint32_t id);
142
152 [[nodiscard]] GREM_API(graphics) CStringView getName() const;
153
163 [[nodiscard]] GREM_API(graphics) Region2D getBounds() const;
164
175 [[nodiscard]] GREM_API(graphics) Region2D getUsableBounds() const;
176
183 [[nodiscard]] GREM_API(graphics) Orientation getOrientation() const noexcept;
184
193 [[nodiscard]] GREM_API(graphics) DisplayMode getDisplayMode() const;
194
204 [[nodiscard]] GREM_API(graphics) float getContentScale() const;
205
214 [[nodiscard]] GREM_API(graphics) DisplayMode getDesktopDisplayMode() const;
215
226
232 [[nodiscard]] GREM_API(graphics) uint32_t getID() const noexcept;
233
234private:
235 friend Window;
236
237 [[nodiscard]] static constexpr DisplayMode createDisplayMode(uint32_t displayID, uint32_t format, Extent2D size, float pixelDensity, float refreshRate,
238 int refreshRateNumerator, int refreshRateDenominator, void* internal) noexcept {
239 return DisplayMode{displayID, format, size, pixelDensity, refreshRate, refreshRateNumerator, refreshRateDenominator, internal};
240 }
241
242 constexpr explicit Display(uint32_t displayID) noexcept
243 : displayID(displayID) {}
244
245 uint32_t displayID;
246};
247
249
251public:
252 using difference_type = ptrdiff_t;
253 using value_type = Display;
254 using reference = const value_type&;
255 using pointer = const value_type*;
256 using iterator_category = std::forward_iterator_tag;
258
259 DisplayIterator() noexcept = default;
260
261 [[nodiscard]] bool operator==(const DisplayIterator& other) const {
262 return p == other.p;
263 }
264
265 [[nodiscard]] bool operator==(sentinel) const {
266 return display.displayID == 0;
267 }
268
269 [[nodiscard]] reference operator*() const {
270 GREM_ASSERT(p && *p != 0);
271 return display;
272 }
273
274 [[nodiscard]] pointer operator->() const {
275 return &**this;
276 }
277
279 GREM_ASSERT(p);
280 ++p;
281 display = Display{*p};
282 return *this;
283 }
284
286 DisplayIterator old = *this;
287 ++*this;
288 return old;
289 }
290
291private:
292 friend DisplayRange;
293
294 constexpr explicit DisplayIterator(uint32_t* p) noexcept
295 : p(p)
296 , display(*p) {}
297
298 uint32_t* p = nullptr;
299 Display display{0};
300};
301
303public:
304 using value_type = Display;
305 using size_type = size_t;
314
315 [[nodiscard]] iterator begin() const noexcept {
316 return iterator{displayIDs.get()};
317 }
318
319 [[nodiscard]] sentinel end() const noexcept {
320 return sentinel{};
321 }
322
323 [[nodiscard]] size_type size() const noexcept {
324 return static_cast<size_type>(displayCount);
325 }
326
327 [[nodiscard]] bool empty() const noexcept {
328 return displayCount == 0;
329 }
330
331private:
332 friend Display;
333
334 struct DisplayIDsDeleter {
335 GREM_API(graphics) void operator()(uint32_t* displayIDs) const noexcept;
336 };
337
338 constexpr DisplayRange(uint32_t* displayIDs, int displayCount) noexcept
339 : displayIDs(displayIDs)
340 , displayCount(displayCount) {}
341
343 int displayCount;
344};
345
347
349public:
350 using difference_type = ptrdiff_t;
352 using reference = const value_type&;
353 using pointer = const value_type*;
354 using iterator_category = std::forward_iterator_tag;
356
357 DisplayModeIterator() noexcept = default;
358
359 [[nodiscard]] bool operator==(const DisplayModeIterator& other) const {
360 return p == other.p;
361 }
362
363 [[nodiscard]] bool operator==(sentinel) const {
364 GREM_ASSERT(p);
365 return *p == nullptr;
366 }
367
368 [[nodiscard]] reference operator*() const {
369 GREM_ASSERT(p && *p != nullptr);
370 return displayMode;
371 }
372
373 [[nodiscard]] pointer operator->() const {
374 return &**this;
375 }
376
378
380 DisplayModeIterator old = *this;
381 ++*this;
382 return old;
383 }
384
385private:
386 friend DisplayModeRange;
387
388 DisplayModeIterator(void** p);
389
390 void** p = nullptr;
391 DisplayMode displayMode = createDisplayMode(0, 0, {}, 0.0f, 0.0f, 0, 0, nullptr);
392};
393
395public:
397 using size_type = size_t;
406
407 [[nodiscard]] iterator begin() const noexcept {
408 return iterator{displayModes.get()};
409 }
410
411 [[nodiscard]] sentinel end() const noexcept {
412 return sentinel{};
413 }
414
415 [[nodiscard]] size_type size() const noexcept {
416 return static_cast<size_type>(displayModeCount);
417 }
418
419 [[nodiscard]] bool empty() const noexcept {
420 return displayModeCount == 0;
421 }
422
423private:
424 friend Display;
425
426 struct DisplayModesDeleter {
427 GREM_API(graphics) void operator()(void** displayModes) const noexcept;
428 };
429
430 constexpr DisplayModeRange(void** displayModes, int displayModeCount) noexcept
431 : displayModes(displayModes)
432 , displayModeCount(displayModeCount) {}
433
435 int displayModeCount;
436};
437
438} // namespace grem::graphics
439
440#endif
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
Generic nullable RAII resource handle with exclusive ownership of a resource that is automatically de...
Definition UniqueHandle.hpp:26
Information about a specific display mode available to a Display.
Definition Display.hpp:26
Extent2D getSize() const noexcept
Get the size of the display mode.
Definition Display.hpp:34
float getPixelDensity() const noexcept
Get the pixel density of the display mode.
Definition Display.hpp:45
int getBitsPerPixel() const noexcept
Get the color depth of the display mode.
float getRefreshRate() const noexcept
Get the refresh rate of the display mode.
Definition Display.hpp:55
bool operator==(sentinel) const
Definition Display.hpp:265
Display value_type
Definition Display.hpp:253
const value_type * pointer
Definition Display.hpp:255
DisplayIterator operator++(int)
Definition Display.hpp:285
std::forward_iterator_tag iterator_category
Definition Display.hpp:256
ptrdiff_t difference_type
Definition Display.hpp:252
const value_type & reference
Definition Display.hpp:254
DisplayIterator & operator++()
Definition Display.hpp:278
pointer operator->() const
Definition Display.hpp:274
reference operator*() const
Definition Display.hpp:269
DisplaySentinel sentinel
Definition Display.hpp:257
pointer operator->() const
Definition Display.hpp:373
DisplayModeSentinel sentinel
Definition Display.hpp:355
std::forward_iterator_tag iterator_category
Definition Display.hpp:354
DisplayMode value_type
Definition Display.hpp:351
const value_type * pointer
Definition Display.hpp:353
DisplayModeIterator operator++(int)
Definition Display.hpp:379
ptrdiff_t difference_type
Definition Display.hpp:350
bool operator==(sentinel) const
Definition Display.hpp:363
const value_type & reference
Definition Display.hpp:352
reference operator*() const
Definition Display.hpp:368
bool empty() const noexcept
Definition Display.hpp:419
pointer const_pointer
Definition Display.hpp:402
DisplayModeSentinel sentinel
Definition Display.hpp:405
DisplayMode value_type
Definition Display.hpp:396
reference const_reference
Definition Display.hpp:400
DisplayModeIterator::reference reference
Definition Display.hpp:399
size_t size_type
Definition Display.hpp:397
DisplayModeIterator iterator
Definition Display.hpp:403
iterator const_iterator
Definition Display.hpp:404
size_type size() const noexcept
Definition Display.hpp:415
sentinel end() const noexcept
Definition Display.hpp:411
iterator begin() const noexcept
Definition Display.hpp:407
DisplayModeIterator::difference_type difference_type
Definition Display.hpp:398
Definition Display.hpp:302
DisplaySentinel sentinel
Definition Display.hpp:313
size_type size() const noexcept
Definition Display.hpp:323
DisplayIterator::reference reference
Definition Display.hpp:307
iterator begin() const noexcept
Definition Display.hpp:315
size_t size_type
Definition Display.hpp:305
bool empty() const noexcept
Definition Display.hpp:327
sentinel end() const noexcept
Definition Display.hpp:319
Display value_type
Definition Display.hpp:304
pointer const_pointer
Definition Display.hpp:310
iterator const_iterator
Definition Display.hpp:312
DisplayIterator iterator
Definition Display.hpp:311
DisplayIterator::difference_type difference_type
Definition Display.hpp:306
reference const_reference
Definition Display.hpp:308
Information about a display on the host system.
Definition Display.hpp:95
DisplayModeRange getAvailableFullscreenDisplayModes() const
Get a list of all available fulscreen display modes for this display.
CStringView getName() const
Get the manufacturer-specified name of the display.
DisplayMode getDesktopDisplayMode() const
Get the display mode of the full desktop.
uint32_t getID() const noexcept
Get the unique identifier of this display.
float getContentScale() const
Get the expected scale for content based on the DPI settings of the display.
Region2D getBounds() const
Get the bounds of the display region on the desktop.
static Display getPrimary()
Get the current primary display.
static Optional< Display > findByID(uint32_t id)
Get the display with a specific identifier.
DisplayMode getDisplayMode() const
Get the current display mode of this display.
Orientation
Definition Display.hpp:105
@ LANDSCAPE
Landscape mode, rotated counter-clockwise relative to portrait mode.
Definition Display.hpp:107
@ UNKNOWN
Unknown display orientation.
Definition Display.hpp:106
@ PORTRAIT
Portrait mode.
Definition Display.hpp:109
@ LANDSCAPE_FLIPPED
Landscape mode, rotated clockwise relative to portrait mode.
Definition Display.hpp:108
@ PORTRAIT_FLIPPED
Portrait mode, upside down.
Definition Display.hpp:110
Orientation getOrientation() const noexcept
Get the current orientation of the display.
static DisplayRange getAll()
Get a list of all available displays.
Region2D getUsableBounds() const
Get the bounds of the usable display region on the desktop, which may exclude areas reserved by the s...
Graphical window that can be rendered to.
Definition Window.hpp:131
Definition concepts.hpp:106
Definition buffer_layouts.hpp:20
Extent< 2 > Extent2D
Size of a 2D data region.
Definition extents.hpp:31
Region< 2 > Region2D
Rectangular region of 2D data.
Definition extents.hpp:819
CStringViewBase< char, std::char_traits< char > > CStringView
Definition CStringView.hpp:635