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

@@ -85,19 +85,24 @@ std::time_t monitormodehelper;
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();
static auto lastTime = std::chrono::steady_clock::now();
auto currentTime = std::chrono::steady_clock::now();
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
if (elapsed.count() < frameTime) {
std::this_thread::sleep_for(std::chrono::duration<double>(frameTime - elapsed.count()));
double timeToWait = frameTime - elapsed.count(); // Obliczamy czas do czekania
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 keyWPressed = false;
bool keySPressed = false;
@@ -851,6 +856,7 @@ void SetupRC() {
void RenderScene(void) {
//float normal[3]; // Storage for calculated surface normal
// 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ę
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
fpsCounter.update();
user.draw();
UpdateRover(fences);
glPopMatrix();
@@ -933,7 +939,7 @@ void RenderScene(void) {
//glColor3f(0.0, 1.0, 0.0);
//mapa.draw();
//glColor3f(0.0, 0.0, 0.0);
fpsCounter.update();
// Zamiana buforów (double buffering)
@@ -1243,11 +1249,10 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
//break;
// Limit FPS
LimitFPS(targetFPS);
// Uaktualniaj FPS
LimitFPS(targetFPS); // Ogranicz FPS
// Update FPS counter
if (monitormode) fpsCounter.update();
break;
case WM_QUERYNEWPALETTE:
// If the palette was created.