1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 15:02:34 +02:00

Use unique_ptr for GameWorld instance

This commit is contained in:
Daniel Evans 2016-10-20 00:49:15 +01:00
parent 81c27da97b
commit fb4d9ea8c3
6 changed files with 32 additions and 49 deletions

View File

@ -154,7 +154,7 @@ bool CollisionInstance::createPhysicsBody(GameObject* object,
void CollisionInstance::changeMass(float newMass) { void CollisionInstance::changeMass(float newMass) {
GameObject* object = static_cast<GameObject*>(m_body->getUserPointer()); GameObject* object = static_cast<GameObject*>(m_body->getUserPointer());
auto dynamicsWorld = object->engine->dynamicsWorld; auto& dynamicsWorld = object->engine->dynamicsWorld;
dynamicsWorld->removeRigidBody(m_body); dynamicsWorld->removeRigidBody(m_body);
btVector3 inert; btVector3 inert;
m_body->getCollisionShape()->calculateLocalInertia(newMass, inert); m_body->getCollisionShape()->calculateLocalInertia(newMass, inert);

View File

@ -87,12 +87,15 @@ GameWorld::GameWorld(Logger* log, WorkContext* work, GameData* dat)
: logger(log), data(dat), randomEngine(rand()), _work(work), paused(false) { : logger(log), data(dat), randomEngine(rand()), _work(work), paused(false) {
data->engine = this; data->engine = this;
collisionConfig = new btDefaultCollisionConfiguration; collisionConfig = std::make_unique<btDefaultCollisionConfiguration>();
collisionDispatcher = new WorldCollisionDispatcher(collisionConfig); collisionDispatcher =
broadphase = new btDbvtBroadphase(); std::make_unique<WorldCollisionDispatcher>(collisionConfig.get());
solver = new btSequentialImpulseConstraintSolver; broadphase = std::make_unique<btDbvtBroadphase>();
dynamicsWorld = new btDiscreteDynamicsWorld(collisionDispatcher, broadphase, solver = std::make_unique<btSequentialImpulseConstraintSolver>();
solver, collisionConfig); dynamicsWorld = std::make_unique<btDiscreteDynamicsWorld>(
collisionDispatcher.get(), broadphase.get(), solver.get(),
collisionConfig.get());
dynamicsWorld->setGravity(btVector3(0.f, 0.f, -9.81f)); dynamicsWorld->setGravity(btVector3(0.f, 0.f, -9.81f));
broadphase->getOverlappingPairCache()->setInternalGhostPairCallback( broadphase->getOverlappingPairCache()->setInternalGhostPairCallback(
new btGhostPairCallback()); new btGhostPairCallback());
@ -104,14 +107,6 @@ GameWorld::~GameWorld() {
for (auto& p : allObjects) { for (auto& p : allObjects) {
delete p; delete p;
} }
delete dynamicsWorld;
delete solver;
delete broadphase;
delete collisionDispatcher;
delete collisionConfig;
/// @todo delete other things.
} }
bool GameWorld::placeItems(const std::string& name) { bool GameWorld::placeItems(const std::string& name) {

View File

@ -269,11 +269,11 @@ public:
/** /**
* Bullet * Bullet
*/ */
btDefaultCollisionConfiguration* collisionConfig; std::unique_ptr<btDefaultCollisionConfiguration> collisionConfig;
btCollisionDispatcher* collisionDispatcher; std::unique_ptr<btCollisionDispatcher> collisionDispatcher;
btBroadphaseInterface* broadphase; std::unique_ptr<btDbvtBroadphase> broadphase;
btSequentialImpulseConstraintSolver* solver; std::unique_ptr<btSequentialImpulseConstraintSolver> solver;
btDiscreteDynamicsWorld* dynamicsWorld; std::unique_ptr<btDiscreteDynamicsWorld> dynamicsWorld;
/** /**
* @brief physicsNearCallback * @brief physicsNearCallback

View File

@ -100,7 +100,7 @@ VehicleObject::VehicleObject(GameWorld* engine, const glm::vec3& pos,
collision = new CollisionInstance; collision = new CollisionInstance;
if (collision->createPhysicsBody(this, modelinfo->name, nullptr, if (collision->createPhysicsBody(this, modelinfo->name, nullptr,
&info->handling)) { &info->handling)) {
physRaycaster = new VehicleRaycaster(this, engine->dynamicsWorld); physRaycaster = new VehicleRaycaster(this, engine->dynamicsWorld.get());
btRaycastVehicle::btVehicleTuning tuning; btRaycastVehicle::btVehicleTuning tuning;
float travel = fabs(info->handling.suspensionUpperLimit - float travel = fabs(info->handling.suspensionUpperLimit -

View File

@ -103,31 +103,23 @@ RWGame::~RWGame() {
log.info("Game", "Cleaning up scripts"); log.info("Game", "Cleaning up scripts");
delete script; delete script;
log.info("Game", "Cleaning up world");
delete world;
} }
void RWGame::newGame() { void RWGame::newGame() {
if (world != nullptr) {
log.error("Game", "Cannot start a new game: game is already running.");
return;
}
// Get a fresh state // Get a fresh state
state = GameState(); state = GameState();
world = new GameWorld(&log, &work, &data);
// Destroy the current world and start over
world = std::make_unique<GameWorld>(&log, &work, &data);
world->dynamicsWorld->setDebugDrawer(&debug); world->dynamicsWorld->setDebugDrawer(&debug);
// Associate the new world with the new state and vice versa // Associate the new world with the new state and vice versa
state.world = world; state.world = world.get();
world->state = &state; world->state = &state;
for (std::map<std::string, std::string>::iterator it = for (auto ipl : world->data->iplLocations) {
world->data->iplLocations.begin(); world->data->loadZone(ipl.second);
it != world->data->iplLocations.end(); ++it) { world->placeItems(ipl.second);
world->data->loadZone(it->second);
world->placeItems(it->second);
} }
} }
@ -136,9 +128,7 @@ void RWGame::saveGame(const std::string& savename) {
} }
void RWGame::loadGame(const std::string& savename) { void RWGame::loadGame(const std::string& savename) {
delete world;
delete state.script; delete state.script;
world = nullptr;
log.info("Game", "Loading game " + savename); log.info("Game", "Loading game " + savename);
@ -152,7 +142,7 @@ void RWGame::loadGame(const std::string& savename) {
} }
void RWGame::startScript(const std::string& name) { void RWGame::startScript(const std::string& name) {
SCMFile* f = world->data->loadSCM(name); SCMFile* f = data.loadSCM(name);
if (f) { if (f) {
if (script) delete script; if (script) delete script;
@ -480,7 +470,7 @@ int RWGame::run() {
void RWGame::tick(float dt) { void RWGame::tick(float dt) {
// Process the Engine's background work. // Process the Engine's background work.
world->_work->update(); work.update();
State* currState = StateManager::get().states.back().get(); State* currState = StateManager::get().states.back().get();
@ -625,7 +615,7 @@ void RWGame::render(float alpha, float time) {
renderer.getRenderer()->pushDebugGroup("World"); renderer.getRenderer()->pushDebugGroup("World");
RW_PROFILE_BEGIN("world"); RW_PROFILE_BEGIN("world");
renderer.renderWorld(world, viewCam, alpha); renderer.renderWorld(world.get(), viewCam, alpha);
RW_PROFILE_END(); RW_PROFILE_END();
renderer.getRenderer()->popDebugGroup(); renderer.getRenderer()->popDebugGroup();
@ -636,10 +626,8 @@ void RWGame::render(float alpha, float time) {
renderDebugStats(time); renderDebugStats(time);
break; break;
case DebugViewMode::Physics: case DebugViewMode::Physics:
if (world) { world->dynamicsWorld->debugDrawWorld();
world->dynamicsWorld->debugDrawWorld(); debug.flush(&renderer);
debug.flush(&renderer);
}
break; break;
case DebugViewMode::Navigation: case DebugViewMode::Navigation:
renderDebugPaths(time); renderDebugPaths(time);
@ -652,7 +640,7 @@ void RWGame::render(float alpha, float time) {
} }
RW_PROFILE_END(); RW_PROFILE_END();
drawOnScreenText(world, &renderer); drawOnScreenText(world.get(), &renderer);
} }
void RWGame::renderDebugStats(float time) { void RWGame::renderDebugStats(float time) {

View File

@ -21,7 +21,7 @@ class RWGame : public GameBase {
DebugDraw debug; DebugDraw debug;
GameState state; GameState state;
GameWorld* world = nullptr; std::unique_ptr<GameWorld> world;
ScriptMachine* script = nullptr; ScriptMachine* script = nullptr;
std::chrono::steady_clock clock; std::chrono::steady_clock clock;
std::chrono::steady_clock::time_point last_clock_time; std::chrono::steady_clock::time_point last_clock_time;
@ -60,8 +60,8 @@ public:
return &state; return &state;
} }
GameWorld* getWorld() const { GameWorld* getWorld() {
return world; return world.get();
} }
const GameData& getGameData() const { const GameData& getGameData() const {