Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refacto: don't use RenderingEngine singleton on CAO
* we don't need on CAO side more than SceneManager, and temporary. Pass only required SceneManager as a parameter to build CAO and add them to the current scene
* Use temporary the RenderingEngine singleton from ClientEnvironment, waitfor for better solution
* Make ClientActiveObject::addToScene virtual function mandatory to be defined by children to ensure we don't forget to properly define it
  • Loading branch information
nerzhul committed May 3, 2021
1 parent 1bc8556 commit 809e68f
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/client/clientenvironment.cpp
Expand Up @@ -352,7 +352,7 @@ u16 ClientEnvironment::addActiveObject(ClientActiveObject *object)
if (!m_ao_manager.registerObject(object))
return 0;

object->addToScene(m_texturesource);
object->addToScene(m_texturesource, RenderingEngine::get_scene_manager());

// Update lighting immediately
object->updateLight(getDayNightRatio());
Expand Down
6 changes: 5 additions & 1 deletion src/client/clientobject.h
Expand Up @@ -33,13 +33,17 @@ class LocalPlayer;
struct ItemStack;
class WieldMeshSceneNode;

namespace irr { namespace scene {
class ISceneManager;
}}

class ClientActiveObject : public ActiveObject
{
public:
ClientActiveObject(u16 id, Client *client, ClientEnvironment *env);
virtual ~ClientActiveObject();

virtual void addToScene(ITextureSource *tsrc) {}
virtual void addToScene(ITextureSource *tsrc, irr::scene::ISceneManager *smgr) = 0;
virtual void removeFromScene(bool permanent) {}

virtual void updateLight(u32 day_night_ratio) {}
Expand Down
30 changes: 12 additions & 18 deletions src/client/content_cao.cpp
Expand Up @@ -24,7 +24,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <IMeshManipulator.h>
#include <IAnimatedMeshSceneNode.h>
#include "client/client.h"
#include "client/renderingengine.h"
#include "client/sound.h"
#include "client/tile.h"
#include "util/basic_macros.h"
Expand Down Expand Up @@ -189,7 +188,7 @@ class TestCAO : public ClientActiveObject

static ClientActiveObject* create(Client *client, ClientEnvironment *env);

void addToScene(ITextureSource *tsrc);
void addToScene(ITextureSource *tsrc, irr::scene::ISceneManager *smgr);
void removeFromScene(bool permanent);
void updateLight(u32 day_night_ratio);
void updateNodePos();
Expand Down Expand Up @@ -220,7 +219,7 @@ ClientActiveObject* TestCAO::create(Client *client, ClientEnvironment *env)
return new TestCAO(client, env);
}

void TestCAO::addToScene(ITextureSource *tsrc)
void TestCAO::addToScene(ITextureSource *tsrc, irr::scene::ISceneManager *smgr)
{
if(m_node != NULL)
return;
Expand Down Expand Up @@ -249,7 +248,7 @@ void TestCAO::addToScene(ITextureSource *tsrc)
// Add to mesh
mesh->addMeshBuffer(buf);
buf->drop();
m_node = RenderingEngine::get_scene_manager()->addMeshSceneNode(mesh, NULL);
m_node = smgr->addMeshSceneNode(mesh, NULL);
mesh->drop();
updateNodePos();
}
Expand Down Expand Up @@ -591,9 +590,9 @@ void GenericCAO::removeFromScene(bool permanent)
m_client->getMinimap()->removeMarker(&m_marker);
}

void GenericCAO::addToScene(ITextureSource *tsrc)
void GenericCAO::addToScene(ITextureSource *tsrc, irr::scene::ISceneManager *smgr)
{
m_smgr = RenderingEngine::get_scene_manager();
m_smgr = smgr;

if (getSceneNode() != NULL) {
return;
Expand Down Expand Up @@ -625,8 +624,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
}

auto grabMatrixNode = [this] {
m_matrixnode = RenderingEngine::get_scene_manager()->
addDummyTransformationSceneNode();
m_matrixnode = m_smgr->addDummyTransformationSceneNode();
m_matrixnode->grab();
};

Expand All @@ -644,7 +642,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)

if (m_prop.visual == "sprite") {
grabMatrixNode();
m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
m_spritenode = m_smgr->addBillboardSceneNode(
m_matrixnode, v2f(1, 1), v3f(0,0,0), -1);
m_spritenode->grab();
m_spritenode->setMaterialTexture(0,
Expand Down Expand Up @@ -729,8 +727,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
mesh->addMeshBuffer(buf);
buf->drop();
}
m_meshnode = RenderingEngine::get_scene_manager()->
addMeshSceneNode(mesh, m_matrixnode);
m_meshnode = m_smgr->addMeshSceneNode(mesh, m_matrixnode);
m_meshnode->grab();
mesh->drop();
// Set it to use the materials of the meshbuffers directly.
Expand All @@ -739,8 +736,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
} else if (m_prop.visual == "cube") {
grabMatrixNode();
scene::IMesh *mesh = createCubeMesh(v3f(BS,BS,BS));
m_meshnode = RenderingEngine::get_scene_manager()->
addMeshSceneNode(mesh, m_matrixnode);
m_meshnode = m_smgr->addMeshSceneNode(mesh, m_matrixnode);
m_meshnode->grab();
mesh->drop();

Expand All @@ -753,8 +749,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
grabMatrixNode();
scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh, true);
if (mesh) {
m_animated_meshnode = RenderingEngine::get_scene_manager()->
addAnimatedMeshSceneNode(mesh, m_matrixnode);
m_animated_meshnode = m_smgr->addAnimatedMeshSceneNode(mesh, m_matrixnode);
m_animated_meshnode->grab();
mesh->drop(); // The scene node took hold of it

Expand Down Expand Up @@ -795,8 +790,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
infostream << "serialized form: " << m_prop.wield_item << std::endl;
item.deSerialize(m_prop.wield_item, m_client->idef());
}
m_wield_meshnode = new WieldMeshSceneNode(
RenderingEngine::get_scene_manager(), -1);
m_wield_meshnode = new WieldMeshSceneNode(m_smgr, -1);
m_wield_meshnode->setItem(item, m_client,
(m_prop.visual == "wielditem"));

Expand Down Expand Up @@ -1074,7 +1068,7 @@ void GenericCAO::step(float dtime, ClientEnvironment *env)
}

removeFromScene(false);
addToScene(m_client->tsrc());
addToScene(m_client->tsrc(), m_smgr);

// Attachments, part 2: Now that the parent has been refreshed, put its attachments back
for (u16 cao_id : m_attachment_child_ids) {
Expand Down
2 changes: 1 addition & 1 deletion src/client/content_cao.h
Expand Up @@ -236,7 +236,7 @@ class GenericCAO : public ClientActiveObject

void removeFromScene(bool permanent);

void addToScene(ITextureSource *tsrc);
void addToScene(ITextureSource *tsrc, irr::scene::ISceneManager *smgr);

inline void expireVisuals()
{
Expand Down
1 change: 1 addition & 0 deletions src/unittest/test_clientactiveobjectmgr.cpp
Expand Up @@ -29,6 +29,7 @@ class TestClientActiveObject : public ClientActiveObject
TestClientActiveObject() : ClientActiveObject(0, nullptr, nullptr) {}
~TestClientActiveObject() = default;
ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_TEST; }
virtual void addToScene(ITextureSource *tsrc, irr::scene::ISceneManager *smgr) {}
};

class TestClientActiveObjectMgr : public TestBase
Expand Down

0 comments on commit 809e68f

Please sign in to comment.