Hey, I finally got something working with timing functions. Would this be a suitable technique for a simplistic text based game engine? Would there be any issues with it in the future? I can forsee an issue with stability if the engine using it couldn't run fast enough to pull under the CORE_TIME, but that could be increased.
Commenting out the dummy loop unsync's the timing completely, with it in it lines up perfectly with my system clock. With no loop perhaps the difftime is just too small to register?
Thoughts?
edit: Just found an issue :P Changing CORE_TIME higher than 1 breaks it. tmp_timer controls the conditional display, and from what I have here, it won't fire off. This could also be the reason for it not working when removing the loop as tmp_timer doesn't increment to overcome x/core_time >= 1.
edit2: Fixed the core_time problem, still hangs and messes up when removing the loop, probably can't fix that. cpu was running at 50% :P
Code:
#include <stdio.h>
#include <time.h>
#define CORE_TIME 1
int main(void) {
double tot_time = 0.0;
time_t ref_time, chk_time;
int i = 0, a = 0, elapsed = 0;
while(1) {
ref_time = time(&ref_time);
for(i = 0; i < 10000; i++) {
a++;
}
chk_time = time(&chk_time);
tot_time += difftime(chk_time, ref_time);
if( (tot_time / CORE_TIME) >= 1 ) {
tot_time = 0;
elapsed += CORE_TIME;
printf("Time: %d Seconds.\n", elapsed);
}
}
return 0;
}