added texture swapping
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
#include <draw_sampling.h>
|
||||
#include "./texture_wrap_settings.hpp"
|
||||
#include "./texture_link.hpp"
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* Class which contains texture data.
|
||||
@@ -44,7 +45,7 @@ public:
|
||||
|
||||
const u8 &getHeight() const { return height; };
|
||||
|
||||
const u32 &getTextureLinksCount() const { return texLinkCount; };
|
||||
u32 getTextureLinksCount() const { return static_cast<u32>(texLinks.size()); };
|
||||
|
||||
texwrap_t *getWrapSettings() { return &wrapSettings; };
|
||||
|
||||
@@ -67,7 +68,19 @@ public:
|
||||
char *getName() const { return name; };
|
||||
|
||||
/** Array of texture links. Size of getTextureLinksCount() */
|
||||
TextureLink *getTextureLinks() const { return texLinks; };
|
||||
const std::vector<TextureLink> &getTextureLinks() const { return texLinks; };
|
||||
|
||||
/**
|
||||
* Returns index of link.
|
||||
* -1 if not found.
|
||||
*/
|
||||
const s32 getIndexOfLink(const u32 &t_meshId, const u32 &t_materialId) const
|
||||
{
|
||||
for (u32 i = 0; i < texLinks.size(); i++)
|
||||
if (texLinks[i].materialId == t_materialId && texLinks[i].meshId == t_meshId)
|
||||
return i;
|
||||
return -1;
|
||||
};
|
||||
|
||||
// ----
|
||||
// Setters
|
||||
@@ -108,27 +121,33 @@ public:
|
||||
/** Assign texture to mesh and mesh material */
|
||||
void addLink(const u32 &t_meshId, const u32 &t_materialId);
|
||||
|
||||
/** Remove mesh and mesh material assignment */
|
||||
void removeLink(const u32 &t_meshId, const u32 &t_materialId);
|
||||
|
||||
const u8 &isNameSet() const { return _isNameSet; };
|
||||
|
||||
const u8 &isSizeSet() const { return _isSizeSet; };
|
||||
|
||||
const u8 isLinkedWith(const u32 &t_meshId, const u32 &t_materialId) const
|
||||
{
|
||||
for (u32 i = 0; i < texLinkCount; i++)
|
||||
if (texLinks[i].materialId == t_materialId && texLinks[i].meshId == t_meshId)
|
||||
return true;
|
||||
return false;
|
||||
s32 index = getIndexOfLink(t_meshId, t_materialId);
|
||||
if (index != -1)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
};
|
||||
|
||||
void removeLink(const u32 &t_index) { texLinks.erase(texLinks.begin() + t_index); }
|
||||
|
||||
void removeLink(const u32 &t_meshId, const u32 &t_materialId)
|
||||
{
|
||||
u32 index = getIndexOfLink(t_meshId, t_materialId);
|
||||
removeLink(index);
|
||||
}
|
||||
|
||||
private:
|
||||
void setDefaultWrapSettings();
|
||||
texwrap_t wrapSettings;
|
||||
char *name;
|
||||
TextureLink *texLinks;
|
||||
u32 id, texLinkCount;
|
||||
std::vector<TextureLink> texLinks;
|
||||
u32 id;
|
||||
u8 width, height, _isNameSet, _isSizeSet;
|
||||
unsigned char *data;
|
||||
};
|
||||
|
||||
@@ -40,7 +40,10 @@ void BmpLoader::load(MeshTexture &o_texture, char *t_subfolder, char *t_name, ch
|
||||
FILE *file = fopen(t_path, "rb");
|
||||
|
||||
if (file == NULL)
|
||||
{
|
||||
PRINT_ERR("Failed to load .bmp file!");
|
||||
return;
|
||||
}
|
||||
|
||||
unsigned char header[54];
|
||||
fread(header, sizeof(unsigned char), 54, file);
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
MeshTexture::MeshTexture()
|
||||
{
|
||||
id = rand() % 1000000;
|
||||
texLinkCount = 0;
|
||||
_isSizeSet = false;
|
||||
_isNameSet = false;
|
||||
setDefaultWrapSettings();
|
||||
@@ -29,8 +28,8 @@ MeshTexture::MeshTexture()
|
||||
|
||||
MeshTexture::~MeshTexture()
|
||||
{
|
||||
if (texLinkCount > 0)
|
||||
delete[] texLinks;
|
||||
if (getTextureLinksCount() > 0)
|
||||
texLinks.clear();
|
||||
if (_isNameSet)
|
||||
delete[] name;
|
||||
if (_isSizeSet)
|
||||
@@ -83,31 +82,8 @@ void MeshTexture::setWrapSettings(const WrapSettings t_horizontal, const WrapSet
|
||||
|
||||
void MeshTexture::addLink(const u32 &t_meshId, const u32 &t_materialId)
|
||||
{
|
||||
TextureLink *savedLinks = texLinks;
|
||||
texLinks = new TextureLink[texLinkCount + 1];
|
||||
for (u32 i = 0; i < texLinkCount; i++)
|
||||
{
|
||||
texLinks[i].materialId = savedLinks[i].materialId;
|
||||
texLinks[i].meshId = savedLinks[i].meshId;
|
||||
}
|
||||
if (texLinkCount != 0)
|
||||
delete[] savedLinks;
|
||||
texLinkCount++;
|
||||
texLinks[texLinkCount - 1].materialId = t_materialId;
|
||||
texLinks[texLinkCount - 1].meshId = t_meshId;
|
||||
}
|
||||
|
||||
void MeshTexture::removeLink(const u32 &t_meshId, const u32 &t_materialId)
|
||||
{
|
||||
TextureLink *savedLinks = texLinks;
|
||||
texLinks = new TextureLink[texLinkCount - 1];
|
||||
u32 savedCount = 0;
|
||||
for (u32 i = 0; i < texLinkCount; i++)
|
||||
if (savedLinks[i].materialId != t_materialId && savedLinks[i].meshId != t_meshId)
|
||||
{
|
||||
texLinks[savedCount++].materialId = savedLinks[i].materialId;
|
||||
texLinks[savedCount++].meshId = savedLinks[i].meshId;
|
||||
}
|
||||
delete[] savedLinks;
|
||||
texLinkCount--;
|
||||
TextureLink link;
|
||||
link.materialId = t_materialId;
|
||||
link.meshId = t_meshId;
|
||||
texLinks.push_back(link);
|
||||
}
|
||||
|
||||
@@ -83,6 +83,7 @@ void Renderer::deallocateTextureBuffer()
|
||||
|
||||
void Renderer::changeTexture(Mesh *t_mesh, u32 t_materialId)
|
||||
{
|
||||
|
||||
MeshTexture *tex = textureRepo.getByMesh(t_mesh->id, t_materialId);
|
||||
if (tex != NULL)
|
||||
{
|
||||
|
||||
@@ -28,6 +28,10 @@ TextureRepository::~TextureRepository() {}
|
||||
// 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);
|
||||
|
||||
Reference in New Issue
Block a user