From 72a07395c75600694f5847255d75aa237ab4dfa4 Mon Sep 17 00:00:00 2001 From: Justin Date: Sun, 11 May 2025 18:06:28 -0500 Subject: [PATCH] Added the game loop the game. --- src/game.cpp | 26 ++++++++++++++++++++++++++ src/include/game.h | 12 ++++++++++++ src/include/main.h | 1 + src/main.cpp | 6 ++++-- 4 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index e69de29..163c7f3 100644 --- a/src/game.cpp +++ b/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 target_frame_duration = chrono::duration(1.0 / target_fps); + auto previous_time = std::chrono::steady_clock::now(); + + + while(true){ + auto current_time = std::chrono::steady_clock::now(); + chrono::duration 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 sleep_time = target_frame_duration - elapsed_time; + this_thread::sleep_for(sleep_time); + } + + previous_time = chrono::steady_clock::now(); + + } +} diff --git a/src/include/game.h b/src/include/game.h index e69de29..69ef995 100644 --- a/src/include/game.h +++ b/src/include/game.h @@ -0,0 +1,12 @@ +#include +#include +#include +#include +using namespace std; + + +class Game{ + public: + void gameloop(); + +}; diff --git a/src/include/main.h b/src/include/main.h index bac4dbc..1567953 100644 --- a/src/include/main.h +++ b/src/include/main.h @@ -2,4 +2,5 @@ #include "intro.h" #include "title.h" #include +#include "game.h" using namespace std; diff --git a/src/main.cpp b/src/main.cpp index f6ab216..9e22f87 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; }