candlewick 0.10.0
A tiny cross-platform renderer based on SDL3
Loading...
Searching...
No Matches
Window.h
Go to the documentation of this file.
1#pragma once
2
3#include "Core.h"
4#include "errors.h"
5#include <array>
6#include <SDL3/SDL_gpu.h>
7
8namespace candlewick {
9
11struct Window {
12
14 explicit Window(const char *title, Sint32 w, Sint32 h, SDL_WindowFlags flags);
15
17 explicit Window(SDL_Window *ptr) : _handle(ptr) {}
18
19 Window(const Window &) = delete;
20 Window(Window &&other) noexcept : _handle(other._handle) {
21 other._handle = nullptr;
22 }
23
24 Window &operator=(Window &&other) noexcept {
25 if (this != &other) {
26 this->destroy();
27 _handle = other._handle;
28 other._handle = nullptr;
29 }
30 return *this;
31 }
32
33 operator SDL_Window *() const { return _handle; }
34
35 std::array<int, 2> size() const;
36
37 std::array<int, 2> sizeInPixels() const;
38
39 float pixelDensity() const { return SDL_GetWindowPixelDensity(_handle); }
40
41 float displayScale() const { return SDL_GetWindowDisplayScale(_handle); }
42
43 SDL_PixelFormat pixelFormat() const {
44 return SDL_GetWindowPixelFormat(_handle);
45 }
46
47 SDL_WindowFlags flags() const { return SDL_GetWindowFlags(_handle); }
48
49 bool setTitle(const char *title) {
50 return SDL_SetWindowTitle(_handle, title);
51 }
52
53 std::string_view title() const { return SDL_GetWindowTitle(_handle); }
54
55 void destroy() noexcept {
56 if (_handle)
57 SDL_DestroyWindow(_handle);
58 _handle = nullptr;
59 }
60
61 ~Window() noexcept { this->destroy(); }
62
63private:
64 SDL_Window *_handle;
65};
66
67inline Window::Window(const char *title, Sint32 w, Sint32 h,
68 SDL_WindowFlags flags) {
69 _handle = SDL_CreateWindow(title, w, h, flags);
70 if (!_handle) {
71 throw RAIIException(SDL_GetError());
72 }
73}
74
75inline std::array<int, 2> Window::size() const {
76 int width, height;
77 SDL_GetWindowSize(_handle, &width, &height);
78 return {width, height};
79}
80
81inline std::array<int, 2> Window::sizeInPixels() const {
82 int width, height;
83 SDL_GetWindowSizeInPixels(_handle, &width, &height);
84 return {width, height};
85}
86
87} // namespace candlewick
Definition Camera.h:8
Wrapper for std::runtime_error, which prints out the filename and code line.
Definition errors.h:27
Window(const Window &)=delete
bool setTitle(const char *title)
Definition Window.h:49
float displayScale() const
Definition Window.h:41
void destroy() noexcept
Definition Window.h:55
SDL_WindowFlags flags() const
Definition Window.h:47
std::array< int, 2 > size() const
Definition Window.h:75
SDL_PixelFormat pixelFormat() const
Definition Window.h:43
float pixelDensity() const
Definition Window.h:39
Window(const char *title, Sint32 w, Sint32 h, SDL_WindowFlags flags)
Standard constructor, which forwards to SDL_CreateWindow.
Definition Window.h:67
~Window() noexcept
Definition Window.h:61
std::string_view title() const
Definition Window.h:53
Window & operator=(Window &&other) noexcept
Definition Window.h:24
Window(Window &&other) noexcept
Definition Window.h:20
Window(SDL_Window *ptr)
This constructor takes ownership of the provided handle.
Definition Window.h:17
std::array< int, 2 > sizeInPixels() const
Definition Window.h:81