Klasa do fps + Limit Fps
This commit is contained in:
36
main.cpp
36
main.cpp
@@ -32,6 +32,7 @@
|
||||
#include <ctime>
|
||||
#include "timeh.hpp"
|
||||
#include "FPSCounter.cpp"
|
||||
#include <thread>
|
||||
|
||||
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<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 keySPressed = false;
|
||||
bool keyAPressed = false;
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user