]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Render system is now decoupled from the world system
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Mon, 16 Sep 2019 22:16:43 +0000 (18:16 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Mon, 16 Sep 2019 22:16:43 +0000 (18:16 -0400)
src/engine.cpp
src/events/world.hpp
src/render.cpp
src/render.hpp
src/world.cpp
src/world.hpp

index e38020d60ec50876178a2d879ead951ad8bcd91d..a2d0e9b4febbfd172aeab5c3e4003285511b6022 100644 (file)
@@ -45,7 +45,7 @@ int Engine::init(void)
     systems.add<InputSystem>();
     systems.add<PlayerSystem>(entities);
     systems.add<WorldSystem>();
-    systems.add<RenderSystem>(*(systems.system<WorldSystem>().get()));
+    systems.add<RenderSystem>();
     systems.add<ScriptSystem>(entities, *(systems.system<WorldSystem>().get()));
     systems.add<PhysicsSystem>();
     systems.configure();
@@ -57,13 +57,19 @@ int Engine::init(void)
                      "it." << std::endl;
     }
 
+    // Initially update the world to send all systems world data
+    systems.update<WorldSystem>(0);
     return 0;
 }
 
 void Engine::logicLoop(void)
 {
     entityx::TimeDelta dt = 0; /**< Elapsed milliseconds since each loop */
-    double elapsed = 0;
+    double elapsed = 1000;     /**< Time elapsed since last logic loop. This
+                                    should be initialized to something larger
+                                    than our logic loop period (50ms), so
+                                    the logic loop is run during our first
+                                    loop. */
 
     while (shouldRun()) {
         auto start = mc::now();
index 41b9569bb53b1b7f58cac1f1982a7634d6c7c4e5..e5969c0948d062fd9a2799aa075ed9fd0a002361 100644 (file)
@@ -31,9 +31,13 @@ struct WorldChangeEvent
 struct WorldMeshUpdateEvent
 {
     GLuint worldVBO;
+    unsigned int numVertex;
+    GLuint worldTexture;
+    GLuint worldNormal;
 
-    WorldMeshUpdateEvent(GLuint v) :
-        worldVBO(v) {}
+    WorldMeshUpdateEvent(GLuint v, unsigned int p,
+                         GLuint t, GLuint n) :
+        worldVBO(v), numVertex(p), worldTexture(t), worldNormal(n) {}
 };
 
 #endif//EVENTS_WORLD_HPP
index 0c60b0004fc4998b9f64af11a8eabcfe14aa44b7..b991b2f882bee59985a49b31802926e7594dcd6a 100644 (file)
@@ -27,6 +27,7 @@
 void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
                              [[maybe_unused]] entityx::EventManager& events)
 {
+    events.subscribe<WorldMeshUpdateEvent>(*this);
     init();
 }
 
@@ -179,27 +180,28 @@ void RenderSystem::update([[maybe_unused]] entityx::EntityManager& entities,
         glDrawArrays(GL_TRIANGLES, 0, 6);
     });
 
-    std::basic_string<WorldMeshData>& wm = worldSystem.current()->getMesh();
-
-    glActiveTexture(GL_TEXTURE0);
-    glBindTexture(GL_TEXTURE_2D, worldSystem.current()->getTexture());
-    glUniform1i(q, 0);
+    // If we were given a world VBO render it
+    if (worldVBO) {
+        glActiveTexture(GL_TEXTURE0);
+        glBindTexture(GL_TEXTURE_2D, worldTexture);
+        glUniform1i(q, 0);
 
-    glActiveTexture(GL_TEXTURE1);
-    glBindTexture(GL_TEXTURE_2D, worldSystem.current()->getNormal());
-    glUniform1i(n, 1);
+        glActiveTexture(GL_TEXTURE1);
+        glBindTexture(GL_TEXTURE_2D, worldNormal);
+        glUniform1i(n, 1);
 
-    glBindBuffer(GL_ARRAY_BUFFER, world_vbo);
-    glBufferData(GL_ARRAY_BUFFER, 
-                 wm.size() * sizeof(WorldMeshData), 
-                 &wm.front(), 
-                 GL_STREAM_DRAW);
+        glBindBuffer(GL_ARRAY_BUFFER, worldVBO);
+        //glBufferData(GL_ARRAY_BUFFER, 
+        //             wm.size() * sizeof(WorldMeshData), 
+        //             &wm.front(), 
+        //             GL_STREAM_DRAW);
 
-    glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE,
-                          6*sizeof(float), 0);
-    glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, 
-                          6*sizeof(float), (void*)(3*sizeof(float)));
-    glDrawArrays(GL_TRIANGLES, 0, wm.size());
+        glVertexAttribPointer(a, 3, GL_FLOAT, GL_FALSE,
+                              6*sizeof(float), 0);
+        glVertexAttribPointer(t, 2, GL_FLOAT, GL_FALSE, 
+                              6*sizeof(float), (void*)(3*sizeof(float)));
+        glDrawArrays(GL_TRIANGLES, 0, worldVertex);
+    }
 
     /*************
     *  CLEANUP  *
@@ -283,8 +285,17 @@ int RenderSystem::init(void)
     //glClearColor(0.6, 0.8, 1.0, 0.0);
     
     camPos = glm::vec3(0.0f, 0.0f, 5.0f);
-    glGenBuffers(1, &world_vbo);
 
     return 0;
 }
 
+/************
+*  EVENTS  *
+************/
+void RenderSystem::receive(const WorldMeshUpdateEvent &wmu)
+{
+    worldVBO = wmu.worldVBO;
+    worldVertex = wmu.numVertex;
+    worldTexture = wmu.worldTexture;
+    worldNormal = wmu.worldNormal;
+}
index 82ca844d8134e3ce041f548501821be098bc3dac..26e525b35e7cc99d7f2a3af54030458852372c2e 100644 (file)
 
 #include "shader.hpp"
 #include "world.hpp"
+#include "events/world.hpp"
 
-class RenderSystem : public entityx::System<RenderSystem>
+class RenderSystem : public entityx::System<RenderSystem>,
+                     public entityx::Receiver<RenderSystem>
 {
 private:
     constexpr static const char *title = "gamedev2";
@@ -49,12 +51,14 @@ private:
 
     Shader worldShader;
     glm::vec3 camPos;
-    GLuint world_vbo;
 
-    WorldSystem &worldSystem;
+    GLuint worldVBO = 0;
+    unsigned int worldVertex = 0;
+    GLuint worldTexture = 0;
+    GLuint worldNormal = 0;
 public:
-    RenderSystem(WorldSystem & _ws) :
-        window(nullptr, SDL_DestroyWindow), worldSystem(_ws) {}
+    RenderSystem() :
+        window(nullptr, SDL_DestroyWindow) {}
 
     ~RenderSystem(void)
     {
@@ -80,6 +84,12 @@ public:
      * @return Zero on success, non-zero on error
      */
     int init(void);
+
+    /************
+    *  EVENTS  *
+    ************/
+    void receive(const WorldMeshUpdateEvent &wmu);
+    
 };
 
 #endif // SYSTEM_RENDER_HPP_
index 9e45d6061da0f2aabf02910cac10991d57b2e011..7710ecc31f91601142cd45eee03eb5fbdaf39d6b 100644 (file)
@@ -55,6 +55,8 @@ World::World(sol::object param)
     if (generate != sol::nil)
         generate(this);
 
+    // Create our world VBO
+    glGenBuffers(1, &worldVBO);
     // Generate our mesh
     generateMesh();
 }
@@ -74,7 +76,6 @@ void World::registerMaterial(std::string name, sol::object data)
             std::cerr << "Material: " << name 
                       << " was already registered" << std::endl;
         }
-
     } else {
         // TODO better logging
         std::cerr << "Material registration must have a table" << std::endl;
@@ -157,6 +158,14 @@ void World::generateMesh()
             }
         }
     }
+
+    glBindBuffer(GL_ARRAY_BUFFER, worldVBO);
+    glBufferData(GL_ARRAY_BUFFER, 
+                 mesh.size() * sizeof(mesh), 
+                 &mesh.front(), 
+                 GL_STREAM_DRAW);
+
+    meshUpdated = true;
 }
 
 /* SEED */
@@ -199,4 +208,13 @@ void WorldSystem::update([[maybe_unused]]entityx::EntityManager& entities,
         events.emit<WorldChangeEvent>(currentWorld);
         std::cout << "Emitted" << std::endl;
     }
+
+    if (currentWorld->meshUpdated) {
+        events.emit<WorldMeshUpdateEvent>(
+            currentWorld->worldVBO, 
+            currentWorld->mesh.size(),
+            currentWorld->getTexture(),
+            currentWorld->getNormal()
+        );
+    }
 }
index 5933306f47ba3bcfe2fc62b2e6b73199274df2ba..696f0aa0e65f9d0b314d5a3886b8a0b8f8efcf8b 100644 (file)
@@ -59,6 +59,7 @@ struct WorldMaterial
 
 class World
 {
+    friend class WorldSystem;
 private:
     unsigned int seed;
     unsigned int layers;
@@ -71,7 +72,11 @@ private:
     std::unordered_map<std::string, int> string_registry;
     std::vector<WorldMaterial> registry;
 
+protected:
+    // RENDER
     std::basic_string<WorldMeshData> mesh;
+    GLuint worldVBO;
+    bool meshUpdated = false;
 public:
     /* VARS */
     sol::function generate;