aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/weather.hpp10
-rw-r--r--src/particle.cpp65
2 files changed, 57 insertions, 18 deletions
diff --git a/include/weather.hpp b/include/weather.hpp
index f7b53f1..3aac2e6 100644
--- a/include/weather.hpp
+++ b/include/weather.hpp
@@ -38,14 +38,18 @@ public:
(void)dt;
static auto& partSystem = *game::engine.getSystem<ParticleSystem>();
+ static int newPartDelay = 0; // TODO no
switch (weather) {
case Weather::Sunny:
break;
case Weather::Rainy:
- partSystem.add(vec2(offset.x - game::SCREEN_WIDTH / 2 + randGet() % game::SCREEN_WIDTH,
- offset.y + game::SCREEN_HEIGHT / 2 + 100),
- ParticleType::Drop);
+ if (newPartDelay++ == 4) {
+ newPartDelay = 0;
+ partSystem.add(vec2(offset.x - game::SCREEN_WIDTH / 2 + randGet() % game::SCREEN_WIDTH,
+ offset.y + game::SCREEN_HEIGHT / 2 + 100),
+ ParticleType::Drop);
+ }
break; // TODO
case Weather::Snowy:
break; // TODO
diff --git a/src/particle.cpp b/src/particle.cpp
index 8090e2d..3e140ae 100644
--- a/src/particle.cpp
+++ b/src/particle.cpp
@@ -12,9 +12,9 @@ ParticleSystem::ParticleSystem(int count, bool m)
void ParticleSystem::add(const vec2& pos, const ParticleType& type)
{
- // TODO enforce max
- //if (max && parts.size() >= std::end(parts))
- // return;
+ // TODO not enforce max
+ if (/*max &&*/ parts.size() + 1 >= parts.capacity())
+ return;
parts.emplace_back(pos, type);
}
@@ -28,16 +28,32 @@ void ParticleSystem::addMultiple(const int& count, const ParticleType& type, std
void ParticleSystem::render(void) const
{
- static const GLfloat tex[12] = {
- 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0
- };
+ static GLuint particleVBO = 9999, texVBO = 0;
+
+ if (particleVBO == 9999) {
+ glGenBuffers(1, &particleVBO);
+ glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
+ glBufferData(GL_ARRAY_BUFFER, parts.capacity() * 18 * sizeof(GLfloat), nullptr,
+ GL_STREAM_DRAW);
+
+ glGenBuffers(1, &texVBO);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, texVBO);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, parts.capacity() * 12 * sizeof(GLfloat), nullptr,
+ GL_STATIC_DRAW);
+ int top = (parts.capacity() - 1) * 12 * sizeof(GLfloat), i = 0;
+ unsigned char zero = 0;
+ while (i < top) {
+ glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, i, 1, &zero);
+ i++;
+ }
+ }
- Render::worldShader.use();
- Render::worldShader.enable();
- Colors::blue.use();
+ glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
+ // copy data into VBO
+ int offset = 0;
for (const auto& p : parts) {
- GLfloat coord[18] = {
+ GLfloat coords[18] = {
p.location.x, p.location.y, -1,
p.location.x, p.location.y + 5, -1,
p.location.x + 5, p.location.y + 5, -1,
@@ -45,14 +61,33 @@ void ParticleSystem::render(void) const
p.location.x + 5, p.location.y, -1,
p.location.x, p.location.y, -1
};
-
- glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coord);
- glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex);
- glDrawArrays(GL_TRIANGLES, 0, 6);
+
+ glBufferSubData(GL_ARRAY_BUFFER, offset, 18 * sizeof(GLfloat), coords);
+ offset += 18 * sizeof(GLfloat);
}
+
+ Render::worldShader.use();
+ Render::worldShader.enable();
+ Colors::blue.use();
+ //glUniform4f(Render::worldShader.uniform[WU_tex_color], 0.0f, 0.0f, 1.0f, 1.0f);
+
+ glEnableClientState(GL_VERTEX_ARRAY);
+
+ glBindBuffer(GL_ARRAY_BUFFER, particleVBO);
+ glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, 0);
+
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, texVBO);
+ glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0, 0);
+
+ glDrawArrays(GL_TRIANGLES, 0, parts.size() * 6);
+
+ glDisableClientState(GL_VERTEX_ARRAY);
Render::worldShader.disable();
Render::worldShader.unuse();
+
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt)
@@ -74,7 +109,7 @@ void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &e
// update movement
switch (p.type) {
case ParticleType::Drop:
- if (p.velocity.y > -.5)
+ if (p.velocity.y > -.6)
p.velocity.y -= 0.001f;
break;
case ParticleType::Confetti: