Don't abuse the use of std::shared_ptr for moving texture

This commit is contained in:
Benau 2018-01-18 13:49:35 +08:00
parent 8a5994b9e3
commit 5b440ae04b
9 changed files with 30 additions and 44 deletions

View File

@ -33,6 +33,7 @@ MovingTexture::MovingTexture(core::matrix4 *matrix, const XMLNode &node)
m_x = 0.0f;
m_y = 0.0f;
m_count = 0.0f;
m_sp_tm = NULL;
if (m_matrix)
{
@ -49,8 +50,6 @@ MovingTexture::MovingTexture(core::matrix4 *matrix, const XMLNode &node)
node.get("dt", &m_dt);
node.get("animByStep", &m_isAnimatedByStep);
m_sp_tm[0].reset(new float(0.0f));
m_sp_tm[1].reset(new float(0.0f));
} // MovingTexture
//-----------------------------------------------------------------------------
@ -71,8 +70,8 @@ MovingTexture::MovingTexture(core::matrix4 *matrix, float dx, float dy)
core::vector3df v = m_matrix->getTranslation();
m_x = v.X;
m_y = v.Y;
m_sp_tm[0].reset(new float(0.0f));
m_sp_tm[1].reset(new float(0.0f));
m_count = 0.0f;
m_sp_tm = NULL;
} // MovingTexture
//-----------------------------------------------------------------------------
@ -85,9 +84,9 @@ MovingTexture::MovingTexture(float dx, float dy)
m_dy = dy;
m_x = 0;
m_y = 0;
m_count = 0.0f;
m_matrix = NULL;
m_sp_tm[0].reset(new float(0.0f));
m_sp_tm[1].reset(new float(0.0f));
m_sp_tm = NULL;
} // MovingTexture
//-----------------------------------------------------------------------------
@ -103,12 +102,15 @@ MovingTexture::~MovingTexture()
void MovingTexture::reset()
{
m_x = m_y = 0;
*m_sp_tm[0] = 0;
*m_sp_tm[1] = 0;
if (m_matrix)
{
m_matrix->setTextureTranslate(m_x, m_y);
}
else if (m_sp_tm)
{
m_sp_tm[0] = 0.0f;
m_sp_tm[1] = 0.0f;
}
} // reset
//-----------------------------------------------------------------------------
@ -134,10 +136,10 @@ void MovingTexture::update(float dt)
{
m_matrix->setTextureTranslate(m_x, m_y);
}
else
else if (m_sp_tm)
{
*m_sp_tm[0] = m_x;
*m_sp_tm[1] = m_y;
m_sp_tm[0] = m_x;
m_sp_tm[1] = m_y;
}
}
}
@ -151,10 +153,10 @@ void MovingTexture::update(float dt)
{
m_matrix->setTextureTranslate(m_x, m_y);
}
else
else if (m_sp_tm)
{
*m_sp_tm[0] = m_x;
*m_sp_tm[1] = m_y;
m_sp_tm[0] = m_x;
m_sp_tm[1] = m_y;
}
}
} // update

View File

@ -20,8 +20,6 @@
#define MOVING_TEXTURE_HPP
#include "utils/no_copy.hpp"
#include <array>
#include <memory>
#include <string>
#include <matrix4.h>
using namespace irr;
@ -49,7 +47,7 @@ private:
core::matrix4 *m_matrix;
// For sp
std::array<std::shared_ptr<float>, 2> m_sp_tm;
float* m_sp_tm;
public:
MovingTexture(core::matrix4 *matrix, const XMLNode &node);
@ -61,7 +59,7 @@ public:
void setSpeed(float dx, float dy) {m_dx = dx; m_dy = dy;}
/** Sets the texture matrix. */
void setTextureMatrix(core::matrix4 *matrix) {m_matrix=matrix;}
std::array<std::shared_ptr<float>, 2> getSPTM() const { return m_sp_tm; }
void setSPTM(float* sp_tm) { m_sp_tm = sp_tm; }
virtual void update (float dt);
virtual void reset ();
}

View File

@ -248,9 +248,7 @@ void Referee::selectReadySetGo(int rsg)
SP::SPMeshNode* spmn = dynamic_cast<SP::SPMeshNode*>(m_scene_node);
if (spmn)
{
spmn->setTextureMatrix(m_st_traffic_buffer,
{{ std::make_shared<float>(0.0f),
std::make_shared<float>(rsg * 0.333f) }});
spmn->setTextureMatrix(m_st_traffic_buffer, {{ 0.0f, rsg * 0.333f }});
}
else
{

View File

@ -59,7 +59,7 @@ SlipStream::SlipStream(AbstractKart* kart) : MovingTexture(0, 0), m_kart(kart)
m_node->setVisible(false);
SP::SPMeshNode* spmn = dynamic_cast<SP::SPMeshNode*>(m_node);
assert(spmn);
spmn->setTextureMatrix(0, getSPTM());
setSPTM(spmn->getTextureMatrix(0).data());
}
#endif

View File

@ -1260,16 +1260,9 @@ void addObject(SPMeshNode* node)
float hue = node->getRenderInfo(m) ?
node->getRenderInfo(m)->getHue() : 0.0f;
float tm_x = 0.0f;
float tm_y = 0.0f;
const auto& ret = node->getTextureMatrix(m);
if (ret[0] && ret[1])
{
tm_x = *ret[0];
tm_y = *ret[1];
}
SPInstancedData id = SPInstancedData
(node->getAbsoluteTransformation(), tm_x, tm_y, hue,
(node->getAbsoluteTransformation(), node->getTextureMatrix(m)[0],
node->getTextureMatrix(m)[1], hue,
(short)node->getSkinningOffset());
for (int dc_type = 0; dc_type < (g_handle_shadow ? 5 : 1); dc_type++)

View File

@ -88,7 +88,8 @@ void SPMeshNode::setMesh(irr::scene::IAnimatedMesh* mesh)
cleanRenderInfo();
if (m_mesh)
{
m_texture_matrices.resize(m_mesh->getMeshBufferCount());
m_texture_matrices.resize(m_mesh->getMeshBufferCount(),
{{ 0.0f, 0.0f }});
if (!m_mesh->isStatic())
{
#ifndef SERVER_ONLY

View File

@ -56,7 +56,7 @@ private:
video::SColorf m_glow_color;
std::vector<std::array<std::shared_ptr<float>, 2> > m_texture_matrices;
std::vector<std::array<float, 2> > m_texture_matrices;
// ------------------------------------------------------------------------
void cleanRenderInfo();
@ -143,14 +143,13 @@ public:
m_glow_color.b == 0.0f);
}
// ------------------------------------------------------------------------
std::array<std::shared_ptr<float>, 2>& getTextureMatrix(unsigned mb_id)
std::array<float, 2>& getTextureMatrix(unsigned mb_id)
{
assert(mb_id < m_texture_matrices.size());
return m_texture_matrices[mb_id];
}
// ------------------------------------------------------------------------
void setTextureMatrix(unsigned mb_id,
std::array<std::shared_ptr<float>, 2> tm)
void setTextureMatrix(unsigned mb_id, const std::array<float, 2>& tm)
{
assert(mb_id < m_texture_matrices.size());
m_texture_matrices[mb_id] = tm;

View File

@ -1135,13 +1135,8 @@ void KartModel::update(float dt, float distance, float steer, float speed,
for (unsigned i = 0; i < spmn->getSPM()->getMeshBufferCount(); i++)
{
auto& ret = spmn->getTextureMatrix(i);
if (!ret[0] || !ret[1])
{
ret[0].reset(new float);
ret[1].reset(new float);
}
*ret[0] = obj.m_texture_cur_offset.X;
*ret[1] = obj.m_texture_cur_offset.Y;
ret[0] = obj.m_texture_cur_offset.X;
ret[1] = obj.m_texture_cur_offset.Y;
}
}
else

View File

@ -1480,7 +1480,7 @@ void Track::handleAnimatedTextures(scene::ISceneNode *node, const XMLNode &xml)
spmb->enableTextureMatrix(j);
MovingTexture* mt =
new MovingTexture(NULL, *texture_node);
spmn->setTextureMatrix(i, mt->getSPTM());
mt->setSPTM(spmn->getTextureMatrix(i).data());
m_animated_textures.push_back(mt);
// For spm only 1 texture matrix per mesh buffer is
// possible