SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_metal.h
1#ifndef SDL3PP_METAL_H_
2#define SDL3PP_METAL_H_
3
4#include <SDL3/SDL_metal.h>
5#include "SDL3pp_video.h"
6
7namespace SDL {
8
21// Forward decl
22struct MetalView;
23
25using MetalViewRaw = SDL_MetalView;
26
27// Forward decl
28struct MetalViewRef;
29
32{
34
37 : value(value)
38 {
39 }
40
42 constexpr MetalViewParam(std::nullptr_t = nullptr)
43 : value(0)
44 {
45 }
46
48 constexpr explicit operator bool() const { return !!value; }
49
51 constexpr auto operator<=>(const MetalViewParam& other) const = default;
52
54 constexpr operator MetalViewRaw() const { return value; }
55};
56
65{
66 MetalViewRaw m_resource = 0;
67
68public:
70 constexpr MetalView(std::nullptr_t = nullptr) noexcept
71 : m_resource(0)
72 {
73 }
74
82 constexpr explicit MetalView(const MetalViewRaw resource) noexcept
83 : m_resource(resource)
84 {
85 }
86
87protected:
89 constexpr MetalView(const MetalView& other) noexcept = default;
90
91public:
93 constexpr MetalView(MetalView&& other) noexcept
94 : MetalView(other.release())
95 {
96 }
97
98 constexpr MetalView(const MetalViewRef& other) = delete;
99
100 constexpr MetalView(MetalViewRef&& other) = delete;
101
121 : m_resource(SDL_Metal_CreateView(window))
122 {
123 }
124
126 ~MetalView() { SDL_Metal_DestroyView(m_resource); }
127
129 constexpr MetalView& operator=(MetalView&& other) noexcept
130 {
131 std::swap(m_resource, other.m_resource);
132 return *this;
133 }
134
135protected:
137 constexpr MetalView& operator=(const MetalView& other) noexcept = default;
138
139public:
141 constexpr MetalViewRaw get() const noexcept { return m_resource; }
142
144 constexpr MetalViewRaw release() noexcept
145 {
146 auto r = m_resource;
147 m_resource = 0;
148 return r;
149 }
150
152 constexpr auto operator<=>(const MetalView& other) const noexcept = default;
153
155 constexpr explicit operator bool() const noexcept { return !!m_resource; }
156
158 constexpr operator MetalViewParam() const noexcept { return {m_resource}; }
159
170 void Destroy();
171
179 void* GetLayer();
180};
181
184{
186
194 MetalViewRef(MetalViewParam resource) noexcept
195 : MetalView(resource.value)
196 {
197 }
198
206 MetalViewRef(MetalViewRaw resource) noexcept
207 : MetalView(resource)
208 {
209 }
210
212 constexpr MetalViewRef(const MetalViewRef& other) noexcept = default;
213
216};
217
237{
238 return MetalView(window);
239}
240
254{
255 SDL_Metal_DestroyView(view);
256}
257
259
269{
270 return SDL_Metal_GetLayer(view);
271}
272
273inline void* MetalView::GetLayer() { return SDL::Metal_GetLayer(m_resource); }
274
276
277} // namespace SDL
278
279#endif /* SDL3PP_METAL_H_ */
A handle to a CAMetalLayer-backed NSView (macOS) or UIView (iOS/tvOS).
Definition: SDL3pp_metal.h:65
constexpr MetalView(MetalView &&other) noexcept
Move constructor.
Definition: SDL3pp_metal.h:93
constexpr MetalView(const MetalViewRaw resource) noexcept
Constructs from MetalViewParam.
Definition: SDL3pp_metal.h:82
constexpr MetalView(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_metal.h:70
constexpr MetalView & operator=(MetalView &&other) noexcept
Assignment operator.
Definition: SDL3pp_metal.h:129
constexpr MetalViewRaw release() noexcept
Retrieves underlying MetalViewRaw and clear this.
Definition: SDL3pp_metal.h:144
~MetalView()
Destructor.
Definition: SDL3pp_metal.h:126
constexpr MetalViewRaw get() const noexcept
Retrieves underlying MetalViewRaw.
Definition: SDL3pp_metal.h:141
constexpr auto operator<=>(const MetalView &other) const noexcept=default
Comparison.
constexpr MetalView(const MetalView &other) noexcept=default
Copy constructor.
constexpr MetalView & operator=(const MetalView &other) noexcept=default
Assignment operator.
MetalView(WindowParam window)
Create a CAMetalLayer-backed NSView/UIView and attach it to the specified window.
Definition: SDL3pp_metal.h:120
void * Metal_GetLayer(MetalViewParam view)
Get a pointer to the backing CAMetalLayer for the given view.
Definition: SDL3pp_metal.h:268
MetalView Metal_CreateView(WindowParam window)
Create a CAMetalLayer-backed NSView/UIView and attach it to the specified window.
Definition: SDL3pp_metal.h:236
void Destroy()
Destroy an existing MetalView object.
Definition: SDL3pp_metal.h:258
void * GetLayer()
Get a pointer to the backing CAMetalLayer for the given view.
Definition: SDL3pp_metal.h:273
SDL_MetalView MetalViewRaw
Alias to raw representation for MetalView.
Definition: SDL3pp_metal.h:25
void Metal_DestroyView(MetalViewRaw view)
Destroy an existing MetalView object.
Definition: SDL3pp_metal.h:253
Main include header for the SDL3pp library.
Safely wrap MetalView for non owning parameters.
Definition: SDL3pp_metal.h:32
constexpr MetalViewParam(MetalViewRaw value)
Constructs from MetalViewRaw.
Definition: SDL3pp_metal.h:36
constexpr MetalViewParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_metal.h:42
MetalViewRaw value
parameter's MetalViewRaw
Definition: SDL3pp_metal.h:33
constexpr auto operator<=>(const MetalViewParam &other) const =default
Comparison.
Semi-safe reference for MetalView.
Definition: SDL3pp_metal.h:184
constexpr MetalViewRef(const MetalViewRef &other) noexcept=default
Copy constructor.
MetalViewRef(MetalViewRaw resource) noexcept
Constructs from MetalViewParam.
Definition: SDL3pp_metal.h:206
MetalViewRef(MetalViewParam resource) noexcept
Constructs from MetalViewParam.
Definition: SDL3pp_metal.h:194
~MetalViewRef()
Destructor.
Definition: SDL3pp_metal.h:215
Safely wrap Window for non owning parameters.
Definition: SDL3pp_video.h:54