]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
more efficient particles
authordrumsetmonkey <abelleisle@roadrunner.com>
Mon, 16 May 2016 01:25:41 +0000 (21:25 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Mon, 16 May 2016 01:25:41 +0000 (21:25 -0400)
config/settings.xml.example
include/common.hpp
include/entities.hpp
src/entities.cpp
src/world.cpp

index e82c4546549caeded5bc48fec676a9a7869344d2..1d694f131b0944021ece80a87b95e1f66ead7141 100644 (file)
@@ -16,7 +16,7 @@ Available fonts:
 -->
 <font path="ttf/FreePixel.ttf"/>
 
-<hline size="3"/>
+<hline size="5"/>
 
 <volume>
     <master volume="100"/>
index 240cd01cd8037ab2fd22b15d4d0ff739a8cc4a1e..fabf4c7b0f82f668f7411b8fcaeb730f2e2fe2f1 100644 (file)
@@ -14,6 +14,8 @@
 #include <vector>
 #include <cmath>
 #include <algorithm>
+#include <list>
+#include <iterator>
 
 #ifndef __WIN32__
 #      include <thread>
index 1c3b6ff86a9f663cfca5570cb2d2672a3ebe69ff..83b47b9b3d2273301caf489d458eae9b0dfa46c5 100644 (file)
@@ -150,7 +150,7 @@ public:
        ~Particles(void){}
 
        // draws the particle
-       void draw(std::vector<GLfloat> &verts, std::vector<GLfloat> &tex) const;
+       void draw(std::vector<GLfloat> &p) const;
 
        // updates a particle
        void update(float _gravity, float ground_y);
index 205e848a0fe4e13105bd733061090a7464cf0bd0..af9747dd49f573f71d03a48556fda54d66dc735e 100644 (file)
@@ -187,7 +187,7 @@ void Entity::moveTo(float dest_x)
        targetx = dest_x;
 }
 
-Player::Player() : Entity() 
+Player::Player() : Entity()
 {
        width = HLINES(10);
        height = HLINES(16);
@@ -473,12 +473,12 @@ if (health != maxHealth) {
                loc.x,                              loc.y + height + game::HLINE * 2, z,
                loc.x,                              loc.y + height,                   z,
        };
-       
+
        glBindTexture(GL_TEXTURE_2D, backH);
        GLfloat tex[] = { 0.0, 0.0,
                                                   1.0, 0.0,
                                                   1.0, 1.0,
-                                                  
+
                                                   1.0, 1.0,
                                                   0.0, 1.0,
                                                   0.0, 0.0,
@@ -647,7 +647,7 @@ COMMONAIFUNC:
                                        // save the associated XMLElement
                                        dopt.push_back(oxml);
                                } while ((oxml = oxml->NextSiblingElement()));
-                               
+
                                // run the dialog stuff
                                ui::dialogBox(name, optstr, false, ptr);
                                ui::waitForDialog();
@@ -889,7 +889,7 @@ Particles::Particles(float x, float y, float w, float h, float vx, float vy, Col
        index = Texture::getIndex(c);
 }
 
-void Particles::draw(std::vector<GLfloat> &verts, std::vector<GLfloat> &tex) const
+void Particles::draw(std::vector<GLfloat> &p) const
 {
        vec2 tc = vec2 {0.25f * index.x, 0.125f * (8-index.y)};
 
@@ -897,35 +897,53 @@ void Particles::draw(std::vector<GLfloat> &verts, std::vector<GLfloat> &tex) con
     if (behind)
         z = 2.0;
 
-    verts.push_back(loc.x);
-    verts.push_back(loc.y);
-    verts.push_back(z);
+       // lower left
+    p.push_back(loc.x);
+    p.push_back(loc.y);
+    p.push_back(z);
+
+       p.push_back(tc.x);
+       p.push_back(tc.y);
+
+       // lower right
+    p.push_back(loc.x + width);
+    p.push_back(loc.y);
+    p.push_back(z);
+
+       p.push_back(tc.x);
+       p.push_back(tc.y);
+
+       // upper right
+    p.push_back(loc.x + width);
+    p.push_back(loc.y + height);
+    p.push_back(z);
+
+       p.push_back(tc.x);
+       p.push_back(tc.y);
 
-    verts.push_back(loc.x + width);
-    verts.push_back(loc.y);
-    verts.push_back(z);
-       
-    verts.push_back(loc.x + width);
-    verts.push_back(loc.y + height);
-    verts.push_back(z);
+       // upper right
+    p.push_back(loc.x + width);
+    p.push_back(loc.y + height);
+    p.push_back(z);
 
+       p.push_back(tc.x);
+       p.push_back(tc.y);
 
-    verts.push_back(loc.x + width);
-    verts.push_back(loc.y + height);
-    verts.push_back(z);
+       // upper left
+    p.push_back(loc.x);
+    p.push_back(loc.y + height);
+    p.push_back(z);
 
-    verts.push_back(loc.x);
-    verts.push_back(loc.y + height);
-    verts.push_back(z);
+       p.push_back(tc.x);
+    p.push_back(tc.y);
 
-    verts.push_back(loc.x);
-    verts.push_back(loc.y);
-    verts.push_back(z);
+       // lower left
+    p.push_back(loc.x);
+    p.push_back(loc.y);
+    p.push_back(z);
 
-    for (int i = 0; i < 6; i++){
-        tex.push_back(tc.x);
-        tex.push_back(tc.y);
-    }
+    p.push_back(tc.x);
+    p.push_back(tc.y);
 }
 
 void Particles::update(float _gravity, float ground_y)
index 327a7873b1edd3b6c18b3729f5dea6e14dff476e..0c5007dc362c62e04956774691a13c8fed8c7ba3 100644 (file)
@@ -410,17 +410,19 @@ void World::draw(Player *p)
     glEnableVertexAttribArray(worldShader_attribute_coord);
     glEnableVertexAttribArray(worldShader_attribute_tex);
 
-    std::vector<GLfloat> partc(0);
-    std::vector<GLfloat> partt(0);
+    uint ps = particles.size();
+
+    std::vector<GLfloat> partVec;
+    partVec.reserve(ps * 6 * 5);
 
     for (auto &p : particles) {
-        if (p.behind)
-            p.draw(partc, partt);
+        if (!p.behind)
+            p.draw(partVec);
     }
 
-    glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, &partc[0]);
-    glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, &partt[0]);
-    glDrawArrays(GL_TRIANGLES, 0, partc.size()/3);
+    glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &partVec[0]);
+    glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &partVec[3]);
+    glDrawArrays(GL_TRIANGLES, 0, ps * 6);
 
     glDisableVertexAttribArray(worldShader_attribute_tex);
     glDisableVertexAttribArray(worldShader_attribute_coord);
@@ -618,17 +620,17 @@ void World::draw(Player *p)
     glEnableVertexAttribArray(worldShader_attribute_coord);
     glEnableVertexAttribArray(worldShader_attribute_tex);
 
-    partc.clear();
-    partt.clear();
+    partVec.clear();
+    partVec.reserve(ps * 6 * 5);
 
     for (auto &p : particles) {
         if (!p.behind)
-            p.draw(partc, partt);
+            p.draw(partVec);
     }
 
-    glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, &partc[0]);
-    glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, &partt[0]);
-    glDrawArrays(GL_TRIANGLES, 0, partc.size()/3);
+    glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &partVec[0]);
+    glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), &partVec[3]);
+    glDrawArrays(GL_TRIANGLES, 0, ps * 6);
 
     glDisableVertexAttribArray(worldShader_attribute_tex);
     glDisableVertexAttribArray(worldShader_attribute_coord);
@@ -855,8 +857,8 @@ update(Player *p, unsigned int delta, unsigned int ticks)
                if (weather == WorldWeather::Sunny)
                        weather = WorldWeather::Dark;
                else if (weather == WorldWeather::Dark)
-                       weather = WorldWeather::Sunny;  
-       }       
+                       weather = WorldWeather::Sunny;
+       }
 
     // update player coords
        p->loc.y += p->vel.y                     * delta;
@@ -1285,7 +1287,7 @@ bool World::goWorldRight(NPC *e)
 
                npc.erase(std::find(std::begin(npc), std::end(npc), e));
                entity.erase(std::find(std::begin(entity), std::end(entity), e));
-       
+
                e->loc.x = currentWorldToRight->worldStart + HLINES(15);
                e->loc.y = GROUND_HEIGHT_MINIMUM;
                --e->outnabout;