candlewick 0.1.0
A renderer
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 this->~Window();
26 _handle = other._handle;
27 other._handle = nullptr;
28 return *this;
29 }
30
31 operator SDL_Window *() const { return _handle; }
32
33 std::array<int, 2> size() const;
34
35 std::array<int, 2> sizeInPixels() const;
36
37 float pixelDensity() const { return SDL_GetWindowPixelDensity(_handle); }
38
39 float displayScale() const { return SDL_GetWindowDisplayScale(_handle); }
40
41 SDL_PixelFormat pixelFormat() const {
42 return SDL_GetWindowPixelFormat(_handle);
43 }
44
45 SDL_WindowFlags flags() const { return SDL_GetWindowFlags(_handle); }
46
47 bool setTitle(const char *title) {
48 return SDL_SetWindowTitle(_handle, title);
49 }
50
51 std::string_view title() const { return SDL_GetWindowTitle(_handle); }
52
53 void destroy() noexcept {
54 if (_handle)
55 SDL_DestroyWindow(_handle);
56 _handle = nullptr;
57 }
58
59 ~Window() noexcept { this->destroy(); }
60
61private:
62 SDL_Window *_handle;
63};
64
65inline Window::Window(const char *title, Sint32 w, Sint32 h,
66 SDL_WindowFlags flags) {
67 _handle = SDL_CreateWindow(title, w, h, flags);
68 if (!_handle) {
69 throw RAIIException(SDL_GetError());
70 }
71}
72
73inline std::array<int, 2> Window::size() const {
74 int width, height;
75 SDL_GetWindowSize(_handle, &width, &height);
76 return {width, height};
77}
78
79inline std::array<int, 2> Window::sizeInPixels() const {
80 int width, height;
81 if (!SDL_GetWindowSizeInPixels(_handle, &width, &height)) {
82 }
83 return {width, height};
84}
85
86} // namespace candlewick
Definition Camera.h:8
Wrapper for std::runtime_error, which prints out the filename and code line.
Definition errors.h:29
Window(const Window &)=delete
bool setTitle(const char *title)
Definition Window.h:47
float displayScale() const
Definition Window.h:39
void destroy() noexcept
Definition Window.h:53
SDL_WindowFlags flags() const
Definition Window.h:45
std::array< int, 2 > size() const
Definition Window.h:73
SDL_PixelFormat pixelFormat() const
Definition Window.h:41
float pixelDensity() const
Definition Window.h:37
Window(const char *title, Sint32 w, Sint32 h, SDL_WindowFlags flags)
Standard constructor, which forwards to SDL_CreateWindow.
Definition Window.h:65
~Window() noexcept
Definition Window.h:59
std::string_view title() const
Definition Window.h:51
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:79