implemented vector in tex repo
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#include <draw_sampling.h>
|
||||
#include "./texture_wrap_settings.hpp"
|
||||
#include "./texture_link.hpp"
|
||||
#include "../include/utils/debug.hpp"
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
@@ -138,8 +139,11 @@ public:
|
||||
|
||||
void removeLink(const u32 &t_meshId, const u32 &t_materialId)
|
||||
{
|
||||
u32 index = getIndexOfLink(t_meshId, t_materialId);
|
||||
removeLink(index);
|
||||
s32 index = getIndexOfLink(t_meshId, t_materialId);
|
||||
if (index != -1)
|
||||
removeLink(index);
|
||||
else
|
||||
PRINT_ERR("Cant remove link, because it was not found!");
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#ifndef _TYRA_TEXTURE_REPOSITORY_
|
||||
#define _TYRA_TEXTURE_REPOSITORY_
|
||||
|
||||
#include <vector>
|
||||
#include <tamtypes.h>
|
||||
#include <draw_buffers.h>
|
||||
#include "../models/mesh.hpp"
|
||||
@@ -30,9 +31,9 @@ public:
|
||||
// ----
|
||||
|
||||
/** 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.
|
||||
@@ -40,7 +41,7 @@ public:
|
||||
*/
|
||||
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))
|
||||
return textures[i];
|
||||
return NULL;
|
||||
@@ -52,12 +53,24 @@ public:
|
||||
*/
|
||||
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())
|
||||
return textures[i];
|
||||
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
|
||||
// ----
|
||||
@@ -66,22 +79,39 @@ public:
|
||||
// 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);
|
||||
|
||||
/** 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);
|
||||
|
||||
/**
|
||||
* Remove texture from repository.
|
||||
* 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:
|
||||
void increaseArray(u32 newLength);
|
||||
u32 texturesCount;
|
||||
MeshTexture **textures;
|
||||
std::vector<MeshTexture *> textures;
|
||||
BmpLoader loader;
|
||||
};
|
||||
|
||||
|
||||
@@ -10,7 +10,6 @@
|
||||
|
||||
#include "../include/models/mesh_texture.hpp"
|
||||
#include "../include/utils/string.hpp"
|
||||
#include "../include/utils/debug.hpp"
|
||||
#include <cstdlib>
|
||||
#include <draw_sampling.h>
|
||||
|
||||
|
||||
@@ -18,72 +18,40 @@
|
||||
TextureRepository::TextureRepository()
|
||||
{
|
||||
PRINT_LOG("Initializing texture repository");
|
||||
texturesCount = 0;
|
||||
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
|
||||
// ----
|
||||
|
||||
/** 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)
|
||||
{
|
||||
increaseArray(texturesCount + 1);
|
||||
// texturesCount is updated
|
||||
loader.load(*textures[texturesCount - 1], t_subfolder, t_name, ".bmp");
|
||||
return textures[texturesCount - 1];
|
||||
MeshTexture *texture = new MeshTexture();
|
||||
loader.load(*texture, t_subfolder, t_name, ".bmp");
|
||||
texture->setName(t_name);
|
||||
textures.push_back(texture);
|
||||
return texture;
|
||||
}
|
||||
|
||||
void TextureRepository::addByMesh(char *t_path, Mesh &mesh)
|
||||
{
|
||||
#define MATERIAL mesh.getMaterial(i)
|
||||
u32 newLength = texturesCount + mesh.getMaterialsCount();
|
||||
increaseArray(newLength);
|
||||
for (u8 i = newLength - mesh.getMaterialsCount(); i < newLength; i++)
|
||||
for (u32 i = 0; i < mesh.getMaterialsCount(); i++)
|
||||
{
|
||||
textures[i] = new MeshTexture();
|
||||
loader.load(*textures[i], t_path, MATERIAL.getName(), ".bmp");
|
||||
textures[i]->setName(mesh.getMaterial(i).getName());
|
||||
textures[i]->addLink(mesh.id, MATERIAL.getId());
|
||||
MeshTexture *texture = new MeshTexture();
|
||||
loader.load(*texture, t_path, mesh.getMaterial(i).getName(), ".bmp");
|
||||
texture->setName(mesh.getMaterial(i).getName());
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -66,6 +66,7 @@ void calcSpiral(int X, int Y)
|
||||
// TEST END
|
||||
|
||||
#include "models/mesh_frame.hpp"
|
||||
#include <vector>
|
||||
|
||||
void Ari::onInit()
|
||||
{
|
||||
@@ -94,10 +95,11 @@ void Ari::onInit()
|
||||
Vector3 testpos = Vector3(0.0F, 10.0F, 0.0F);
|
||||
test->loadObj("objanim/", "untitled", testpos, 10.0F, 2);
|
||||
texRepo->addByMesh("objanim/", *test);
|
||||
MeshTexture **textures = texRepo->getAll();
|
||||
textures[0]->removeLink(test->id, test->getMaterial(0).getId());
|
||||
std::vector<MeshTexture *> *textures = texRepo->getAll();
|
||||
textures->at(0)->removeLink(test->id, test->getMaterial(0).getId());
|
||||
MeshTexture *tex = texRepo->add("objanim/", "water");
|
||||
tex->addLink(test->id, test->getMaterial(0).getId());
|
||||
texRepo->removeById(textures->at(0)->getId());
|
||||
test->playAnimation(0, 1);
|
||||
test->shouldBeBackfaceCulled = true;
|
||||
// test->shouldBeFrustumCulled = true;
|
||||
|
||||
Reference in New Issue
Block a user