implemented vector in tex repo

This commit is contained in:
Sandro Sobczyński
2020-10-31 22:51:46 +01:00
parent f9dc79caf1
commit d4be8e94e7
5 changed files with 70 additions and 67 deletions
+5 -1
View File
@@ -15,6 +15,7 @@
#include <draw_sampling.h> #include <draw_sampling.h>
#include "./texture_wrap_settings.hpp" #include "./texture_wrap_settings.hpp"
#include "./texture_link.hpp" #include "./texture_link.hpp"
#include "../include/utils/debug.hpp"
#include <vector> #include <vector>
/** /**
@@ -138,8 +139,11 @@ public:
void removeLink(const u32 &t_meshId, const u32 &t_materialId) void removeLink(const u32 &t_meshId, const u32 &t_materialId)
{ {
u32 index = getIndexOfLink(t_meshId, t_materialId); s32 index = getIndexOfLink(t_meshId, t_materialId);
if (index != -1)
removeLink(index); removeLink(index);
else
PRINT_ERR("Cant remove link, because it was not found!");
} }
private: private:
@@ -11,6 +11,7 @@
#ifndef _TYRA_TEXTURE_REPOSITORY_ #ifndef _TYRA_TEXTURE_REPOSITORY_
#define _TYRA_TEXTURE_REPOSITORY_ #define _TYRA_TEXTURE_REPOSITORY_
#include <vector>
#include <tamtypes.h> #include <tamtypes.h>
#include <draw_buffers.h> #include <draw_buffers.h>
#include "../models/mesh.hpp" #include "../models/mesh.hpp"
@@ -30,9 +31,9 @@ public:
// ---- // ----
/** Returns all repository textures. */ /** Returns all repository textures. */
MeshTexture **getAll() const { return textures; } std::vector<MeshTexture *> *getAll() { return &textures; }
const u32 &getTexturesCount() const { return texturesCount; }; u32 getTexturesCount() const { return static_cast<u32>(textures.size()); };
/** /**
* Returns single texture. * Returns single texture.
@@ -40,7 +41,7 @@ public:
*/ */
MeshTexture *getByMesh(const u32 &t_meshId, const u32 &t_materialId) MeshTexture *getByMesh(const u32 &t_meshId, const u32 &t_materialId)
{ {
for (u32 i = 0; i < texturesCount; i++) for (u32 i = 0; i < textures.size(); i++)
if (textures[i]->isLinkedWith(t_meshId, t_materialId)) if (textures[i]->isLinkedWith(t_meshId, t_materialId))
return textures[i]; return textures[i];
return NULL; return NULL;
@@ -52,12 +53,24 @@ public:
*/ */
MeshTexture *getByTextureId(const u32 &t_id) const MeshTexture *getByTextureId(const u32 &t_id) const
{ {
for (u32 i = 0; i < texturesCount; i++) for (u32 i = 0; i < textures.size(); i++)
if (t_id == textures[i]->getId()) if (t_id == textures[i]->getId())
return textures[i]; return textures[i];
return NULL; return NULL;
} }
/**
* Returns index of link.
* -1 if not found.
*/
const s32 getIndexOf(const u32 &t_texId) const
{
for (u32 i = 0; i < textures.size(); i++)
if (textures[i]->getId() == t_texId)
return i;
return -1;
};
// ---- // ----
// Setters // Setters
// ---- // ----
@@ -66,22 +79,39 @@ public:
// Other // Other
// ---- // ----
/** Add single unlinked texture to repository. */ /** Add unlinked texture.
* @param t_subfolder Relative path. Ex.: "textures/"
* @param t_name Filename without extension. Ex.: "water"
*/
MeshTexture *add(char *t_subfolder, char *t_name); MeshTexture *add(char *t_subfolder, char *t_name);
/** Add linked textures in given subpath for mesh material names. */ /**
* Add linked textures in given subpath for mesh material names.
* @param t_path Relative path where textures should be searched
*/
void addByMesh(char *t_path, Mesh &mesh); void addByMesh(char *t_path, Mesh &mesh);
/** /**
* Remove texture from repository. * Remove texture from repository.
* Texture is NOT destructed. * Texture is NOT destructed.
*/ */
void remove(u32 textureId); void removeByIndex(const u32 &t_index) { textures.erase(textures.begin() + t_index); }
/**
* Remove texture from repository.
* Texture is NOT destructed.
*/
const void removeById(const u32 &t_texId)
{
s32 index = getIndexOf(t_texId);
if (index != -1)
removeByIndex(index);
else
PRINT_ERR("Cant remove texture, because it was not found!");
}
private: private:
void increaseArray(u32 newLength); std::vector<MeshTexture *> textures;
u32 texturesCount;
MeshTexture **textures;
BmpLoader loader; BmpLoader loader;
}; };
-1
View File
@@ -10,7 +10,6 @@
#include "../include/models/mesh_texture.hpp" #include "../include/models/mesh_texture.hpp"
#include "../include/utils/string.hpp" #include "../include/utils/string.hpp"
#include "../include/utils/debug.hpp"
#include <cstdlib> #include <cstdlib>
#include <draw_sampling.h> #include <draw_sampling.h>
+20 -52
View File
@@ -18,72 +18,40 @@
TextureRepository::TextureRepository() TextureRepository::TextureRepository()
{ {
PRINT_LOG("Initializing texture repository"); PRINT_LOG("Initializing texture repository");
texturesCount = 0;
PRINT_LOG("Texture repository initialized!"); PRINT_LOG("Texture repository initialized!");
} }
TextureRepository::~TextureRepository() {} TextureRepository::~TextureRepository()
{
if (getTexturesCount() > 0)
{
for (u32 i = 0; i < getTexturesCount(); i++)
delete textures[i];
textures.clear();
}
}
// ---- // ----
// Methods // Methods
// ---- // ----
/** Add unlinked texture.
* @param t_subfolder Relative path. Ex.: "textures/"
* @param t_name Filename without extension. Ex.: "water"
*/
MeshTexture *TextureRepository::add(char *t_subfolder, char *t_name) MeshTexture *TextureRepository::add(char *t_subfolder, char *t_name)
{ {
increaseArray(texturesCount + 1); MeshTexture *texture = new MeshTexture();
// texturesCount is updated loader.load(*texture, t_subfolder, t_name, ".bmp");
loader.load(*textures[texturesCount - 1], t_subfolder, t_name, ".bmp"); texture->setName(t_name);
return textures[texturesCount - 1]; textures.push_back(texture);
return texture;
} }
void TextureRepository::addByMesh(char *t_path, Mesh &mesh) void TextureRepository::addByMesh(char *t_path, Mesh &mesh)
{ {
#define MATERIAL mesh.getMaterial(i) for (u32 i = 0; i < mesh.getMaterialsCount(); i++)
u32 newLength = texturesCount + mesh.getMaterialsCount();
increaseArray(newLength);
for (u8 i = newLength - mesh.getMaterialsCount(); i < newLength; i++)
{ {
textures[i] = new MeshTexture(); MeshTexture *texture = new MeshTexture();
loader.load(*textures[i], t_path, MATERIAL.getName(), ".bmp"); loader.load(*texture, t_path, mesh.getMaterial(i).getName(), ".bmp");
textures[i]->setName(mesh.getMaterial(i).getName()); texture->setName(mesh.getMaterial(i).getName());
textures[i]->addLink(mesh.id, MATERIAL.getId()); texture->addLink(mesh.id, mesh.getMaterial(i).getId());
textures.push_back(texture);
} }
} }
/**
* Remove texture from repository.
* Texture is not destructed.
*/
void TextureRepository::remove(u32 t_textureId)
{
MeshTexture **savedTextures = textures;
textures = new MeshTexture *[texturesCount - 1];
u32 savedCount = 0;
for (u32 i = 0; i < texturesCount; i++)
if (savedTextures[i]->getId() != t_textureId)
textures[savedCount++] = savedTextures[i];
delete[] savedTextures;
texturesCount--;
}
/**
* Resize textures array.
* texturesCount variable is updated.
* Classes are NOT instantiated.
*/
void TextureRepository::increaseArray(u32 newLength)
{
MeshTexture **savedTextures = textures;
textures = new MeshTexture *[newLength];
if (texturesCount)
{
for (u32 i = 0; i < texturesCount; i++)
textures[i] = savedTextures[i];
delete[] savedTextures;
}
texturesCount = newLength;
}
+4 -2
View File
@@ -66,6 +66,7 @@ void calcSpiral(int X, int Y)
// TEST END // TEST END
#include "models/mesh_frame.hpp" #include "models/mesh_frame.hpp"
#include <vector>
void Ari::onInit() void Ari::onInit()
{ {
@@ -94,10 +95,11 @@ void Ari::onInit()
Vector3 testpos = Vector3(0.0F, 10.0F, 0.0F); Vector3 testpos = Vector3(0.0F, 10.0F, 0.0F);
test->loadObj("objanim/", "untitled", testpos, 10.0F, 2); test->loadObj("objanim/", "untitled", testpos, 10.0F, 2);
texRepo->addByMesh("objanim/", *test); texRepo->addByMesh("objanim/", *test);
MeshTexture **textures = texRepo->getAll(); std::vector<MeshTexture *> *textures = texRepo->getAll();
textures[0]->removeLink(test->id, test->getMaterial(0).getId()); textures->at(0)->removeLink(test->id, test->getMaterial(0).getId());
MeshTexture *tex = texRepo->add("objanim/", "water"); MeshTexture *tex = texRepo->add("objanim/", "water");
tex->addLink(test->id, test->getMaterial(0).getId()); tex->addLink(test->id, test->getMaterial(0).getId());
texRepo->removeById(textures->at(0)->getId());
test->playAnimation(0, 1); test->playAnimation(0, 1);
test->shouldBeBackfaceCulled = true; test->shouldBeBackfaceCulled = true;
// test->shouldBeFrustumCulled = true; // test->shouldBeFrustumCulled = true;