]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Basic particle stack overflow patch
authordrumsetmonkey <abelleisle@roadrunner.com>
Fri, 20 May 2016 11:33:54 +0000 (07:33 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Fri, 20 May 2016 11:33:54 +0000 (07:33 -0400)
include/entities.hpp
src/entities.cpp
src/inventory.cpp
src/ui.cpp
src/world.cpp

index 93c1caa5984f6e60bed90e09c019b92a3f3b9340..82f80c735723c13f9fe6c0b556c855c0ecfb3b1b 100644 (file)
@@ -109,6 +109,7 @@ class Particles{
 public:
        // the location of the particle
        vec2 loc;
+       float zOffset;
 
        // the width of the particle, in pixels
        float width;
index b9acc201a0fa13df7fd4652508b2d7fa34b2f64f..8299e2c30a0b64926523eb0c1d7636c75d55f7c8 100644 (file)
@@ -915,15 +915,18 @@ Particles::Particles(float x, float y, float w, float h, float vx, float vy, Col
        behind = false;
        bounce = false;
        index = Texture::getIndex(c);
+       zOffset = ((rand()%20)-10)/1000.0f;
 }
 
 void Particles::draw(GLfloat*& p) const
 {
-       vec2 tc = vec2 {0.25f * index.x, 0.125f * (8-index.y)};
+       vec2 tc = vec2(0.25f * this->index.x, 0.125f * (8.0f - this->index.y));
 
-    float z = 0.0;
-    if (behind)
-        z = 2.0;
+    float z = 0.9;
+       if (behind)
+               z = 2.0;
+
+       z += zOffset;
 
        // lower left
     *(p++) = loc.x;
index 9727dc07401e506aac2b8b3d8e66522f036f329c..a7c19dac2a99b021ca7ae09cf72f68f3a4b058d1 100644 (file)
@@ -665,13 +665,13 @@ void itemDraw(Player *p, Item *d) {
         itemTex[10] = 1.0;
     }
 
-    GLfloat itemCoords[] = {itemLoc.x,          itemLoc.y,          1.0,
-                            itemLoc.x+d->dim.x, itemLoc.y,          1.0,
-                            itemLoc.x+d->dim.x, itemLoc.y+d->dim.y, 1.0,
+    GLfloat itemCoords[] = {itemLoc.x,          itemLoc.y,          p->z,
+                            itemLoc.x+d->dim.x, itemLoc.y,          p->z,
+                            itemLoc.x+d->dim.x, itemLoc.y+d->dim.y, p->z,
 
-                            itemLoc.x+d->dim.x, itemLoc.y+d->dim.y, 1.0,
-                            itemLoc.x,          itemLoc.y+d->dim.y, 1.0,
-                            itemLoc.x,          itemLoc.y,          1.0};
+                            itemLoc.x+d->dim.x, itemLoc.y+d->dim.y, p->z,
+                            itemLoc.x,          itemLoc.y+d->dim.y, p->z,
+                            itemLoc.x,          itemLoc.y,          p->z};
 
        glBindTexture(GL_TEXTURE_2D,d->tex->image[0]);
 
index a687a3569e232c8342534a771d201a3fc23f4870..6eca97d5bed6a5761fab68edbb8353c879f29e96 100644 (file)
@@ -1468,10 +1468,10 @@ EXIT:
                         0.0, 1.0,
                         1.0, 1.0};
 
-        GLfloat backdrop[] = {offset.x - SCREEN_WIDTH / 2 - 1, offset.y - SCREEN_HEIGHT / 2, -8.1,
-                              offset.x + SCREEN_WIDTH / 2, offset.y - SCREEN_HEIGHT / 2,        -8.1,
-                              offset.x - SCREEN_WIDTH / 2 - 1, offset.y + SCREEN_HEIGHT / 2, -8.1,
-                              offset.x + SCREEN_WIDTH / 2, offset.y + SCREEN_HEIGHT / 2,        -8.1};
+        GLfloat backdrop[] = {offset.x - SCREEN_WIDTH / 2 - 1, offset.y - SCREEN_HEIGHT / 2, -7.9,
+                              offset.x + SCREEN_WIDTH / 2, offset.y - SCREEN_HEIGHT / 2,        -7.9,
+                              offset.x - SCREEN_WIDTH / 2 - 1, offset.y + SCREEN_HEIGHT / 2, -7.9,
+                              offset.x + SCREEN_WIDTH / 2, offset.y + SCREEN_HEIGHT / 2,        -7.9};
                
                setFontZ(-8.2);
         glUniform1i(textShader_uniform_texture, 0);
index f6df7d66c5700be6d4da94bc36d70da4efdde902..efafa554ea1909069f13a13a01baefb75cb2b592 100644 (file)
@@ -283,7 +283,15 @@ void World::drawBackgrounds(void)
                                 offset.x - backgroundOffset.x - 5, offset.y - backgroundOffset.y, 9.9f,
                                 offset.x - backgroundOffset.x - 5, offset.y + backgroundOffset.y, 9.9f};
 
-    glUseProgram(worldShader);
+    GLfloat fron_tex_coord[] = {offset.x - backgroundOffset.x - 5, offset.y + backgroundOffset.y, 9.8f,
+                                offset.x + backgroundOffset.x + 5, offset.y + backgroundOffset.y, 9.8f,
+                                offset.x + backgroundOffset.x + 5, offset.y - backgroundOffset.y, 9.8f,
+
+                                offset.x + backgroundOffset.x + 5, offset.y - backgroundOffset.y, 9.8f,
+                                offset.x - backgroundOffset.x - 5, offset.y - backgroundOffset.y, 9.8f,
+                                offset.x - backgroundOffset.x - 5, offset.y + backgroundOffset.y, 9.8f};
+    
+       glUseProgram(worldShader);
 
     glEnableVertexAttribArray(worldShader_attribute_coord);
     glEnableVertexAttribArray(worldShader_attribute_tex);
@@ -296,7 +304,7 @@ void World::drawBackgrounds(void)
 
        bgTex++;
        glUniform4f(worldShader_uniform_color, .8, .8, .8, 1.3 - static_cast<float>(alpha)/255.0f);
-    glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, back_tex_coord);
+    glVertexAttribPointer(worldShader_attribute_coord, 3, GL_FLOAT, GL_FALSE, 0, fron_tex_coord);
     glVertexAttribPointer(worldShader_attribute_tex, 2, GL_FLOAT, GL_FALSE, 0, tex_coord);
     glDrawArrays(GL_TRIANGLES, 0 , 6);
 
@@ -467,32 +475,6 @@ void World::draw(Player *p)
 
        drawBackgrounds();
 
-       // draw particles and buildings
-    glBindTexture(GL_TEXTURE_2D, colorIndex);
-    glUniform1i(worldShader_uniform_texture, 0);
-    glUseProgram(worldShader);
-
-    glEnableVertexAttribArray(worldShader_attribute_coord);
-    glEnableVertexAttribArray(worldShader_attribute_tex);
-
-    uint ps = particles.size();
-       
-       GLfloat partVec[ps * 6 * 5 + 1];
-       GLfloat *pIndex = &partVec[0];
-
-    for (auto &p : particles) {
-        if (!p.behind)
-            p.draw(pIndex);
-    }
-
-    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);
-
-    glUseProgram(0);
 
     for (auto &l : light) {
         if (l.belongsTo) {
@@ -716,6 +698,44 @@ void World::draw(Player *p)
 
     // draw the player
        p->draw();
+
+       // draw particles like a MASTAH
+    glBindTexture(GL_TEXTURE_2D, colorIndex);
+    glUniform1i(worldShader_uniform_texture, 0);
+    glUseProgram(worldShader);
+
+       glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, .8);
+
+    glEnableVertexAttribArray(worldShader_attribute_coord);
+    glEnableVertexAttribArray(worldShader_attribute_tex);
+
+    uint ps = particles.size();
+    uint pss = ps * 6 * 5;     
+       uint pc = 0;
+       
+       std::vector<GLfloat> partVec(pss);
+       GLfloat *pIndex = &partVec[0];
+    
+       for (uint p = 0; p < ps; p++) {
+        pc += 30;
+               if (pc > pss) {
+                       // TODO resize the vector or something better than breaking
+                       std::cout << "Whoops:" << pc << "," << partVec.size() << std::endl;
+                       break;
+               }
+               particles[p].draw(pIndex);
+    }
+
+    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);
+
+       glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.0);
+
+    glUseProgram(0);
 }
 
 /**
@@ -863,7 +883,7 @@ detect(Player *p)
                                                        HLINES(1.25),                                                                           // width
                                                        HLINES(1.25),                                                                           // height
                                                        randGet() % 7 * .01 * (randGet() % 2 == 0 ? -1 : 1),    // vel.x
-                                                       (4 + randGet() % 6) * .05,                                                      // vel.y
+                                                       randGet() % 1 ? (8 + randGet() % 6) * .05 : (4 + randGet() % 6) * .05,                                                  // vel.y
                                                        { 0, 0, 255 },                                                                          // RGB color
                                                        2500                                                                                            // duration (ms)
                                                        );