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