Apologies if this has been asked many times before...
I'm new to game coding and thought it would be fun to write my own isometric engine. I'm trying to limit how fast the game loop runs so that my engine will run at the same rate on all systems (I'm able to test the idea out on my netbook vs main PC), but for some reason this little bit of code just won't work...
As you can see I'm using c++11 and allegro 4.4.2
The idea is that using <chrono> I could tell how long the system took to run 1 loop of the game loop and subtract that from 20 (target framerate being 50 fps) so that it takes exactly 20 milliseconds to run the whole thing. However when I run the game on both systems the hero sprite moves noticeably slower on my netbook even when I lower the target framerate. Is this duff coding, bad idea etc. etc.
Thanks for your help,
Andy
#include <allegro.h>
#include <chrono>
#define FPS 50
int main() {
while(!game.QUIT) {
auto started = std::chrono::high_resolution_clock::now();
process_input();
update();
render();
auto done = std::chrono::high_resolution_clock::now();
int elapsed_t = std::chrono::duration_cast<std::chrono::milliseconds>(done-started).count();
int rest_t = rest_calc(elapsed_t);
rest(rest_t);
}
deinit();
return 0;
}
END_OF_MAIN();
int rest_calc(int elapsed_t) {
int rest_time = (1000 / FPS) - elapsed_t; //calculate rest time to achieve framerate set by FPS
if(rest_time > 0) return rest_time;
else return 0;
}