Trying ttf loading
This commit is contained in:
@@ -40,24 +40,14 @@ Asset::~Asset()
|
||||
{
|
||||
}
|
||||
|
||||
std::ifstream &Asset::getReadStream()
|
||||
std::ifstream Asset::getReadStream() const
|
||||
{
|
||||
if(inStream.is_open())
|
||||
{
|
||||
return inStream;
|
||||
}
|
||||
inStream.open(fullPath);
|
||||
return inStream;
|
||||
return std::ifstream(fullPath);
|
||||
}
|
||||
|
||||
std::ofstream &Asset::getWriteStream()
|
||||
std::ofstream Asset::getWriteStream() const
|
||||
{
|
||||
if(outStream.is_open())
|
||||
{
|
||||
return outStream;
|
||||
}
|
||||
outStream.open(fullPath);
|
||||
return outStream;
|
||||
return std::ofstream(fullPath);
|
||||
}
|
||||
|
||||
std::string Asset::getFileName() const
|
||||
|
||||
@@ -38,8 +38,8 @@ public:
|
||||
}
|
||||
protected:
|
||||
std::mutex lock;
|
||||
std::ifstream& getReadStream();
|
||||
std::ofstream& getWriteStream();
|
||||
std::ifstream getReadStream() const;
|
||||
std::ofstream getWriteStream() const;
|
||||
private:
|
||||
// Path relative to the project root
|
||||
std::filesystem::path fullPath;
|
||||
@@ -48,8 +48,6 @@ private:
|
||||
std::filesystem::path extension;
|
||||
Status status;
|
||||
uint32 byteSize;
|
||||
std::ifstream inStream;
|
||||
std::ofstream outStream;
|
||||
};
|
||||
DEFINE_REF(Asset)
|
||||
} // namespace Seele
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "AssetRegistry.h"
|
||||
#include "MeshAsset.h"
|
||||
#include "FontAsset.h"
|
||||
#include "TextureAsset.h"
|
||||
#include "FontLoader.h"
|
||||
#include "TextureLoader.h"
|
||||
#include "MaterialLoader.h"
|
||||
#include "MeshLoader.h"
|
||||
@@ -37,6 +39,10 @@ void AssetRegistry::importFile(const std::string &filePath)
|
||||
{
|
||||
get().importTexture(fsPath);
|
||||
}
|
||||
if(extension.compare(".ttf") == 0)
|
||||
{
|
||||
get().importFont(fsPath);
|
||||
}
|
||||
if (extension.compare(".asset") == 0)
|
||||
{
|
||||
get().importMaterial(fsPath);
|
||||
@@ -55,6 +61,11 @@ PTextureAsset AssetRegistry::findTexture(const std::string &filePath)
|
||||
return get().textures[filePath];
|
||||
}
|
||||
|
||||
PFontAsset AssetRegistry::findFont(const std::string& name)
|
||||
{
|
||||
return get().fonts[name];
|
||||
}
|
||||
|
||||
PMaterialAsset AssetRegistry::findMaterial(const std::string &filePath)
|
||||
{
|
||||
return get().materials[filePath];
|
||||
@@ -84,6 +95,7 @@ void AssetRegistry::init(const std::filesystem::path &rootFolder, Gfx::PGraphics
|
||||
{
|
||||
AssetRegistry ® = get();
|
||||
reg.rootFolder = rootFolder;
|
||||
reg.fontLoader = new FontLoader(graphics);
|
||||
reg.meshLoader = new MeshLoader(graphics);
|
||||
reg.textureLoader = new TextureLoader(graphics);
|
||||
reg.materialLoader = new MaterialLoader(graphics);
|
||||
@@ -104,6 +116,11 @@ void AssetRegistry::importTexture(const std::filesystem::path &filePath)
|
||||
textureLoader->importAsset(filePath);
|
||||
}
|
||||
|
||||
void AssetRegistry::importFont(const std::filesystem::path& filePath)
|
||||
{
|
||||
fontLoader->importAsset(filePath);
|
||||
}
|
||||
|
||||
void AssetRegistry::importMaterial(const std::filesystem::path &filePath)
|
||||
{
|
||||
materialLoader->importAsset(filePath);
|
||||
@@ -131,6 +148,11 @@ void AssetRegistry::registerTexture(PTextureAsset texture)
|
||||
textures[texture->getFileName()] = texture;
|
||||
}
|
||||
|
||||
void AssetRegistry::registerFont(PFontAsset font)
|
||||
{
|
||||
fonts[font->getFileName()] = font;
|
||||
}
|
||||
|
||||
void AssetRegistry::registerMaterial(PMaterialAsset material)
|
||||
{
|
||||
materials[material->getFileName()] = material;
|
||||
|
||||
@@ -7,9 +7,11 @@
|
||||
namespace Seele
|
||||
{
|
||||
DECLARE_REF(TextureLoader)
|
||||
DECLARE_REF(FontLoader)
|
||||
DECLARE_REF(MeshLoader)
|
||||
DECLARE_REF(MaterialLoader)
|
||||
DECLARE_REF(TextureAsset)
|
||||
DECLARE_REF(FontAsset)
|
||||
DECLARE_REF(MeshAsset)
|
||||
DECLARE_REF(MaterialAsset)
|
||||
DECLARE_NAME_REF(Gfx, Graphics)
|
||||
@@ -25,6 +27,7 @@ public:
|
||||
|
||||
static PMeshAsset findMesh(const std::string& filePath);
|
||||
static PTextureAsset findTexture(const std::string& filePath);
|
||||
static PFontAsset findFont(const std::string& name);
|
||||
static PMaterialAsset findMaterial(const std::string& filePath);
|
||||
|
||||
static std::ofstream createWriteStream(const std::string& relativePath, std::ios_base::openmode openmode = std::ios::out);
|
||||
@@ -37,10 +40,12 @@ private:
|
||||
|
||||
void importMesh(const std::filesystem::path& filePath);
|
||||
void importTexture(const std::filesystem::path& filePath);
|
||||
void importFont(const std::filesystem::path& filePath);
|
||||
void importMaterial(const std::filesystem::path& filePath);
|
||||
|
||||
void registerMesh(PMeshAsset mesh);
|
||||
void registerTexture(PTextureAsset texture);
|
||||
void registerFont(PFontAsset font);
|
||||
void registerMaterial(PMaterialAsset material);
|
||||
|
||||
std::ofstream internalCreateWriteStream(const std::string& relativePath, std::ios_base::openmode openmode = std::ios::out);
|
||||
@@ -49,12 +54,15 @@ private:
|
||||
std::filesystem::path rootFolder;
|
||||
//Todo: Seele::Map doesn't really work with strings for some reason, so just use std::map for now
|
||||
std::map<std::string, PTextureAsset> textures;
|
||||
std::map<std::string, PFontAsset> fonts;
|
||||
std::map<std::string, PMeshAsset> meshes;
|
||||
std::map<std::string, PMaterialAsset> materials;
|
||||
UPTextureLoader textureLoader;
|
||||
UPFontLoader fontLoader;
|
||||
UPMeshLoader meshLoader;
|
||||
UPMaterialLoader materialLoader;
|
||||
friend class TextureLoader;
|
||||
friend class FontLoader;
|
||||
friend class MaterialLoader;
|
||||
friend class MeshLoader;
|
||||
};
|
||||
|
||||
@@ -4,6 +4,10 @@ target_sources(Engine
|
||||
Asset.cpp
|
||||
AssetRegistry.h
|
||||
AssetRegistry.cpp
|
||||
FontAsset.h
|
||||
FontAsset.cpp
|
||||
FontLoader.h
|
||||
FontLoader.cpp
|
||||
MaterialLoader.h
|
||||
MaterialLoader.cpp
|
||||
MeshAsset.h
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
#include "FontAsset.h"
|
||||
#include "Graphics/GraphicsResources.h"
|
||||
#define TTF_FONT_PARSER_IMPLEMENTATION
|
||||
#include "ttfParser.h"
|
||||
|
||||
using namespace Seele;
|
||||
|
||||
FontAsset::FontAsset()
|
||||
{
|
||||
}
|
||||
|
||||
FontAsset::FontAsset(const std::string& directory, const std::string& name)
|
||||
: Asset(directory, name)
|
||||
{
|
||||
}
|
||||
|
||||
FontAsset::FontAsset(const std::filesystem::path& fullPath)
|
||||
: Asset(fullPath)
|
||||
{
|
||||
}
|
||||
|
||||
FontAsset::~FontAsset()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void FontAsset::save()
|
||||
{
|
||||
assert(false && "Cannot save font files");
|
||||
}
|
||||
|
||||
void FontAsset::load()
|
||||
{
|
||||
TTFFontParser::FontData font_data;
|
||||
int8_t error = TTFFontParser::parse_file(getFullPath().c_str(), &font_data, [](void*, void*, int){}, nullptr);
|
||||
assert(!error);
|
||||
for(auto pair : font_data.glyphs)
|
||||
{
|
||||
const TTFFontParser::Glyph& glyphData = pair.second;
|
||||
Glyph& glyph = glyphs[pair.first];
|
||||
glyph.advance = glyphData.advance_width;
|
||||
std::memcpy(glyph.boundingBox, glyphData.bounding_box, sizeof(glyphData.bounding_box));
|
||||
glyph.center = Vector2(glyphData.glyph_center.x, glyphData.glyph_center.y);
|
||||
glyph.index = glyphData.glyph_index;
|
||||
glyph.leftSideBearing = glyphData.left_side_bearing;
|
||||
glyph.numContours = glyphData.num_contours;
|
||||
Array<float> curveTextureData;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
#include "Asset.h"
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
DECLARE_NAME_REF(Gfx, Texture2D)
|
||||
class FontAsset : public Asset
|
||||
{
|
||||
public:
|
||||
FontAsset();
|
||||
FontAsset(const std::string& directory, const std::string& name);
|
||||
FontAsset(const std::filesystem::path& fullPath);
|
||||
virtual ~FontAsset();
|
||||
virtual void save() override;
|
||||
virtual void load() override;
|
||||
private:
|
||||
struct Glyph
|
||||
{
|
||||
int16 index;
|
||||
int16 numContours;
|
||||
uint16 advance;
|
||||
int16 leftSideBearing;
|
||||
int16 boundingBox[4];
|
||||
Vector2 center;
|
||||
Gfx::PTexture2D curveTexture;
|
||||
Gfx::PTexture2D bandTexture;
|
||||
};
|
||||
Map<uint32, Glyph> glyphs;
|
||||
};
|
||||
DECLARE_REF(FontAsset)
|
||||
} // namespace Seele
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "FontLoader.h"
|
||||
#include "Graphics/Graphics.h"
|
||||
#include "FontAsset.h"
|
||||
#include "AssetRegistry.h"
|
||||
|
||||
using namespace Seele;
|
||||
|
||||
FontLoader::FontLoader(Gfx::PGraphics graphics)
|
||||
: graphics(graphics)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
FontLoader::~FontLoader()
|
||||
{
|
||||
}
|
||||
|
||||
void FontLoader::importAsset(const std::filesystem::path& filePath)
|
||||
{
|
||||
std::filesystem::path assetPath = filePath.filename();
|
||||
assetPath.replace_extension("asset");
|
||||
PFontAsset asset = new FontAsset(assetPath.generic_string());
|
||||
std::error_code code;
|
||||
std::filesystem::copy_file(filePath, asset->getFullPath(), code);
|
||||
asset->setStatus(Asset::Status::Loading);
|
||||
AssetRegistry::get().registerFont(asset);
|
||||
import(filePath, asset);
|
||||
}
|
||||
|
||||
void FontLoader::import(std::filesystem::path path, PFontAsset asset)
|
||||
{
|
||||
asset->load();
|
||||
AssetRegistry::get().registerFont(asset);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
#include "MinimalEngine.h"
|
||||
#include "Containers/List.h"
|
||||
#include "ThreadPool.h"
|
||||
#include <filesystem>
|
||||
|
||||
namespace Seele
|
||||
{
|
||||
DECLARE_REF(FontAsset)
|
||||
DECLARE_NAME_REF(Gfx, Graphics)
|
||||
class FontLoader
|
||||
{
|
||||
public:
|
||||
FontLoader(Gfx::PGraphics graphic);
|
||||
~FontLoader();
|
||||
void importAsset(const std::filesystem::path& filePath);
|
||||
private:
|
||||
void import(std::filesystem::path path, PFontAsset asset);
|
||||
Gfx::PGraphics graphics;
|
||||
};
|
||||
DEFINE_REF(FontLoader)
|
||||
} // namespace Seele
|
||||
@@ -2,8 +2,6 @@
|
||||
#include "MinimalEngine.h"
|
||||
#include "Containers/List.h"
|
||||
#include "ThreadPool.h"
|
||||
#include <thread>
|
||||
#include <future>
|
||||
#include <filesystem>
|
||||
|
||||
namespace Seele
|
||||
@@ -20,7 +18,6 @@ public:
|
||||
private:
|
||||
void import(std::filesystem::path filePath, PMaterialAsset asset);
|
||||
Gfx::PGraphics graphics;
|
||||
List<std::future<void>> futures;
|
||||
PMaterialAsset placeholderMaterial;
|
||||
};
|
||||
DEFINE_REF(MaterialLoader)
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
#include "MinimalEngine.h"
|
||||
#include "Containers/List.h"
|
||||
#include "ThreadPool.h"
|
||||
#include <thread>
|
||||
#include <future>
|
||||
#include <filesystem>
|
||||
|
||||
struct aiScene;
|
||||
@@ -27,7 +25,6 @@ private:
|
||||
void convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels);
|
||||
|
||||
void import(std::filesystem::path path, PMeshAsset meshAsset);
|
||||
List<std::future<void>> futures;
|
||||
Gfx::PGraphics graphics;
|
||||
};
|
||||
DEFINE_REF(MeshLoader)
|
||||
|
||||
@@ -2,8 +2,6 @@
|
||||
#include "MinimalEngine.h"
|
||||
#include "Containers/List.h"
|
||||
#include "ThreadPool.h"
|
||||
#include <thread>
|
||||
#include <future>
|
||||
#include <filesystem>
|
||||
|
||||
namespace Seele
|
||||
@@ -21,7 +19,6 @@ public:
|
||||
private:
|
||||
void import(std::filesystem::path path, PTextureAsset asset);
|
||||
Gfx::PGraphics graphics;
|
||||
List<std::future<void>> futures;
|
||||
PTextureAsset placeholderAsset;
|
||||
};
|
||||
DEFINE_REF(TextureLoader)
|
||||
|
||||
Reference in New Issue
Block a user