Photon Engine 2.0.0-beta
A physically based renderer.
Loading...
Searching...
No Matches
TSdl.ipp
Go to the documentation of this file.
1#pragma once
2
3#include "SDL/TSdl.h"
4#include "SDL/ISdlResource.h"
7#include "SDL/sdl_helpers.h"
8
9#include <Common/assertion.h>
10#include <Common/logging.h>
11
12#include <utility>
13
14namespace ph
15{
16
17inline std::shared_ptr<ISdlResource> TSdl<void>::makeResource(const SdlClass* clazz)
18{
19 if(!clazz)
20 {
21 return nullptr;
22 }
23
24 // Creates an uninitialized resource
25 std::shared_ptr<ISdlResource> resource = clazz->createResource();
26
27 // Could be empty due to `T` being abstract or being defined to be uninstantiable
28 if(!resource)
29 {
30 PH_DEFAULT_LOG(Warning,
31 "default resource creation failed, {} could be abstract or defined to be uninstantiable",
32 clazz->genPrettyName());
33 return nullptr;
34 }
35
36 clazz->initDefaultResource(*resource);
37 return resource;
38}
39
40template<CSdlResource T>
42{
43 return sdl::category_of<T>();
44}
45
46template<CSdlResource T>
47inline std::shared_ptr<T> TSdl<T>::makeResource()
48{
49 static_assert(CHasSdlClassDefinition<T>,
50 "No SDL class definition found. Did you call PH_DEFINE_SDL_CLASS() in the body of type T?");
51
52 const SdlClass* clazz = T::getSdlClass();
53 PH_ASSERT(clazz);
54
55 std::shared_ptr<ISdlResource> resource = TSdl<>::makeResource(clazz);
56 if(!resource)
57 {
58 return nullptr;
59 }
60
61 // Obtain typed resource. This dynamic cast also guard against the case where
62 // `T` might not actually have SDL class defined locally but inherited (i.e., the resource
63 // created was in fact not of type `T`).
64 std::shared_ptr<T> typedResource = std::dynamic_pointer_cast<T>(std::move(resource));
65 if(!typedResource)
66 {
67 PH_DEFAULT_LOG(Warning,
68 "default resource creation failed, {} may not have SDL class defined",
69 clazz->genPrettyName());
70 }
71 return typedResource;
72}
73
74template<CSdlResource T>
75template<typename... DeducedArgs>
76inline T TSdl<T>::make(DeducedArgs&&... args)
77{
78 static_assert(std::is_constructible_v<T, DeducedArgs...>,
79 "SDL class type T is not constructible using the specified arguments.");
80
81 T instance(std::forward<DeducedArgs>(args)...);
82 if constexpr(CHasSdlClassDefinition<T>)
83 {
84 const SdlClass* clazz = T::getSdlClass();
85 PH_ASSERT(clazz);
86
87 clazz->initDefaultResource(instance);
88 }
90 {
92 "SDL struct definition of T does not support initializing to default values");
93
94 const auto* ztruct = T::getSdlStruct();
95 PH_ASSERT(ztruct);
96
97 ztruct->initDefaultStruct(instance);
98 }
99 else
100 {
102 "No SDL class/struct definition found. Did you call "
103 "PH_DEFINE_SDL_CLASS()/PH_DEFINE_SDL_STRUCT() in the body of type T?");
104 }
105
106 return instance;
107}
108
109template<CSdlResource T>
110inline std::shared_ptr<T> TSdl<T>::loadResource(const Path& file)
111{
112 if constexpr(CHasSdlClassDefinition<T>)
113 {
114 auto loadedResource = detail::load_single_resource(T::getSdlClass(), file);
115#if PH_DEBUG
116 if(loadedResource)
117 {
118 PH_ASSERT(std::dynamic_pointer_cast<T>(loadedResource));
119 }
120#endif
121
122 // Static cast is enough here as the result is guaranteed to have the specified
123 // class if non-null.
124 return std::static_pointer_cast<T>(loadedResource);
125 }
126 else
127 {
128 PH_STATIC_ASSERT_DEPENDENT_FALSE(T,
129 "Cannot load resource without SDL class definition. Did you call PH_DEFINE_SDL_CLASS() "
130 "in the body of type T?");
131 }
132}
133
134template<CSdlResource T>
135inline void TSdl<T>::saveResource(const std::shared_ptr<T>& resource, const Path& file)
136{
137 detail::save_single_resource(resource, file);
138}
139
140}// end namespace ph
SDL instance helpers.
General path representation. Does not check whether the target actually exists (e....
Definition Path.h:21
Definition SdlClass.h:25
std::string genPrettyName() const
Definition SdlClass.cpp:27
virtual std::shared_ptr< ISdlResource > createResource() const =0
virtual void initDefaultResource(ISdlResource &resource) const =0
Initialize a resource to default values. Default values are defined by the resource class's SDL defin...
Definition TSdl.h:32
Whether T is a well-defined SDL class.
Definition sdl_traits.h:29
Whether T is a well-defined SDL struct.
Definition sdl_traits.h:36
Definition sdl_traits.h:61
std::shared_ptr< ISdlResource > load_single_resource(const SdlClass *resourceClass, const Path &file)
Definition TSdl.cpp:20
void save_single_resource(const std::shared_ptr< ISdlResource > &resource, const Path &file)
Definition TSdl.cpp:65
constexpr ESdlTypeCategory category_of()
Statically gets the SDL category of T.
Definition sdl_helpers.ipp:409
The root for all renderer implementations.
Definition EEngineProject.h:6
ESdlTypeCategory
Definition ESdlTypeCategory.h:15
Low-level helpers for SDL. Helpers are in an additional sdl namespace.