aboutsummaryrefslogtreecommitdiff
path: root/timing/timing.cpp
diff options
context:
space:
mode:
authorjacopograndi <jak.sk8@hotmail.it>2021-08-19 18:46:51 +0200
committerjacopograndi <jak.sk8@hotmail.it>2021-08-19 18:46:51 +0200
commita8bcacc95045102e67f2feabbdddf79535837554 (patch)
tree5781dd4cb2fe66b67deab84ff4641b7e21b9c174 /timing/timing.cpp
forgot to make repo until now
Diffstat (limited to 'timing/timing.cpp')
-rw-r--r--timing/timing.cpp44
1 files changed, 44 insertions, 0 deletions
diff --git a/timing/timing.cpp b/timing/timing.cpp
new file mode 100644
index 0000000..37c5f40
--- /dev/null
+++ b/timing/timing.cpp
@@ -0,0 +1,44 @@
+#include "timing.h"
+
+timing_sdl::timing_sdl (double fps) {
+ frame_time = 1/fps;
+ last_time = SDL_GetTicks();
+ frame_counter = 0;
+ tot_time = 0;
+ unprocessed_time = 0;
+ frames = 0;
+ render = false;
+}
+
+void timing_sdl::process () {
+ render = false;
+
+ double startTime = SDL_GetTicks();
+ double passedTime = (startTime - last_time)/1000;
+ last_time = startTime;
+
+ unprocessed_time += passedTime;
+ frame_counter += passedTime;
+ tot_time += passedTime;
+
+ if (frame_counter >= 1.0) {
+ //printf("FPS: %i | %f ms\n", frames, 1000.0 / ((double)frames));
+ frames = 0;
+ frame_counter = 0;
+ }
+}
+
+bool timing_sdl::check_process () {
+ if (unprocessed_time > frame_time) {
+ render = true;
+ unprocessed_time -= frame_time;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+bool timing_sdl::check_render () {
+ if (render) { frames ++; }
+ return render;
+} \ No newline at end of file