diff --git a/rwengine/src/data/ModelData.hpp b/rwengine/src/data/ModelData.hpp index 25f8c7cb..237f5985 100644 --- a/rwengine/src/data/ModelData.hpp +++ b/rwengine/src/data/ModelData.hpp @@ -135,14 +135,14 @@ public: } /// @todo change with librw - void setAtomic(Clump* model, int n, AtomicPtr atomic) { + void setAtomic(ClumpPtr model, int n, AtomicPtr atomic) { model_ = model; /// @todo disassociated the Atomic from Clump atomics_[n] = atomic; } /// @todo remove this - Clump* getModel() const { + ClumpPtr getModel() const { return model_; } @@ -178,11 +178,10 @@ public: } bool isLoaded() const override { - return model_ != nullptr; + return model_.get() != nullptr; } void unload() override { - delete model_; model_ = nullptr; } @@ -249,7 +248,7 @@ public: } private: - Clump* model_ = nullptr; + ClumpPtr model_; std::array atomics_; float loddistances_[3] = {}; uint8_t numatomics_ = 0; @@ -280,25 +279,24 @@ public: ClumpModelInfo(ModelDataType type) : BaseModelInfo(type) { } - void setModel(Clump* model) { + void setModel(ClumpPtr model) { model_ = model; } - Clump* getModel() const { + ClumpPtr getModel() const { return model_; } bool isLoaded() const override { - return model_ != nullptr; + return model_.get() != nullptr; } void unload() override { - delete model_; model_ = nullptr; } private: - Clump* model_ = nullptr; + ClumpPtr model_ = nullptr; }; /** diff --git a/rwengine/src/engine/Animator.cpp b/rwengine/src/engine/Animator.cpp index 3f302b17..11453ec1 100644 --- a/rwengine/src/engine/Animator.cpp +++ b/rwengine/src/engine/Animator.cpp @@ -4,7 +4,7 @@ #include #include -Animator::Animator(Clump* model) : model(model) { +Animator::Animator(ClumpPtr model) : model(model) { } void Animator::tick(float dt) { diff --git a/rwengine/src/engine/Animator.hpp b/rwengine/src/engine/Animator.hpp index dc1cd894..709a874a 100644 --- a/rwengine/src/engine/Animator.hpp +++ b/rwengine/src/engine/Animator.hpp @@ -8,7 +8,7 @@ #include #include -class Clump; +#include class ModelFrame; /** @@ -40,7 +40,7 @@ class Animator { /** * @brief model The model being animated. */ - Clump* model; + ClumpPtr model; /** * @brief Currently playing animations @@ -48,7 +48,7 @@ class Animator { std::vector animations; public: - Animator(Clump* model); + Animator(ClumpPtr model); Animation* getAnimation(unsigned int slot) { if (slot < animations.size()) { diff --git a/rwengine/src/engine/GameData.cpp b/rwengine/src/engine/GameData.cpp index 52458125..80e21cad 100644 --- a/rwengine/src/engine/GameData.cpp +++ b/rwengine/src/engine/GameData.cpp @@ -385,7 +385,7 @@ void GameData::getNameAndLod(std::string& name, int& lod) { } } -Clump* GameData::loadClump(const std::string& name) { +ClumpPtr GameData::loadClump(const std::string& name) { auto file = index.openFile(name); if (!file) { logger->error("Data", "Failed to load model " + name); diff --git a/rwengine/src/engine/GameData.hpp b/rwengine/src/engine/GameData.hpp index c6b1f121..b5de5f3d 100644 --- a/rwengine/src/engine/GameData.hpp +++ b/rwengine/src/engine/GameData.hpp @@ -133,7 +133,7 @@ public: /** * Loads an archived model and returns it directly */ - Clump* loadClump(const std::string& name); + ClumpPtr loadClump(const std::string& name); /** * Loads a DFF and associates its atomics with models. diff --git a/rwengine/src/objects/CharacterObject.cpp b/rwengine/src/objects/CharacterObject.cpp index 97aaa0fa..0344b094 100644 --- a/rwengine/src/objects/CharacterObject.cpp +++ b/rwengine/src/objects/CharacterObject.cpp @@ -37,7 +37,7 @@ CharacterObject::CharacterObject(GameWorld* engine, const glm::vec3& pos, setClump(ClumpPtr(info->getModel()->clone())); if (info->getModel()) { setModel(info->getModel()); - animator = new Animator(getClump().get()); + animator = new Animator(getClump()); createActor(); } @@ -279,7 +279,7 @@ void CharacterObject::changeCharacterModel(const std::string& name) { setModel(newmodel); - animator = new Animator(getClump().get()); + animator = new Animator(getClump()); } void CharacterObject::updateCharacter(float dt) { diff --git a/rwengine/src/objects/CutsceneObject.cpp b/rwengine/src/objects/CutsceneObject.cpp index bc35a74e..745e983b 100644 --- a/rwengine/src/objects/CutsceneObject.cpp +++ b/rwengine/src/objects/CutsceneObject.cpp @@ -1,8 +1,8 @@ -#include #include +#include CutsceneObject::CutsceneObject(GameWorld *engine, const glm::vec3 &pos, - const glm::quat &rot, Clump *model, + const glm::quat &rot, ClumpPtr model, BaseModelInfo *modelinfo) : GameObject(engine, pos, rot, modelinfo) , _parent(nullptr) @@ -14,7 +14,7 @@ CutsceneObject::CutsceneObject(GameWorld *engine, const glm::vec3 &pos, setModel(getModelInfo()->getModel()); } setClump(ClumpPtr(getModel()->clone())); - animator = new Animator(getClump().get()); + animator = new Animator(getClump()); } CutsceneObject::~CutsceneObject() { diff --git a/rwengine/src/objects/CutsceneObject.hpp b/rwengine/src/objects/CutsceneObject.hpp index 969043aa..4bd1e825 100644 --- a/rwengine/src/objects/CutsceneObject.hpp +++ b/rwengine/src/objects/CutsceneObject.hpp @@ -12,7 +12,7 @@ class CutsceneObject : public GameObject, public ClumpObject { public: CutsceneObject(GameWorld* engine, const glm::vec3& pos, - const glm::quat& rot, Clump* model, + const glm::quat& rot, ClumpPtr model, BaseModelInfo* modelinfo); ~CutsceneObject(); diff --git a/rwengine/src/objects/GameObject.hpp b/rwengine/src/objects/GameObject.hpp index 5eea8630..5176dc10 100644 --- a/rwengine/src/objects/GameObject.hpp +++ b/rwengine/src/objects/GameObject.hpp @@ -31,7 +31,7 @@ class GameObject { /** * Model used for rendering */ - Clump* model_; + ClumpPtr model_; protected: void changeModelInfo(BaseModelInfo* next) { @@ -102,14 +102,14 @@ public: /** * @return The model used in rendering */ - Clump* getModel() const { + ClumpPtr getModel() const { return model_; } /** * Changes the current model, used for re-dressing chars */ - void setModel(Clump* model) { + void setModel(ClumpPtr model) { model_ = model; } diff --git a/rwengine/src/render/GameRenderer.cpp b/rwengine/src/render/GameRenderer.cpp index 5a3bd50a..7bab1bcb 100644 --- a/rwengine/src/render/GameRenderer.cpp +++ b/rwengine/src/render/GameRenderer.cpp @@ -317,7 +317,7 @@ void GameRenderer::renderWorld(GameWorld* world, const ViewCamera& camera, m, glm::vec3(i.radius + 0.15f * glm::sin(_renderWorld->getGameTime() * 5.f))); - objectRenderer.renderClump(sphereModel, m, nullptr, renderList); + objectRenderer.renderClump(sphereModel.get(), m, nullptr, renderList); } // Render arrows above anything that isn't radar only (or hidden) @@ -344,7 +344,7 @@ void GameRenderer::renderWorld(GameWorld* world, const ViewCamera& camera, glm::vec3(0.f, 0.f, 2.5f + glm::sin(a) * 0.5f)); model = glm::rotate(model, a, glm::vec3(0.f, 0.f, 1.f)); model = glm::scale(model, glm::vec3(1.5f, 1.5f, 1.5f)); - objectRenderer.renderClump(arrowModel, model, nullptr, renderList); + objectRenderer.renderClump(arrowModel.get(), model, nullptr, renderList); } RW_PROFILE_END(); diff --git a/rwengine/src/render/GameRenderer.hpp b/rwengine/src/render/GameRenderer.hpp index 8a4fb4ef..2ab2c45d 100644 --- a/rwengine/src/render/GameRenderer.hpp +++ b/rwengine/src/render/GameRenderer.hpp @@ -62,7 +62,7 @@ class GameRenderer { ViewCamera _camera; ViewCamera cullingCamera; bool cullOverride; - + /** Number of culling events */ size_t culled; @@ -99,7 +99,7 @@ public: GLuint getMissingTexture() const { return m_missingTexture; } - + size_t getCulledCount() { return culled; } @@ -174,16 +174,15 @@ public: * * GameRenderer will take ownership of the Model* pointer */ - void setSpecialModel(SpecialModel usage, Clump* model) { - specialmodels_[usage].reset(model); + void setSpecialModel(SpecialModel usage, ClumpPtr model) { + specialmodels_[usage] = model; } private: /// Hard-coded models to use for each of the special models - std::unique_ptr - specialmodels_[SpecialModel::SpecialModelCount]; - Clump* getSpecialModel(SpecialModel usage) const { - return specialmodels_[usage].get(); + ClumpPtr specialmodels_[SpecialModel::SpecialModelCount]; + ClumpPtr getSpecialModel(SpecialModel usage) const { + return specialmodels_[usage]; } }; diff --git a/rwlib/CMakeLists.txt b/rwlib/CMakeLists.txt index 439ae5f7..0fc419a4 100644 --- a/rwlib/CMakeLists.txt +++ b/rwlib/CMakeLists.txt @@ -17,6 +17,7 @@ SET(RWLIB_SOURCES "source/gl/TextureData.cpp" "source/rw/abort.cpp" + "source/rw/forward.hpp" "source/rw/types.hpp" "source/rw/defines.hpp" diff --git a/rwlib/source/data/Clump.hpp b/rwlib/source/data/Clump.hpp index ee14b3ce..e180080e 100644 --- a/rwlib/source/data/Clump.hpp +++ b/rwlib/source/data/Clump.hpp @@ -12,18 +12,7 @@ #include #include -// Forward Declerations -class ModelFrame; -struct Geometry; -class Atomic; -class Clump; - -// Pointer types -using ModelFramePtr = std::shared_ptr; -using GeometryPtr = std::shared_ptr; -using AtomicPtr = std::shared_ptr; -using AtomicList = std::vector; -using ClumpPtr = std::shared_ptr; +#include /** * ModelFrame stores transformation hierarchy diff --git a/rwlib/source/loaders/LoaderDFF.cpp b/rwlib/source/loaders/LoaderDFF.cpp index b90ba8c5..09ee4a8b 100644 --- a/rwlib/source/loaders/LoaderDFF.cpp +++ b/rwlib/source/loaders/LoaderDFF.cpp @@ -439,8 +439,8 @@ AtomicPtr LoaderDFF::readAtomic(FrameList &framelist, return atomic; } -Clump *LoaderDFF::loadFromMemory(FileHandle file) { - auto model = new Clump; +ClumpPtr LoaderDFF::loadFromMemory(FileHandle file) { + auto model = std::make_shared(); RWBStream rootStream(file->data, file->length); diff --git a/rwlib/source/loaders/LoaderDFF.hpp b/rwlib/source/loaders/LoaderDFF.hpp index a36e0dbc..bbe034f2 100644 --- a/rwlib/source/loaders/LoaderDFF.hpp +++ b/rwlib/source/loaders/LoaderDFF.hpp @@ -29,7 +29,7 @@ public: using GeometryList = std::vector; using FrameList = std::vector; - Clump* loadFromMemory(FileHandle file); + ClumpPtr loadFromMemory(FileHandle file); void setTextureLookupCallback(TextureLookupCallback tlc) { texturelookup = tlc; diff --git a/rwlib/source/rw/forward.hpp b/rwlib/source/rw/forward.hpp new file mode 100644 index 00000000..6820289a --- /dev/null +++ b/rwlib/source/rw/forward.hpp @@ -0,0 +1,22 @@ +#ifndef RWLIB_FORWARD_HPP +#define RWLIB_FORWARD_HPP + +#include +#include + +// Forward Declarations +class Clump; +class ModelFrame; +struct Geometry; +class Atomic; +class Clump; + +// Pointer types +using ModelFramePtr = std::shared_ptr; +using GeometryPtr = std::shared_ptr; +using AtomicPtr = std::shared_ptr; +using AtomicList = std::vector; +using ClumpPtr = std::shared_ptr; + +#endif /* FORWARD_HPP */ + diff --git a/rwviewer/ViewerWidget.cpp b/rwviewer/ViewerWidget.cpp index e3e9a0bc..02d3e614 100644 --- a/rwviewer/ViewerWidget.cpp +++ b/rwviewer/ViewerWidget.cpp @@ -103,7 +103,7 @@ void ViewerWidget::paintGL() { vc.frustum.fov = viewFov; vc.frustum.aspectRatio = width() / (height() * 1.f); - Clump* model = activeModel; + ClumpPtr model = activeModel; if (model != _lastModel) { _lastModel = model; emit modelChanged(_lastModel); @@ -133,7 +133,7 @@ void ViewerWidget::paintGL() { ObjectRenderer renderer(world(), vc, 1.f, 0); RenderList renders; - renderer.renderClump(model, glm::mat4(), nullptr, renders); + renderer.renderClump(model.get(), glm::mat4(), nullptr, renders); r.getRenderer()->drawBatched(renders); drawFrameWidget(model->getFrame().get()); @@ -165,7 +165,7 @@ void ViewerWidget::drawFrameWidget(ModelFrame* f, const glm::mat4& m) { glLineWidth(1.f); } dp.textures = {whiteTex}; - + RW_CHECK(renderer != nullptr, "GameRenderer is null"); if(renderer != nullptr) { renderer->getRenderer()->drawArrays(thisM, _frameWidgetDraw, dp); @@ -207,7 +207,7 @@ void ViewerWidget::showObject(qint16 item) { } } -void ViewerWidget::showModel(Clump* model) { +void ViewerWidget::showModel(ClumpPtr model) { if (dummyObject) gworld->destroyObject(dummyObject); dummyObject = nullptr; activeModel = model; @@ -269,7 +269,7 @@ void ViewerWidget::keyReleaseEvent(QKeyEvent* e) { if (e->key() == Qt::Key_Shift) moveFast = false; } -Clump* ViewerWidget::currentModel() const { +ClumpPtr ViewerWidget::currentModel() const { return activeModel; } diff --git a/rwviewer/ViewerWidget.hpp b/rwviewer/ViewerWidget.hpp index ba216814..f4cd9713 100644 --- a/rwviewer/ViewerWidget.hpp +++ b/rwviewer/ViewerWidget.hpp @@ -27,12 +27,12 @@ class ViewerWidget : public QGLWidget { QTimer timer; GameWorld* gworld; - Clump* activeModel; + ClumpPtr activeModel; ModelFrame* selectedFrame; GameObject* dummyObject; quint16 currentObjectID; - Clump* _lastModel; + ClumpPtr _lastModel; Animation* canimation; float viewDistance; @@ -60,7 +60,7 @@ public: virtual void paintGL(); - Clump* currentModel() const; + ClumpPtr currentModel() const; GameObject* currentObject() const; GameWorld* world(); @@ -68,7 +68,7 @@ public: public slots: void showObject(qint16 item); - void showModel(Clump* model); + void showModel(ClumpPtr model); void selectFrame(ModelFrame* frame); void exportModel(); @@ -81,7 +81,7 @@ signals: void fileOpened(const QString& file); - void modelChanged(Clump* model); + void modelChanged(ClumpPtr model); protected: void keyPressEvent(QKeyEvent*) override; diff --git a/rwviewer/models/DFFFramesTreeModel.cpp b/rwviewer/models/DFFFramesTreeModel.cpp index 3950482c..9af8b116 100644 --- a/rwviewer/models/DFFFramesTreeModel.cpp +++ b/rwviewer/models/DFFFramesTreeModel.cpp @@ -1,7 +1,7 @@ #include "DFFFramesTreeModel.hpp" #include -DFFFramesTreeModel::DFFFramesTreeModel(Clump* m, +DFFFramesTreeModel::DFFFramesTreeModel(ClumpPtr m, QObject* parent) : QAbstractItemModel(parent), model(m) { } diff --git a/rwviewer/models/DFFFramesTreeModel.hpp b/rwviewer/models/DFFFramesTreeModel.hpp index f4ec521f..3e764024 100644 --- a/rwviewer/models/DFFFramesTreeModel.hpp +++ b/rwviewer/models/DFFFramesTreeModel.hpp @@ -2,15 +2,14 @@ #ifndef _DFFFRAMESTREEMODEL_HPP_ #define _DFFFRAMESTREEMODEL_HPP_ #include +#include #include -class Clump; - class DFFFramesTreeModel : public QAbstractItemModel { - Clump* model; + ClumpPtr model; public: - explicit DFFFramesTreeModel(Clump* m, QObject* parent = 0); + explicit DFFFramesTreeModel(ClumpPtr m, QObject* parent = 0); virtual int columnCount(const QModelIndex& parent = QModelIndex()) const; diff --git a/rwviewer/views/ModelViewer.cpp b/rwviewer/views/ModelViewer.cpp index be210123..fdae9346 100644 --- a/rwviewer/views/ModelViewer.cpp +++ b/rwviewer/views/ModelViewer.cpp @@ -39,7 +39,7 @@ void ModelViewer::setViewerWidget(ViewerWidget* widget) { showModel(viewing); } -void ModelViewer::showModel(Clump* model) { +void ModelViewer::showModel(ClumpPtr model) { viewing = model; viewerWidget->showModel(model); frames->setModel(model); diff --git a/rwviewer/views/ModelViewer.hpp b/rwviewer/views/ModelViewer.hpp index c8085c6d..1db3b028 100644 --- a/rwviewer/views/ModelViewer.hpp +++ b/rwviewer/views/ModelViewer.hpp @@ -21,7 +21,7 @@ class Animation; class ModelViewer : public ViewerInterface { Q_OBJECT - Clump* viewing; + ClumpPtr viewing; QSplitter* mainSplit; QVBoxLayout* mainLayout; @@ -43,7 +43,7 @@ public slots: /** * Display a raw model */ - void showModel(Clump* model); + void showModel(ClumpPtr model); /** * Display a game object's model diff --git a/rwviewer/widgets/ModelFramesWidget.cpp b/rwviewer/widgets/ModelFramesWidget.cpp index 51720ce5..e2312db0 100644 --- a/rwviewer/widgets/ModelFramesWidget.cpp +++ b/rwviewer/widgets/ModelFramesWidget.cpp @@ -2,7 +2,7 @@ #include #include -void ModelFramesWidget::updateInfoBox(Clump* model, ModelFrame* f) { +void ModelFramesWidget::updateInfoBox(ClumpPtr model, ModelFrame* f) { if (f == nullptr) { _frameLabel->setText(""); } else { @@ -33,14 +33,14 @@ ModelFramesWidget::ModelFramesWidget(QWidget* parent, Qt::WindowFlags flags) setLayout(_layout); } -void ModelFramesWidget::setModel(Clump* model) { +void ModelFramesWidget::setModel(ClumpPtr model) { if (framemodel) { delete framemodel; framemodel = nullptr; tree->setModel(nullptr); } gmodel = model; - if (model != nullptr) { + if (model.get() != nullptr) { framemodel = new DFFFramesTreeModel(model, this); tree->setModel(framemodel); tree->setDisabled(false); diff --git a/rwviewer/widgets/ModelFramesWidget.hpp b/rwviewer/widgets/ModelFramesWidget.hpp index 3bdf48f8..53bb0783 100644 --- a/rwviewer/widgets/ModelFramesWidget.hpp +++ b/rwviewer/widgets/ModelFramesWidget.hpp @@ -13,7 +13,7 @@ class ModelFrame; class ModelFramesWidget : public QWidget { Q_OBJECT - Clump* gmodel; + ClumpPtr gmodel; DFFFramesTreeModel* framemodel; QTreeView* tree; QVBoxLayout* _layout; @@ -21,7 +21,7 @@ class ModelFramesWidget : public QWidget { private slots: - void updateInfoBox(Clump* model, ModelFrame* f); + void updateInfoBox(ClumpPtr model, ModelFrame* f); void selectedModelChanged(const QModelIndex&, const QModelIndex&); @@ -30,7 +30,7 @@ public: public slots: - void setModel(Clump* model); + void setModel(ClumpPtr model); signals: diff --git a/tests/test_loaderdff.cpp b/tests/test_loaderdff.cpp index 2875f16c..8217c6b4 100644 --- a/tests/test_loaderdff.cpp +++ b/tests/test_loaderdff.cpp @@ -11,9 +11,9 @@ BOOST_AUTO_TEST_CASE(test_load_dff) { LoaderDFF loader; - Clump* m = loader.loadFromMemory(d); + auto m = loader.loadFromMemory(d); - BOOST_REQUIRE(m != nullptr); + BOOST_REQUIRE(m.get() != nullptr); BOOST_REQUIRE(m->getFrame()); @@ -23,7 +23,7 @@ BOOST_AUTO_TEST_CASE(test_load_dff) { BOOST_REQUIRE(atomic->getGeometry()); BOOST_REQUIRE(atomic->getFrame()); - delete m; + m.reset(); } }