Files
hdk-sdl/include/hdk/sdl/video/Display.hpp
T
2026-04-26 14:39:01 +00:00

156 lines
7.4 KiB
C++

#pragma once
/**
* @file Display.hpp
* @brief HDK sdl video header only wrapper for the SDL_DisplayID type and related functions
*/
#include <SDL3/SDL_video.h>
#include <hdk/grid/PrimitiveWrapper.hpp>
#include <hdk/sdl/Properties.hpp>
#include <vector>
/** Displays represent physical display devices such as monitors. */
namespace hdk::sdl {
/**
* @brief A wrapper for SDL_DisplayID that provides type safety and convenience functions for working with display IDs
* in SDL.
*
*/
class Display : public grid::PrimitiveWrapper<SDL_DisplayID> {
public:
using grid::PrimitiveWrapper<SDL_DisplayID>::PrimitiveWrapper;
/** @see SDL_GetClosestFullscreenDisplayMode
* https://wiki.libsdl.org/SDL3/SDL_GetClosestFullscreenDisplayMode
* @brief Get the closest match to the requested display mode for fullscreen mode.
* @param w requested width in pixels
* @param h requested height in pixels
* @param refresh_rate 0.0f for desktop refresh rate,
* @param include_high_density_modes whether to include high density display modes in the search
* @param closest A pointer to a display mode to be filled in with the closest match of the available display
* modes. May be NULL to just get the number of matching video modes.
* @return bool true on success
*/
bool GetClosestFullscreenMode(
int w, int h, float refresh_rate, bool include_high_density_modes, SDL_DisplayMode* closest) {
return SDL_GetClosestFullscreenDisplayMode(*this, w, h, refresh_rate, include_high_density_modes, closest);
}
/** @see SDL_GetCurrentDisplayMode
* https://wiki.libsdl.org/SDL3/SDL_GetCurrentDisplayMode
* @brief Get the current display mode of a display.
* @return display mode of the display.
*/
const SDL_DisplayMode* GetCurrentMode() const { return SDL_GetCurrentDisplayMode(*this); }
/** @see SDL_GetCurrentDisplayOrientation
* https://wiki.libsdl.org/SDL3/SDL_GetCurrentDisplayOrientation
* @return orientation of the display
*/
SDL_DisplayOrientation GetCurrentOrientation() const { return SDL_GetCurrentDisplayOrientation(*this); }
/** @see SDL_GetDesktopDisplayMode
* https://wiki.libsdl.org/SDL3/SDL_GetDesktopDisplayMode
* @return desktop display mode of the display
*/
const SDL_DisplayMode* GetDesktopMode() const { return SDL_GetDesktopDisplayMode(*this); }
/** @see SDL_GetDisplayBounds
* https://wiki.libsdl.org/SDL3/SDL_GetDisplayBounds
* @brief Get the bounds of a display.
* @param rect A pointer to an SDL_Rect structure to be filled in with the display
* @return bool true on success
*/
bool GetBounds(SDL_Rect* rect) const { return SDL_GetDisplayBounds(*this, rect); }
/** @see SDL_GetDisplayContentScale
* https://wiki.libsdl.org/SDL3/SDL_GetDisplayContentScale
* @return The content scale factor for the display
*/
float GetContentScale() const { return SDL_GetDisplayContentScale(*this); }
/** @see SDL_GetDisplayForPoint
* https://wiki.libsdl.org/SDL3/SDL_GetDisplayForPoint
* @brief Get the display that contains a point.
* @param point A pointer to an SDL_Point structure representing the point to query
*/
static Display GetForPoint(const SDL_Point* point) { return Display(SDL_GetDisplayForPoint(point)); }
/** @see SDL_GetDisplayForRect
* https://wiki.libsdl.org/SDL3/SDL_GetDisplayForRect
* @brief Get the display that most closely intersects a rectangle.
* @param rect A pointer to an SDL_Rect structure representing the rectangle to query
*/
static Display GetForRect(const SDL_Rect* rect) { return Display(SDL_GetDisplayForRect(rect)); }
/** @see SDL_GetDisplayForWindow
* https://wiki.libsdl.org/SDL3/SDL_GetDisplayForWindow
* @brief Get the display associated with a window.
* @param window The window to query
* @return Display The display associated with the window
*/
static Display GetForWindow(SDL_Window* window) { return Display(SDL_GetDisplayForWindow(window)); }
/** @see SDL_GetDisplayName
* https://wiki.libsdl.org/SDL3/SDL_GetDisplayName
* @return The name of the display.
*/
const char* GetName() const { return SDL_GetDisplayName(*this); }
/** @see SDL_GetDisplayProperties
* https://wiki.libsdl.org/SDL3/SDL_GetDisplayProperties
* @return Properties associated with the display.
*/
Properties GetProperties() const { return Properties(SDL_GetDisplayProperties(*this)); }
/** @see SDL_GetDisplays
* https://wiki.libsdl.org/SDL3/SDL_GetDisplays
* @param count pointer to int filled with the count of displays
* @returns an array of DisplayIDs representing the displays
* @note Must be freed with SDL_free() when finished.
*/
static SDL_DisplayID* GetDisplays(int* count) { return SDL_GetDisplays(count); }
/** @see SDL_GetDisplays
* @returns std::vector Display(s) available
*/
static std::vector<Display> GetDisplays() {
int count;
SDL_DisplayID* displayIDs = SDL_GetDisplays(&count);
std::vector<Display> displays;
if (displayIDs) {
displays.reserve(count);
for (int i = 0; i < count; ++i) {
displays.emplace_back(displayIDs[i]);
}
SDL_free(displayIDs);
}
return displays;
}
/** @see SDL_GetDisplayUsableBounds
* https://wiki.libsdl.org/SDL3/SDL_GetDisplayUsableBounds
* @brief Get the usable bounds of a display.
* @param rect A pointer to an SDL_Rect structure to be filled in with the usable bounds
* @return bool true on success
*/
bool GetUsableBounds(SDL_Rect* rect) const { return SDL_GetDisplayUsableBounds(*this, rect); }
/** @see SDL_GetFullscreenDisplayModes
* https://wiki.libsdl.org/SDL3/SDL_GetFullscreenDisplayModes
* @param count pointer to int filled with the count of display modes
* @return null terminated array of display modes available for fullscreen mode
* @note Must be freed with SDL_free() when finished.
*/
SDL_DisplayMode** GetFullscreenModes(int* count) const { return SDL_GetFullscreenDisplayModes(*this, count); }
/** @see SDL_GetFullscreenDisplayModes
* @returns std::vector of display modes available for fullscreen mode
*/
std::vector<SDL_DisplayMode> GetFullscreenModes() const {
int count;
SDL_DisplayMode** modes = SDL_GetFullscreenDisplayModes(*this, &count);
std::vector<SDL_DisplayMode> displayModes;
if (modes) {
displayModes.reserve(count);
for (int i = 0; i < count; ++i) {
displayModes.push_back(*modes[i]);
}
SDL_free(modes);
}
return displayModes;
}
/** @see SDL_GetNaturalDisplayOrientation
* https://wiki.libsdl.org/SDL3/SDL_GetNaturalDisplayOrientation
* @return The natural orientation of the display
*/
SDL_DisplayOrientation GetNaturalOrientation() const { return SDL_GetNaturalDisplayOrientation(*this); }
/** @see SDL_GetPrimaryDisplay
* https://wiki.libsdl.org/SDL3/SDL_GetPrimaryDisplay
* @return The primary display
*/
static Display GetPrimaryDisplay() { return Display(SDL_GetPrimaryDisplay()); }
};
}