]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Added physics loop function that runs in it's own thread
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 7 Sep 2019 05:13:59 +0000 (01:13 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 7 Sep 2019 05:13:59 +0000 (01:13 -0400)
src/engine.cpp
src/engine.hpp

index 9e77b401a74b1858849dd94d7adc82038302d2b0..d8e8bab625e06296ed0b8f8478999dcfbd324397 100644 (file)
@@ -26,6 +26,7 @@
 #include "player.hpp"
 #include "script.hpp"
 #include "render.hpp"
+#include "physics.hpp"
 
 #include "components/EventListener.hpp"
 #include "components/Script.hpp"
@@ -45,6 +46,7 @@ int Engine::init(void)
     systems.add<PlayerSystem>(entities);
     systems.add<RenderSystem>();
     systems.add<ScriptSystem>();
+    systems.add<PhysicsSystem>();
     systems.configure();
 
     // Load game script and entity data
@@ -65,16 +67,6 @@ void Engine::logicLoop(void)
     while (shouldRun()) {
         auto start = mc::now();
 
-        /***********************
-        *  UPDATE FREQUENTLY  *
-        ***********************/
-        
-        entities.each<Position, Velocity>
-            ([&](entityx::Entity, Position &p, Velocity &v){
-            p.x += (v.x * dt/1000.0);
-            p.y += (v.y * dt/1000.0);
-        });
-
         systems.update<InputSystem>(dt);
         //systems.update<ScriptSystem>(dt);
 
@@ -92,14 +84,36 @@ void Engine::logicLoop(void)
             });
         }
 
+        std::this_thread::yield();
+
         auto end = mc::now();
         auto diff = end - start;
         auto micros = cr::duration_cast<cr::microseconds>(diff);
         auto msc = micros.count();
         dt = static_cast<double>(msc) / 1000.0;
         elapsed += dt;
+    }
+}
+
+void Engine::physicsLoop(void)
+{
+    entityx::TimeDelta dt = 0; /**< Elapsed milliseconds since each loop */
+
+    while (shouldRun()) {
+        auto start = mc::now();
+
+        // Update the entities physics/position
+        systems.update<PhysicsSystem>(dt);
+
         std::this_thread::yield();
+
+        auto end = mc::now();
+        auto diff = end - start;
+        auto micros = cr::duration_cast<cr::microseconds>(diff);
+        auto msc = micros.count();
+        dt = static_cast<double>(msc) / 1000.0;
     }
+    std::cout << std::endl;
 }
 
 void Engine::renderLoop(void)
@@ -117,11 +131,16 @@ void Engine::run(void)
         logicLoop();
     });
 
+    physicsThread = std::thread([this](void) {
+        physicsLoop();
+    });
+
     // Keep render loop on main thread
     renderLoop();
 
     // Done, bring logic thread back
     logicThread.join();
+    physicsThread.join();
 
     // Save the entities' data
     GameState::save("save.json", entities);
index 26b1c451af50104ce39129df2a1b8138481ed333..ab6f16b16613ec6be30c255ffe9ca68d18650bac 100644 (file)
@@ -37,8 +37,10 @@ private:
     entityx::SystemManager systems;
 
     std::thread logicThread;
+    std::thread physicsThread;
 
     void logicLoop(void);
+    void physicsLoop(void);
     void renderLoop(void);
 
     bool shouldRun(void);