From 4a91f12ff0ed21b76b42f5702fafc6532c5551d5 Mon Sep 17 00:00:00 2001 From: blockmaster2000 Date: Thu, 17 Nov 2016 17:49:11 +0100 Subject: [PATCH] Fix selectionmesh being always animated --- src/selection_mesh.cpp | 87 +++++++++++++++++++++--------------------- src/selection_mesh.h | 15 +------- 2 files changed, 46 insertions(+), 56 deletions(-) diff --git a/src/selection_mesh.cpp b/src/selection_mesh.cpp index 6f5cea5..e8bb4f4 100644 --- a/src/selection_mesh.cpp +++ b/src/selection_mesh.cpp @@ -118,6 +118,7 @@ void selection_draw(video::IVideoDriver* driver, Client &client, v3s16 camera_of bool render_trilinear = g_settings->getBool("trilinear_filter"); bool render_bilinear = g_settings->getBool("bilinear_filter"); bool render_anisotropic = g_settings->getBool("anisotropic_filter"); + bool anim_textures = g_settings->getBool("enable_animated_textures"); for (std::vector::iterator i = meshes.begin(); i != meshes.end(); i++) { SelectionMesh *mesh = *i; @@ -129,50 +130,11 @@ void selection_draw(video::IVideoDriver* driver, Client &client, v3s16 camera_of if (cos_changed) translateMesh(m, cos_diff); + if (anim_textures && mesh->isAnimated()) + mesh->animate(client.getAnimationTime()); + u32 c = m->getMeshBufferCount(); - if (mesh->isAnimated()) { - std::map anim_data = mesh->getAnimationData(); - for (std::map::iterator it = anim_data.begin(); - it != anim_data.end(); ++it) { - - AnimationData temp_data = it->second; - const TileSpec &tile = temp_data.tile; - - // Figure out current frame - int frame = (int)(client.getAnimationTime() * 1000 / tile.animation_frame_length_ms) % tile.animation_frame_count; - - // If frame doesn't change, skip - if (frame == temp_data.frame)// || temp_data.frame < 0) - continue; - - temp_data.frame = frame; - - anim_data[it->first] = temp_data; - - // Make sure we don't cause an overflow. Can get removed if future is no problems occuring - if (it->first >= c) { - errorstream << ": animate() Tying to index non existent Buffer." << std::endl; - return; - } - - scene::IMeshBuffer *buf = m->getMeshBuffer(it->first); - - // Create new texture name from original - if (g_texturesource && frame >= 0) { - std::ostringstream os(std::ios::binary); - os << g_texturesource->getTextureName(tile.texture.id); - os << "^[verticalframe:" << (int)tile.animation_frame_count << ":" << frame; - // Set the texture - AtlasPointer ap = g_texturesource->getTexture(os.str()); - buf->getMaterial().setTexture(0, ap.atlas); - } - } - - // update mesh data - mesh->setAnimationData(anim_data); - } - for (u32 i=0; igetMeshBuffer(i); if (buf == NULL) @@ -196,6 +158,45 @@ SelectionMesh::SelectionMesh(MeshMakeData *data): generate(data); } +void SelectionMesh::animate(float time) +{ + for (std::map::iterator it = m_animation_data.begin(); + it != m_animation_data.end(); ++it) { + + AnimationData temp_data = it->second; + const TileSpec &tile = temp_data.tile; + + // Figure out current frame + int frame = (int)(time * 1000 / tile.animation_frame_length_ms) % tile.animation_frame_count; + + // If frame doesn't change, skip + if (frame == temp_data.frame)// || temp_data.frame < 0) + continue; + + temp_data.frame = frame; + + m_animation_data[it->first] = temp_data; + + // Make sure we don't cause an overflow. Can get removed if future is no problems occuring + if (it->first >= m_mesh->getMeshBufferCount()) { + errorstream << ": animate() Tying to index non existent Buffer." << std::endl; + return; + } + + scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(it->first); + + // Create new texture name from original + if (g_texturesource && frame >= 0) { + std::ostringstream os(std::ios::binary); + os << g_texturesource->getTextureName(tile.texture.id); + os << "^[verticalframe:" << (int)tile.animation_frame_count << ":" << frame; + // Set the texture + AtlasPointer ap = g_texturesource->getTexture(os.str()); + buf->getMaterial().setTexture(0, ap.atlas); + } + } +} + SelectionMesh::~SelectionMesh() { m_mesh->drop(); @@ -335,7 +336,7 @@ void SelectionMesh::generate(MeshMakeData *data) m_meshdata.swap(data->m_meshdata); refresh(data->m_daynight_ratio); m_mesh->recalculateBoundingBox(); - + animate(0.0); // init first frame END_DEBUG_EXCEPTION_HANDLER(errorstream) } diff --git a/src/selection_mesh.h b/src/selection_mesh.h index 555b922..d31e835 100644 --- a/src/selection_mesh.h +++ b/src/selection_mesh.h @@ -42,24 +42,13 @@ public: void generate(MeshMakeData *data); void refresh(u32 daynight_ratio); + void animate(float time); + bool isAnimated() { return !m_animation_data.empty(); } - std::map getAnimationData() - { - return m_animation_data; - } - - void setAnimationData(std::map new_data) - { - if (!m_animation_data.empty()) - m_animation_data.clear(); - - m_animation_data = new_data; - } - private: v3s16 m_pos; scene::SMesh *m_mesh;