From 57b545e51729d8d333d1b5a38cae7591162dbb58 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 16 Feb 2023 21:01:22 +0000 Subject: [PATCH] Changed to using clock_t for timing --- box/box_common.h | 18 +++++++++++++++--- box/box_engine.c | 14 +++++++------- box/box_engine.h | 7 +++---- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/box/box_common.h b/box/box_common.h index 6f1d339..2361a94 100644 --- a/box/box_common.h +++ b/box/box_common.h @@ -12,12 +12,24 @@ #define BOX_VERSION_PATCH 0 #define BOX_VERSION_BUILD __DATE__ " " __TIME__ -//platform exports/imports -#if defined(__linux__) +//platform/compiler-specific instructions +#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__) + +#include #define BOX_API extern +#elif defined(_MSC_VER) + +#include +#ifndef BOX_EXPORT +#define BOX_API __declspec(dllimport) #else -#define BOX_API +#define BOX_API __declspec(dllexport) +#endif + +#else + +#define BOX_API extern #endif diff --git a/box/box_engine.c b/box/box_engine.c index 336c412..a9c81d6 100644 --- a/box/box_engine.c +++ b/box/box_engine.c @@ -336,29 +336,29 @@ void Box_execEngine() { } //set up time - gettimeofday(&engine.realTime, NULL); + engine.realTime = clock(); engine.simTime = engine.realTime; - struct timeval delta = { .tv_sec = 0, .tv_usec = 1000 * 1000 / 60 }; //60 frames per second + clock_t delta = (double) CLOCKS_PER_SEC / 60.0; while (engine.running) { execEvents(); //calc the time passed - gettimeofday(&engine.realTime, NULL); + engine.realTime = clock(); //if not enough time has passed - if (timercmp(&engine.simTime, &engine.realTime, <)) { + if (engine.simTime < engine.realTime) { //while not enough time has passed - while(timercmp(&engine.simTime, &engine.realTime, <)) { + while(engine.simTime < engine.realTime) { //simulate the world execStep(); //calc the time simulation - timeradd(&delta, &engine.simTime, &engine.simTime); + engine.simTime += delta; } } else { - SDL_Delay(10); //let the machine sleep, 10ms + sleep(0.01); //let the machine sleep, 10ms } //render the world diff --git a/box/box_engine.h b/box/box_engine.h index 6ed331b..06cec62 100644 --- a/box/box_engine.h +++ b/box/box_engine.h @@ -7,15 +7,14 @@ #include "toy_literal_array.h" #include "toy_literal_dictionary.h" -//TODO: remove this, and replace with time.h -#include +#include //the base engine object, which represents the state of the game typedef struct Box_private_engine { //engine stuff Box_EngineNode* rootNode; - struct timeval simTime; - struct timeval realTime; + clock_t simTime; + clock_t realTime; bool running; //Toy stuff