Fixed Fps limit

This commit is contained in:
Pc
2025-01-16 23:59:40 +01:00
parent cfb3f12ef4
commit bea13b98f3
2 changed files with 18 additions and 12 deletions

View File

@@ -10,6 +10,7 @@ public:
auto currentTime = std::chrono::high_resolution_clock::now(); auto currentTime = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = currentTime - lastTime; std::chrono::duration<double> elapsed = currentTime - lastTime;
// Aktualizujemy FPS co 1 sekundê
if (elapsed.count() >= 1.0) { if (elapsed.count() >= 1.0) {
double fps = frameCount / elapsed.count(); double fps = frameCount / elapsed.count();
std::cout << "FPS: " << fps << std::endl; std::cout << "FPS: " << fps << std::endl;

View File

@@ -85,19 +85,24 @@ std::time_t monitormodehelper;
FPSCounter fpsCounter; FPSCounter fpsCounter;
static const int targetFPS = 144; // Celowany FPS static const int targetFPS = 144; // Celowany FPS
//Fps counter //Fps counter
void LimitFPS(int targetFPS) { void LimitFPS(int targetFPS) {
static auto lastTime = std::chrono::high_resolution_clock::now(); static auto lastTime = std::chrono::steady_clock::now();
auto currentTime = std::chrono::high_resolution_clock::now(); auto currentTime = std::chrono::steady_clock::now();
std::chrono::duration<double> elapsed = currentTime - lastTime; std::chrono::duration<double> elapsed = currentTime - lastTime;
// Jeśli upłynęło za mało czasu, aby osiągnąć target FPS, czekamy // Obliczamy czas na jedną klatkę
double frameTime = 1.0 / targetFPS; // Czas na jedną klatkę w sekundach double frameTime = 1.0 / targetFPS; // Czas na jedną klatkę w sekundach
if (elapsed.count() < frameTime) { double timeToWait = frameTime - elapsed.count(); // Obliczamy czas do czekania
std::this_thread::sleep_for(std::chrono::duration<double>(frameTime - elapsed.count()));
if (timeToWait > 0.0) {
// Jeśli czas do czekania jest większy niż 0, to śpimy przez tę wartość
std::this_thread::sleep_for(std::chrono::duration<double>(timeToWait));
} }
lastTime = std::chrono::high_resolution_clock::now(); lastTime = currentTime; // Zaktualizuj czas dla następnej iteracji
} }
bool Kolizja = false; bool Kolizja = false;
bool keyWPressed = false; bool keyWPressed = false;
bool keySPressed = false; bool keySPressed = false;
@@ -851,6 +856,7 @@ void SetupRC() {
void RenderScene(void) { void RenderScene(void) {
//float normal[3]; // Storage for calculated surface normal //float normal[3]; // Storage for calculated surface normal
// Save the matrix state and do the rotations // Save the matrix state and do the rotations
@@ -913,7 +919,7 @@ void RenderScene(void) {
glTranslatef(Foward, 0.0f, Sides); // Translacja łazika na jego pozycję glTranslatef(Foward, 0.0f, Sides); // Translacja łazika na jego pozycję
glRotatef(Rotation, 0.0f, 1.0f, 0.0f); // Obrót łazika wokół własnej osi glRotatef(Rotation, 0.0f, 1.0f, 0.0f); // Obrót łazika wokół własnej osi
glColor3f(1.0, 0.0, 0.0); // Czerwony kolor dla łazika glColor3f(1.0, 0.0, 0.0); // Czerwony kolor dla łazika
fpsCounter.update();
user.draw(); user.draw();
UpdateRover(fences); UpdateRover(fences);
glPopMatrix(); glPopMatrix();
@@ -933,7 +939,7 @@ void RenderScene(void) {
//glColor3f(0.0, 1.0, 0.0); //glColor3f(0.0, 1.0, 0.0);
//mapa.draw(); //mapa.draw();
//glColor3f(0.0, 0.0, 0.0); //glColor3f(0.0, 0.0, 0.0);
fpsCounter.update();
// Zamiana buforów (double buffering) // Zamiana buforów (double buffering)
@@ -1243,11 +1249,10 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
//break; //break;
// Limit FPS // Limit FPS
LimitFPS(targetFPS); // Uaktualniaj FPS
LimitFPS(targetFPS); // Ogranicz FPS
// Update FPS counter
if (monitormode) fpsCounter.update();
break;
case WM_QUERYNEWPALETTE: case WM_QUERYNEWPALETTE:
// If the palette was created. // If the palette was created.