overloads, tutorial 04 init

This commit is contained in:
h4570
2022-08-21 19:44:47 +02:00
parent 409ee294eb
commit 4fa65fc535
25 changed files with 232 additions and 77 deletions
@@ -15,6 +15,7 @@
#include <string>
#include "renderer/models/color.hpp"
#include "loaders/3d/obj_loader/tiny_obj_loader.hpp"
#include <memory.h>
namespace Tyra {
@@ -40,11 +41,12 @@ class ObjLoader : public Loader {
ObjLoader();
~ObjLoader();
MeshBuilderData* load(const char* fullpath);
MeshBuilderData* load(const char* fullpath, const ObjLoaderOptions& options);
MeshBuilderData* load(const std::string& fullpath);
MeshBuilderData* load(const std::string& fullpath,
const ObjLoaderOptions& options);
std::unique_ptr<MeshBuilderData> load(const char* fullpath);
std::unique_ptr<MeshBuilderData> load(const char* fullpath,
const ObjLoaderOptions& options);
std::unique_ptr<MeshBuilderData> load(const std::string& fullpath);
std::unique_ptr<MeshBuilderData> load(const std::string& fullpath,
const ObjLoaderOptions& options);
private:
void addOutputMaterialsAndFrames(
@@ -18,8 +18,8 @@ namespace Tyra {
class DynamicMesh : public Mesh {
public:
explicit DynamicMesh(const MeshBuilderData& data);
explicit DynamicMesh(const DynamicMesh& mesh);
explicit DynamicMesh(const MeshBuilderData* data);
DynamicMesh(const DynamicMesh& mesh);
~DynamicMesh();
std::vector<MeshFrame*> frames;
+2 -2
View File
@@ -19,8 +19,8 @@ namespace Tyra {
class Mesh {
public:
explicit Mesh(const MeshBuilderData& data);
explicit Mesh(const Mesh& mesh);
explicit Mesh(const MeshBuilderData* data);
Mesh(const Mesh& mesh);
~Mesh();
u8 isMother;
@@ -18,7 +18,7 @@ namespace Tyra {
class StaticMesh : public Mesh {
public:
StaticMesh(const MeshBuilderData& data);
explicit StaticMesh(const MeshBuilderData* data);
StaticMesh(const StaticMesh& mesh);
~StaticMesh();
@@ -15,6 +15,7 @@
#include <vector>
#include "./models/texture.hpp"
#include "renderer/3d/mesh/mesh.hpp"
#include "renderer/core/2d/sprite/sprite.hpp"
#include "loaders/texture/base/texture_loader_selector.hpp"
#include <string>
@@ -32,11 +33,14 @@ class TextureRepository {
/**
* Returns single texture.
* nullptr if not found.
* @param t_id
* For 3D: MeshMaterial id.
* For 2D: Sprite id.
*/
Texture* getBySpriteOrMesh(const u32& t_id) const;
Texture* getBySpriteId(const u32& t_id) const;
/**
* Returns single texture.
* nullptr if not found.
*/
Texture* getByMeshMaterialId(const u32& t_id) const;
/**
* Returns single texture.
@@ -50,14 +54,6 @@ class TextureRepository {
*/
const s32 getIndexOf(const u32& t_texId) const;
// ----
// Setters
// ----
// ----
// Other
// ----
/**
* Add unlinked texture.
* @param fullpath Full path to texture file. Example: "host:texture.png"
@@ -81,37 +77,38 @@ class TextureRepository {
/**
* Add linked textures in given path for mesh material names.
*/
void addByMesh(Mesh* mesh, const char* directory, const char* extension);
void addByMesh(const Mesh* mesh, const char* directory,
const char* extension);
/**
* Add linked textures in given path for mesh material names.
*/
inline void addByMesh(Mesh* mesh, const std::string& directory,
inline void addByMesh(const Mesh* mesh, const std::string& directory,
const char* extension) {
addByMesh(mesh, directory.c_str(), extension);
}
/**
* Remove texture from repository.
* Texture is NOT destructed.
* Texture IS destructed.
*/
void removeByIndex(const u32& t_index);
void free(const u32& texId);
void free(const Texture* tex);
void free(const Texture& tex);
void freeBySprite(const Sprite& sprite);
void freeByMesh(const Mesh& mesh);
void freeByMesh(const Mesh* mesh);
/**
* Remove texture from repository.
* Texture is NOT destructed.
* Not recommended.
*/
void removeById(const u32& t_texId);
/**
* Remove texture from repository.
* Texture IS destructed.
*/
void free(const u32& t_texId);
void free(const Texture* t_tex);
void free(const Texture& t_tex);
private:
void removeByIndex(const u32& t_index);
std::vector<Texture*> textures;
TextureLoaderSelector texLoaderSelector;
};
@@ -28,21 +28,21 @@ ObjLoader::ObjLoader() {}
ObjLoader::~ObjLoader() {}
MeshBuilderData* ObjLoader::load(const char* fullpath) {
std::unique_ptr<MeshBuilderData> ObjLoader::load(const char* fullpath) {
return load(fullpath, ObjLoaderOptions());
}
MeshBuilderData* ObjLoader::load(const std::string& fullpath) {
std::unique_ptr<MeshBuilderData> ObjLoader::load(const std::string& fullpath) {
return load(fullpath.c_str(), ObjLoaderOptions());
}
MeshBuilderData* ObjLoader::load(const std::string& fullpath,
const ObjLoaderOptions& options) {
std::unique_ptr<MeshBuilderData> ObjLoader::load(
const std::string& fullpath, const ObjLoaderOptions& options) {
return load(fullpath.c_str(), options);
}
MeshBuilderData* ObjLoader::load(const char* fullpath,
const ObjLoaderOptions& options) {
std::unique_ptr<MeshBuilderData> ObjLoader::load(
const char* fullpath, const ObjLoaderOptions& options) {
std::string path = fullpath;
std::string basePath = getPathFromFilename(path);
@@ -50,8 +50,7 @@ MeshBuilderData* ObjLoader::load(const char* fullpath,
auto rawFilename = getFilenameWithoutExtension(path);
auto extension = getExtensionOfFilename(path);
auto* result = new MeshBuilderData();
auto result = std::make_unique<MeshBuilderData>();
tinyobj::ObjReaderConfig readerConfig;
readerConfig.triangulate = options.animation.count == 1;
@@ -93,11 +92,11 @@ MeshBuilderData* ObjLoader::load(const char* fullpath,
if (i == 1) {
auto scanResult = scan(shapes, materials);
addOutputMaterialsAndFrames(result, attrib, shapes, materials,
addOutputMaterialsAndFrames(result.get(), attrib, shapes, materials,
options.animation.count, scanResult);
}
importFrame(result, attrib, shapes, materials, i - 1, options.scale,
importFrame(result.get(), attrib, shapes, materials, i - 1, options.scale,
options.flipUVs, options.animation.count);
}
+1 -1
View File
@@ -20,7 +20,7 @@ void Renderer2D::init(RendererCore* t_rendererCore) { core = t_rendererCore; }
void Renderer2D::render(const Sprite* sprite) { render(*sprite); }
void Renderer2D::render(const Sprite& sprite) {
auto* texture = core->texture.repository.getBySpriteOrMesh(sprite.id);
auto* texture = core->texture.repository.getBySpriteId(sprite.id);
TYRA_ASSERT(
texture, "Texture for sprite with id: ", sprite.id,
@@ -15,15 +15,15 @@
namespace Tyra {
DynamicMesh::DynamicMesh(const MeshBuilderData& data) : Mesh(data) {
if (data.materials[0]->frames.size() == 1) {
DynamicMesh::DynamicMesh(const MeshBuilderData* data) : Mesh(data) {
if (data->materials[0]->frames.size() == 1) {
TYRA_WARN(
"Frames count should be greater than 1 for DynamicMesh! Maybe you "
"should use StaticMesh?");
}
for (u32 i = 0; i < data.materials[0]->frames.size(); i++) {
frames.push_back(new MeshFrame(data, i));
for (u32 i = 0; i < data->materials[0]->frames.size(); i++) {
frames.push_back(new MeshFrame(*data, i));
}
animation.resetAll(frames);
+4 -4
View File
@@ -13,14 +13,14 @@
namespace Tyra {
Mesh::Mesh(const MeshBuilderData& data) {
Mesh::Mesh(const MeshBuilderData* data) {
init();
TYRA_ASSERT(data.materials.size() > 0,
TYRA_ASSERT(data->materials.size() > 0,
"Materials count must be greater than 0");
for (u32 i = 0; i < data.materials.size(); i++) {
auto* material = new MeshMaterial(data, i);
for (u32 i = 0; i < data->materials.size(); i++) {
auto* material = new MeshMaterial(*data, i);
if (material->frames.size() == 0) {
TYRA_WARN("Found empty material: ", material->name, ". Skipping...");
@@ -13,12 +13,12 @@
namespace Tyra {
StaticMesh::StaticMesh(const MeshBuilderData& data) : Mesh(data) {
if (data.materials[0]->frames.size() > 1)
StaticMesh::StaticMesh(const MeshBuilderData* data) : Mesh(data) {
if (data->materials[0]->frames.size() > 1)
TYRA_WARN("Static meshes should have only one frame, but ",
data.materials[0]->frames.size(), " frames were found");
data->materials[0]->frames.size(), " frames were found");
frame = new MeshFrame(data, 0);
frame = new MeshFrame(*data, 0);
}
StaticMesh::StaticMesh(const StaticMesh& mesh) : Mesh(mesh) {
@@ -106,7 +106,7 @@ void DynamicPipeline::render(const DynamicMesh* mesh,
u8 isPartInitialized = false;
auto* texture =
rendererCore->texture.repository.getBySpriteOrMesh(material->id);
rendererCore->texture.repository.getByMeshMaterialId(material->id);
TYRA_ASSERT(
texture, "Texture for material: ", material->name, "Id: ", material->id,
@@ -143,7 +143,7 @@ StaPipTextureBag* StaticPipeline::getTextureBag(
auto* result = new StaPipTextureBag();
result->texture =
rendererCore->texture.repository.getBySpriteOrMesh(material->id);
rendererCore->texture.repository.getByMeshMaterialId(material->id);
TYRA_ASSERT(
result->texture, "Texture for material: ", material->name,
@@ -22,7 +22,14 @@ TextureRepository::~TextureRepository() {
}
}
Texture* TextureRepository::getBySpriteOrMesh(const u32& t_id) const {
Texture* TextureRepository::getBySpriteId(const u32& t_id) const {
for (u32 i = 0; i < textures.size(); i++) {
if (textures[i]->isLinkedWith(t_id)) return textures[i];
}
return nullptr;
}
Texture* TextureRepository::getByMeshMaterialId(const u32& t_id) const {
for (u32 i = 0; i < textures.size(); i++) {
if (textures[i]->isLinkedWith(t_id)) return textures[i];
}
@@ -56,6 +63,26 @@ void TextureRepository::removeById(const u32& t_texId) {
removeByIndex(index);
}
void TextureRepository::freeByMesh(const Mesh& mesh) {
for (auto* material : mesh.materials) {
auto* texture = getByMeshMaterialId(material->id);
if (texture != nullptr) {
free(texture);
}
}
}
void TextureRepository::freeByMesh(const Mesh* mesh) { freeByMesh(*mesh); }
void TextureRepository::freeBySprite(const Sprite& sprite) {
auto* texture = getBySpriteId(sprite.id);
if (texture != nullptr) {
free(texture);
}
}
void TextureRepository::free(const Texture* t_tex) { free(t_tex->id); }
void TextureRepository::free(const Texture& t_tex) { free(t_tex.id); }
@@ -81,7 +108,7 @@ Texture* TextureRepository::add(const char* fullpath) {
return texture;
}
void TextureRepository::addByMesh(Mesh* mesh, const char* directory,
void TextureRepository::addByMesh(const Mesh* mesh, const char* directory,
const char* extension) {
auto& loader = texLoaderSelector.getLoaderByExtension(extension);