diff options
author | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-09-16 18:16:43 -0400 |
---|---|---|
committer | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-09-16 18:16:43 -0400 |
commit | 0236eb7f6391c9d925dcaaddb8cb01ecfb7d5e55 (patch) | |
tree | fcc7ad078ec6e2eb0691ab1c4de2915ff6baed78 | |
parent | bce8d0687f44def4b0171cd84bf1441fc4390e58 (diff) |
Render system is now decoupled from the world system
-rw-r--r-- | src/engine.cpp | 10 | ||||
-rw-r--r-- | src/events/world.hpp | 8 | ||||
-rw-r--r-- | src/render.cpp | 49 | ||||
-rw-r--r-- | src/render.hpp | 20 | ||||
-rw-r--r-- | src/world.cpp | 20 | ||||
-rw-r--r-- | src/world.hpp | 5 |
6 files changed, 83 insertions, 29 deletions
diff --git a/src/engine.cpp b/src/engine.cpp index e38020d..a2d0e9b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -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(); diff --git a/src/events/world.hpp b/src/events/world.hpp index 41b9569..e5969c0 100644 --- a/src/events/world.hpp +++ b/src/events/world.hpp @@ -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 diff --git a/src/render.cpp b/src/render.cpp index 0c60b00..b991b2f 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -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; +} diff --git a/src/render.hpp b/src/render.hpp index 82ca844..26e525b 100644 --- a/src/render.hpp +++ b/src/render.hpp @@ -36,8 +36,10 @@ #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_ diff --git a/src/world.cpp b/src/world.cpp index 9e45d60..7710ecc 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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() + ); + } } diff --git a/src/world.hpp b/src/world.hpp index 5933306..696f0aa 100644 --- a/src/world.hpp +++ b/src/world.hpp @@ -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; |