Klasa do fps + Limit Fps

This commit is contained in:
Pc
2025-01-07 23:01:34 +01:00
parent 01d7766b6a
commit 79c6a1e144

View File

@@ -32,6 +32,7 @@
#include <ctime> #include <ctime>
#include "timeh.hpp" #include "timeh.hpp"
#include "FPSCounter.cpp" #include "FPSCounter.cpp"
#include <thread>
using namespace glm; using namespace glm;
@@ -113,11 +114,22 @@ void RotateRoverAndCamera(float angle) {
} }
*/ */
//Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FPSCounter fpsCounter; FPSCounter fpsCounter;
static const int targetFPS = 144; // Celowany FPS
//Fps counter //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<double> 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<double>(frameTime - elapsed.count()));
}
lastTime = std::chrono::high_resolution_clock::now();
}
bool keyWPressed = false; bool keyWPressed = false;
bool keySPressed = false; bool keySPressed = false;
bool keyAPressed = false; bool keyAPressed = false;
@@ -429,7 +441,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
@@ -457,7 +469,7 @@ void RenderScene(void) {
// prymitywny licznik FPS // prymitywny licznik FPS
if (monitormode) { if (monitormode) {
std::time_t now_t = std::time(nullptr); std::time_t now_t = std::time(nullptr);
yRot -= 10; // showcase demo //yRot -= 1; // showcase demo
if (now_t > monitormodehelper) { if (now_t > monitormodehelper) {
std::cout << (int)(monitormodecounter / (now_t - monitormodehelper)) << " fps\n"; std::cout << (int)(monitormodecounter / (now_t - monitormodehelper)) << " fps\n";
monitormodehelper = now_t; monitormodehelper = now_t;
@@ -776,13 +788,19 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
SwapBuffers(hDC); SwapBuffers(hDC);
// Validate the newly painted client area // Validate the newly painted client area
if (!monitormode) ValidateRect(hWnd, NULL); if (!monitormode) {
else InvalidateRect(hWnd, NULL, FALSE); ValidateRect(hWnd, NULL);
break; }
else {
InvalidateRect(hWnd, NULL, FALSE);
}
// Windows is telling the application that it may modify // Limit FPS
// the system palette. This message in essance asks the LimitFPS(targetFPS);
// application for a new palette.
// Update FPS counter
fpsCounter.update();
break;
case WM_QUERYNEWPALETTE: case WM_QUERYNEWPALETTE:
// If the palette was created. // If the palette was created.
if (hPalette) { if (hPalette) {