summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederico Linhares <fred@linhares.blue>2022-08-08 11:12:51 -0300
committerFrederico Linhares <fred@linhares.blue>2022-08-08 11:12:51 -0300
commitb8838794e135c1b849ac7a8638ca8e948042ef86 (patch)
treeac6e08d58a6c577d9a4d63e806474cd471bc557b
parentddbd513fc4a227420ed3475dfb6025ca6457334b (diff)
refa Replace SDL timer with C++ timer
-rw-r--r--src/core.cpp5
-rw-r--r--src/core.hpp3
-rw-r--r--src/main.cpp19
3 files changed, 17 insertions, 10 deletions
diff --git a/src/core.cpp b/src/core.cpp
index 30acbf9..a1c996f 100644
--- a/src/core.cpp
+++ b/src/core.cpp
@@ -54,6 +54,8 @@ vk_debug_callback(
static void
load_variables(void *obj)
{
+ using namespace std::chrono;
+
YAML::Node root = YAML::LoadFile(cg_core.config_file);
std::string game_name = root["name"].as<std::string>().c_str();
@@ -70,7 +72,8 @@ load_variables(void *obj)
cg_core.game_version_patch = 0;
cg_core.fps = 30;
- cg_core.max_frame_duration_ms = 1000 / cg_core.fps;
+ cg_core.max_frame_duration =
+ duration<long long, std::milli>(1000 / cg_core.fps);
}
static void
diff --git a/src/core.hpp b/src/core.hpp
index a51d2b4..d9e68e8 100644
--- a/src/core.hpp
+++ b/src/core.hpp
@@ -23,6 +23,7 @@
#define DATA_DIR "/usr/local/share/candy_gear"
+#include <chrono>
#include <random>
#include <mruby.h>
@@ -87,7 +88,7 @@ struct cg_sCore
int game_version_major, game_version_minor, game_version_patch;
Uint32 fps;
- Uint32 max_frame_duration_ms;
+ std::chrono::duration<long long, std::milli> max_frame_duration;
SDL_Window *window;
diff --git a/src/main.cpp b/src/main.cpp
index 34cbcb4..5155f60 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -14,6 +14,9 @@
* limitations under the License.
*/
+#include <chrono>
+#include <thread>
+
#include "camera.hpp"
#include "candy_gear.hpp"
#include "core.hpp"
@@ -33,8 +36,10 @@ static void handle_error(mrb_state *mrb)
int main(int argc, char *argv[])
{
+ using namespace std::chrono;
+ using namespace std::this_thread;
+
SDL_Event event;
- Uint32 frame_start;
// Random numbers
random_number_generator.seed(random_seed());
@@ -79,7 +84,7 @@ int main(int argc, char *argv[])
if (cg_core.mrb->exc) handle_error(cg_core.mrb);
}
- frame_start = SDL_GetTicks();
+ auto frame_start = steady_clock::now();
// Game main loop.
while(!cg_core.quit_game)
@@ -118,14 +123,12 @@ int main(int argc, char *argv[])
// Timer
{
- Uint32 frame_stop, frame_duration;
-
- frame_stop = SDL_GetTicks();
- frame_duration = frame_stop - frame_start;
+ auto frame_stop = steady_clock::now();
+ auto frame_duration = frame_stop - frame_start;
// If frame take less time than maximum allowed.
- if(cg_core.max_frame_duration_ms > frame_duration)
- SDL_Delay(cg_core.max_frame_duration_ms - frame_duration);
+ if(cg_core.max_frame_duration > frame_duration)
+ sleep_for(cg_core.max_frame_duration - frame_duration);
frame_start = frame_stop;
}