]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
thread work
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 14 Jan 2017 17:48:49 +0000 (12:48 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 14 Jan 2017 17:48:49 +0000 (12:48 -0500)
include/particle.hpp
main.cpp
src/particle.cpp
src/render.cpp

index 55228c035912fb56f4322d332d2c13d1d9dc9845..48a89380fa908aee2b1615acd2a6bc39b0a27d6d 100644 (file)
@@ -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> {
index 2e5e69dd0e7c0359cedd5f57369c98d64e281fc2..49199a0b2d5800cd4299a16c8e92e14b1ebfb21f 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -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
        }
 
index 198387c2d5c585d8fee243857a07b4edcfa86374..6eeec338ccc01a5cae80c53c69a345ccd6ffdaa5 100644 (file)
@@ -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)
index a63d63a5b2fa403d16ec7944e94e925652836f94..5cbd11e15001e482b75acfd3193ee8936093a4d4 100644 (file)
@@ -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