]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
independent particle colors
authorClyne Sullivan <tullivan99@gmail.com>
Mon, 9 Jan 2017 14:20:14 +0000 (09:20 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Mon, 9 Jan 2017 14:20:14 +0000 (09:20 -0500)
include/particle.hpp
include/weather.hpp
src/particle.cpp
src/player.cpp
xml/!town.xml

index 63f23e379de6c577c526c2f1c0dde9b8fd0b1a96..3fcd409236564d96ed87bb6650a219c94c529d55 100644 (file)
@@ -2,6 +2,7 @@
 #define PARTICLE_HPP_
 
 #include <common.hpp>
+#include <texture.hpp>
 
 #include <list>
 
@@ -18,12 +19,11 @@ struct Particle {
        vec2 velocity;
        ParticleType type;
        int timeLeft;
+       vec2 color; // assets/colorIndex.png
 
-       //const Texture& color; // TODO
-
-       Particle(vec2 p, ParticleType t = ParticleType::Drop, int tl = 3000)
-               : location(p), type(t), timeLeft(tl) {}
-} __attribute__ ((packed));
+       Particle(vec2 p, ParticleType t, int tl, vec2 c)
+               : location(p), type(t), timeLeft(tl), color(c) {}
+};// __attribute__ ((packed));
 
 class ParticleSystem : public entityx::System<ParticleSystem> {
 private:
@@ -33,8 +33,10 @@ private:
 public:
        ParticleSystem(int count = 2048, bool m = false);
 
-       void add(const vec2& pos, const ParticleType& type, const int& timeleft = 3000);
-       void addMultiple(const int& count, const ParticleType& type, std::function<vec2(void)> f, const int& timeleft = 3000);
+       void add(const vec2& pos, const ParticleType& type, const int& timeleft = 3000,
+               const unsigned char& color = 0);
+       void addMultiple(const int& count, const ParticleType& type, std::function<vec2(void)> f,
+               const int& timeleft = 3000, const unsigned char& color = 0);
 
        void render(void);
        void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override;
index c348c15d3eb2f4fa3896d4779023f8343261f92f..f2f5fed1309736efc97cdc7ea0655dfaa64bca2b 100644 (file)
@@ -48,7 +48,7 @@ public:
                                newPartDelay = 0;
                                partSystem.add(vec2(offset.x - game::SCREEN_WIDTH / 2 + randGet() % game::SCREEN_WIDTH,
                                        offset.y + game::SCREEN_HEIGHT / 2 + 100),
-                                       ParticleType::Drop);
+                                       ParticleType::Drop, 3000, 3);
                        }
                        break; // TODO
                case Weather::Snowy:
@@ -56,7 +56,7 @@ public:
                                newPartDelay = 0;
                                partSystem.add(vec2(offset.x - game::SCREEN_WIDTH / 2 + randGet() % game::SCREEN_WIDTH,
                                        offset.y + game::SCREEN_HEIGHT / 2 + 100),
-                                       ParticleType::Confetti, 6000);
+                                       ParticleType::Confetti, 6000, 0);
                        }
                        break; // TODO
                default:
index c9c79c7274cc85a110b5872ce37a278d4c5fd17b..b97d0e963130903afe32e1f18df5dcc564e3f016 100644 (file)
@@ -10,20 +10,21 @@ ParticleSystem::ParticleSystem(int count, bool m)
        parts.reserve(count);
 }
 
-void ParticleSystem::add(const vec2& pos, const ParticleType& type, const int& timeleft)
+void ParticleSystem::add(const vec2& pos, const ParticleType& type, const int& timeleft, const unsigned char& color)
 {
        // TODO not enforce max
        if (/*max &&*/ parts.size() + 1 >= parts.capacity())
                return;
 
-       parts.emplace_back(pos, type, timeleft);
+       parts.emplace_back(pos, type, timeleft, vec2(color / 8 * 0.25f, color % 8 * 0.125f + 0.0625f));
 }
 
-void ParticleSystem::addMultiple(const int& count, const ParticleType& type, std::function<vec2(void)> f, const int& timeleft)
+void ParticleSystem::addMultiple(const int& count, const ParticleType& type, std::function<vec2(void)> f,
+       const int& timeleft, const unsigned char& color)
 {
        int togo = count;
        while (togo-- > 0)
-               parts.emplace_back(f(), type, timeleft);
+               parts.emplace_back(f(), type, timeleft, vec2(color / 8 * 0.25f, color % 8 * 0.125f + 0.0625f));
 }
 
 void ParticleSystem::render(void)
@@ -31,6 +32,7 @@ void ParticleSystem::render(void)
        static GLuint particleVBO = 9999;
        // six vertices, 3d coord + 2d tex coord = 5
        constexpr auto entrySize = (6 * 5) * sizeof(GLfloat);
+       static const Texture palette ("assets/colorIndex.png");
 
        if (particleVBO == 9999) {
                // generate VBO
@@ -51,12 +53,12 @@ void ParticleSystem::render(void)
        for (const auto& p : parts) {
                static const auto hl = game::HLINE;
                GLfloat coords[30] = {
-                       p.location.x,      p.location.y,      -1, 0, 0,
-                       p.location.x,      p.location.y + hl, -1, 0, 1,
-                       p.location.x + hl, p.location.y + hl, -1, 1, 1,
-                       p.location.x + hl, p.location.y + hl, -1, 1, 1,
-                       p.location.x + hl, p.location.y,      -1, 0, 1,
-                       p.location.x,      p.location.y,      -1, 0, 0
+                       p.location.x,      p.location.y,      -1, p.color.x, p.color.y,
+                       p.location.x,      p.location.y + hl, -1, p.color.x, p.color.y,
+                       p.location.x + hl, p.location.y + hl, -1, p.color.x, p.color.y,
+                       p.location.x + hl, p.location.y + hl, -1, p.color.x, p.color.y,
+                       p.location.x + hl, p.location.y,      -1, p.color.x, p.color.y,
+                       p.location.x,      p.location.y,      -1, p.color.x, p.color.y
                };
 
                glBufferSubData(GL_ARRAY_BUFFER, offset, entrySize, coords);
@@ -67,8 +69,6 @@ void ParticleSystem::render(void)
        Render::worldShader.use();
        Render::worldShader.enable();
 
-       Colors::white.use();
-
        // set coords
        glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
        glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE,
@@ -77,6 +77,7 @@ void ParticleSystem::render(void)
        glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE,
                5 * sizeof(GLfloat), reinterpret_cast<void*>(3 * sizeof(GLfloat)));
 
+       palette.use();
        glDrawArrays(GL_TRIANGLES, 0, parts.size() * 6);
 
        glBindBuffer(GL_ARRAY_BUFFER, 0);
index 0e75e05da1eae63267c4b463c4d406831611b9fa..bdfc9fc4e1eb54bae076e5c60dc67decd37f1b8e 100644 (file)
@@ -110,7 +110,7 @@ void PlayerSystem::receive(const KeyDownEvent &kde)
                                speed = 2.0f;
 
                        game::engine.getSystem<ParticleSystem>()->addMultiple(10, ParticleType::SmallBlast,
-                               [&](){ return vec2(loc.x, loc.y); }, 1000);
+                               [&](){ return vec2(loc.x, loc.y); }, 500, 7);
                } else if (kc == getControl(4)) {
                        speed = .5;
                } else if (kc == getControl(5)) {
index 0a281240eed22257c18a2bd4126b5b57ef054942..c197d8df6f627c7b59460cc1713a0b2a70cc8428 100644 (file)
@@ -4,7 +4,7 @@
 <World>
     <style background="0" bgm="assets/music/embark.wav" folder="assets/style/classic/"/>
     <generation width="320"/>
-       <weather>Snowy</weather>
+       <weather>Rainy</weather>
     <time>6000</time>
     <link right="!town2.xml"/>
     <spawnx>-300</spawnx>