Skip to content

Commit

Permalink
Implement fixed frame rate.
Browse files Browse the repository at this point in the history
  • Loading branch information
ASxa86 committed Jun 11, 2024
1 parent 46c6409 commit 21c9a1e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/aspire/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ auto main() -> int
auto rect = std::make_unique<aspire::scene::Rectangle>();
rect->setPosition({80, 80});
rect->setSize({64, 64});
rect->onFrame(
rect->onFrameFixed(
[r = rect.get()]
{
auto pos = r->getPosition();
Expand All @@ -37,7 +37,7 @@ auto main() -> int
subrect->setColor(sf::Color::Red);
subrect->setRotation(30);

subrect->onFrame(
subrect->onFrameFixed(
[s = subrect.get()]
{
auto rot = s->getRotation();
Expand Down
1 change: 1 addition & 0 deletions include/aspire/core/Kernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace aspire::core
class ASPIRE_CORE_EXPORT Kernel : public Object
{
public:
static constexpr std::chrono::milliseconds FrameFixedRate{100};
static auto Instance() -> Kernel*;

Kernel();
Expand Down
14 changes: 12 additions & 2 deletions src/core/Kernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct Kernel::Impl
std::mutex mutexEvent;
std::chrono::steady_clock::time_point start;
std::chrono::steady_clock::duration elapsed{};
std::chrono::steady_clock::duration accumulate{};
static inline Kernel* Instance{nullptr};
bool running{false};
};
Expand Down Expand Up @@ -78,7 +79,9 @@ auto Kernel::run() -> int
{
const auto frameStart = std::chrono::steady_clock::now();

this->pimpl->elapsed += std::chrono::steady_clock::now() - this->pimpl->start;
const auto elapsed = std::chrono::steady_clock::now() - this->pimpl->start;
this->pimpl->elapsed += elapsed;
this->pimpl->accumulate += elapsed;

{
std::scoped_lock lock(this->pimpl->mutexEvent);
Expand All @@ -89,7 +92,14 @@ auto Kernel::run() -> int
}
}

this->pimpl->frameFixed();
auto count = 0;

while(this->pimpl->accumulate > Kernel::FrameFixedRate && count < 5)
{
this->pimpl->accumulate -= Kernel::FrameFixedRate;
this->pimpl->frameFixed();
count++;
}

this->pimpl->frame();

Expand Down

0 comments on commit 21c9a1e

Please sign in to comment.