TTF
This commit is contained in:
@ -5,6 +5,7 @@
|
||||
|
||||
#include "AssetManager.hpp"
|
||||
#include "loaders/TextureLoader.hpp"
|
||||
#include "loaders/TrueTypeLoader.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
@ -53,16 +54,11 @@ template<>
|
||||
std::shared_ptr<Texture> AssetManager::get<Texture>(
|
||||
const std::string filename
|
||||
) {
|
||||
|
||||
auto existing = this->getExisting<TextureLoader>(filename);
|
||||
if(existing) {
|
||||
std::cout << "Found existing texture loader for " << filename << std::endl;
|
||||
|
||||
// Check pointer hasn't gone stale, if it has remove it and create new.
|
||||
auto texture = existing->weakTexture.lock();
|
||||
if(texture) return texture;
|
||||
|
||||
std::cout << "Texture loader for " << filename << " has gone stale." << std::endl;
|
||||
this->removeExisting(filename);
|
||||
}
|
||||
|
||||
@ -73,6 +69,26 @@ std::shared_ptr<Texture> AssetManager::get<Texture>(
|
||||
return loader->sharedTexture;
|
||||
}
|
||||
|
||||
template<>
|
||||
std::shared_ptr<TrueTypeTexture> AssetManager::get<TrueTypeTexture>(
|
||||
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);
|
||||
}
|
||||
|
||||
AssetManager::~AssetManager() {
|
||||
|
||||
}
|
@ -60,9 +60,28 @@ namespace Dawn {
|
||||
*/
|
||||
void update();
|
||||
|
||||
/**
|
||||
* Returns the asset loader for the given asset.
|
||||
*
|
||||
* @param filename The filename of the asset to get.
|
||||
* @return The asset loader for the given asset.
|
||||
*/
|
||||
template<class T>
|
||||
std::shared_ptr<T> get(const std::string filename);
|
||||
|
||||
/**
|
||||
* 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>
|
||||
std::shared_ptr<T> get(
|
||||
const std::string filename,
|
||||
const uint32_t fontSize
|
||||
);
|
||||
|
||||
/**
|
||||
* Dispose the asset manager, and all attached assets.
|
||||
*/
|
||||
|
@ -7,4 +7,5 @@
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
TextureLoader.cpp
|
||||
TrueTypeLoader.cpp
|
||||
)
|
67
src/dawn/asset/loaders/TrueTypeLoader.cpp
Normal file
67
src/dawn/asset/loaders/TrueTypeLoader.cpp
Normal file
@ -0,0 +1,67 @@
|
||||
// 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")
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void TrueTypeLoader::updateSync() {
|
||||
}
|
||||
|
||||
void TrueTypeLoader::updateAsync() {
|
||||
if(state != TrueTypeLoaderState::INITIAL) return;
|
||||
state = TrueTypeLoaderState::ASYNC_LOADING;
|
||||
|
||||
// Load the data.
|
||||
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_Init_FreeType(&fontLibrary);
|
||||
assertTrue(ret == 0, "Failed to initialize FreeType library.");
|
||||
|
||||
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;
|
||||
this->loaded = true;
|
||||
}
|
||||
|
||||
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>(face, fontSize);
|
||||
textures[fontSize] = texture;
|
||||
return texture;
|
||||
}
|
||||
|
||||
TrueTypeLoader::~TrueTypeLoader() {
|
||||
if(buffer != nullptr) {
|
||||
delete[] buffer;
|
||||
buffer = nullptr;
|
||||
}
|
||||
}
|
55
src/dawn/asset/loaders/TrueTypeLoader.hpp
Normal file
55
src/dawn/asset/loaders/TrueTypeLoader.hpp
Normal file
@ -0,0 +1,55 @@
|
||||
// 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
|
||||
};
|
||||
|
||||
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();
|
||||
};
|
||||
}
|
@ -9,14 +9,9 @@
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
TrueTypeTexture::TrueTypeTexture(
|
||||
FT_Face face,
|
||||
uint32_t fontSize,
|
||||
uint8_t style
|
||||
) :
|
||||
TrueTypeTexture::TrueTypeTexture(FT_Face face, uint32_t fontSize) :
|
||||
face(face),
|
||||
fontSize(fontSize),
|
||||
style(style)
|
||||
fontSize(fontSize)
|
||||
{
|
||||
assertTrue(fontSize < 256, "Font size cannot be greater than 256");
|
||||
|
||||
@ -29,8 +24,8 @@ TrueTypeTexture::TrueTypeTexture(
|
||||
|
||||
// Set the texture size
|
||||
texture->setSize(
|
||||
4096,
|
||||
4096,
|
||||
fontSize * 26,
|
||||
fontSize * 26,
|
||||
TextureFormat::R,
|
||||
TextureDataFormat::UNSIGNED_BYTE
|
||||
);
|
||||
|
@ -15,20 +15,15 @@ namespace Dawn {
|
||||
FT_Face face;
|
||||
std::shared_ptr<Texture> texture;
|
||||
uint32_t fontSize;
|
||||
uint8_t style;
|
||||
std::unordered_map<wchar_t, struct TrueTypeCharacter> characterData;
|
||||
|
||||
/**
|
||||
* Construct a new New True Type Face Texture object
|
||||
*
|
||||
* @param face The freetype face object.
|
||||
* @param style Style that this font has, used for locking.
|
||||
* @param fontSize The font size to render at.
|
||||
*/
|
||||
TrueTypeTexture(
|
||||
FT_Face face,
|
||||
uint32_t fontSize,
|
||||
uint8_t style
|
||||
);
|
||||
TrueTypeTexture(FT_Face face, uint32_t fontSize);
|
||||
|
||||
/**
|
||||
* Returns the character data for the given character.
|
||||
|
@ -29,22 +29,19 @@ void Dawn::helloWorldScene(Scene &s) {
|
||||
&face
|
||||
);
|
||||
assertTrue(ret == 0, "Failed to load font face");
|
||||
texture = std::make_shared<TrueTypeTexture>(face, 128, 0);
|
||||
|
||||
auto test = texture->getCharacterData(L'あ');
|
||||
texture = std::make_shared<TrueTypeTexture>(face, 128);
|
||||
|
||||
auto cameraItem = s.createSceneItem();
|
||||
auto camera = cameraItem->addComponent<Camera>();
|
||||
// cameraItem->lookAt({ 5, 5, 5 }, { 0, 0, 0 }, { 0, 1, 0 });
|
||||
cameraItem->lookAt({ 0, 0, 3 }, { 0, 0, 0 }, { 0, 1, 0 });
|
||||
cameraItem->lookAt({ 5, 5, 5 }, { 0, 0, 0 }, { 0, 1, 0 });
|
||||
|
||||
auto quad = s.createSceneItem();
|
||||
auto quadMesh = std::make_shared<Mesh>();
|
||||
quadMesh->createBuffers(QUAD_VERTICE_COUNT, QUAD_INDICE_COUNT);
|
||||
QuadMesh::buffer(
|
||||
quadMesh,
|
||||
glm::vec4(0, 0, test.size.x, test.size.y) / 128.0f,
|
||||
test.quad,
|
||||
glm::vec4(-1, -1, 1, 1),
|
||||
glm::vec4(0, 1, 1, 0),
|
||||
0,
|
||||
0
|
||||
);
|
||||
|
Reference in New Issue
Block a user