Covellite++  Version: 2.3.0 Revision: ??? Platform: x64 Build: 23:13 04.01.2025
Кроссплатформенный фреймворк для разработки приложений на С++
Загрузка...
Поиск...
Не найдено
GraphicApi.Constants.hpp
1
2#pragma once
3#include <alicorn/cpp/math.hpp>
4#include <alicorn/std/string.hpp>
5#include <alicorn/std.fast/unordered-map.hpp>
6#include "GraphicApi.hpp"
7
8namespace covellite
9{
10
11namespace api
12{
13
14namespace renderer
15{
16
17class GraphicApi::Constants
18{
19public:
20 template<class>
21 class Data;
22
23private:
24 using CameraId_t = String_t;
25 template<class T>
26 using DataPtr_t = ::std::shared_ptr<Data<T>>;
27
28public:
29 template<class T>
30 T & Get(void);
31 template<class>
32 void Update(void);
33
34private:
35 template<class, template <class> class, class ... TArgs>
36 void Create(TArgs && ...);
37 template<class T>
38 Data<T> & GetBuffer(void);
39 friend GraphicApi;
40
41private:
42 ::alicorn::extension::std::fast::unordered_map<::std::size_t, ::covellite::Any_t> m_Data;
43 CameraId_t m_CurrentCameraId;
44};
45
46template<class T>
47class GraphicApi::Constants::Data
48{
49public:
50 T m_Data;
51
52public:
53 virtual void Update(void) const = 0;
54
55public:
56 Data(void) noexcept { memset(&m_Data, 0, sizeof(m_Data)); }
57 Data(const Data &) = delete;
58 Data(Data &&) = delete;
59 Data & operator= (const Data &) = delete;
60 Data & operator= (Data &&) = delete;
61 virtual ~Data(void) = default;
62};
63
64template<class T, template <class> class TBuffer, class ... TArgs>
65void GraphicApi::Constants::Create(TArgs && ... _Args)
66{
67 static const auto hCode = typeid(T).hash_code();
68
69 const DataPtr_t<T> pBuffer = ::std::make_shared<TBuffer<T>>(_Args ...);
70 m_Data[hCode] = pBuffer;
71}
72
73template<class T>
74inline T & GraphicApi::Constants::Get(void)
75{
76 return GetBuffer<T>().m_Data;
77}
78
79template<class T>
80inline void GraphicApi::Constants::Update(void)
81{
82 GetBuffer<T>().Update();
83}
84
85template<class T>
86auto GraphicApi::Constants::GetBuffer(void) -> Data<T> &
87{
88 static const auto hCode = typeid(T).hash_code();
89
90 const auto itConstant = m_Data.find(hCode);
91 if (itConstant == m_Data.end())
92 {
93 throw STD_EXCEPTION << "Unexpected type: " << typeid(T).name();
94 }
95
96 return *::covellite::any_cast<DataPtr_t<T> &>(itConstant->second);
97}
98
99template<template <class> class TBuffer, class ... TArgs>
100void GraphicApi::MakeConstants(TArgs && ... _Args)
101{
102 m_pConstants = ::std::make_shared<Constants>();
103 m_pConstants->Create<::Camera, TBuffer>(_Args ...);
104 m_pConstants->Create<::Object, TBuffer>(_Args ...);
105}
106
107template<class TColor>
108/*static*/ TColor GraphicApi::ARGBtoFloat4(uint32_t _HexColor)
109{
110 return TColor
111 {
112 ((_HexColor & 0x00FF0000) >> 16) / 255.0f,
113 ((_HexColor & 0x0000FF00) >> 8) / 255.0f,
114 ((_HexColor & 0x000000FF) >> 0) / 255.0f,
115 ((_HexColor & 0xFF000000) >> 24) / 255.0f
116 };
117}
118
119} // namespace renderer
120
121} // namespace api
122
123} // namespace covellite