diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-01-14 12:48:49 -0500 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-01-14 12:48:49 -0500 |
commit | d20633705e53a122467fb39fdbb2de3cfec279f7 (patch) | |
tree | db9d18f65b0461f183bfe38ca07521b4fdcbcce4 | |
parent | 3da25ab8c6ad1b52b808bfeffc0ad1b32621cfac (diff) |
thread work
-rw-r--r-- | include/particle.hpp | 4 | ||||
-rw-r--r-- | main.cpp | 76 | ||||
-rw-r--r-- | src/particle.cpp | 2 | ||||
-rw-r--r-- | src/render.cpp | 2 |
4 files changed, 51 insertions, 33 deletions
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<ParticleSystem> { @@ -21,6 +21,31 @@ using namespace std::literals::chrono_literals; #include <render.hpp> #include <ui.hpp> +class GameThread : public entityx::Receiver<GameThread> { +private: + std::atomic_bool die; + bool running; + std::thread thread; + +public: + template<typename F> + GameThread(F&& func) { + die.store(false); + running = true; + thread = std::thread([this](F&& f) { + while (!die.load()) + f(); + running = false; + }, std::forward<F>(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<MainSDLEvent>(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<MainSDLEvent>(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<WorldSystem>()->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<const char *>(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 |