Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/engine/internal/clock.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

#include <thread>

#include "clock.h"

using namespace libscratchcpp;
Expand All @@ -24,3 +26,8 @@ std::chrono::system_clock::time_point Clock::currentSystemTime() const
{
return std::chrono::system_clock::now();
}

void Clock::sleep(const std::chrono::milliseconds &time) const
{
std::this_thread::sleep_for(time);
}
2 changes: 2 additions & 0 deletions src/engine/internal/clock.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Clock : public IClock
std::chrono::steady_clock::time_point currentSteadyTime() const override;
std::chrono::system_clock::time_point currentSystemTime() const override;

void sleep(const std::chrono::milliseconds &time) const override;

private:
static std::shared_ptr<Clock> m_instance;
};
Expand Down
11 changes: 6 additions & 5 deletions src/engine/internal/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@
#include <scratchcpp/keyevent.h>
#include <cassert>
#include <iostream>
#include <thread>

#include "engine.h"
#include "blocksectioncontainer.h"
#include "timer.h"
#include "clock.h"
#include "../../blocks/standardblocks.h"

using namespace libscratchcpp;

Engine::Engine() :
m_defaultTimer(std::make_unique<Timer>()),
m_timer(m_defaultTimer.get())
m_timer(m_defaultTimer.get()),
m_clock(Clock::instance().get())
{
}

Expand Down Expand Up @@ -354,7 +355,7 @@ void Engine::run()
start();

while (true) {
auto lastFrameTime = std::chrono::steady_clock::now();
auto lastFrameTime = m_clock->currentSteadyTime();
m_skipFrame = false;

// Execute the frame
Expand All @@ -363,13 +364,13 @@ void Engine::run()
break;

// Sleep until the time for the next frame
auto currentTime = std::chrono::steady_clock::now();
auto currentTime = m_clock->currentSteadyTime();
auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastFrameTime);
auto sleepTime = m_frameDuration - elapsedTime;
bool timeOut = sleepTime <= std::chrono::milliseconds::zero();

if (!timeOut && !m_skipFrame)
std::this_thread::sleep_for(sleepTime);
m_clock->sleep(sleepTime);

if ((m_skipFrame && timeOut) || !m_skipFrame) {
// TODO: Repaint here
Expand Down
3 changes: 3 additions & 0 deletions src/engine/internal/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace libscratchcpp
{

class Entity;
class IClock;

class Engine : public IEngine
{
Expand Down Expand Up @@ -116,6 +117,8 @@ class Engine : public IEngine
BlockSectionContainer *blockSectionContainer(const std::string &opcode) const;
BlockSectionContainer *blockSectionContainer(IBlockSection *section) const;

IClock *m_clock = nullptr;

private:
void finalize();
void deleteClones();
Expand Down
2 changes: 2 additions & 0 deletions src/engine/internal/iclock.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class IClock

virtual std::chrono::steady_clock::time_point currentSteadyTime() const = 0;
virtual std::chrono::system_clock::time_point currentSystemTime() const = 0;

virtual void sleep(const std::chrono::milliseconds &time) const = 0;
};

} // namespace libscratchcpp
Binary file added test/2_frames.sb3
Binary file not shown.
37 changes: 37 additions & 0 deletions test/engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@
#include <scratchcpp/variable.h>
#include <scratchcpp/list.h>
#include <timermock.h>
#include <clockmock.h>

#include "../common.h"
#include "testsection.h"
#include "engine/internal/engine.h"
#include "engine/internal/clock.h"

using namespace libscratchcpp;

using ::testing::Return;

// NOTE: resolveIds() and compile() are tested in load_project_test

TEST(EngineTest, Clock)
{
Engine engine;
ASSERT_EQ(engine.m_clock, Clock::instance().get());
}

TEST(EngineTest, Clear)
{
Engine engine;
Expand Down Expand Up @@ -61,6 +71,33 @@ TEST(EngineTest, Fps)
ASSERT_EQ(engine.fps(), 60.25);
}

TEST(EngineTest, FpsProject)
{
Project p("2_frames.sb3");
ASSERT_TRUE(p.load());

ClockMock clock;
Engine *engine = dynamic_cast<Engine *>(p.engine().get());
engine->m_clock = &clock;

std::chrono::steady_clock::time_point time1(std::chrono::milliseconds(50));
std::chrono::steady_clock::time_point time2(std::chrono::milliseconds(75));
std::chrono::steady_clock::time_point time3(std::chrono::milliseconds(83));
std::chrono::steady_clock::time_point time4(std::chrono::milliseconds(116));
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time1)).WillOnce(Return(time2)).WillOnce(Return(time3)).WillOnce(Return(time4)).WillOnce(Return(time4));
EXPECT_CALL(clock, sleep(std::chrono::milliseconds(8)));
p.run();

engine->setFps(10);
std::chrono::steady_clock::time_point time5(std::chrono::milliseconds(100));
std::chrono::steady_clock::time_point time6(std::chrono::milliseconds(115));
std::chrono::steady_clock::time_point time7(std::chrono::milliseconds(200));
std::chrono::steady_clock::time_point time8(std::chrono::milliseconds(300));
EXPECT_CALL(clock, currentSteadyTime()).WillOnce(Return(time5)).WillOnce(Return(time6)).WillOnce(Return(time7)).WillOnce(Return(time8)).WillOnce(Return(time8));
EXPECT_CALL(clock, sleep(std::chrono::milliseconds(85)));
p.run();
}

TEST(EngineTest, KeyState)
{
Engine engine;
Expand Down
2 changes: 2 additions & 0 deletions test/mocks/clockmock.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ class ClockMock : public IClock
public:
MOCK_METHOD(std::chrono::steady_clock::time_point, currentSteadyTime, (), (const, override));
MOCK_METHOD(std::chrono::system_clock::time_point, currentSystemTime, (), (const, override));

MOCK_METHOD(void, sleep, (const std::chrono::milliseconds &time), (const, override));
};