diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-01-30 13:03:50 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-01-30 13:03:50 -0500 |
commit | e3fcd5fc180b48e0710fbcbf6cfa94e906b8219c (patch) | |
tree | 61783526e178e7253b8704299bec31a190878790 | |
parent | f3166702d7dd30312b5a401f52941aad43ac51c3 (diff) | |
download | tanetane-e3fcd5fc180b48e0710fbcbf6cfa94e906b8219c.tar.gz tanetane-e3fcd5fc180b48e0710fbcbf6cfa94e906b8219c.tar.bz2 tanetane-e3fcd5fc180b48e0710fbcbf6cfa94e906b8219c.zip |
Abstracted frame timing stuff
-rw-r--r-- | src/main.cpp | 20 | ||||
-rw-r--r-- | src/timer.h | 28 |
2 files changed, 35 insertions, 13 deletions
diff --git a/src/main.cpp b/src/main.cpp index 1d3d58e..83f63e0 100644 --- a/src/main.cpp +++ b/src/main.cpp | |||
@@ -2,6 +2,7 @@ | |||
2 | #include "renderer.h" | 2 | #include "renderer.h" |
3 | #include "game.h" | 3 | #include "game.h" |
4 | #include "party.h" | 4 | #include "party.h" |
5 | #include "timer.h" | ||
5 | 6 | ||
6 | void loop(Renderer& renderer) { | 7 | void loop(Renderer& renderer) { |
7 | Game game; | 8 | Game game; |
@@ -18,11 +19,8 @@ void loop(Renderer& renderer) { | |||
18 | 19 | ||
19 | renderer.render(game); | 20 | renderer.render(game); |
20 | 21 | ||
21 | size_t inputDt = 50; | 22 | Timer inputTimer(50); |
22 | size_t inputAcc = 0; | 23 | Timer animTimer(1000/5);//30fps * 1000 t/s; |
23 | |||
24 | size_t animDt = 1000/5;//30fps * 1000 t/s; | ||
25 | size_t animAcc = 0; | ||
26 | 24 | ||
27 | size_t lastTime = SDL_GetTicks(); | 25 | size_t lastTime = SDL_GetTicks(); |
28 | 26 | ||
@@ -44,17 +42,13 @@ void loop(Renderer& renderer) { | |||
44 | keystate.up = state[SDL_SCANCODE_UP]; | 42 | keystate.up = state[SDL_SCANCODE_UP]; |
45 | keystate.down = state[SDL_SCANCODE_DOWN]; | 43 | keystate.down = state[SDL_SCANCODE_DOWN]; |
46 | 44 | ||
47 | inputAcc += frameTime; | 45 | inputTimer.accumulate(frameTime); |
48 | while (inputAcc > inputDt) { | 46 | while (inputTimer.step()) { |
49 | inputAcc -= inputDt; | ||
50 | |||
51 | party.move(game, keystate); | 47 | party.move(game, keystate); |
52 | } | 48 | } |
53 | 49 | ||
54 | animAcc += frameTime; | 50 | animTimer.accumulate(frameTime); |
55 | while (animAcc > animDt) { | 51 | while (animTimer.step()) { |
56 | animAcc -= animDt; | ||
57 | |||
58 | game.tickSpriteAnim(); | 52 | game.tickSpriteAnim(); |
59 | } | 53 | } |
60 | 54 | ||
diff --git a/src/timer.h b/src/timer.h new file mode 100644 index 0000000..0809610 --- /dev/null +++ b/src/timer.h | |||
@@ -0,0 +1,28 @@ | |||
1 | #ifndef TIMER_H_45E2F1F9 | ||
2 | #define TIMER_H_45E2F1F9 | ||
3 | |||
4 | class Timer { | ||
5 | public: | ||
6 | |||
7 | Timer(int dt) : dt_(dt) {} | ||
8 | |||
9 | void accumulate(int t) { | ||
10 | acc_ += t; | ||
11 | } | ||
12 | |||
13 | bool step() { | ||
14 | if (acc_ > dt_) { | ||
15 | acc_ -= dt_; | ||
16 | return true; | ||
17 | } else { | ||
18 | return false; | ||
19 | } | ||
20 | } | ||
21 | |||
22 | private: | ||
23 | |||
24 | int dt_; | ||
25 | int acc_ = 0; | ||
26 | }; | ||
27 | |||
28 | #endif /* end of include guard: TIMER_H_45E2F1F9 */ | ||