Sterowanie 5.0 + Klasa do fps
This commit is contained in:
24
FPSCounter.cpp
Normal file
24
FPSCounter.cpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
class FPSCounter {
|
||||||
|
public:
|
||||||
|
FPSCounter() : frameCount(0), lastTime(std::chrono::high_resolution_clock::now()) {}
|
||||||
|
|
||||||
|
void update() {
|
||||||
|
frameCount++;
|
||||||
|
auto currentTime = std::chrono::high_resolution_clock::now();
|
||||||
|
std::chrono::duration<double> elapsed = currentTime - lastTime;
|
||||||
|
|
||||||
|
if (elapsed.count() >= 1.0) {
|
||||||
|
double fps = frameCount / elapsed.count();
|
||||||
|
std::cout << "FPS: " << fps << std::endl;
|
||||||
|
frameCount = 0;
|
||||||
|
lastTime = currentTime;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
int frameCount;
|
||||||
|
std::chrono::time_point<std::chrono::high_resolution_clock> lastTime;
|
||||||
|
};
|
||||||
@@ -121,6 +121,7 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="FPSCounter.cpp" />
|
||||||
<ClCompile Include="glew.c" />
|
<ClCompile Include="glew.c" />
|
||||||
<ClCompile Include="lazik.cpp" />
|
<ClCompile Include="lazik.cpp" />
|
||||||
<ClCompile Include="loadOBJ.cpp" />
|
<ClCompile Include="loadOBJ.cpp" />
|
||||||
|
|||||||
@@ -33,6 +33,9 @@
|
|||||||
<ClCompile Include="plane.cpp">
|
<ClCompile Include="plane.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="FPSCounter.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="loadOBJ.h">
|
<ClInclude Include="loadOBJ.h">
|
||||||
|
|||||||
73
main.cpp
73
main.cpp
@@ -31,6 +31,7 @@
|
|||||||
#include "GL/glfw3.h"
|
#include "GL/glfw3.h"
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include "timeh.hpp"
|
#include "timeh.hpp"
|
||||||
|
#include "FPSCounter.cpp"
|
||||||
|
|
||||||
using namespace glm;
|
using namespace glm;
|
||||||
|
|
||||||
@@ -113,8 +114,9 @@ void RotateRoverAndCamera(float angle) {
|
|||||||
*/
|
*/
|
||||||
//Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
//Zmienne do ruchu ##############################################^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
|
||||||
|
FPSCounter fpsCounter;
|
||||||
|
|
||||||
|
//Fps counter
|
||||||
|
|
||||||
bool keyWPressed = false;
|
bool keyWPressed = false;
|
||||||
bool keySPressed = false;
|
bool keySPressed = false;
|
||||||
@@ -133,7 +135,10 @@ float velocity = 0.0f; // Aktualna prędkość łazika
|
|||||||
const float friction = 0.1f; // Współczynnik tarcia (μ)
|
const float friction = 0.1f; // Współczynnik tarcia (μ)
|
||||||
const float maxSpeed = 3.0f; // Maksymalna prędkość łazika
|
const float maxSpeed = 3.0f; // Maksymalna prędkość łazika
|
||||||
const float acceleration = 0.1f;
|
const float acceleration = 0.1f;
|
||||||
|
float rotationVelocity = 0.0f; // Prędkość obrotu łazika
|
||||||
|
const float rotationAcceleration = 0.1f; // Przyspieszenie obrotu
|
||||||
|
const float rotationFriction = 0.05f; // Współczynnik tarcia obrotu
|
||||||
|
const float maxRotationSpeed = 5.0f; // Maksymalna prędkość obrotu
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -167,17 +172,18 @@ void RotateRoverAndCamera(float angle) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void UpdateRover() {
|
void UpdateRover() {
|
||||||
// Przyspieszanie
|
// Przyspieszanie w przód
|
||||||
if (keyWPressed) {
|
if (keyWPressed) {
|
||||||
velocity += acceleration;
|
velocity += acceleration;
|
||||||
if (velocity > maxSpeed) velocity = maxSpeed;
|
if (velocity > maxSpeed) velocity = maxSpeed;
|
||||||
}
|
}
|
||||||
|
// Przyspieszanie w tył
|
||||||
else if (keySPressed) {
|
else if (keySPressed) {
|
||||||
velocity -= acceleration;
|
velocity -= acceleration;
|
||||||
if (velocity < -maxSpeed) velocity = -maxSpeed;
|
if (velocity < -maxSpeed) velocity = -maxSpeed;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
// Hamowanie (wytracanie prędkości z powodu tarcia)
|
// Hamowanie (wytracanie prędkości z powodu tarcia)
|
||||||
|
else {
|
||||||
if (velocity > 0) {
|
if (velocity > 0) {
|
||||||
velocity -= friction;
|
velocity -= friction;
|
||||||
if (velocity < 0) velocity = 0;
|
if (velocity < 0) velocity = 0;
|
||||||
@@ -190,16 +196,34 @@ void UpdateRover() {
|
|||||||
|
|
||||||
// Obracanie
|
// Obracanie
|
||||||
if (keyAPressed) {
|
if (keyAPressed) {
|
||||||
RotateRoverAndCamera(3.0f);
|
rotationVelocity += rotationAcceleration;
|
||||||
|
if (rotationVelocity > maxRotationSpeed) rotationVelocity = maxRotationSpeed;
|
||||||
}
|
}
|
||||||
else if (keyDPressed) {
|
else if (keyDPressed) {
|
||||||
RotateRoverAndCamera(-3.0f);
|
rotationVelocity -= rotationAcceleration;
|
||||||
|
if (rotationVelocity < -maxRotationSpeed) rotationVelocity = -maxRotationSpeed;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Hamowanie obrotu (wytracanie prędkości z powodu tarcia)
|
||||||
|
if (rotationVelocity > 0) {
|
||||||
|
rotationVelocity -= rotationFriction;
|
||||||
|
if (rotationVelocity < 0) rotationVelocity = 0;
|
||||||
|
}
|
||||||
|
else if (rotationVelocity < 0) {
|
||||||
|
rotationVelocity += rotationFriction;
|
||||||
|
if (rotationVelocity > 0) rotationVelocity = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Aktualizacja pozycji na podstawie prędkości
|
// Aktualizacja pozycji na podstawie prędkości
|
||||||
float radRotation = Rotation * GL_PI / 180.0f;
|
float radRotation = Rotation * GL_PI / 180.0f;
|
||||||
Sides-= velocity * cos(radRotation);
|
Sides -= velocity * cos(radRotation);
|
||||||
Foward-= velocity * sin(radRotation);
|
Foward -= velocity * sin(radRotation);
|
||||||
|
|
||||||
|
// Aktualizacja kąta obrotu
|
||||||
|
Rotation += rotationVelocity;
|
||||||
|
if (Rotation >= 360.0f) Rotation -= 360.0f;
|
||||||
|
if (Rotation < 0.0f) Rotation += 360.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change viewing volume and viewport. Called when window is resized
|
// Change viewing volume and viewport. Called when window is resized
|
||||||
@@ -433,7 +457,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 -= 1; // showcase demo
|
yRot -= 10; // 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;
|
||||||
@@ -483,7 +507,7 @@ void RenderScene(void) {
|
|||||||
user.draw();
|
user.draw();
|
||||||
UpdateRover();
|
UpdateRover();
|
||||||
glPopMatrix();
|
glPopMatrix();
|
||||||
|
fpsCounter.update();
|
||||||
|
|
||||||
// Zamiana buforów (double buffering)
|
// Zamiana buforów (double buffering)
|
||||||
|
|
||||||
@@ -573,9 +597,26 @@ HPALETTE GetOpenGLPalette(HDC hDC) {
|
|||||||
// Return the handle to the new palette
|
// Return the handle to the new palette
|
||||||
return hRetPal;
|
return hRetPal;
|
||||||
}
|
}
|
||||||
|
void CreateConsole()
|
||||||
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
|
{
|
||||||
|
// Tworzenie nowej konsoli
|
||||||
|
if (AllocConsole())
|
||||||
|
{
|
||||||
|
// Przekierowanie standardowych strumieni do konsoli
|
||||||
|
FILE* conin;
|
||||||
|
FILE* conout;
|
||||||
|
FILE* conerr;
|
||||||
|
freopen_s(&conin, "conin$", "r", stdin);
|
||||||
|
freopen_s(&conout, "conout$", "w", stdout);
|
||||||
|
freopen_s(&conerr, "conout$", "w", stderr);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
MessageBox(NULL, "Nie udało się utworzyć konsoli.", "Błąd", MB_OK | MB_ICONERROR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
|
||||||
|
CreateConsole();
|
||||||
MSG msg; // Windows message structure
|
MSG msg; // Windows message structure
|
||||||
WNDCLASS wc; // Windows class structure
|
WNDCLASS wc; // Windows class structure
|
||||||
HWND hWnd; // Storeage for window handle
|
HWND hWnd; // Storeage for window handle
|
||||||
@@ -612,7 +653,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
|
|||||||
|
|
||||||
// Window position and size
|
// Window position and size
|
||||||
50, 50,
|
50, 50,
|
||||||
400, 400,
|
800, 800,
|
||||||
NULL,
|
NULL,
|
||||||
NULL,
|
NULL,
|
||||||
hInstance,
|
hInstance,
|
||||||
@@ -625,6 +666,8 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
|
|||||||
const WORD ID_TIMER = 1;
|
const WORD ID_TIMER = 1;
|
||||||
SetTimer(hWnd, ID_TIMER, 100, NULL);
|
SetTimer(hWnd, ID_TIMER, 100, NULL);
|
||||||
// Display the window
|
// Display the window
|
||||||
|
|
||||||
|
|
||||||
ShowWindow(hWnd, SW_SHOW);
|
ShowWindow(hWnd, SW_SHOW);
|
||||||
UpdateWindow(hWnd);
|
UpdateWindow(hWnd);
|
||||||
|
|
||||||
@@ -635,7 +678,7 @@ int APIENTRY WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
|
|||||||
}
|
}
|
||||||
|
|
||||||
return msg.wParam;
|
return msg.wParam;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Window procedure, handles all messages for this program
|
// Window procedure, handles all messages for this program
|
||||||
|
|||||||
Reference in New Issue
Block a user