Example scene loading

This commit is contained in:
2024-12-02 14:53:41 -06:00
parent ac0f0e86c5
commit 2af55041c8
48 changed files with 698 additions and 38 deletions

View File

@ -6,4 +6,5 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
String.cpp
JSON.cpp
)

77
src/dawn/util/JSON.cpp Normal file
View File

@ -0,0 +1,77 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "JSON.hpp"
#include "assert/assert.hpp"
using namespace Dawn;
glm::vec3 JSON::vec3(const json &j) {
if(j.type() == json::value_t::array) {
return glm::vec3(j[0].get<float_t>(), j[1].get<float_t>(), j[2].get<float_t>());
} else if(j.type() == json::value_t::object) {
assertTrue(j.contains("x"), "Missing x in vec3");
assertTrue(j.contains("y"), "Missing y in vec3");
assertTrue(j.contains("z"), "Missing z in vec3");
return glm::vec3(
j["x"].get<float_t>(),
j["y"].get<float_t>(),
j["z"].get<float_t>()
);
} else {
assertUnreachable("Invalid JSON type for vec3");
}
}
struct Color JSON::color(const json &j) {
if(j.type() == json::value_t::array) {
return {
j[0].get<float_t>(),
j[1].get<float_t>(),
j[2].get<float_t>(),
j[3].get<float_t>()
};
} else if(j.type() == json::value_t::object) {
float_t r, g, b, a = 1.0f;
if(j.contains("r")) {
r = j["r"].get<float_t>();
} else if(j.contains("red")) {
r = j["red"].get<float_t>();
} else {
assertTrue(j.contains("red"), "Missing red in color");
}
if(j.contains("g")) {
g = j["g"].get<float_t>();
} else if(j.contains("green")) {
g = j["green"].get<float_t>();
} else {
assertTrue(j.contains("green"), "Missing green in color");
}
if(j.contains("b")) {
b = j["b"].get<float_t>();
} else if(j.contains("blue")) {
b = j["blue"].get<float_t>();
} else {
assertTrue(j.contains("blue"), "Missing blue in color");
}
if(j.contains("a")) {
a = j["a"].get<float_t>();
} else if(j.contains("alpha")) {
a = j["alpha"].get<float_t>();
}
return { r, g, b, a };
} else if(j.type() == json::value_t::string) {
return Color::fromString(j.get<std::string>());
} else {
assertUnreachable("Invalid JSON type for color");
}
}

29
src/dawn/util/JSON.hpp Normal file
View File

@ -0,0 +1,29 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawn.hpp"
#include "display/Color.hpp"
namespace Dawn {
class JSON final {
public:
/**
* Parses the given JSON string as a vec3.
*
* @param j JSON obj to parse.
* @return Parsed vec3.
*/
static glm::vec3 vec3(const json &j);
/**
* Parses the given JSON string as a color.
*
* @param j JSON obj to parse.
* @return Parsed color.
*/
static struct Color color(const json &j);
};
}