Changed to using clock_t for timing

This commit is contained in:
2023-02-16 21:01:22 +00:00
parent f854979f4c
commit 57b545e517
3 changed files with 25 additions and 14 deletions

View File

@@ -12,12 +12,24 @@
#define BOX_VERSION_PATCH 0 #define BOX_VERSION_PATCH 0
#define BOX_VERSION_BUILD __DATE__ " " __TIME__ #define BOX_VERSION_BUILD __DATE__ " " __TIME__
//platform exports/imports //platform/compiler-specific instructions
#if defined(__linux__) #if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
#include <unistd.h>
#define BOX_API extern #define BOX_API extern
#elif defined(_MSC_VER)
#include <windows.h>
#ifndef BOX_EXPORT
#define BOX_API __declspec(dllimport)
#else #else
#define BOX_API #define BOX_API __declspec(dllexport)
#endif
#else
#define BOX_API extern
#endif #endif

View File

@@ -336,29 +336,29 @@ void Box_execEngine() {
} }
//set up time //set up time
gettimeofday(&engine.realTime, NULL); engine.realTime = clock();
engine.simTime = engine.realTime; 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) { while (engine.running) {
execEvents(); execEvents();
//calc the time passed //calc the time passed
gettimeofday(&engine.realTime, NULL); engine.realTime = clock();
//if not enough time has passed //if not enough time has passed
if (timercmp(&engine.simTime, &engine.realTime, <)) { if (engine.simTime < engine.realTime) {
//while not enough time has passed //while not enough time has passed
while(timercmp(&engine.simTime, &engine.realTime, <)) { while(engine.simTime < engine.realTime) {
//simulate the world //simulate the world
execStep(); execStep();
//calc the time simulation //calc the time simulation
timeradd(&delta, &engine.simTime, &engine.simTime); engine.simTime += delta;
} }
} }
else { else {
SDL_Delay(10); //let the machine sleep, 10ms sleep(0.01); //let the machine sleep, 10ms
} }
//render the world //render the world

View File

@@ -7,15 +7,14 @@
#include "toy_literal_array.h" #include "toy_literal_array.h"
#include "toy_literal_dictionary.h" #include "toy_literal_dictionary.h"
//TODO: remove this, and replace with time.h #include <time.h>
#include <sys/time.h>
//the base engine object, which represents the state of the game //the base engine object, which represents the state of the game
typedef struct Box_private_engine { typedef struct Box_private_engine {
//engine stuff //engine stuff
Box_EngineNode* rootNode; Box_EngineNode* rootNode;
struct timeval simTime; clock_t simTime;
struct timeval realTime; clock_t realTime;
bool running; bool running;
//Toy stuff //Toy stuff