compteur de fps

This commit is contained in:
StratiX0 2024-03-29 15:50:08 +01:00
parent 64d39c8869
commit 0568195aec
6 changed files with 223 additions and 0 deletions

View File

@ -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;
}

View File

@ -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

View File

@ -27,6 +27,7 @@
<ClCompile Include="d3dclass.cpp" />
<ClCompile Include="fontclass.cpp" />
<ClCompile Include="fontshaderclass.cpp" />
<ClCompile Include="fpsclass.cpp" />
<ClCompile Include="inputclass.cpp" />
<ClCompile Include="Lightclass.cpp" />
<ClCompile Include="Lightshaderclass.cpp" />
@ -48,6 +49,7 @@
<ClInclude Include="d3dclass.h" />
<ClInclude Include="fontclass.h" />
<ClInclude Include="fontshaderclass.h" />
<ClInclude Include="fpsclass.h" />
<ClInclude Include="inputclass.h" />
<ClInclude Include="lightclass.h" />
<ClInclude Include="lightshaderclass.h" />

View File

@ -84,6 +84,9 @@
<ClCompile Include="Timerclass.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
<ClCompile Include="fpsclass.cpp">
<Filter>Fichiers sources</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="applicationclass.h">
@ -140,6 +143,9 @@
<ClInclude Include="Timerclass.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
<ClInclude Include="fpsclass.h">
<Filter>Fichiers d%27en-tête</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="font01.tga">

46
enginecustom/fpsclass.cpp Normal file
View File

@ -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;
}

36
enginecustom/fpsclass.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef _FPSCLASS_H_
#define _FPSCLASS_H_
/////////////
// LINKING //
/////////////
#pragma comment(lib, "winmm.lib")
//////////////
// INCLUDES //
//////////////
#include <windows.h>
#include <mmsystem.h>
////////////////////////////////////////////////////////////////////////////////
// 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