Covellite++  Version: 2.3.1 Revision: ??? Platform: x64 Build: 21:47 08.04.2025
Кроссплатформенный фреймворк для разработки приложений на С++
Загрузка...
Поиск...
Не найдено
Component.hpp
1
2#pragma once
3#include <alicorn/std/string.hpp>
4#include <alicorn/std/string/encoding.hpp>
5#include <alicorn/std/regex.hpp>
6#include <alicorn/std/vector.hpp>
7#include <boost/algorithm/string/replace.hpp>
8#include <Covellite/Api/Component.inl>
9#include <Covellite/Api/Defines.hpp>
10
11namespace covellite::api::renderer
12{
13
29class Component final
30{
31 using String_t = ::alicorn::extension::std::String;
33
34public:
35 class Rasterizer;
36 class Scissor;
37 template<class>
38 class Buffer;
39 class Texture;
40 class Shader;
41 class Shader2;
42 class Transform;
43 class Position;
44 class Rotation;
45 class Scale;
46 class Fog;
47};
48
49class Component::Rasterizer
50{
51public:
52 const String_t CullMode;
53
54public:
55 explicit Rasterizer(Component_t & _Component) :
56 CullMode(_Component[uT("cull")].Default(uT("Back")))
57 {
58
59 }
60 Rasterizer(const Rasterizer &) = delete;
61 Rasterizer(Rasterizer &&) = delete;
62 Rasterizer & operator= (const Rasterizer &) = delete;
63 Rasterizer & operator= (Rasterizer &&) = delete;
64 ~Rasterizer(void) = default;
65};
66
67class Component::Scissor
68{
69public:
70 const int Left;
71 const int Top;
72 const int Right;
73 const int Bottom;
74 const bool IsEnabled;
75 const uint8_t Align[3] = { 0 };
76
77public:
78 explicit Scissor(Component_t & _Component) :
79 Left(_Component[uT("left")].Default(0)),
80 Top(_Component[uT("top")].Default(0)),
81 Right(_Component[uT("right")].Default(0)),
82 Bottom(_Component[uT("bottom")].Default(0)),
83 IsEnabled(_Component[uT("enabled")].Default(false))
84 {
85
86 }
87 Scissor(const Scissor &) = delete;
88 Scissor(Scissor &&) = delete;
89 Scissor & operator= (const Scissor &) = delete;
90 Scissor & operator= (Scissor &&) = delete;
91 ~Scissor(void) = default;
92};
93
94template<class T>
95class Component::Buffer
96{
97 using Buffers_t = ::std::vector<Buffer_t<T>>;
98
99 static const Buffer_t<T> & GetFakeData(void) noexcept
100 {
101 static const Buffer_t<T> FakeData;
102 return FakeData;
103 };
104
105public:
106 const size_t DataCount;
107 const Buffer_t<T> Data;
108 const int Dimension;
109 const uint8_t Align[4] = { 0 };
110
111private:
112 inline static size_t GetDataCount(
113 Component_t & _Component,
114 const Buffer_t<T> & _Data)
115 {
116 return _Component[uT("content")].IsType<Buffers_t>() ?
117 ::std::size((Buffers_t &)_Component[uT("content")].Default(Buffers_t{ _Data })) : 1;
118 }
119
120 inline static Buffer_t<T> GetContent(
121 Component_t & _Component,
122 const Buffer_t<T> & _Data)
123 {
124 if (_Component[uT("content")].IsType<Buffer_t<T>>())
125 {
126 return ::std::move((Buffer_t<T> &)_Component[uT("content")].Default(_Data));
127 }
128
129 using namespace ::alicorn::extension::std;
130
131 const Buffers_t Buffers =
132 ::std::move((Buffers_t &)_Component[uT("content")].Default(Buffers_t{ _Data }));
133
134 Buffer_t<T> AllTextureArrayData;
135 for (const auto & Data : Buffers) AllTextureArrayData += Data;
136
137 return AllTextureArrayData;
138 }
139
140public:
141 explicit Buffer(Component_t & _Component, const Buffer_t<T> & _Data = GetFakeData()) :
142 DataCount(GetDataCount(_Component, _Data)),
143 Data(GetContent(_Component, _Data)),
144 Dimension(_Component[uT("dimension")].Default(3))
145 {
146 }
147 Buffer(const Buffer &) = delete;
148 Buffer(Buffer &&) = delete;
149 Buffer & operator= (const Buffer &) = delete;
150 Buffer & operator= (Buffer &&) = delete;
151 ~Buffer(void) = default;
152};
153
154class Component::Texture :
155 public Buffer<uint8_t>
156{
157public:
158 const uint8_t * const pTextureData; // может быть nullptr
159 const int Width;
160 const int Height;
161 const String_t Name;
162 const String_t Destination;
163 const int Index;
164 const int Capacity;
165 const bool IsUsingMipmapping;
166 const bool IsMapping;
167 // cppcheck-suppress duplInheritedMember
168 const uint8_t Align[6] = { 0 };
169
170public:
171 Texture(Component_t & _Component, const String_t & _DefaultDestination) :
172 Buffer(_Component),
173 pTextureData(Data.empty() ? nullptr : Data.data()),
174 Width(_Component[uT("width")].Default(0)),
175 Height(_Component[uT("height")].Default(0)),
176 Name(_Component[uT("name")].Default(uT("Unknown"))),
177 Destination(_Component[uT("destination")].Default(_DefaultDestination)),
178 Index(_Component[uT("index")].Default(-1)),
179 Capacity(_Component[uT("capacity")].Default(8)),
180 IsUsingMipmapping(_Component[uT("mipmapping")].Default(false)),
181 IsMapping(_Component[uT("mapper")].IsType<const cbBufferMap_t<const void> &>())
182 {
183
184 }
185 Texture(const Texture &) = delete;
186 Texture(Texture &&) = delete;
187 Texture & operator= (const Texture &) = delete;
188 Texture & operator= (Texture &&) = delete;
189 ~Texture(void) = default;
190};
191
192class Component::Shader :
193 public Buffer<uint8_t>
194{
195public:
196 const ::std::string Entry;
197 ::std::string ReturnType;
198 const String_t Kind;
199 const ::std::vector<String_t> Instance;
200
201private:
202 static String_t GetShaderType(
203 const ::std::string & _Entry,
204 const uint8_t * _pBegin,
205 const uint8_t * _pEnd,
206 ::std::string & _ReturnType)
207 {
208 // Определение типа шейдера, функция вернет строку типа параметра
209 // функции точки входа шейдера.
210
211 const auto * pLastBreak = _pBegin;
212
213 while (true)
214 {
215 const auto * pBreak = ::std::find(pLastBreak, _pEnd, '\n');
216 if (pBreak == _pEnd)
217 {
218 throw STD_EXCEPTION << "Entry point not found: " << _Entry;
219 }
220
221 const ::std::string Line{ pLastBreak, pBreak };
222 const auto EntryPosition = Line.find(" " + _Entry + "(", 0);
223 if (EntryPosition != ::std::string::npos)
224 {
225 _ReturnType = Line.substr(0, Line.find(" ", 0));
226
227 const auto TypeBegin = EntryPosition + _Entry.length()
228 + 2; // начальный пробел + скобка после
229 const auto TypeEnd = Line.find(" ", TypeBegin);
230
231 using namespace ::alicorn::extension::std;
232
233 return string_cast<String, Encoding::Ascii128>(
234 Line.substr(TypeBegin, TypeEnd - TypeBegin));
235 }
236
237 pLastBreak = pBreak + 1;
238 }
239 }
240
241 static ::std::vector<String_t> GetInstance(const String_t _Value)
242 {
243 ::std::vector<String_t> Result;
244
245 if (_Value == uT("")) return Result;
246
247 namespace regex = ::alicorn::extension::std::regex;
248
249 regex::Match mInstanceDataStruct{ uT("(?:[if]4)*") };
250 if (!mInstanceDataStruct.IsMatch(_Value))
251 {
252 throw STD_EXCEPTION << "Unexpected instance value: " << _Value;
253 }
254
255 regex::Search sInstanceDataStruct{ uT("(?:([if])4)") };
256 if (!sInstanceDataStruct.IsSearch(_Value)) return Result;
257
258 const auto & Elements = sInstanceDataStruct.GetCoincided();
259
260 for (::std::size_t i = 0; i < Elements.size(); i++)
261 {
262 Result.push_back(Elements[i].second[0]);
263 }
264
265 return Result;
266 };
267
268 ::std::string GetInstanceInput(const ::std::string & _Input) const
269 {
270 static const ::std::string InstanceBlockDeclaration =
271 "/* place for instance variables */";
272 ::std::string InstanceBlockImplementation;
273
274 for (::std::size_t i = 0; i < Instance.size(); i++)
275 {
276 const auto Index = ::std::to_string(i + 1);
277 const auto Type =
278 (Instance[i] == uT("f")) ? "float4" :
279 (Instance[i] == uT("i")) ? "int4" : "";
280
281 InstanceBlockImplementation += ::std::string{ "COVELLITE_IN " } +
282 Type + " iValue" + Index + " COVELLITE_INPUT_SEMANTIC(TEXCOORD" +
283 Index + ");" + char{ 0x5C } + "\r\n";
284 }
285
286 return ::boost::algorithm::replace_first_copy(_Input,
287 InstanceBlockDeclaration, InstanceBlockImplementation);
288 };
289
290public:
291 inline BinaryData_t GetInstanceInput(const BinaryData_t & _Input) const
292 {
293 if (Instance.empty()) return _Input;
294
295 const auto strInput =
296 GetInstanceInput(::std::string{ _Input.cbegin(), _Input.cend() });
297 return BinaryData_t{ strInput.cbegin(), strInput.cend() };
298 }
299
300 ::std::string GetInstanceCopyData(void) const
301 {
302 ::std::string InstanceCopyDataImplementation;
303
304 for (::std::size_t i = 0; i < Instance.size(); i++)
305 {
306 const auto iValue = "iValue" + ::std::to_string(i + 1);
307
308 InstanceCopyDataImplementation +=
309 " InputData." + iValue + " = " + iValue + ";\r\n";
310 }
311
312 return InstanceCopyDataImplementation;
313 }
314
315public:
316 Shader(Component_t & _Component, const BinaryData_t & _Data) :
317 Buffer(_Component, _Data),
318 Entry((const ::std::string &)_Component[uT("entry")].Default("Unknown")),
319 Kind(GetShaderType(Entry, Data.data(), Data.data() + Data.size(), ReturnType)),
320 Instance{ GetInstance(_Component[uT("instance")].Default(uT(""))) }
321 {
322 }
323 Shader(const Shader &) = delete;
324 Shader(Shader &&) = delete;
325 Shader & operator= (const Shader &) = delete;
326 Shader & operator= (Shader &&) = delete;
327 ~Shader(void) = default;
328};
329
330class Component::Transform
331{
332private:
333 inline static size_t GetHashX(void)
334 {
335 static const size_t Hash = ::std::hash<String_t>{}(uT("x"));
336 return Hash;
337 }
338
339 inline static size_t GetHashY(void)
340 {
341 static const size_t Hash = ::std::hash<String_t>{}(uT("y"));
342 return Hash;
343 }
344
345 inline static size_t GetHashZ(void)
346 {
347 static const size_t Hash = ::std::hash<String_t>{}(uT("z"));
348 return Hash;
349 }
350
351public:
352 const float X;
353 const float Y;
354 const float Z;
355
356protected:
357 Transform(Component_t & _Component, const float _DefaultValue) :
358 X(_Component[GetHashX()].Default(_DefaultValue)),
359 Y(_Component[GetHashY()].Default(_DefaultValue)),
360 Z(_Component[GetHashZ()].Default(_DefaultValue))
361 {
362
363 }
364 Transform(const Transform &) = delete;
365 Transform(Transform &&) = delete;
366 Transform & operator= (const Transform &) = delete;
367 Transform & operator= (Transform &&) = delete;
368 ~Transform(void) = default;
369};
370
371class Component::Position :
372 public Transform
373{
374public:
375 explicit Position(Component_t & _Component) :
376 Transform(_Component, 0.0f)
377 {
378
379 }
380 Position(const Position &) = delete;
381 Position(Position &&) = delete;
382 Position & operator= (const Position &) = delete;
383 Position & operator= (Position &&) = delete;
384 ~Position(void) = default;
385};
386
387class Component::Rotation :
388 public Transform
389{
390public:
391 explicit Rotation(Component_t & _Component) :
392 Transform(_Component, 0.0f)
393 {
394
395 }
396 Rotation(const Rotation &) = delete;
397 Rotation(Rotation &&) = delete;
398 Rotation & operator= (const Rotation &) = delete;
399 Rotation & operator= (Rotation &&) = delete;
400 ~Rotation(void) = default;
401};
402
403class Component::Scale :
404 public Transform
405{
406public:
407 explicit Scale(Component_t & _Component) :
408 Transform(_Component, 1.0f)
409 {
410
411 }
412 Scale(const Scale &) = delete;
413 Scale(Scale &&) = delete;
414 Scale & operator= (const Scale &) = delete;
415 Scale & operator= (Scale &&) = delete;
416 ~Scale(void) = default;
417};
418
419class Component::Fog
420{
421public:
422 const uint32_t Color;
423 const float Near;
424 const float Far;
425 const float Density;
426
427public:
428 explicit Fog(Component_t & _Component) :
429 Color(_Component[uT("color")].Default(0xFFFFFFFF)),
430 Near(_Component[uT("near")].Default(10.0f)),
431 Far(_Component[uT("far")].Default(100.0f)),
432 Density(_Component[uT("density")].Default(1.0f))
433 {
434
435 }
436 Fog(const Fog &) = delete;
437 Fog(Fog &&) = delete;
438 Fog & operator= (const Fog &) = delete;
439 Fog & operator= (Fog &&) = delete;
440 ~Fog(void) = default;
441};
442
443} // namespace covellite::api::renderer
Класс входит в проект Covellite.Api Класс компонента.
Definition Component.hpp:35
Класс входит в проект Covellite.Api Вспомогательный класс для извлечения данных компонентов.
Definition Component.hpp:30