Skip to content

Commit

Permalink
Added Timer and FPSCounter
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSerpskiStalker committed Sep 4, 2024
1 parent 2ed80b3 commit 6904ce7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/RenderLibrary/FPSCounter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "StdAfx.h"

#include <chrono>
#include <iostream>
#include "FPSCounter.hpp"

using namespace RenderLibary;

FPSCounter::FPSCounter() : _frame_count(0), _fps(0)
{
_last_time = std::chrono::high_resolution_clock::now();
}

void FPSCounter::update()
{
_frame_count++;
auto now = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = now - _last_time;

if (elapsed.count() >= 1.0)
{
_fps = _frame_count / elapsed.count();
_frame_count = 0;
_last_time = now;
std::cout << "FPS: " << _fps << std::endl;
}
}
19 changes: 19 additions & 0 deletions src/RenderLibrary/FPSCounter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

namespace RenderLibary
{
class FPSCounter final
{
public:
FPSCounter();
~FPSCounter() = default;

void update();

private:
double _fps;
int _frame_count;

std::chrono::high_resolution_clock::time_point _last_time;
};
}
4 changes: 4 additions & 0 deletions src/RenderLibrary/RenderLibrary.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,25 @@
<ClCompile Include="Exports.cpp" />
<ClCompile Include="FabricCustomWindow.cpp" />
<ClCompile Include="FabricNativeWindow.cpp" />
<ClCompile Include="FPSCounter.cpp" />
<ClCompile Include="Memory.cpp" />
<ClCompile Include="StdAfx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="Timer.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CreateDirectXDevice.hpp" />
<ClInclude Include="Exports.hpp" />
<ClInclude Include="FabricCustomWindow.hpp" />
<ClInclude Include="FabricNativeWindow.hpp" />
<ClInclude Include="FPSCounter.hpp" />
<ClInclude Include="Memory.hpp" />
<ClInclude Include="StdAfx.h" />
<ClInclude Include="Timer.hpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
4 changes: 4 additions & 0 deletions src/RenderLibrary/RenderLibrary.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
<ClCompile Include="FabricCustomWindow.cpp" />
<ClCompile Include="Memory.cpp" />
<ClCompile Include="CreateDirectXDevice.cpp" />
<ClCompile Include="Timer.cpp" />
<ClCompile Include="FPSCounter.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="FabricNativeWindow.hpp" />
Expand All @@ -23,5 +25,7 @@
<ClInclude Include="FabricCustomWindow.hpp" />
<ClInclude Include="Memory.hpp" />
<ClInclude Include="CreateDirectXDevice.hpp" />
<ClInclude Include="Timer.hpp" />
<ClInclude Include="FPSCounter.hpp" />
</ItemGroup>
</Project>
24 changes: 24 additions & 0 deletions src/RenderLibrary/Timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "StdAfx.h"

#include <chrono>
#include "Timer.hpp"

using namespace RenderLibary;

Timer::Timer() : _start_time(std::chrono::high_resolution_clock::now())
{

}

void Timer::reset()
{
_start_time = std::chrono::high_resolution_clock::now();
}

double Timer::elapsed() const
{
auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> diff = end_time - _start_time;

return diff.count();
}
18 changes: 18 additions & 0 deletions src/RenderLibrary/Timer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

namespace RenderLibary
{
class Timer final
{
public:
Timer();
~Timer() = default;

void reset();
double elapsed() const;

private:
std::chrono::high_resolution_clock::time_point _start_time;
};
}

0 comments on commit 6904ce7

Please sign in to comment.