From 7644b740e87053838f3c7a80e88ad192fcf1a5e2 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 21 Oct 2017 13:50:04 -0400 Subject: lightsgit status! fireflies --- src/systems/light.cpp | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/systems/light.cpp (limited to 'src/systems') diff --git a/src/systems/light.cpp b/src/systems/light.cpp new file mode 100644 index 0000000..361099d --- /dev/null +++ b/src/systems/light.cpp @@ -0,0 +1,60 @@ +#include + +#include +#include +#include + +std::vector LightSystem::lights; + +void LightSystem::update(entityx::EntityManager& en, entityx::EventManager& ev, entityx::TimeDelta dt) { + (void)ev; + (void)dt; + + en.each([&](entityx::Entity e, Illuminate& ill, Position& pos, + Solid& dim) { + (void)e; + vec2 p (pos.x, pos.y); + vec2 d (dim.width, dim.height); + LightSystem::updateLight(ill.index, p + d / 2); + }); +} + +void LightSystem::render(void) { + auto coords = new GLfloat[lights.size() * 4]; + auto colors = new GLfloat[lights.size() * 4]; + + unsigned int offset = 0; + for (const auto& l : lights) { + coords[offset] = l.pos.x, coords[offset + 1] = l.pos.y, + coords[offset + 2] = 0, coords[offset + 3] = l.radius; + colors[offset] = l.color.red, colors[offset + 1] = l.color.green, + colors[offset + 2] = l.color.blue, colors[offset + 3] = 1.0f; + offset += 4; + } + + Render::worldShader.use(); + Render::worldShader.enable(); + + glUniform4fv(Render::worldShader.uniform[WU_light], lights.size(), coords); + glUniform4fv(Render::worldShader.uniform[WU_light_color], lights.size(), colors); + glUniform1i(Render::worldShader.uniform[WU_light_size], lights.size()); + + Render::worldShader.disable(); + Render::worldShader.unuse(); +} + +int LightSystem::addLight(vec2 pos, float radius, Color color) { + lights.emplace_back(pos, radius, color); + return lights.size() - 1; +} + +void LightSystem::updateLight(int index, vec2 pos, float radius) { + lights[index].pos = pos; + if (radius >= 0) + lights[index].radius = radius; +} + +void LightSystem::removeLight(int index) { + lights.erase(lights.begin() + index); +} + -- cgit v1.2.3