From 79c6a1e144e0115bdd05af9faf50507ee269002f Mon Sep 17 00:00:00 2001 From: Pc Date: Tue, 7 Jan 2025 23:01:34 +0100 Subject: [PATCH] Klasa do fps + Limit Fps --- main.cpp | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index d798968..a0b6e21 100644 --- a/main.cpp +++ b/main.cpp @@ -32,6 +32,7 @@ #include #include "timeh.hpp" #include "FPSCounter.cpp" +#include using namespace glm; @@ -113,11 +114,22 @@ void RotateRoverAndCamera(float angle) { } */ //Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - FPSCounter fpsCounter; - +static const int targetFPS = 144; // Celowany FPS //Fps counter +void LimitFPS(int targetFPS) { + static auto lastTime = std::chrono::high_resolution_clock::now(); + auto currentTime = std::chrono::high_resolution_clock::now(); + std::chrono::duration elapsed = currentTime - lastTime; + // Jeśli upłynęło za mało czasu, aby osiągnąć target FPS, czekamy + double frameTime = 1.0 / targetFPS; // Czas na jedną klatkę w sekundach + if (elapsed.count() < frameTime) { + std::this_thread::sleep_for(std::chrono::duration(frameTime - elapsed.count())); + } + + lastTime = std::chrono::high_resolution_clock::now(); +} bool keyWPressed = false; bool keySPressed = false; bool keyAPressed = false; @@ -429,7 +441,7 @@ void SetupRC() { } void RenderScene(void) { - + //float normal[3]; // Storage for calculated surface normal // Save the matrix state and do the rotations @@ -457,7 +469,7 @@ void RenderScene(void) { // prymitywny licznik FPS if (monitormode) { std::time_t now_t = std::time(nullptr); - yRot -= 10; // showcase demo + //yRot -= 1; // showcase demo if (now_t > monitormodehelper) { std::cout << (int)(monitormodecounter / (now_t - monitormodehelper)) << " fps\n"; monitormodehelper = now_t; @@ -776,13 +788,19 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) SwapBuffers(hDC); // Validate the newly painted client area - if (!monitormode) ValidateRect(hWnd, NULL); - else InvalidateRect(hWnd, NULL, FALSE); - break; + if (!monitormode) { + ValidateRect(hWnd, NULL); + } + else { + InvalidateRect(hWnd, NULL, FALSE); + } - // Windows is telling the application that it may modify - // the system palette. This message in essance asks the - // application for a new palette. + // Limit FPS + LimitFPS(targetFPS); + + // Update FPS counter + fpsCounter.update(); + break; case WM_QUERYNEWPALETTE: // If the palette was created. if (hPalette) {