diff --git a/rwengine/src/dynamics/CollisionInstance.cpp b/rwengine/src/dynamics/CollisionInstance.cpp index 344c306d..b0cf5d68 100644 --- a/rwengine/src/dynamics/CollisionInstance.cpp +++ b/rwengine/src/dynamics/CollisionInstance.cpp @@ -154,7 +154,7 @@ bool CollisionInstance::createPhysicsBody(GameObject* object, void CollisionInstance::changeMass(float newMass) { GameObject* object = static_cast(m_body->getUserPointer()); - auto dynamicsWorld = object->engine->dynamicsWorld; + auto& dynamicsWorld = object->engine->dynamicsWorld; dynamicsWorld->removeRigidBody(m_body); btVector3 inert; m_body->getCollisionShape()->calculateLocalInertia(newMass, inert); diff --git a/rwengine/src/engine/GameWorld.cpp b/rwengine/src/engine/GameWorld.cpp index 1b1735ba..49d35fbf 100644 --- a/rwengine/src/engine/GameWorld.cpp +++ b/rwengine/src/engine/GameWorld.cpp @@ -87,12 +87,15 @@ GameWorld::GameWorld(Logger* log, WorkContext* work, GameData* dat) : logger(log), data(dat), randomEngine(rand()), _work(work), paused(false) { data->engine = this; - collisionConfig = new btDefaultCollisionConfiguration; - collisionDispatcher = new WorldCollisionDispatcher(collisionConfig); - broadphase = new btDbvtBroadphase(); - solver = new btSequentialImpulseConstraintSolver; - dynamicsWorld = new btDiscreteDynamicsWorld(collisionDispatcher, broadphase, - solver, collisionConfig); + collisionConfig = std::make_unique(); + collisionDispatcher = + std::make_unique(collisionConfig.get()); + broadphase = std::make_unique(); + solver = std::make_unique(); + dynamicsWorld = std::make_unique( + collisionDispatcher.get(), broadphase.get(), solver.get(), + collisionConfig.get()); + dynamicsWorld->setGravity(btVector3(0.f, 0.f, -9.81f)); broadphase->getOverlappingPairCache()->setInternalGhostPairCallback( new btGhostPairCallback()); @@ -104,14 +107,6 @@ GameWorld::~GameWorld() { for (auto& p : allObjects) { delete p; } - - delete dynamicsWorld; - delete solver; - delete broadphase; - delete collisionDispatcher; - delete collisionConfig; - - /// @todo delete other things. } bool GameWorld::placeItems(const std::string& name) { diff --git a/rwengine/src/engine/GameWorld.hpp b/rwengine/src/engine/GameWorld.hpp index 874d424d..302b2b28 100644 --- a/rwengine/src/engine/GameWorld.hpp +++ b/rwengine/src/engine/GameWorld.hpp @@ -269,11 +269,11 @@ public: /** * Bullet */ - btDefaultCollisionConfiguration* collisionConfig; - btCollisionDispatcher* collisionDispatcher; - btBroadphaseInterface* broadphase; - btSequentialImpulseConstraintSolver* solver; - btDiscreteDynamicsWorld* dynamicsWorld; + std::unique_ptr collisionConfig; + std::unique_ptr collisionDispatcher; + std::unique_ptr broadphase; + std::unique_ptr solver; + std::unique_ptr dynamicsWorld; /** * @brief physicsNearCallback diff --git a/rwengine/src/objects/VehicleObject.cpp b/rwengine/src/objects/VehicleObject.cpp index 03a75b29..5fee9fa9 100644 --- a/rwengine/src/objects/VehicleObject.cpp +++ b/rwengine/src/objects/VehicleObject.cpp @@ -100,7 +100,7 @@ VehicleObject::VehicleObject(GameWorld* engine, const glm::vec3& pos, collision = new CollisionInstance; if (collision->createPhysicsBody(this, modelinfo->name, nullptr, &info->handling)) { - physRaycaster = new VehicleRaycaster(this, engine->dynamicsWorld); + physRaycaster = new VehicleRaycaster(this, engine->dynamicsWorld.get()); btRaycastVehicle::btVehicleTuning tuning; float travel = fabs(info->handling.suspensionUpperLimit - diff --git a/rwgame/RWGame.cpp b/rwgame/RWGame.cpp index 071a99f2..69e386e2 100644 --- a/rwgame/RWGame.cpp +++ b/rwgame/RWGame.cpp @@ -103,31 +103,23 @@ RWGame::~RWGame() { log.info("Game", "Cleaning up scripts"); delete script; - - log.info("Game", "Cleaning up world"); - delete world; } void RWGame::newGame() { - if (world != nullptr) { - log.error("Game", "Cannot start a new game: game is already running."); - return; - } - // Get a fresh state state = GameState(); - world = new GameWorld(&log, &work, &data); + + // Destroy the current world and start over + world = std::make_unique(&log, &work, &data); world->dynamicsWorld->setDebugDrawer(&debug); // Associate the new world with the new state and vice versa - state.world = world; + state.world = world.get(); world->state = &state; - for (std::map::iterator it = - world->data->iplLocations.begin(); - it != world->data->iplLocations.end(); ++it) { - world->data->loadZone(it->second); - world->placeItems(it->second); + for (auto ipl : world->data->iplLocations) { + world->data->loadZone(ipl.second); + world->placeItems(ipl.second); } } @@ -136,9 +128,7 @@ void RWGame::saveGame(const std::string& savename) { } void RWGame::loadGame(const std::string& savename) { - delete world; delete state.script; - world = nullptr; log.info("Game", "Loading game " + savename); @@ -152,7 +142,7 @@ void RWGame::loadGame(const std::string& savename) { } void RWGame::startScript(const std::string& name) { - SCMFile* f = world->data->loadSCM(name); + SCMFile* f = data.loadSCM(name); if (f) { if (script) delete script; @@ -480,7 +470,7 @@ int RWGame::run() { void RWGame::tick(float dt) { // Process the Engine's background work. - world->_work->update(); + work.update(); State* currState = StateManager::get().states.back().get(); @@ -625,7 +615,7 @@ void RWGame::render(float alpha, float time) { renderer.getRenderer()->pushDebugGroup("World"); RW_PROFILE_BEGIN("world"); - renderer.renderWorld(world, viewCam, alpha); + renderer.renderWorld(world.get(), viewCam, alpha); RW_PROFILE_END(); renderer.getRenderer()->popDebugGroup(); @@ -636,10 +626,8 @@ void RWGame::render(float alpha, float time) { renderDebugStats(time); break; case DebugViewMode::Physics: - if (world) { - world->dynamicsWorld->debugDrawWorld(); - debug.flush(&renderer); - } + world->dynamicsWorld->debugDrawWorld(); + debug.flush(&renderer); break; case DebugViewMode::Navigation: renderDebugPaths(time); @@ -652,7 +640,7 @@ void RWGame::render(float alpha, float time) { } RW_PROFILE_END(); - drawOnScreenText(world, &renderer); + drawOnScreenText(world.get(), &renderer); } void RWGame::renderDebugStats(float time) { diff --git a/rwgame/RWGame.hpp b/rwgame/RWGame.hpp index 43b94a91..d9fda452 100644 --- a/rwgame/RWGame.hpp +++ b/rwgame/RWGame.hpp @@ -21,7 +21,7 @@ class RWGame : public GameBase { DebugDraw debug; GameState state; - GameWorld* world = nullptr; + std::unique_ptr world; ScriptMachine* script = nullptr; std::chrono::steady_clock clock; std::chrono::steady_clock::time_point last_clock_time; @@ -60,8 +60,8 @@ public: return &state; } - GameWorld* getWorld() const { - return world; + GameWorld* getWorld() { + return world.get(); } const GameData& getGameData() const {