New game interface

This commit is contained in:
Dynamitos
2023-02-01 22:13:04 +01:00
parent 0dce84459e
commit 9e1e4076f0
91 changed files with 268 additions and 21014 deletions
+2 -2
View File
@@ -42,12 +42,12 @@ Asset::~Asset()
std::ifstream Asset::getReadStream() const
{
return std::ifstream(fullPath);
return std::ifstream(fullPath, std::ios::binary);
}
std::ofstream Asset::getWriteStream() const
{
return std::ofstream(fullPath);
return std::ofstream(fullPath, std::ios::binary);
}
std::string Asset::getFileName() const
+18 -46
View File
@@ -17,6 +17,8 @@
using namespace Seele;
using json = nlohmann::json;
extern AssetRegistry* instance;
AssetRegistry::~AssetRegistry()
{
}
@@ -26,36 +28,27 @@ void AssetRegistry::init(const std::string& rootFolder, Gfx::PGraphics graphics)
get().initialize(rootFolder, graphics);
}
void AssetRegistry::importFile(const std::string &filePath)
void AssetRegistry::importMesh(MeshImportArgs args)
{
importFile(filePath, "");
get().meshLoader->importAsset(args);
}
void AssetRegistry::importFile(const std::string &filePath, const std::string& importPath)
void AssetRegistry::importTexture(TextureImportArgs args)
{
std::filesystem::path fsPath = std::filesystem::path(filePath);
std::string extension = fsPath.extension().string();
if (extension.compare(".fbx") == 0
|| extension.compare(".obj") == 0
|| extension.compare(".blend") == 0)
{
get().importMesh(fsPath, importPath);
}
if (extension.compare(".png") == 0
|| extension.compare(".jpg") == 0)
{
get().importTexture(fsPath, importPath);
}
if(extension.compare(".ttf") == 0)
{
get().importFont(fsPath, importPath);
}
if (extension.compare(".asset") == 0)
{
get().importMaterial(fsPath, importPath);
}
get().textureLoader->importAsset(args);
}
void AssetRegistry::importFont(FontImportArgs args)
{
get().fontLoader->importAsset(args);
}
void AssetRegistry::importMaterial(MaterialImportArgs args)
{
get().materialLoader->importAsset(args);
}
PMeshAsset AssetRegistry::findMesh(const std::string &filePath)
{
AssetFolder& folder = get().assetRoot;
@@ -125,8 +118,7 @@ std::ifstream AssetRegistry::createReadStream(const std::string& relativePath, s
AssetRegistry &AssetRegistry::get()
{
static AssetRegistry instance;
return instance;
return *instance;
}
AssetRegistry::AssetRegistry()
@@ -148,26 +140,6 @@ std::string AssetRegistry::getRootFolder()
return get().rootFolder.generic_string();
}
void AssetRegistry::importMesh(const std::filesystem::path &filePath, const std::string& importPath)
{
meshLoader->importAsset(filePath, importPath);
}
void AssetRegistry::importTexture(const std::filesystem::path &filePath, const std::string& importPath)
{
textureLoader->importAsset(filePath, importPath);
}
void AssetRegistry::importFont(const std::filesystem::path& filePath, const std::string& importPath)
{
fontLoader->importAsset(filePath, importPath);
}
void AssetRegistry::importMaterial(const std::filesystem::path &filePath, const std::string& importPath)
{
materialLoader->importAsset(filePath, importPath);
}
void AssetRegistry::registerMesh(PMeshAsset mesh, const std::string& importPath)
{
AssetFolder& folder = getOrCreateFolder(importPath);
+8 -8
View File
@@ -15,6 +15,7 @@ DECLARE_REF(FontAsset)
DECLARE_REF(MeshAsset)
DECLARE_REF(MaterialAsset)
DECLARE_NAME_REF(Gfx, Graphics)
class AssetRegistry
{
public:
@@ -23,9 +24,6 @@ public:
static std::string getRootFolder();
static void importFile(const std::string& filePath);
static void importFile(const std::string& filePath, const std::string& importPath);
static PMeshAsset findMesh(const std::string& filePath);
static PTextureAsset findTexture(const std::string& filePath);
static PFontAsset findFont(const std::string& name);
@@ -33,6 +31,13 @@ public:
static std::ofstream createWriteStream(const std::string& relativePath, std::ios_base::openmode openmode = std::ios::out);
static std::ifstream createReadStream(const std::string& relativePath, std::ios_base::openmode openmode = std::ios::in);
static void importMesh(struct MeshImportArgs args);
static void importTexture(struct TextureImportArgs args);
static void importFont(struct FontImportArgs args);
static void importMaterial(struct MaterialImportArgs args);
static void set(AssetRegistry registry);
AssetRegistry();
private:
struct AssetFolder
{
@@ -46,13 +51,8 @@ private:
static AssetRegistry& get();
AssetRegistry();
void initialize(const std::filesystem::path& rootFolder, Gfx::PGraphics graphics);
void importMesh(const std::filesystem::path& filePath, const std::string& importPath);
void importTexture(const std::filesystem::path& filePath, const std::string& importPath);
void importFont(const std::filesystem::path& filePath, const std::string& importPath);
void importMaterial(const std::filesystem::path& filePath, const std::string& importPath);
void registerMesh(PMeshAsset mesh, const std::string& importPath);
void registerTexture(PTextureAsset texture, const std::string& importPath);
-1
View File
@@ -24,7 +24,6 @@ FontAsset::~FontAsset()
void FontAsset::save(Gfx::PGraphics graphics)
{
assert(false && "Cannot save font files");
}
+2 -2
View File
@@ -21,9 +21,9 @@ public:
IVector2 bearing;
uint32 advance;
};
const std::map<uint32, Glyph> getGlyphData() const { return glyphs; }
const Map<uint32, Glyph> getGlyphData() const { return glyphs; }
private:
std::map<uint32, Glyph> glyphs;
Map<uint32, Glyph> glyphs;
friend class FontLoader;
};
DECLARE_REF(FontAsset)
+6 -6
View File
@@ -18,22 +18,22 @@ FontLoader::~FontLoader()
{
}
void FontLoader::importAsset(const std::filesystem::path& filePath, const std::string& importPath)
void FontLoader::importAsset(FontImportArgs args)
{
std::filesystem::path assetPath = filePath.filename();
std::filesystem::path assetPath = args.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);
std::filesystem::copy_file(args.filePath, asset->getFullPath(), code);
asset->setStatus(Asset::Status::Loading);
AssetRegistry::get().registerFont(asset, importPath);
import(filePath, asset);
AssetRegistry::get().registerFont(asset, args.importPath);
import(args, asset);
}
// in case of the space character there is no bitmap
// so we create a single pixel empty texture
uint8 transparentPixel = 0;
void FontLoader::import(std::filesystem::path path, PFontAsset asset)
void FontLoader::import(FontImportArgs args, PFontAsset asset)
{
FT_Library ft;
FT_Error error = FT_Init_FreeType(&ft);
+7 -2
View File
@@ -7,14 +7,19 @@ namespace Seele
{
DECLARE_REF(FontAsset)
DECLARE_NAME_REF(Gfx, Graphics)
struct FontImportArgs
{
std::filesystem::path filePath;
std::string importPath;
};
class FontLoader
{
public:
FontLoader(Gfx::PGraphics graphic);
~FontLoader();
void importAsset(const std::filesystem::path& filePath, const std::string& importPath);
void importAsset(FontImportArgs args);
private:
void import(std::filesystem::path path, PFontAsset asset);
void import(FontImportArgs args, PFontAsset asset);
Gfx::PGraphics graphics;
};
DEFINE_REF(FontLoader)
+10 -7
View File
@@ -15,26 +15,29 @@ using json = nlohmann::json;
MaterialLoader::MaterialLoader(Gfx::PGraphics graphics)
: graphics(graphics)
{
importAsset(std::filesystem::absolute("./shaders/Placeholder.asset"), "");
importAsset(MaterialImportArgs{
.filePath = std::filesystem::absolute("./shaders/Placeholder.asset"),
.importPath = "",
});
}
MaterialLoader::~MaterialLoader()
{
}
void MaterialLoader::importAsset(const std::filesystem::path& name, const std::string& importPath)
void MaterialLoader::importAsset(MaterialImportArgs args)
{
std::filesystem::path assetPath = name.filename();
std::filesystem::path assetPath = args.filePath.filename();
assetPath.replace_extension("asset");
PMaterialAsset asset = new MaterialAsset(assetPath.generic_string());
asset->setStatus(Asset::Status::Loading);
AssetRegistry::get().registerMaterial(asset, importPath);
import(name, asset);
AssetRegistry::get().registerMaterial(asset, args.importPath);
import(args, asset);
}
void MaterialLoader::import(std::filesystem::path name, PMaterialAsset asset)
void MaterialLoader::import(MaterialImportArgs args, PMaterialAsset asset)
{
auto stream = std::ifstream(name.c_str());
auto stream = std::ifstream(args.filePath.c_str());
json j;
stream >> j;
std::string materialName = j["name"].get<std::string>() + "Material";
+7 -2
View File
@@ -7,15 +7,20 @@ namespace Seele
{
DECLARE_REF(MaterialAsset)
DECLARE_NAME_REF(Gfx, Graphics)
struct MaterialImportArgs
{
std::filesystem::path filePath;
std::string importPath;
};
class MaterialLoader
{
public:
MaterialLoader(Gfx::PGraphics graphic);
~MaterialLoader();
void importAsset(const std::filesystem::path& name, const std::string& importPath);
void importAsset(MaterialImportArgs args);
PMaterialAsset getPlaceHolderMaterial();
private:
void import(std::filesystem::path filePath, PMaterialAsset asset);
void import(MaterialImportArgs args, PMaterialAsset asset);
Gfx::PGraphics graphics;
PMaterialAsset placeholderMaterial;
};
+19 -11
View File
@@ -15,6 +15,8 @@
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/material.h>
#include <Asset/MaterialLoader.h>
#include <Asset/TextureLoader.h>
using namespace Seele;
@@ -27,14 +29,14 @@ MeshLoader::~MeshLoader()
{
}
void MeshLoader::importAsset(const std::filesystem::path &path, const std::string& importPath)
void MeshLoader::importAsset(MeshImportArgs args)
{
std::filesystem::path assetPath = path.filename();
std::filesystem::path assetPath = args.filePath.filename();
assetPath.replace_extension("asset");
PMeshAsset asset = new MeshAsset(assetPath.generic_string());
asset->setStatus(Asset::Status::Loading);
AssetRegistry::get().registerMesh(asset, importPath);
import(path, asset);
AssetRegistry::get().registerMesh(asset, args.importPath);
import(args, asset);
}
void MeshLoader::loadMaterials(const aiScene* scene, Array<PMaterial>& globalMaterials)
@@ -93,7 +95,10 @@ void MeshLoader::loadMaterials(const aiScene* scene, Array<PMaterial>& globalMat
outMatFile.close();
std::cout << "writing json to " << outMatFilename << std::endl;
AssetRegistry::importFile(AssetRegistry::getRootFolder() + "/" + outMatFilename);
AssetRegistry::importMaterial(MaterialImportArgs{
.filePath = AssetRegistry::getRootFolder() + "/" + outMatFilename,
.importPath = "",
});
PMaterialAsset asset = AssetRegistry::findMaterial(matCode["name"].get<std::string>());
globalMaterials[i] = asset->getMaterial();
}
@@ -262,15 +267,18 @@ void MeshLoader::loadTextures(const aiScene* scene, const std::filesystem::path&
delete[] texData;
}
std::cout << "Loading model texture " << texPngPath.string() << std::endl;
AssetRegistry::importFile(texPngPath.string());
AssetRegistry::importTexture(TextureImportArgs {
.filePath = texPath,
.importPath = ""
});
}
}
void MeshLoader::import(std::filesystem::path path, PMeshAsset meshAsset)
void MeshLoader::import(MeshImportArgs args, PMeshAsset meshAsset)
{
std::cout << "Starting to import "<<path << std::endl;
std::cout << "Starting to import "<<args.filePath<< std::endl;
meshAsset->setStatus(Asset::Status::Loading);
Assimp::Importer importer;
importer.ReadFile(path.string().c_str(), (uint32)(
importer.ReadFile(args.filePath.string().c_str(), (uint32)(
aiProcess_FlipUVs |
aiProcess_Triangulate |
aiProcess_SortByPType |
@@ -281,7 +289,7 @@ void MeshLoader::import(std::filesystem::path path, PMeshAsset meshAsset)
const aiScene *scene = importer.ApplyPostProcessing(aiProcess_CalcTangentSpace);
Array<PMaterial> globalMaterials(scene->mNumMaterials);
loadTextures(scene, path.parent_path());
loadTextures(scene, args.filePath.parent_path());
loadMaterials(scene, globalMaterials);
Array<PMesh> globalMeshes(scene->mNumMeshes);
@@ -300,5 +308,5 @@ void MeshLoader::import(std::filesystem::path path, PMeshAsset meshAsset)
}
meshAsset->physicsMesh = std::move(collider);
meshAsset->setStatus(Asset::Status::Ready);
std::cout << "Finished loading " << path << std::endl;
std::cout << "Finished loading " << args.filePath<< std::endl;
}
+7 -2
View File
@@ -12,19 +12,24 @@ DECLARE_REF(Mesh)
DECLARE_REF(MeshAsset)
DECLARE_REF(Material)
DECLARE_NAME_REF(Gfx, Graphics)
struct MeshImportArgs
{
std::filesystem::path filePath;
std::string importPath;
};
class MeshLoader
{
public:
MeshLoader(Gfx::PGraphics graphic);
~MeshLoader();
void importAsset(const std::filesystem::path& filePath, const std::string& importPath);
void importAsset(MeshImportArgs args);
private:
void loadMaterials(const aiScene* scene, Array<PMaterial>& globalMaterials);
void loadTextures(const aiScene* scene, const std::filesystem::path& meshPath);
void loadGlobalMeshes(const aiScene* scene, const Array<PMaterial>& materials, Array<PMesh>& globalMeshes, Component::Collider& collider);
void convertAssimpARGB(unsigned char* dst, aiTexel* src, uint32 numPixels);
void import(std::filesystem::path path, PMeshAsset meshAsset);
void import(MeshImportArgs args, PMeshAsset meshAsset);
Gfx::PGraphics graphics;
};
DEFINE_REF(MeshLoader)
+15 -15
View File
@@ -23,15 +23,15 @@ TextureLoader::~TextureLoader()
{
}
void TextureLoader::importAsset(const std::filesystem::path& path, const std::string& importPath)
void TextureLoader::importAsset(TextureImportArgs args)
{
std::filesystem::path assetPath = path.filename();
std::filesystem::path assetPath = args.filePath.filename();
assetPath.replace_extension("asset");
PTextureAsset asset = new TextureAsset(assetPath.generic_string());
asset->setStatus(Asset::Status::Loading);
asset->setTexture(placeholderAsset->getTexture());
AssetRegistry::get().registerTexture(asset, importPath);
import(path, asset);
AssetRegistry::get().registerTexture(asset, args.importPath);
import(args, asset);
}
PTextureAsset TextureLoader::getPlaceholderTexture()
@@ -39,14 +39,14 @@ PTextureAsset TextureLoader::getPlaceholderTexture()
return placeholderAsset;
}
void TextureLoader::import(std::filesystem::path path, PTextureAsset textureAsset)
void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset)
{
int totalWidth, totalHeight, n;
unsigned char* data = stbi_load(path.string().c_str(), &totalWidth, &totalHeight, &n, 4);
unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4);
ktxTexture2* kTexture;
ktxTextureCreateInfo createInfo;
if (totalWidth / 4 == totalHeight / 3)
if (args.type == TextureImportType::TEXTURE_CUBEMAP)
{
uint32 faceWidth = totalWidth / 4;
uint32 faceHeight = totalHeight / 3;
@@ -56,7 +56,7 @@ void TextureLoader::import(std::filesystem::path path, PTextureAsset textureAsse
createInfo.baseHeight = totalHeight / 3;
createInfo.baseDepth = 1;
createInfo.numFaces = 6;
createInfo.numDimensions = 3;
createInfo.numDimensions = 2;
createInfo.numLevels = 1;
createInfo.numLayers = 1;
createInfo.isArray = false;
@@ -66,7 +66,7 @@ void TextureLoader::import(std::filesystem::path path, PTextureAsset textureAsse
KTX_TEXTURE_CREATE_ALLOC_STORAGE,
&kTexture);
auto loadCubeFace = [kTexture, faceWidth, totalWidth, data](int xPos, int yPos, int faceName)
auto loadCubeFace = [&kTexture, &faceWidth, &totalWidth, &data](int xPos, int yPos, int faceName)
{
std::vector<unsigned char> vec(faceWidth * faceWidth * 4);
for (int y = 0; y < faceWidth; ++y)
@@ -81,12 +81,12 @@ void TextureLoader::import(std::filesystem::path path, PTextureAsset textureAsse
ktxTexture_SetImageFromMemory(ktxTexture(kTexture),
0, 0, faceName, vec.data(), vec.size());
};
loadCubeFace(1, 0, 0);
loadCubeFace(0, 1, 1);
loadCubeFace(1, 1, 2);
loadCubeFace(2, 1, 3);
loadCubeFace(3, 1, 4);
loadCubeFace(1, 2, 5);
loadCubeFace(2, 1, 0); // +X
loadCubeFace(0, 1, 1); // -X
loadCubeFace(1, 0, 2); // +Y
loadCubeFace(1, 2, 3); // -Y
loadCubeFace(1, 1, 4); // -Z
loadCubeFace(3, 1, 5); // -Z
}
else
{
+13 -2
View File
@@ -8,15 +8,26 @@ namespace Seele
DECLARE_REF(TextureAsset)
DECLARE_NAME_REF(Gfx, Graphics)
DECLARE_NAME_REF(Gfx, Texture2D)
enum class TextureImportType
{
TEXTURE_2D,
TEXTURE_CUBEMAP,
};
struct TextureImportArgs
{
std::filesystem::path filePath;
std::string importPath;
TextureImportType type = TextureImportType::TEXTURE_2D;
};
class TextureLoader
{
public:
TextureLoader(Gfx::PGraphics graphic);
~TextureLoader();
void importAsset(const std::filesystem::path& filePath, const std::string& importPath);
void importAsset(TextureImportArgs args);
PTextureAsset getPlaceholderTexture();
private:
void import(std::filesystem::path path, PTextureAsset asset);
void import(TextureImportArgs args, PTextureAsset asset);
Gfx::PGraphics graphics;
PTextureAsset placeholderAsset;
};