Added the game loop the game.
This commit is contained in:
parent
ee934b2d26
commit
72a07395c7
26
src/game.cpp
26
src/game.cpp
|
@ -0,0 +1,26 @@
|
||||||
|
#include "include/game.h"
|
||||||
|
|
||||||
|
void Game::gameloop(){
|
||||||
|
//This is the main game loop.
|
||||||
|
const int target_fps = 15;
|
||||||
|
const std::chrono::duration<double> target_frame_duration = chrono::duration<double>(1.0 / target_fps);
|
||||||
|
auto previous_time = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
|
|
||||||
|
while(true){
|
||||||
|
auto current_time = std::chrono::steady_clock::now();
|
||||||
|
chrono::duration<double> elapsed_time = current_time - previous_time;
|
||||||
|
|
||||||
|
// Game logic and rendering here
|
||||||
|
system("clear");
|
||||||
|
cout << "Frame time: " << elapsed_time.count() << " seconds" << endl;
|
||||||
|
|
||||||
|
if (elapsed_time < target_frame_duration) {
|
||||||
|
chrono::duration<double> sleep_time = target_frame_duration - elapsed_time;
|
||||||
|
this_thread::sleep_for(sleep_time);
|
||||||
|
}
|
||||||
|
|
||||||
|
previous_time = chrono::steady_clock::now();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
#include <cstdlib>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
|
class Game{
|
||||||
|
public:
|
||||||
|
void gameloop();
|
||||||
|
|
||||||
|
};
|
|
@ -2,4 +2,5 @@
|
||||||
#include "intro.h"
|
#include "intro.h"
|
||||||
#include "title.h"
|
#include "title.h"
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include "game.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
|
@ -5,7 +5,9 @@ int main (){
|
||||||
//Title title;
|
//Title title;
|
||||||
Title::mainTitle();
|
Title::mainTitle();
|
||||||
//Intro intro;
|
//Intro intro;
|
||||||
Intro::mainIntro();
|
//Intro::mainIntro();
|
||||||
//cin >> ;
|
Game game;
|
||||||
|
game.gameloop();
|
||||||
|
cin.get();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue