Add JSON parser
This commit is contained in:
@ -6,6 +6,7 @@
|
||||
#include "AssetManager.hpp"
|
||||
#include "loaders/TextureLoader.hpp"
|
||||
#include "loaders/TrueTypeLoader.hpp"
|
||||
#include "loaders/JSONLoader.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
@ -80,6 +81,20 @@ std::shared_ptr<TrueTypeTexture> AssetManager::get<TrueTypeTexture>(
|
||||
return loader->getTexture(fontSize);
|
||||
}
|
||||
|
||||
template<>
|
||||
std::shared_ptr<json> AssetManager::get<json>(
|
||||
const std::string filename
|
||||
) {
|
||||
auto existing = this->getExisting<JSONLoader>(filename);
|
||||
if(existing) return existing->data;
|
||||
|
||||
std::shared_ptr<JSONLoader> loader = std::make_shared<JSONLoader>(
|
||||
filename
|
||||
);
|
||||
pendingAssetLoaders.push_back(std::static_pointer_cast<AssetLoader>(loader));
|
||||
return loader->data;
|
||||
}
|
||||
|
||||
|
||||
AssetManager::~AssetManager() {
|
||||
|
||||
|
@ -80,6 +80,7 @@ namespace Dawn {
|
||||
* Returns the asset loader for the given asset.
|
||||
*
|
||||
* @param filename The filename of the asset to get.
|
||||
* @param fontSize The font size to get the truetype asset of.
|
||||
* @return The asset loader for the given asset.
|
||||
*/
|
||||
template<class T>
|
||||
|
@ -8,4 +8,5 @@ target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
TextureLoader.cpp
|
||||
TrueTypeLoader.cpp
|
||||
JSONLoader.cpp
|
||||
)
|
46
src/dawn/asset/loaders/JSONLoader.cpp
Normal file
46
src/dawn/asset/loaders/JSONLoader.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright (c) 2024 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "JSONLoader.hpp"
|
||||
#include "assert/assert.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
JSONLoader::JSONLoader(const std::string name) :
|
||||
AssetLoader(name),
|
||||
loader(name + ".json"),
|
||||
state(JSONLoaderLoadState::INITIAL)
|
||||
{
|
||||
data = std::make_shared<json>();
|
||||
}
|
||||
|
||||
void JSONLoader::updateAsync() {
|
||||
if(this->state != JSONLoaderLoadState::INITIAL) return;
|
||||
this->state = JSONLoaderLoadState::LOADING_FILE;
|
||||
|
||||
this->loader.open();
|
||||
auto size = this->loader.getSize();
|
||||
|
||||
auto buffer = new uint8_t[size + 1];
|
||||
assertNotNull(buffer, "Failed to allocate buffer!");
|
||||
|
||||
this->state = JSONLoaderLoadState::PARSING_DATA;
|
||||
auto read = this->loader.read(buffer, size);
|
||||
assertTrue(read == size, "Failed to read entire file!");
|
||||
buffer[size] = '\0';
|
||||
|
||||
*data = json::parse(buffer);
|
||||
delete[] buffer;
|
||||
|
||||
this->state = JSONLoaderLoadState::DONE;
|
||||
this->loaded = true;
|
||||
}
|
||||
|
||||
void JSONLoader::updateSync() {
|
||||
}
|
||||
|
||||
JSONLoader::~JSONLoader() {
|
||||
this->data = nullptr;
|
||||
}
|
42
src/dawn/asset/loaders/JSONLoader.hpp
Normal file
42
src/dawn/asset/loaders/JSONLoader.hpp
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright (c) 2024 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "asset/AssetLoader.hpp"
|
||||
#include "asset/AssetDataLoader.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
enum class JSONLoaderLoadState {
|
||||
INITIAL,
|
||||
LOADING_FILE,
|
||||
PARSING_DATA,
|
||||
DONE
|
||||
};
|
||||
|
||||
class JSONLoader : public AssetLoader {
|
||||
protected:
|
||||
AssetDataLoader loader;
|
||||
enum JSONLoaderLoadState state;
|
||||
|
||||
public:
|
||||
std::shared_ptr<json> data;
|
||||
|
||||
/**
|
||||
* Constructs a JSON asset loader. You should instead use the parent
|
||||
* asset managers' abstracted load method
|
||||
*
|
||||
* @param name File name asset to load, omitting the extension.
|
||||
*/
|
||||
JSONLoader(const std::string name);
|
||||
|
||||
void updateSync() override;
|
||||
void updateAsync() override;
|
||||
|
||||
/**
|
||||
* Dispose / Cleanup the JSON asset.
|
||||
*/
|
||||
~JSONLoader();
|
||||
};
|
||||
}
|
@ -47,4 +47,7 @@ extern "C" {
|
||||
// #include <glm/gtx/intersect.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
#define GLM_ENABLE_EXPERIMENTAL 1
|
||||
#include <glm/gtx/matrix_decompose.hpp>
|
||||
#include <glm/gtx/matrix_decompose.hpp>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
using json = nlohmann::json;
|
Reference in New Issue
Block a user