Added the game loop the game.

This commit is contained in:
Justin 2025-05-11 18:06:28 -05:00
parent ee934b2d26
commit 72a07395c7
4 changed files with 43 additions and 2 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1,12 @@
#include <iostream>
#include <chrono>
#include <thread>
#include <cstdlib>
using namespace std;
class Game{
public:
void gameloop();
};

View File

@ -2,4 +2,5 @@
#include "intro.h"
#include "title.h"
#include <cstdlib>
#include "game.h"
using namespace std;

View File

@ -5,7 +5,9 @@ int main (){
//Title title;
Title::mainTitle();
//Intro intro;
Intro::mainIntro();
//cin >> ;
//Intro::mainIntro();
Game game;
game.gameloop();
cin.get();
return 0;
}