aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordrumsetmonkey <abelleisle@roadrunner.com>2016-05-15 21:25:41 -0400
committerdrumsetmonkey <abelleisle@roadrunner.com>2016-05-15 21:25:41 -0400
commit0d4504017648b56a565216119a82166deb1f8d5d (patch)
treec2139ae55552009e176c7abfc8e98443784fb638
parent23f3b1471b2bbfe92252456f32c9ae55a9d1f213 (diff)
more efficient particles
-rw-r--r--config/settings.xml.example2
-rw-r--r--include/common.hpp2
-rw-r--r--include/entities.hpp2
-rw-r--r--src/entities.cpp74
-rw-r--r--src/world.cpp34
5 files changed, 68 insertions, 46 deletions
diff --git a/config/settings.xml.example b/config/settings.xml.example
index e82c454..1d694f1 100644
--- a/config/settings.xml.example
+++ b/config/settings.xml.example
@@ -16,7 +16,7 @@ Available fonts:
-->
<font path="ttf/FreePixel.ttf"/>
-<hline size="3"/>
+<hline size="5"/>
<volume>
<master volume="100"/>
diff --git a/include/common.hpp b/include/common.hpp
index 240cd01..fabf4c7 100644
--- a/include/common.hpp
+++ b/include/common.hpp
@@ -14,6 +14,8 @@
#include <vector>
#include <cmath>
#include <algorithm>
+#include <list>
+#include <iterator>
#ifndef __WIN32__
# include <thread>
diff --git a/include/entities.hpp b/include/entities.hpp
index 1c3b6ff..83b47b9 100644
--- a/include/entities.hpp
+++ b/include/entities.hpp
@@ -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);
diff --git a/src/entities.cpp b/src/entities.cpp
index 205e848..af9747d 100644
--- a/src/entities.cpp
+++ b/src/entities.cpp
@@ -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)
diff --git a/src/world.cpp b/src/world.cpp
index 327a787..0c5007d 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -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;