id generation texture

This commit is contained in:
Guido Diego Quispe Robles
2024-01-18 21:53:38 -03:00
parent 0c0fcd697f
commit d773173ece
4 changed files with 39 additions and 2 deletions
@@ -16,6 +16,7 @@
#include "./models/texture.hpp"
#include "renderer/3d/mesh/mesh.hpp"
#include "renderer/core/2d/sprite/sprite.hpp"
#include "./renderer_core_texture_buffers.hpp"
#include "loaders/texture/base/texture_loader_selector.hpp"
#include <string>
@@ -25,6 +26,8 @@ class TextureRepository {
TextureRepository();
~TextureRepository();
void init(std::vector<RendererCoreTextureBuffers>* textureBuffers);
/** Returns all repository textures. */
std::vector<Texture*>* getAll() { return &textures; }
@@ -99,6 +102,13 @@ class TextureRepository {
void freeByMesh(const Mesh& mesh);
void freeByMesh(const Mesh* mesh);
/**
* remove texture buffer id if exist.
* Texture buffer is NOT destructed.
* easy way to create another texture buffer.
*/
int removeBufferId(const u32& t_texId);
/**
* Remove texture from repository.
* Texture is NOT destructed.
@@ -110,6 +120,7 @@ class TextureRepository {
void removeByIndex(const u32& t_index);
std::vector<Texture*> textures;
std::vector<RendererCoreTextureBuffers>* textureBuffers;
TextureLoaderSelector texLoaderSelector;
};
} // namespace Tyra
@@ -19,8 +19,16 @@
namespace Tyra {
u32 textureCounter = 0;
std::vector<u32> deletedIDs;
Texture::Texture(TextureBuilderData* t_data) {
id = rand() % 1000000;
if (deletedIDs.empty() == false) {
id = deletedIDs.front();
deletedIDs.erase(deletedIDs.begin());
} else {
id = textureCounter++;
}
name = t_data->name;
@@ -50,6 +58,7 @@ Texture::Texture(TextureBuilderData* t_data) {
}
Texture::~Texture() {
deletedIDs.push_back(id);
if (links.size() > 0) links.clear();
if (core) delete core;
if (clut) delete clut;
@@ -19,6 +19,7 @@ RendererCoreTexture::~RendererCoreTexture() {}
void RendererCoreTexture::init(RendererCoreGS* t_gs, Path3* t_path3) {
gs = t_gs;
sender.init(t_path3, t_gs);
repository.init(&currentAllocations);
path3 = t_path3;
initClut();
}
@@ -22,6 +22,11 @@ TextureRepository::~TextureRepository() {
}
}
void TextureRepository::init(
std::vector<RendererCoreTextureBuffers>* t_textureBuffers) {
textureBuffers = t_textureBuffers;
}
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];
@@ -57,10 +62,20 @@ void TextureRepository::removeByIndex(const u32& t_index) {
textures.erase(textures.begin() + t_index);
}
int TextureRepository::removeBufferId(const u32& t_texId) {
for (u32 i = 0; i < textureBuffers->size(); i++)
if ((*textureBuffers)[i].id == t_texId) {
(*textureBuffers)[i].id = -1;
return 0;
}
return -1;
}
void TextureRepository::removeById(const u32& t_texId) {
s32 index = getIndexOf(t_texId);
TYRA_ASSERT(index != -1, "Cant remove texture, because it was not found!");
removeByIndex(index);
removeBufferId(t_texId);
}
void TextureRepository::freeByMesh(const Mesh& mesh) {
@@ -93,6 +108,7 @@ void TextureRepository::free(const u32& t_texId) {
TYRA_ASSERT(index != -1, "Cant remove texture, because it was not found!");
removeByIndex(index);
removeBufferId(t_texId);
delete tex;
}
@@ -116,7 +132,7 @@ void TextureRepository::addByMesh(const Mesh* mesh, const char* directory,
if (dirFixed.back() != '/' && dirFixed.back() != ':') dirFixed += "/";
for (u32 i = 0; i < mesh->materials.size(); i++) {
if (!mesh->materials[i]->textureName.has_value()) {
if (!mesh->materials[i]->textureName.has_value()) {
continue;
}