]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
entity manager lock
authorClyne Sullivan <tullivan99@gmail.com>
Thu, 12 Jan 2017 00:11:56 +0000 (19:11 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Thu, 12 Jan 2017 00:11:56 +0000 (19:11 -0500)
Makefile
include/engine.hpp
src/components.cpp
src/engine.cpp
src/ui.cpp
src/world.cpp

index 0e1f54e6985f2519e30921a5e6ffe33fb3415c69..f8d751131a94c9a40f810b3c62d352ac811bbc4e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -39,11 +39,11 @@ cleandata:
        touch brice.dat
 
 $(EXEC): $(CXXOUTDIR)/$(CXXOBJ) main.cpp
-       #g++ -I. -std=c++11 -c entityx/help/Pool.cc -o out/Pool.o
-       #g++ -I. -std=c++11 -c entityx/help/Timer.cc -o out/Timer.o
-       #g++ -I. -std=c++11 -c entityx/Event.cc -o out/Event.o
-       #g++ -I. -std=c++11 -c entityx/Entity.cc -o out/Entity.o
-       #g++ -I. -std=c++11 -c entityx/System.cc -o out/System.o
+       g++ -I. -std=c++11 -c entityx/help/Pool.cc -o out/Pool.o
+       g++ -I. -std=c++11 -c entityx/help/Timer.cc -o out/Timer.o
+       g++ -I. -std=c++11 -c entityx/Event.cc -o out/Event.o
+       g++ -I. -std=c++11 -c entityx/Entity.cc -o out/Entity.o
+       g++ -I. -std=c++11 -c entityx/System.cc -o out/System.o
        
        @echo "  CXX/LD  main"
        @$(CXX) $(CXXFLAGS) $(CXXINC) $(CXXWARN) -o $(EXEC) main.cpp out/*.o $(LIBS)
index 2b036967ba24ea2651dd1d343a0f03eaa48b64b9..a331e50121327c875db9ed51a0cd247b1446e82e 100644 (file)
@@ -70,6 +70,38 @@ public:
        }
 };
 
+#include <atomic>
+#include <chrono>
+
+class LockableEntityManager : public entityx::EntityManager {
+private:
+       std::atomic_bool locked;
+       
+public:
+       LockableEntityManager(entityx::EventManager& ev)
+               : EntityManager(ev) {
+               locked.store(false);
+       }
+       
+       void lock(void) {
+               while (locked.load())
+                       std::this_thread::sleep_for(std::chrono::milliseconds(10));
+               
+               locked.store(true);
+       }
+       
+       void unlock(void) {
+               locked.store(false);
+       }
+       
+       bool try_lock(void) {
+               if (locked.load())
+                       return false;
+               
+               locked.store(true);
+               return true;
+       }
+};
 
 namespace game {
        /**
@@ -80,8 +112,8 @@ namespace game {
        /**
         * Handles entity data.
         */
-    extern entityx::EntityManager entities;
-
+    extern LockableEntityManager entities;
+       
        /**
         * An instance of the main game engine.
         */
index fb0977f6e7ec91f1b9249169cdb6d78a93b7a762..a7d504d17a5412a01ebc16afdb8984a50defadee 100644 (file)
@@ -162,6 +162,7 @@ void DialogSystem::configure(entityx::EventManager &ev)
 
 void DialogSystem::receive(const MouseClickEvent &mce)
 {
+       game::entities.lock();
        game::entities.each<Position, Solid, Dialog, Name>(
                [&](entityx::Entity e, Position &pos, Solid &dim, Dialog &d, Name &name) {
                        static std::atomic_bool dialogRun;
@@ -270,6 +271,7 @@ void DialogSystem::receive(const MouseClickEvent &mce)
                        }
                }
        });
+       game::entities.unlock();
 }
 
 void DialogSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
index aa0db73f7efe3ec563937767748ae3665160b70f..abadf3fa1750c8f1ad4ff0a3a0da9530817e3855 100644 (file)
@@ -69,7 +69,7 @@ void Engine::update(entityx::TimeDelta dt)
 
 namespace game {
        entityx::EventManager events;
-       entityx::EntityManager entities (events);
+       LockableEntityManager entities (events);
        //SpriteLoader sprite_l;
 
     Engine engine;
index d1b605526250b8ca197d10cd81136b6fd137aea3..21ed180d55f67e17b63e48869d11974fda270b25 100644 (file)
@@ -535,10 +535,8 @@ namespace ui {
                auto& fi = fadeIntensity;
                fi = 0;
 
-               while (fi < 255) {
-                       fadeUpdate();
-                       std::this_thread::sleep_for(1ms);
-               }
+               //while (fi < 255)
+                       //std::this_thread::sleep_for(1ms);
                        
                fi = 255;
        }
index 6b7b89b5c439b02f57023f8a849eea9583109d99..a1d2f3cd707a45cc91a5c3f0c4be25710f7c0fa1 100644 (file)
@@ -171,6 +171,7 @@ bool WorldSystem::save(void)
        // signature?
        save << "831998 ";
 
+       game::entities.lock();
        game::entities.each<Position>([&](entityx::Entity entity, Position& pos) {
                // save position
                save << "p " << pos.x << ' ' << pos.y << ' ';
@@ -179,6 +180,7 @@ bool WorldSystem::save(void)
                if (entity.has_component<Dialog>())
                        save << "d " << entity.component<Dialog>()->index << ' ';
        });
+       game::entities.unlock();
 
        save.close();
        return true;
@@ -237,6 +239,7 @@ void WorldSystem::load(const std::string& file)
 
        world.toLeft = world.toRight = "";
        currentXMLFile = file;
+       game::entities.lock();
        game::entities.reset();
        game::engine.getSystem<PlayerSystem>()->create();
 
@@ -456,6 +459,7 @@ void WorldSystem::load(const std::string& file)
        }
 
        game::events.emit<BGMToggleEvent>();
+       game::entities.unlock();
 }
 
 /*
@@ -1104,32 +1108,30 @@ void WorldSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
 
 void WorldSystem::detect(entityx::TimeDelta dt)
 {
+       game::entities.lock();
        game::entities.each<Grounded, Position, Solid>(
                [&](entityx::Entity e, Grounded &g, Position &loc, Solid &dim) {
-                       (void)e;
-                       if (!g.grounded) {
-                               // get the line the entity is on
-                               int line = std::clamp(static_cast<int>((loc.x + dim.width / 2 - world.startX) / game::HLINE),
-                                                                         0,
-                                                                         static_cast<int>(world.data.size()));
-
+               (void)e;
+               if (!g.grounded) {
+                       // get the line the entity is on
+                       int line = std::clamp(static_cast<int>((loc.x + dim.width / 2 - world.startX) / game::HLINE),
+                               0, static_cast<int>(world.data.size()));
                                // make sure entity is above ground
-                               const auto& data = world.data;
-                               if (loc.y != data[line].groundHeight) {
-                                       loc.y = data[line].groundHeight;
-                                       e.remove<Grounded>();
-                               }
+                       const auto& data = world.data;
+                       if (loc.y != data[line].groundHeight) {
+                               loc.y = data[line].groundHeight;
+                               e.remove<Grounded>();
                        }
-
-               });
+               }
+       });
 
        game::entities.each<Direction, Physics>(
                [&](entityx::Entity e, Direction &vel, Physics &phys) {
-                       (void)e;
-                       // handle gravity
-               if (vel.y > -2.0f)
-                               vel.y -= (GRAVITY_CONSTANT * phys.g) * dt;
-               });
+               (void)e;
+               // handle gravity
+        if (vel.y > -2.0f)
+                       vel.y -= (GRAVITY_CONSTANT * phys.g) * dt;
+       });
 
        game::entities.each<Position, Direction, Solid>(
            [&](entityx::Entity e, Position &loc, Direction &vel, Solid &dim) {
@@ -1139,8 +1141,7 @@ void WorldSystem::detect(entityx::TimeDelta dt)
 
                // get the line the entity is on
                int line = std::clamp(static_cast<int>((loc.x + dim.width / 2 - world.startX) / game::HLINE),
-                                     0,
-                                     static_cast<int>(world.data.size()));
+                       0, static_cast<int>(world.data.size()));
 
                // make sure entity is above ground
                const auto& data = world.data;
@@ -1171,6 +1172,7 @@ void WorldSystem::detect(entityx::TimeDelta dt)
                        loc.x = -(static_cast<int>(world.startX)) - dim.width - game::HLINE;
                }
        });
+       game::entities.unlock();
 }
 
 void WorldSystem::goWorldRight(Position& p, Solid &d)
@@ -1208,17 +1210,18 @@ void WorldSystem::goWorldPortal(Position& p)
                p.x = world.outdoorCoords.x; // ineffective, player is regen'd
                p.y = world.outdoorCoords.y;
        } else {
+               game::entities.lock();
                game::entities.each<Position, Solid, Portal>(
                        [&](entityx::Entity entity, Position& loc, Solid &dim, Portal &portal) {
-                               (void)entity;
-                               if (!(portal.toFile.empty()) && p.x > loc.x && p.x < loc.x + dim.width)  {
-                                       file = portal.toFile;
-                                       world.outdoor = currentXMLFile;
-                                       world.outdoorCoords = vec2(loc.x + dim.width / 2, 100);
-                                       return;
-                               }
+                       (void)entity;
+                       if (!(portal.toFile.empty()) && p.x > loc.x && p.x < loc.x + dim.width)  {
+                               file = portal.toFile;
+                               world.outdoor = currentXMLFile;
+                               world.outdoorCoords = vec2(loc.x + dim.width / 2, 100);
+                               return;
                        }
-               );
+               });
+               game::entities.unlock();
        }
 
        if (!file.empty()) {