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();
"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();
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
void RenderSystem::configure([[maybe_unused]] entityx::EntityManager& entities,
[[maybe_unused]] entityx::EventManager& events)
{
+ events.subscribe<WorldMeshUpdateEvent>(*this);
init();
}
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 *
//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;
+}
#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";
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)
{
* @return Zero on success, non-zero on error
*/
int init(void);
+
+ /************
+ * EVENTS *
+ ************/
+ void receive(const WorldMeshUpdateEvent &wmu);
+
};
#endif // SYSTEM_RENDER_HPP_
if (generate != sol::nil)
generate(this);
+ // Create our world VBO
+ glGenBuffers(1, &worldVBO);
// Generate our mesh
generateMesh();
}
std::cerr << "Material: " << name
<< " was already registered" << std::endl;
}
-
} else {
// TODO better logging
std::cerr << "Material registration must have a table" << std::endl;
}
}
}
+
+ glBindBuffer(GL_ARRAY_BUFFER, worldVBO);
+ glBufferData(GL_ARRAY_BUFFER,
+ mesh.size() * sizeof(mesh),
+ &mesh.front(),
+ GL_STREAM_DRAW);
+
+ meshUpdated = true;
}
/* SEED */
events.emit<WorldChangeEvent>(currentWorld);
std::cout << "Emitted" << std::endl;
}
+
+ if (currentWorld->meshUpdated) {
+ events.emit<WorldMeshUpdateEvent>(
+ currentWorld->worldVBO,
+ currentWorld->mesh.size(),
+ currentWorld->getTexture(),
+ currentWorld->getNormal()
+ );
+ }
}
class World
{
+ friend class WorldSystem;
private:
unsigned int seed;
unsigned int layers;
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;