diff options
Diffstat (limited to 'src/game.cpp')
-rw-r--r-- | src/game.cpp | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/src/game.cpp b/src/game.cpp index b2de5e9..a494c5f 100644 --- a/src/game.cpp +++ b/src/game.cpp | |||
@@ -50,17 +50,14 @@ void Game::execute(GLFWwindow* window) | |||
50 | Texture buffer(GAME_WIDTH, GAME_HEIGHT); | 50 | Texture buffer(GAME_WIDTH, GAME_HEIGHT); |
51 | 51 | ||
52 | double lastTime = glfwGetTime(); | 52 | double lastTime = glfwGetTime(); |
53 | int nbFrames = 0; | 53 | const double dt = 0.01; |
54 | double accumulator = 0.0; | ||
55 | |||
54 | while (!(shouldQuit || glfwWindowShouldClose(window))) | 56 | while (!(shouldQuit || glfwWindowShouldClose(window))) |
55 | { | 57 | { |
56 | double currentTime = glfwGetTime(); | 58 | double currentTime = glfwGetTime(); |
57 | nbFrames++; | 59 | double frameTime = currentTime - lastTime; |
58 | if (currentTime - lastTime >= 1.0) | 60 | lastTime = currentTime; |
59 | { | ||
60 | printf("%f ms/frame\n", 1000.0/double(nbFrames)); | ||
61 | nbFrames = 0; | ||
62 | lastTime += 1.0; | ||
63 | } | ||
64 | 61 | ||
65 | // Should we load a new world? | 62 | // Should we load a new world? |
66 | if (newWorld) | 63 | if (newWorld) |
@@ -74,23 +71,29 @@ void Game::execute(GLFWwindow* window) | |||
74 | glfwPollEvents(); | 71 | glfwPollEvents(); |
75 | 72 | ||
76 | // Tick! | 73 | // Tick! |
77 | for (auto entity : entities) | 74 | accumulator += frameTime; |
75 | while (accumulator >= dt) | ||
78 | { | 76 | { |
79 | entity->tick(*this); | 77 | for (auto entity : entities) |
78 | { | ||
79 | entity->tick(*this, dt); | ||
80 | } | ||
81 | |||
82 | accumulator -= dt; | ||
80 | } | 83 | } |
81 | 84 | ||
82 | // Do any scheduled tasks | 85 | // Do any scheduled tasks |
83 | for (auto& task : scheduled) | 86 | for (auto& task : scheduled) |
84 | { | 87 | { |
85 | task.first--; | 88 | task.first -= frameTime; |
86 | 89 | ||
87 | if (task.first == 0) | 90 | if (task.first <= 0) |
88 | { | 91 | { |
89 | task.second(); | 92 | task.second(); |
90 | } | 93 | } |
91 | } | 94 | } |
92 | 95 | ||
93 | scheduled.remove_if([] (std::pair<int, std::function<void ()>> value) { return value.first == 0; }); | 96 | scheduled.remove_if([] (std::pair<double, std::function<void ()>> value) { return value.first <= 0; }); |
94 | 97 | ||
95 | // Do rendering | 98 | // Do rendering |
96 | buffer.fill(buffer.entirety(), 0, 0, 0); | 99 | buffer.fill(buffer.entirety(), 0, 0, 0); |
@@ -133,9 +136,9 @@ void Game::saveGame(const Map& map, std::pair<double, double> position) | |||
133 | save = {&map, position}; | 136 | save = {&map, position}; |
134 | } | 137 | } |
135 | 138 | ||
136 | void Game::schedule(int frames, std::function<void ()>&& callback) | 139 | void Game::schedule(double time, std::function<void ()>&& callback) |
137 | { | 140 | { |
138 | scheduled.emplace_front(frames, callback); | 141 | scheduled.emplace_front(time, callback); |
139 | } | 142 | } |
140 | 143 | ||
141 | void Game::playerDie(Entity& player, const Map& curMap) | 144 | void Game::playerDie(Entity& player, const Map& curMap) |
@@ -145,7 +148,7 @@ void Game::playerDie(Entity& player, const Map& curMap) | |||
145 | 148 | ||
146 | playSound("../res/Hit_Hurt5.wav", 0.25); | 149 | playSound("../res/Hit_Hurt5.wav", 0.25); |
147 | 150 | ||
148 | schedule(FRAMES_PER_SECOND * 0.75, [&] () { | 151 | schedule(0.75, [&] () { |
149 | if (&curMap != save.map) | 152 | if (&curMap != save.map) |
150 | { | 153 | { |
151 | loadMap(*(save.map)); | 154 | loadMap(*(save.map)); |