From 0568195aec1fd5c33d514d95e8d6779374821637 Mon Sep 17 00:00:00 2001 From: StratiX0 Date: Fri, 29 Mar 2024 15:50:08 +0100 Subject: [PATCH] compteur de fps --- enginecustom/applicationclass.cpp | 128 ++++++++++++++++++++++ enginecustom/applicationclass.h | 5 + enginecustom/enginecustom.vcxproj | 2 + enginecustom/enginecustom.vcxproj.filters | 6 + enginecustom/fpsclass.cpp | 46 ++++++++ enginecustom/fpsclass.h | 36 ++++++ 6 files changed, 223 insertions(+) create mode 100644 enginecustom/fpsclass.cpp create mode 100644 enginecustom/fpsclass.h diff --git a/enginecustom/applicationclass.cpp b/enginecustom/applicationclass.cpp index 8da9425..b608c7d 100644 --- a/enginecustom/applicationclass.cpp +++ b/enginecustom/applicationclass.cpp @@ -18,6 +18,8 @@ ApplicationClass::ApplicationClass() m_TextString1 = 0; m_TextString2 = 0; m_TextString3 = 0; + m_Fps = 0; + m_FpsString = 0; } @@ -38,6 +40,7 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) char textureFilename1[128], textureFilename2[128]; char bitmapFilename[128]; char spriteFilename[128]; + char fpsString[32]; bool result; @@ -218,12 +221,47 @@ bool ApplicationClass::Initialize(int screenWidth, int screenHeight, HWND hwnd) m_Lights[3].SetDiffuseColor(1.0f, 1.0f, 1.0f, 1.0f); // White m_Lights[3].SetPosition(3.0f, 1.0f, -3.0f); + // Create and initialize the fps object. + m_Fps = new FpsClass(); + + m_Fps->Initialize(); + + // Set the initial fps and fps string. + m_previousFps = -1; + strcpy_s(fpsString, "Fps: 0"); + + // Create and initialize the text object for the fps string. + m_FpsString = new TextClass; + + result = m_FpsString->Initialize(m_Direct3D->GetDevice(), m_Direct3D->GetDeviceContext(), screenWidth, screenHeight, 32, m_Font, fpsString, 10, 10, 0.0f, 1.0f, 0.0f); + if (!result) + { + return false; + } + + return true; } void ApplicationClass::Shutdown() { + // Release the text object for the fps string. + if (m_FpsString) + { + m_FpsString->Shutdown(); + delete m_FpsString; + m_FpsString = 0; + } + + // Release the fps object. + if (m_Fps) + { + delete m_Fps; + m_Fps = 0; + } + + // Release the text string objects. if (m_TextString3) { @@ -359,6 +397,13 @@ bool ApplicationClass::Frame() static float z = 0.f; bool result; + // Update the frames per second each frame. + result = UpdateFps(); + if (!result) + { + return false; + } + // Update the rotation variable each frame. rotation -= 0.0174532925f * 0.1f; if (rotation < 0.0f) @@ -418,6 +463,16 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z) m_Direct3D->TurnZBufferOff(); m_Direct3D->EnableAlphaBlending(); + // Render the fps text string using the font shader. + m_FpsString->Render(m_Direct3D->GetDeviceContext()); + + result = m_FontShader->Render(m_Direct3D->GetDeviceContext(), m_FpsString->GetIndexCount(), worldMatrix, viewMatrix, orthoMatrix, + m_Font->GetTexture(), m_FpsString->GetPixelColor()); + if (!result) + { + return false; + } + // Render the first text string using the font shader. m_TextString1->Render(m_Direct3D->GetDeviceContext()); @@ -523,6 +578,10 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z) // Render the model using the multitexture shader. result = m_MultiTextureShader->Render(m_Direct3D->GetDeviceContext(), m_Model->GetIndexCount(), worldMatrix, viewMatrix, projectionMatrix, m_Model->GetTexture(0), m_Model->GetTexture(1)); + if (!result) + { + return false; + } // Enable the Z buffer and disable alpha blending now that 2D rendering is complete. m_Direct3D->TurnZBufferOn(); @@ -534,3 +593,72 @@ bool ApplicationClass::Render(float rotation, float x, float y, float z) return true; } +bool ApplicationClass::UpdateFps() +{ + int fps; + char tempString[16], finalString[16]; + float red, green, blue; + bool result; + + + // Update the fps each frame. + m_Fps->Frame(); + + // Get the current fps. + fps = m_Fps->GetFps(); + + // Check if the fps from the previous frame was the same, if so don't need to update the text string. + if (m_previousFps == fps) + { + return true; + } + + // Store the fps for checking next frame. + m_previousFps = fps; + + // Truncate the fps to below 100,000. + if (fps > 99999) + { + fps = 99999; + } + + // Convert the fps integer to string format. + sprintf_s(tempString, "%d", fps); + + // Setup the fps string. + strcpy_s(finalString, "Fps: "); + strcat_s(finalString, tempString); + + // If fps is 60 or above set the fps color to green. + if (fps >= 60) + { + red = 0.0f; + green = 1.0f; + blue = 0.0f; + } + + // If fps is below 60 set the fps color to yellow. + if (fps < 60) + { + red = 1.0f; + green = 1.0f; + blue = 0.0f; + } + + // If fps is below 30 set the fps color to red. + if (fps < 30) + { + red = 1.0f; + green = 0.0f; + blue = 0.0f; + } + + // Update the sentence vertex buffer with the new string information. + result = m_FpsString->UpdateText(m_Direct3D->GetDeviceContext(), m_Font, finalString, 10, 10, red, green, blue); + if (!result) + { + return false; + } + + return true; +} \ No newline at end of file diff --git a/enginecustom/applicationclass.h b/enginecustom/applicationclass.h index fe7a529..2ee3d4c 100644 --- a/enginecustom/applicationclass.h +++ b/enginecustom/applicationclass.h @@ -18,6 +18,7 @@ #include "fontshaderclass.h" #include "fontclass.h" #include "textclass.h" +#include "fpsclass.h" ///////////// // GLOBALS // @@ -44,6 +45,7 @@ public: private: bool Render(float, float, float, float); + bool UpdateFps(); private: D3DClass* m_Direct3D; CameraClass* m_Camera; @@ -60,6 +62,9 @@ private: FontShaderClass* m_FontShader; FontClass* m_Font; TextClass *m_TextString1, *m_TextString2, *m_TextString3; + FpsClass* m_Fps; + TextClass* m_FpsString; + int m_previousFps; }; #endif diff --git a/enginecustom/enginecustom.vcxproj b/enginecustom/enginecustom.vcxproj index 0628418..a072ed4 100644 --- a/enginecustom/enginecustom.vcxproj +++ b/enginecustom/enginecustom.vcxproj @@ -27,6 +27,7 @@ + @@ -48,6 +49,7 @@ + diff --git a/enginecustom/enginecustom.vcxproj.filters b/enginecustom/enginecustom.vcxproj.filters index 893de11..f6d3cc8 100644 --- a/enginecustom/enginecustom.vcxproj.filters +++ b/enginecustom/enginecustom.vcxproj.filters @@ -84,6 +84,9 @@ Fichiers sources + + Fichiers sources + @@ -140,6 +143,9 @@ Fichiers d%27en-tĂȘte + + Fichiers d%27en-tĂȘte + diff --git a/enginecustom/fpsclass.cpp b/enginecustom/fpsclass.cpp new file mode 100644 index 0000000..ac24c3e --- /dev/null +++ b/enginecustom/fpsclass.cpp @@ -0,0 +1,46 @@ +#include "fpsclass.h" + + +FpsClass::FpsClass() +{ +} + + +FpsClass::FpsClass(const FpsClass& other) +{ +} + + +FpsClass::~FpsClass() +{ +} + +void FpsClass::Initialize() +{ + m_fps = 0; + m_count = 0; + + m_startTime = timeGetTime(); + + return; +} + +void FpsClass::Frame() +{ + m_count++; + + if (timeGetTime() >= (m_startTime + 1000)) + { + m_fps = m_count; + m_count = 0; + + m_startTime = timeGetTime(); + } + + return; +} + +int FpsClass::GetFps() +{ + return m_fps; +} \ No newline at end of file diff --git a/enginecustom/fpsclass.h b/enginecustom/fpsclass.h new file mode 100644 index 0000000..af3451b --- /dev/null +++ b/enginecustom/fpsclass.h @@ -0,0 +1,36 @@ +#ifndef _FPSCLASS_H_ +#define _FPSCLASS_H_ + + +///////////// +// LINKING // +///////////// +#pragma comment(lib, "winmm.lib") + + +////////////// +// INCLUDES // +////////////// +#include +#include + +//////////////////////////////////////////////////////////////////////////////// +// Class name: FpsClass +//////////////////////////////////////////////////////////////////////////////// +class FpsClass +{ +public: + FpsClass(); + FpsClass(const FpsClass&); + ~FpsClass(); + + void Initialize(); + void Frame(); + int GetFps(); + +private: + int m_fps, m_count; + unsigned long m_startTime; +}; + +#endif \ No newline at end of file