Skip to content

Commit

Permalink
update FrameRateController
Browse files Browse the repository at this point in the history
  • Loading branch information
tyanmahou committed Jan 4, 2024
1 parent d9993e5 commit 8e37065
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Re-Abyss/Re-Abyss.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@
<ClCompile Include="app\utils\Env\detail\EnvParser.cpp" />
<ClCompile Include="app\utils\Env\Env.cpp" />
<ClCompile Include="app\utils\FileUtil\FileUtil.cpp" />
<ClCompile Include="app\utils\FPS\FrameRateController.cpp" />
<ClCompile Include="app\utils\FPS\FrameRateHz.cpp" />
<ClCompile Include="app\utils\Layout\Window\detail\Component.cpp" />
<ClCompile Include="app\utils\Layout\Window\detail\GrabCtrl.cpp" />
Expand Down Expand Up @@ -2049,6 +2050,7 @@
<ClInclude Include="app\utils\Env\detail\EnvParser.hpp" />
<ClInclude Include="app\utils\Env\Env.hpp" />
<ClInclude Include="app\utils\FileUtil\FileUtil.hpp" />
<ClInclude Include="app\utils\FPS\FrameRateController.hpp" />
<ClInclude Include="app\utils\FPS\FrameRateHz.hpp" />
<ClInclude Include="app\utils\Hierarchy\Hierarchy.hpp" />
<ClInclude Include="app\utils\IdGenerator\FreeableIdGen.hpp" />
Expand Down Expand Up @@ -2104,6 +2106,7 @@
<ClInclude Include="app\utils\Reflection\Reflection.hpp" />
<ClInclude Include="app\utils\Ref\Ref.hpp" />
<ClInclude Include="app\utils\Shake\SimpleShake.hpp" />
<ClInclude Include="app\utils\Singleton\AddonSingleton.hpp" />
<ClInclude Include="app\utils\Singleton\DynamicSingleton.hpp" />
<ClInclude Include="app\utils\Singleton\Singleton.hpp" />
<ClInclude Include="app\utils\SourceLocation\SourceLocation.hpp" />
Expand Down
9 changes: 9 additions & 0 deletions Re-Abyss/Re-Abyss.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -2492,6 +2492,9 @@
</ClCompile>
<ClCompile Include="tests\tests\testing.ixx" />
<ClCompile Include="tests\helper\TestRunner\TestRunner.ixx" />
<ClCompile Include="app\utils\FPS\FrameRateController.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include=".editorconfig" />
Expand Down Expand Up @@ -6041,6 +6044,12 @@
<ClInclude Include="app\utils\Version\Version.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="app\utils\FPS\FrameRateController.hpp">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="app\utils\Singleton\AddonSingleton.hpp">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<Image Include="build\icon.ico">
Expand Down
6 changes: 3 additions & 3 deletions Re-Abyss/app/debugs/Menu/MenuBuilder.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <abyss/debugs/Menu/MenuBuilder.hpp>
#include <abyss/debugs/Menu/MenuBuilder.hpp>
#if ABYSS_DEBUG
#include <abyss/commons/Path.hpp>
#include <abyss/scenes/SequenceManager.hpp>
Expand All @@ -14,7 +14,7 @@
#include <abyss/utils/DebugMenu/Button.hpp>
#include <abyss/utils/DebugMenu/HrItem.hpp>
#include <abyss/utils/Enum/EnumTraits.hpp>
#include <abyss/utils/FPS/FrameRateHz.hpp>
#include <abyss/utils/FPS/FrameRateController.hpp>
#include <Siv3D.hpp>

namespace
Expand Down Expand Up @@ -121,7 +121,7 @@ namespace abyss::Debug
}
void MenuBuilder::ExecFPS(s3d::StringView value)
{
FrameRateHz::Set(ParseOpt<double>(value).map([](double fps) {
FrameRateController::Set(ParseOpt<double>(value).map([](double fps) {
return Fps{ fps };
}));
}
Expand Down
7 changes: 4 additions & 3 deletions Re-Abyss/app/debugs/System/System.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <abyss/debugs/System/System.hpp>
#include <abyss/debugs/System/System.hpp>
#if ABYSS_DEBUG
#include <abyss/commons/Constants.hpp>
#include <abyss/utils/FPS/FrameRateHz.hpp>
#include <abyss/utils/FPS/FrameRateController.hpp>
#include <abyss/debugs/FPSViewer/FPSViewer.hpp>
#include <abyss/debugs/Log/Log.hpp>
#include <abyss/debugs/Log/LogViewer.hpp>
Expand Down Expand Up @@ -55,6 +55,8 @@ namespace abyss::Debug
m_fpsViewer.setPrinter([](s3d::int32 fps){
Watcher::Print(U"[FPS] {}"_fmt(fps));
});

FrameRateController::Setup();
}
bool isPause() const
{
Expand All @@ -72,7 +74,6 @@ namespace abyss::Debug

// FPS制御
m_fpsViewer.update();
FrameRateHz::Sleep();

m_pause.update();

Expand Down
21 changes: 21 additions & 0 deletions Re-Abyss/app/utils/FPS/FrameRateController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,28 @@

namespace abyss
{
void FrameRateController::set(const s3d::Optional<Fps>& value)
{
m_value = value;
auto refreshRate = s3d::System::GetCurrentMonitor().refreshRate.map([](double x) {
return Fps{ x };
});
if (value && refreshRate && *refreshRate >= *value) {
m_sleepTime = s3d::DurationCast<Clock::duration>(value->duration());
s3d::Graphics::SetVSyncEnabled(false);
} else {
m_value = s3d::none;
s3d::Graphics::SetVSyncEnabled(true);
}
}
void FrameRateController::postPresent()
{
if (!m_value) {
// 可変フレームレート
return;
}
m_sleepUntil += m_sleepTime;
m_sleepUntil = Max(m_sleepUntil, Clock::now() - m_sleepTime);
std::this_thread::sleep_until(m_sleepUntil);
}
}
26 changes: 24 additions & 2 deletions Re-Abyss/app/utils/FPS/FrameRateController.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
#pragma once
#include <Siv3D/IAddon.hpp>
#include <abyss/utils/Singleton/AddonSingleton.hpp>
#include <abyss/values/Fps.hpp>

namespace abyss
{
class FrameRateController : public IAddon
class FrameRateController :
public AddonSingleton<FrameRateController>,
public s3d::IAddon
{
friend class AddonSingleton<FrameRateController>;
private:
using Clock = std::chrono::steady_clock;
using TimePoint = std::chrono::time_point<Clock>;
public:
static void Set(const s3d::Optional<Fps>& value)
{
Instance()->set(value);
}
private:
static s3d::StringView UniqueName()
{
return U"FrameRateController";
}
protected:
void set(const s3d::Optional<Fps>& value);
void postPresent() override;
private:
FrameRateController() = default;
private:
s3d::Optional<Fps> m_value;

Clock::duration m_sleepTime;
TimePoint m_sleepUntil;
};
}
31 changes: 31 additions & 0 deletions Re-Abyss/app/utils/Singleton/AddonSingleton.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once
#include <Siv3D/Addon.hpp>
#include <concepts>

namespace abyss
{
template<class Type>
class AddonSingleton
{
public:
static Type* Setup()
{
return Instance();
}
static Type* Instance()
{
static Type* instance = [] {
std::unique_ptr<Type> unique(new Type);
Addon::Register(Type::UniqueName(), std::move(unique));
return Addon::GetAddon<Type>(Type::UniqueName());
}();
return instance;
}
protected:
AddonSingleton() {}
virtual ~AddonSingleton() {}
private:
AddonSingleton(const AddonSingleton& other) = delete;
AddonSingleton& operator=(const AddonSingleton& other) = delete;
};
}

0 comments on commit 8e37065

Please sign in to comment.