From 1e9f6aaddb2d21045f5e7dc63d6312a4407b3a08 Mon Sep 17 00:00:00 2001 From: drumsetmonkey Date: Thu, 9 Jun 2016 08:49:21 -0400 Subject: Fixed particle crashing --- brice.dat | 5 +++++ include/entities.hpp | 1 + src/world.cpp | 31 ++++++++++++++++++++----------- xml/playerSpawnHill1.xml | 21 ++++++++++----------- xml/playerSpawnHill1_Building1.xml | 6 +++--- 5 files changed, 39 insertions(+), 25 deletions(-) diff --git a/brice.dat b/brice.dat index e69de29..ea71945 100644 --- a/brice.dat +++ b/brice.dat @@ -0,0 +1,5 @@ +2 +canSprint +0 +canJump +0 diff --git a/include/entities.hpp b/include/entities.hpp index 826b0f6..605cc77 100644 --- a/include/entities.hpp +++ b/include/entities.hpp @@ -17,6 +17,7 @@ #include #include #include +#include // local library includes #include diff --git a/src/world.cpp b/src/world.cpp index 5809ab8..a36a9b8 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -9,6 +9,7 @@ #include #include #include +#include // local game headers #include @@ -30,6 +31,9 @@ extern World *currentWorldToRight; // main.cpp extern bool inBattle; // ui.cpp? extern std::string xmlFolder; +// particle mutex +std::mutex partMutex; + // externally referenced in main.cpp int worldShade = 0; @@ -776,22 +780,23 @@ void World::draw(Player *p) glEnableVertexAttribArray(worldShader_attribute_coord); glEnableVertexAttribArray(worldShader_attribute_tex); - uint ps = particles.size(); + partMutex.lock(); + uint ps = particles.size(); uint pss = ps * 6 * 5; uint pc = 0; std::vector partVec(pss); - GLfloat *pIndex = &partVec[0]; - - for (const auto &p : particles) { + auto *pIndex = &partVec[0]; + for (uint i = 0; i < ps; i++) { pc += 30; if (pc > pss) { // TODO resize the vector or something better than breaking - //std::cout << "Whoops:" << pc << "," << partVec.size() << std::endl; + std::cout << "Whoops:" << pc << "," << partVec.size() << std::endl; break; } - p.draw(pIndex); + particles[i].draw(pIndex); } + partMutex.unlock(); 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]); @@ -802,7 +807,7 @@ void World::draw(Player *p) glUniform4f(worldShader_uniform_color, 1.0, 1.0, 1.0, 1.0); - glUseProgram(0); + glUseProgram(0); } /** @@ -936,6 +941,8 @@ detect(Player *p) singleDetect(e); //std::thread(&World::singleDetect, this, e).detach(); + // qwertyuiop + partMutex.lock(); // handle particles for (auto &part : particles) { // get particle's current world line @@ -983,6 +990,7 @@ detect(Player *p) break; } } + partMutex.unlock(); // draws the village welcome message if the player enters the village bounds for (auto &v : village) { @@ -1033,15 +1041,16 @@ update(Player *p, unsigned int delta, unsigned int ticks) e->loc.y += e->vel.y * delta; } } - // iterate through particles + partMutex.lock(); + // iterate through particles particles.remove_if([](const Particles &part) { return part.duration <= 0; }); for (auto &pa : particles) { if (pa.canMove) { // causes overhead - pa.loc.y += pa.vel.y * delta; - pa.loc.x += pa.vel.x * delta; + pa.loc.y += pa.vel.y * game::time::getDeltaTime(); + pa.loc.x += pa.vel.x * game::time::getDeltaTime(); if (pa.stu != nullptr) { if (pa.loc.x >= pa.stu->loc.x && pa.loc.x <= pa.stu->loc.x + pa.stu->width && @@ -1050,7 +1059,7 @@ update(Player *p, unsigned int delta, unsigned int ticks) } } } - + partMutex.unlock(); // handle music fades if (!Mix_PlayingMusic()) Mix_FadeInMusic(bgmObj, -1, 2000); diff --git a/xml/playerSpawnHill1.xml b/xml/playerSpawnHill1.xml index d7889fc..f16b511 100644 --- a/xml/playerSpawnHill1.xml +++ b/xml/playerSpawnHill1.xml @@ -5,13 +5,13 @@ - - - + + + - - - + + + @@ -37,15 +37,14 @@ And it wasn't stormy. - Hello there! My name is Ralph. - 300 + Hello there! My name is Ralph. 300 You should go talk to my friend Johnny. He's a pretty chill dude. Niice. - + Go check out Johnny. He's cool. @@ -54,7 +53,7 @@ And it wasn't stormy. Sup bro! Have a quest. To complete it, just go talk to Ralph again. - + Dank MayMay,2 Wood Sword,1 @@ -68,5 +67,5 @@ And it wasn't stormy. Hey friend! It's dangerous out there, here take these! Wait, promise you'll stop by my stand in the local market! - + diff --git a/xml/playerSpawnHill1_Building1.xml b/xml/playerSpawnHill1_Building1.xml index 800f007..eb63d7b 100644 --- a/xml/playerSpawnHill1_Building1.xml +++ b/xml/playerSpawnHill1_Building1.xml @@ -10,17 +10,17 @@ Hey. Have a Dank MayMay :) - + What? You want another Dank MayMay? K. - + Well... I'm out of Dank MayMays. Have a sword though. - + -- cgit v1.2.3