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_BUILD __DATE__ " " __TIME__
//platform exports/imports
#if defined(__linux__)
//platform/compiler-specific instructions
#if defined(__linux__) || defined(__MINGW32__) || defined(__GNUC__)
#include <unistd.h>
#define BOX_API extern
#elif defined(_MSC_VER)
#include <windows.h>
#ifndef BOX_EXPORT
#define BOX_API __declspec(dllimport)
#else
#define BOX_API
#define BOX_API __declspec(dllexport)
#endif
#else
#define BOX_API extern
#endif

View File

@@ -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

View File

@@ -7,15 +7,14 @@
#include "toy_literal_array.h"
#include "toy_literal_dictionary.h"
//TODO: remove this, and replace with time.h
#include <sys/time.h>
#include <time.h>
//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