From 3da25ab8c6ad1b52b808bfeffc0ad1b32621cfac Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 14 Jan 2017 09:56:58 -0500 Subject: particle improvements, inventory start --- include/particle.hpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'include/particle.hpp') diff --git a/include/particle.hpp b/include/particle.hpp index 92ab7e4..55228c0 100644 --- a/include/particle.hpp +++ b/include/particle.hpp @@ -9,22 +9,22 @@ #include enum class ParticleType : char { - Drop, - Confetti, - SmallBlast, - SmallPoof + Drop = 1, + Confetti = 2, + SmallBlast = 4, + SmallPoof = 8 }; struct Particle { - vec2 location; + int timeLeft; vec2 velocity; + vec2 location; ParticleType type; - int timeLeft; vec2 color; // assets/colorIndex.png Particle(vec2 p, ParticleType t, int tl, vec2 c) - : location(p), type(t), timeLeft(tl), color(c) {} -};// __attribute__ ((packed)); + : timeLeft(tl), location(p), type(t), color(c) {} +}; class ParticleSystem : public entityx::System { private: -- cgit v1.2.3 From d20633705e53a122467fb39fdbb2de3cfec279f7 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 14 Jan 2017 12:48:49 -0500 Subject: thread work --- include/particle.hpp | 4 +-- main.cpp | 76 ++++++++++++++++++++++++++++++++-------------------- src/particle.cpp | 2 +- src/render.cpp | 2 +- 4 files changed, 51 insertions(+), 33 deletions(-) (limited to 'include/particle.hpp') diff --git a/include/particle.hpp b/include/particle.hpp index 55228c0..48a8938 100644 --- a/include/particle.hpp +++ b/include/particle.hpp @@ -17,13 +17,13 @@ enum class ParticleType : char { struct Particle { int timeLeft; + ParticleType type; vec2 velocity; vec2 location; - ParticleType type; vec2 color; // assets/colorIndex.png Particle(vec2 p, ParticleType t, int tl, vec2 c) - : timeLeft(tl), location(p), type(t), color(c) {} + : timeLeft(tl), type(t), location(p), color(c) {} }; class ParticleSystem : public entityx::System { diff --git a/main.cpp b/main.cpp index 2e5e69d..49199a0 100644 --- a/main.cpp +++ b/main.cpp @@ -21,6 +21,31 @@ using namespace std::literals::chrono_literals; #include #include +class GameThread : public entityx::Receiver { +private: + std::atomic_bool die; + bool running; + std::thread thread; + +public: + template + GameThread(F&& func) { + die.store(false); + running = true; + thread = std::thread([this](F&& f) { + while (!die.load()) + f(); + running = false; + }, std::forward(func)); + } + + void stop(void) { + die.store(true); + while (running) + std::this_thread::sleep_for(2ms); + } +}; + /** * The currently used folder to look for XML files in. */ @@ -112,43 +137,36 @@ int main(int argc, char *argv[]) if (!worldDontReallyRun) { // the main loop, in all of its gloriousness... - std::thread thMain ([&] { - const bool &run = game::engine.shouldRun; - while (run) { - game::time::mainLoopHandler(); - - if (game::time::tickHasPassed()) { - // calculate the world shading value - extern int worldShade; // TODO kill - worldShade = 50 * sin((game::time::getTickCount() + (DAY_CYCLE / 2)) / (DAY_CYCLE / PI)); + GameThread gtMain ([&] { + game::time::mainLoopHandler(); - // update fades - ui::fadeUpdate(); + if (game::time::tickHasPassed()) { + // calculate the world shading value + extern int worldShade; // TODO kill + worldShade = 50 * sin((game::time::getTickCount() + (DAY_CYCLE / 2)) / (DAY_CYCLE / PI)); - // increment game ticker - game::time::tick(); - } + // update fades + ui::fadeUpdate(); - while (!eventQueue.empty()) { - game::events.emit(eventQueue.back()); - eventQueue.pop_back(); - } - - game::engine.update(game::time::getDeltaTime()); + // increment game ticker + game::time::tick(); + } - std::this_thread::sleep_for(1ms); + while (!eventQueue.empty()) { + game::events.emit(eventQueue.back()); + eventQueue.pop_back(); } + + game::engine.update(game::time::getDeltaTime()); + std::this_thread::sleep_for(1ms); }); static int fps = 0, fpsInternal = 0; // the debug loop, gets debug screen values - std::thread thDebug ([&] { - const bool &run = game::engine.shouldRun; - while (run) { - fps = fpsInternal, fpsInternal = 0; - std::this_thread::sleep_for(1s); - } + GameThread gtDebug ([&] { + fps = fpsInternal, fpsInternal = 0; + std::this_thread::sleep_for(1s); }); // the render loop, renders @@ -163,8 +181,8 @@ int main(int argc, char *argv[]) } // on game end, get back together - thMain.join(); - thDebug.join(); + gtMain.stop(); + gtDebug.stop(); //game::engine.getSystem()->thAmbient.join(); // segfault or something } diff --git a/src/particle.cpp b/src/particle.cpp index 198387c..6eeec33 100644 --- a/src/particle.cpp +++ b/src/particle.cpp @@ -89,6 +89,7 @@ void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &e for (unsigned int i = 0; i < parts.size(); i++) { auto& p = parts[i]; + auto& vel = p.velocity; // update timers if (p.timeLeft > 0) @@ -97,7 +98,6 @@ void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &e continue; // update movement - auto& vel = p.velocity; switch (p.type) { case ParticleType::Drop: if (vel.y > -.6) diff --git a/src/render.cpp b/src/render.cpp index a63d63a..5cbd11e 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -79,7 +79,7 @@ void init(void) + reinterpret_cast(glewGetErrorString(glewError))); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // anti-aliasing - SDL_GL_SetSwapInterval(0); // v-sync + SDL_GL_SetSwapInterval(1); // v-sync SDL_ShowCursor(SDL_DISABLE); // hide the cursor glViewport(0, 0, game::SCREEN_WIDTH, game::SCREEN_HEIGHT); // pixel coordinates glEnable(GL_BLEND); // alpha enabling -- cgit v1.2.3