Wow! I can't believe I haven't started this project again!

This commit is contained in:
2024-11-25 15:59:50 -06:00
parent a02e87c3fa
commit f8c008fd45
47 changed files with 177 additions and 845 deletions

View File

@@ -67,6 +67,35 @@ AssetDataLoader::AssetDataLoader(std::string fileName) : fileName(fileName) {
);
}
size_t AssetDataLoader::getSize() {
assertTrue(this->assetArchiveEntry != nullptr, "AssetDataLoader::getSize: Entry is NULL!");
assertTrue(archive_entry_size_is_set(assetArchiveEntry), "assetGetSize: Entry size is not set!");
return archive_entry_size(assetArchiveEntry);
}
size_t AssetDataLoader::getPosition() {
assertNotNull(this->assetArchiveFile, "AssetDataLoader::getPosition: File is not open!");
return this->position;
}
std::string AssetDataLoader::getEntireContentsAsString() {
if(!this->isOpen()) {
this->open();
} else {
this->rewind();
}
std::string buffer;
buffer.resize(this->getSize());
this->read((uint8_t*)buffer.data(), buffer.size());
this->close();
return buffer;
}
bool_t AssetDataLoader::isOpen() {
return this->assetArchive != nullptr;
}
void AssetDataLoader::open() {
assertNull(this->assetArchiveFile, "AssetDataLoader::open: File is already open");
assertNull(this->assetArchive, "AssetDataLoader::open: Archive is already open");
@@ -156,12 +185,6 @@ size_t AssetDataLoader::readUntil(
return i;
}
size_t AssetDataLoader::getSize() {
assertTrue(this->assetArchiveEntry != nullptr, "AssetDataLoader::getSize: Entry is NULL!");
assertTrue(archive_entry_size_is_set(assetArchiveEntry), "assetGetSize: Entry size is not set!");
return archive_entry_size(assetArchiveEntry);
}
size_t AssetDataLoader::skip(size_t n) {
assertTrue(n >= 0, "AssetDataLoader::skip: Byte count must be greater than 0.");
@@ -185,16 +208,14 @@ size_t AssetDataLoader::setPosition(const size_t position) {
}
void AssetDataLoader::rewind() {
assertTrue(this->isOpen(), "Asset is not open!");
if(this->position == 0) return;
// TODO: See if I can optimize this
this->close();
this->open();
}
size_t AssetDataLoader::getPosition() {
assertNotNull(this->assetArchiveFile, "AssetDataLoader::getPosition: File is not open!");
return this->position;
}
AssetDataLoader::~AssetDataLoader() {
if(this->assetArchiveFile != nullptr) this->close();
}

View File

@@ -89,6 +89,33 @@ namespace Dawn {
* @param fileName File name of the asset that is to be loaded.
*/
AssetDataLoader(std::string filename);
/**
* Get the size of the asset.
* @return The size of the asset in bytes.
*/
size_t getSize();
/**
* Returns the current position of the read head.
*
* @return The current read head position.
*/
size_t getPosition();
/**
* Get the entire contents of the asset as a string.
*
* @return The entire contents of the asset as a string.
*/
std::string getEntireContentsAsString();
/**
* Check if the asset is open.
*
* @return True if the asset is open, otherwise false.
*/
bool_t isOpen();
/**
* Platform-centric method to open a file buffer to an asset.
@@ -123,12 +150,6 @@ namespace Dawn {
const size_t maxSize,
const char_t delimiter
);
/**
* Get the size of the asset.
* @return The size of the asset in bytes.
*/
size_t getSize();
/**
* Skips the read head forward to a given position.
@@ -151,13 +172,6 @@ namespace Dawn {
*/
size_t setPosition(const size_t absolutePosition);
/**
* Returns the current position of the read head.
*
* @return The current read head position.
*/
size_t getPosition();
/**
* Cleanup the asset loader.
*/

View File

@@ -92,11 +92,24 @@ namespace Dawn {
* @param fontSize The font size to get the truetype asset of.
* @return The asset loader for the given asset.
*/
template<class T>
std::shared_ptr<T> get(
const std::string filename,
const uint32_t fontSize
);
// std::shared_ptr<TrueTypeTexture> get(
// const std::string filename,
// const uint32_t fontSize
// ) {
// auto existing = this->getExisting<TrueTypeLoader>(filename);
// if(existing) {
// // Check pointer hasn't gone stale, if it has remove it and create new.
// auto texture = existing->getTexture(fontSize);
// if(texture) return texture;
// this->removeExisting(filename);
// }
// std::shared_ptr<TrueTypeLoader> loader = std::make_shared<TrueTypeLoader>(
// filename
// );
// pendingAssetLoaders.push_back(std::static_pointer_cast<AssetLoader>(loader));
// return loader->getTexture(fontSize);
// }
/**
* Dispose the asset manager, and all attached assets.

View File

@@ -7,4 +7,6 @@
target_sources(${DAWN_TARGET_NAME}
PRIVATE
TextureLoader.cpp
JSONLoader.cpp
TrueTypeLoader.cpp
)

View File

@@ -0,0 +1,33 @@
// Copyright (c) 2024 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "JSONLoader.hpp"
using namespace Dawn;
JSONLoader::JSONLoader(const std::string name) :
AssetLoader(name),
loader(name),
state(JSONLoaderState::INITIAL)
{
}
void JSONLoader::updateAsync() {
if(this->state != JSONLoaderState::INITIAL) return;
this->state = JSONLoaderState::LOADING_JSON;
std::string jsonContents = loader.getEntireContentsAsString();
this->data = json::parse(jsonContents);
this->state = JSONLoaderState::DONE;
this->loaded = true;
}
void JSONLoader::updateSync() {
}
JSONLoader::~JSONLoader() {
}

View File

@@ -0,0 +1,33 @@
// 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"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
namespace Dawn {
enum class JSONLoaderState {
INITIAL,
LOADING_JSON,
DONE
};
class JSONLoader : public AssetLoader {
protected:
AssetDataLoader loader;
enum JSONLoaderState state;
public:
json data;
JSONLoader(const std::string name);
void updateSync() override;
void updateAsync() override;
~JSONLoader();
};
}

View File

@@ -0,0 +1,97 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TrueTypeLoader.hpp"
#include "assert/assert.hpp"
using namespace Dawn;
TrueTypeLoader::TrueTypeLoader(const std::string name) :
AssetLoader(name),
loader(name + ".ttf")
{
// Init the font.
auto ret = FT_Init_FreeType(&fontLibrary);
assertTrue(ret == 0, "Failed to initialize FreeType library.");
}
void TrueTypeLoader::updateSync() {
if(state != TrueTypeLoaderState::ASYNC_DONE) return;
state = TrueTypeLoaderState::SYNC_LOADING;
// Init all the textures.
auto it = textures.begin();
while(it != textures.end()) {
auto texture = it->second.lock();
if(texture) {
texture->setFace(face);
it++;
continue;
}
it = textures.erase(it);
}
// Done
state = TrueTypeLoaderState::SYNC_DONE;
this->loaded = true;
}
void TrueTypeLoader::updateAsync() {
if(state != TrueTypeLoaderState::INITIAL) return;
state = TrueTypeLoaderState::ASYNC_LOADING;
// Load the data.
this->loader.open();
size_t size = loader.getSize();
buffer = new uint8_t[size];
// Read the data.
size_t readSize = loader.read(buffer, size);
assertTrue(readSize == size, "Failed to read all data from TrueTypeLoader.");
// Init the font.
auto ret = FT_New_Memory_Face(fontLibrary, buffer, size, 0, &face);
assertTrue(ret == 0, "Failed to load font face.");
// Now close the asset loader
loader.close();
state = TrueTypeLoaderState::ASYNC_DONE;
}
std::shared_ptr<TrueTypeTexture> TrueTypeLoader::getTexture(
const uint32_t fontSize
) {
// Check if we have the texture already and it hasn't gone stale.
auto it = textures.find(fontSize);
if(it != textures.end()) {
if(!it->second.expired()) return it->second.lock();
textures.erase(it);
}
// Create the texture.
auto texture = std::make_shared<TrueTypeTexture>(fontSize);
textures[fontSize] = texture;
if(this->loaded) texture->setFace(face);
return texture;
}
TrueTypeLoader::~TrueTypeLoader() {
if(
this->state == TrueTypeLoaderState::SYNC_DONE ||
this->state == TrueTypeLoaderState::SYNC_LOADING ||
this->state == TrueTypeLoaderState::ASYNC_DONE
) {
FT_Done_Face(face);
}
FT_Done_FreeType(fontLibrary);
if(buffer != nullptr) {
delete[] buffer;
buffer = nullptr;
}
}

View File

@@ -0,0 +1,57 @@
// Copyright (c) 2023 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"
#include "display/font/TrueTypeTexture.hpp"
namespace Dawn {
enum class TrueTypeLoaderState {
INITIAL,
ASYNC_LOADING,
ASYNC_DONE,
SYNC_LOADING,
SYNC_DONE
};
class TrueTypeLoader : public AssetLoader {
protected:
FT_Library fontLibrary;
FT_Face face;
AssetDataLoader loader;
std::unordered_map<uint32_t, std::weak_ptr<TrueTypeTexture>> textures;
enum TrueTypeLoaderState state = TrueTypeLoaderState::INITIAL;
uint8_t *buffer = nullptr;
public:
/**
* Constructs a TrueTypeLoader. You should instead use the parent
* asset managers' abstracted load method
*
* @param name File name asset to load, omitting the extension.
*/
TrueTypeLoader(const std::string name);
void updateSync() override;
void updateAsync() override;
/**
* Returns the texture for the given font size.
*
* @param fontSize Font size to get the texture for.
* @return Texture for the given character.
*/
std::shared_ptr<TrueTypeTexture> getTexture(
const uint32_t fontSize
);
/**
* Dispose / Cleanup the truetype asset. Will also dispose the underlying
* truetype itself.
*/
~TrueTypeLoader();
};
}