]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
lightsgit status! fireflies
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 21 Oct 2017 17:50:04 +0000 (13:50 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 21 Oct 2017 17:50:04 +0000 (13:50 -0400)
assets/style/classic/bg/insideWoodHouse.png
include/components/all.hpp
include/components/light.hpp [new file with mode: 0644]
include/systems/light.hpp [new file with mode: 0644]
src/engine.cpp
src/render.cpp
src/systems/light.cpp [new file with mode: 0644]
src/world.cpp
xcf/insideWoodHouse.xcf
xml/entities.xml

index 47d391844004ddceb3346d1834f2dea33b2ef987..f926cb2ee3c782ff7a3c7db3c6c5c1b63f5d2c0c 100644 (file)
Binary files a/assets/style/classic/bg/insideWoodHouse.png and b/assets/style/classic/bg/insideWoodHouse.png differ
index 7c4e0d1c2930761803a9a8172c130fc3d08bcff1..3c243a7de57fd54069774ef15ba17e107a46de93 100644 (file)
@@ -13,6 +13,7 @@
 #include "hit.hpp"
 #include "hop.hpp"
 #include "itemdrop.hpp"
+#include "light.hpp"
 #include "name.hpp"
 #include "physics.hpp"
 #include "player.hpp"
diff --git a/include/components/light.hpp b/include/components/light.hpp
new file mode 100644 (file)
index 0000000..95fe6ec
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef COMPONENTS_LIGHT_HPP_
+#define COMPONENTS_LIGHT_HPP_
+
+#include <components/base.hpp>
+#include <vector2.hpp>
+#include <color.hpp>
+#include <systems/light.hpp>
+
+struct Illuminate : public Component {
+       int index;
+
+       Illuminate(vec2 pos, float radius, Color color) {
+               index = LightSystem::addLight(pos, radius, color);
+       }
+       Illuminate(XMLElement* imp, XMLElement* def) {
+               fromXML(imp, def);
+       }
+
+       void fromXML(XMLElement* imp, XMLElement* def) final {
+               (void)imp;
+               float radius = def->FloatAttribute("radius");
+               index = LightSystem::addLight(vec2(), radius, Color(1, 1, 0));
+       }
+};
+
+#endif // COMPONENTS_LIGHT_HPP_
diff --git a/include/systems/light.hpp b/include/systems/light.hpp
new file mode 100644 (file)
index 0000000..71c541a
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef SYSTEM_LIGHT_HPP_
+#define SYSTEM_LIGHT_HPP_
+
+#include <color.hpp>
+#include <render.hpp>
+#include <vector2.hpp>
+
+#include <entityx/entityx.h>
+#include <vector>
+
+struct Light {
+       vec2 pos;
+       float radius;
+       Color color;
+
+       Light(vec2 p = vec2(), float r = 0, Color c = Color())
+               : pos(p), radius(r), color(c) {}
+};
+
+class LightSystem : public entityx::System<LightSystem> {
+private:
+       static std::vector<Light> lights;
+
+public:
+       void update(entityx::EntityManager& en, entityx::EventManager& ev, entityx::TimeDelta dt) override;
+
+       static void render(void);
+
+       static int addLight(vec2 pos, float radius, Color color = Color(1, 1, 1));
+       static void updateLight(int index, vec2 pos, float radius = -1);
+       static void removeLight(int index);
+};
+
+#endif // SYSTEM_LIGHT_HPP_
index bf0bca9879c10d47b7b3c8ef19da3000482a51a1..232d11c4774c90b6da816644cb9a92a9c0e968a5 100644 (file)
@@ -13,6 +13,7 @@
 #include <particle.hpp>
 #include <weather.hpp>
 #include <attack.hpp>
+#include <systems/light.hpp>
 
 Engine::Engine(void)
     : shouldRun(true), systems(game::entities, game::events)
@@ -40,6 +41,7 @@ void Engine::init(void) {
        systems.add<AttackSystem>();
 
        systems.add<UISystem>();
+       systems.add<LightSystem>();
        systems.add<SDLReceiver>();
 
     systems.configure();
@@ -69,6 +71,7 @@ void Engine::update(entityx::TimeDelta dt)
        systems.update<ParticleSystem>(dt);
        systems.update<AttackSystem>(dt);
        systems.update<RenderSystem>(dt);
+       systems.update<LightSystem>(dt);
        //systems.update<UISystem>(dt);
 }
 
index 85384cec6c467996510bc0189b9b7729e859b41f..f9f84c925b4cf07b7585bcead4ea4042cf8cdf70 100644 (file)
@@ -7,6 +7,7 @@
 #include <glm.hpp>
 #include <font.hpp>
 #include <texture.hpp>
+#include <systems/light.hpp>
 
 extern vec2 offset;
 
@@ -198,5 +199,6 @@ void render(const int& fps)
                FontSystem::setFontZ();
        }
        
+       LightSystem::render();
        WindowSystem::render();
 }
diff --git a/src/systems/light.cpp b/src/systems/light.cpp
new file mode 100644 (file)
index 0000000..361099d
--- /dev/null
@@ -0,0 +1,60 @@
+#include <systems/light.hpp>
+
+#include <components/light.hpp>
+#include <components/position.hpp>
+#include <components/solid.hpp>
+
+std::vector<Light> LightSystem::lights;
+
+void LightSystem::update(entityx::EntityManager& en, entityx::EventManager& ev, entityx::TimeDelta dt) {
+       (void)ev;
+       (void)dt;
+
+       en.each<Illuminate, Position, Solid>([&](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);
+}
+
index b163979e19c5414ab3d43c4606025d703ebae9f2..ea2566afebc9c08190e95096e20ae409e661d9f2 100644 (file)
@@ -371,6 +371,8 @@ void WorldSystem::loader(void)
                                                entity.assign<Trigger>(wxml, abcd);
                                        else if (tname == "Drop")
                                                entity.assign<Drop>(wxml, abcd);
+                                       else if (tname == "Illuminate")
+                                               entity.assign<Illuminate>(wxml, abcd);
 
                                        abcd = abcd->NextSiblingElement();
                                }
index f08dff8043a05da6ffbfe258282c0f58428b2381..c0a2fcd6d2fc3c432536bf9c39f24f57e5492b7f 100644 (file)
Binary files a/xcf/insideWoodHouse.xcf and b/xcf/insideWoodHouse.xcf differ
index 96c8d796b8e6e526c5d771017bb92b410ac85c6c..b95b5a6c8a951bdc03665ee736f7002f5e7454a6 100644 (file)
@@ -10,6 +10,7 @@
        </Sprite>
        <Direction />
        <Solid />
+       <Illuminate radius="100" />
        <Wander>
                countdown = 0