9#include "glm/gtc/quaternion.hpp"
10#include "hellfire/graphics/Vertex.h"
11#include "nlohmann/json.hpp"
17 static_assert(std::is_trivially_copyable_v<T>,
"Type must be trivially copyable");
18 out.write(
reinterpret_cast<
const char *>(&val),
sizeof(T));
23 static_assert(std::is_trivially_copyable_v<T>,
"Type must be trivially copyable");
24 in.read(
reinterpret_cast<
char *>(&val),
sizeof(T));
30 static_assert(std::is_trivially_copyable_v<T>,
"Type must be trivially copyable");
31 const uint32_t size =
static_cast<uint32_t>(vec.size());
32 write_binary(out, size);
34 out.write(
reinterpret_cast<
const char *>(vec.data()), size *
sizeof(T));
40 static_assert(std::is_trivially_copyable_v<T>,
"Type must be trivially copyable");
42 if (!read_binary(in, size))
return false;
45 in.read(
reinterpret_cast<
char *>(vec.data()), size *
sizeof(T));
51 const uint32_t length =
static_cast<uint32_t>(str.size());
52 write_binary(out, length);
54 out.write(str.data(), length);
60 if (!read_binary(in, length))
return false;
63 in.read(str.data(), length);
72 out.write(
reinterpret_cast<
const char *>(&v.x),
sizeof(
float) * 2);
76 out.write(
reinterpret_cast<
const char *>(&v.x),
sizeof(
float) * 3);
80 out.write(
reinterpret_cast<
const char *>(&v.x),
sizeof(
float) * 4);
84 out.write(
reinterpret_cast<
const char *>(&q.x),
sizeof(
float) * 4);
88 out.write(
reinterpret_cast<
const char *>(&m[0][0]),
sizeof(
float) * 16);
92 out.write(
reinterpret_cast<
const char *>(&m[0][0]),
sizeof(
float) * 9);
96 in.read(
reinterpret_cast<
char *>(&v.x),
sizeof(
float) * 2);
101 in.read(
reinterpret_cast<
char *>(&v.x),
sizeof(
float) * 3);
106 in.read(
reinterpret_cast<
char *>(&v.x),
sizeof(
float) * 4);
111 in.read(
reinterpret_cast<
char *>(&q.x),
sizeof(
float) * 4);
116 in.read(
reinterpret_cast<
char *>(&m[0][0]),
sizeof(
float) * 16);
121 in.read(
reinterpret_cast<
char *>(&m[0][0]),
sizeof(
float) * 9);
127 write_binary(out, v.position);
128 write_binary(out, v.normal);
129 write_binary(out, v.texCoords);
130 write_binary(out, v.color);
131 write_binary(out, v.tangent);
132 write_binary(out, v.bitangent);
136 if (!read_binary(in, v.position))
return false;
137 if (!read_binary(in, v.normal))
return false;
138 if (!read_binary(in, v.texCoords))
return false;
139 if (!read_binary(in, v.color))
return false;
140 if (!read_binary(in, v.tangent))
return false;
141 if (!read_binary(in, v.bitangent))
return false;
147 const uint32_t size =
static_cast<uint32_t>(vec.size());
148 write_binary(out, size);
149 for (
const auto& v : vec) {
150 write_binary(out, v);
156 if (!read_binary(in, size))
return false;
158 for (
auto& v : vec) {
159 if (!read_binary(in, v))
return false;
166 if (!j.contains(key) || !j[key].is_array() || j[key].size() < 2) {
169 return glm::vec2(j[key][0].get<
float>(), j[key][1].get<
float>());
173 if (!j.contains(key) || !j[key].is_array() || j[key].size() < 3) {
177 j[key][0].get<
float>(),
178 j[key][1].get<
float>(),
179 j[key][2].get<
float>()
184 if (!j.is_array() || j.size() != 3)
return std::nullopt;
186 for (
const auto &elem: j) {
187 if (!elem.is_number())
return std::nullopt;
190 return glm::vec3(j[0].get<
float>(), j[1].get<
float>(), j[2].get<
float>());
194 if (!j.contains(key) || !j[key].is_array() || j[key].size() < 4) {
198 j[key][0].get<
float>(),
199 j[key][1].get<
float>(),
200 j[key][2].get<
float>(),
201 j[key][3].get<
float>()
210 return {v.x, v.y, v.z};
214 return {v.x, v.y, v.z, v.w};
224 inline bool write_header(std::ostream &out, uint32_t magic, uint32_t version) {
225 write_binary(out, magic);
226 write_binary(out, version);
231 uint32_t &out_version) {
233 if (!read_binary(in, magic) || magic != expected_magic) {
237 if (!read_binary(in, out_version) || out_version > max_version) {
247 inline void to_json(nlohmann::json &j,
const vec3 &v) {
251 inline void from_json(
const nlohmann::json &j, vec3 &v) {
252 v = {j[0].get<
float>(), j[1].get<
float>(), j[2].get<
float>()};
void import_all_pending()
Import all unprocessed assets in registry.
Registry for storing assets.
void set_intensity(const float intensity)
float get_attenuation() const
LightType get_light_type() const
void set_range(const float range)
void set_attenuation(const float attenuation)
void set_cast_shadows(const bool cast_shadows)
void set_light_type(const LightType type)
void set_mesh_asset(AssetID id)
const std::string & get_name() const
std::unique_ptr< AssetManager > asset_manager_
const ProjectMetadata & get_metadata() const
ProjectMetadata & get_metadata()
std::filesystem::path get_assets_path() const
std::filesystem::path get_settings_path() const
AssetRegistry * get_asset_registry() const
std::unique_ptr< SceneManager > scene_manager_
std::vector< std::filesystem::path > recent_scenes_
void initialize_default_assets()
static std::unique_ptr< Project > create(const std::string &name, const std::filesystem::path &location)
const std::string & get_version() const
static std::unique_ptr< Project > load_data(const std::filesystem::path &project_file)
std::unique_ptr< Renderer > scene_renderer_
std::unique_ptr< AssetRegistry > asset_registry_
Project(const ProjectMetadata &metadata)
SceneManager * get_scene_manager() const
ProjectMetadata metadata_
void create_directory_structure() const
std::filesystem::path project_root_path_
std::filesystem::path project_file_path_
Project(const std::string &name)
void initialize_managers()
std::filesystem::path get_scenes_path() const
std::filesystem::path get_project_root() const
static std::string get_current_timestamp()
void set_material_asset(AssetID id)
void from_json(const nlohmann::json &j, vec3 &v)
void to_json(nlohmann::json &j, const vec3 &v)
GLM Helper extensions.
bool read_binary_vector(std::istream &in, std::vector< T > &vec)
bool write_header(std::ostream &out, uint32_t magic, uint32_t version)
std::optional< glm::vec4 > json_get_vec4(const nlohmann::json &j, const std::string &key)
bool read_binary_string(std::istream &in, std::string &str)
void write_binary_string(std::ostream &out, const std::string &str)
std::optional< glm::vec2 > json_get_vec2(const nlohmann::json &j, const std::string &key)
JSON Helpers.
bool read_vertex_vector(std::istream &in, std::vector< Vertex > &vec)
bool read_binary(std::istream &in, glm::vec2 &v)
void write_vertex_vector(std::ostream &out, const std::vector< Vertex > &vec)
bool read_binary(std::istream &in, Vertex &v)
nlohmann::json vec3_to_json(const glm::vec3 &v)
void write_binary(std::ostream &out, const glm::vec2 &v)
GLM Binary I/O.
bool read_binary(std::istream &in, T &val)
void write_binary(std::ostream &out, const T &val)
Binary I/O.
bool read_and_validate_header(std::istream &in, uint32_t expected_magic, uint32_t max_version, uint32_t &out_version)
std::optional< glm::vec3 > json_get_vec3(const nlohmann::json &j, const std::string &key)
std::optional< glm::vec3 > json_to_vec3(const nlohmann::json &j)
nlohmann::json vec4_to_json(const glm::vec4 &v)
nlohmann::json vec2_to_json(const glm::vec2 &v)
void write_binary_vector(std::ostream &out, const std::vector< T > &vec)
void write_binary(std::ostream &out, const Vertex &v)
Vertex I/O.
constexpr AssetID INVALID_ASSET_ID