Skip to content

Commit b573dd9

Browse files
committed
Add a way to change FPS
1 parent 3877ac0 commit b573dd9

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

include/scratchcpp/iengine.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ class LIBSCRATCHCPP_EXPORT IEngine
8585
*/
8686
virtual void run() = 0;
8787

88+
/*! Returns the framerate of the project. */
89+
virtual double fps() const = 0;
90+
91+
/*! Sets the framerate of the project. */
92+
virtual void setFps(double fps) = 0;
93+
8894
/*! Returns true if there are any running script of the broadcast with the given index. */
8995
virtual bool broadcastRunning(unsigned int index, VirtualMachine *sourceScript) = 0;
9096

src/engine/internal/engine.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ void Engine::initClone(Sprite *clone)
317317

318318
void Engine::run()
319319
{
320-
auto frameDuration = std::chrono::milliseconds(33);
320+
updateFrameDuration();
321321
start();
322322

323323
while (true) {
@@ -332,7 +332,7 @@ void Engine::run()
332332
// Sleep until the time for the next frame
333333
auto currentTime = std::chrono::steady_clock::now();
334334
auto elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastFrameTime);
335-
auto sleepTime = frameDuration - elapsedTime;
335+
auto sleepTime = m_frameDuration - elapsedTime;
336336
bool timeOut = sleepTime <= std::chrono::milliseconds::zero();
337337

338338
if (!timeOut && !m_skipFrame)
@@ -346,6 +346,17 @@ void Engine::run()
346346
}
347347
}
348348

349+
double Engine::fps() const
350+
{
351+
return m_fps;
352+
}
353+
354+
void Engine::setFps(double fps)
355+
{
356+
m_fps = fps;
357+
updateFrameDuration();
358+
}
359+
349360
bool Engine::broadcastRunning(unsigned int index, VirtualMachine *sourceScript)
350361
{
351362
const auto &scripts = m_runningBroadcastMap[index];
@@ -757,3 +768,8 @@ BlockSectionContainer *Engine::blockSectionContainer(IBlockSection *section) con
757768

758769
return nullptr;
759770
}
771+
772+
void Engine::updateFrameDuration()
773+
{
774+
m_frameDuration = std::chrono::milliseconds(static_cast<long>(1000 / m_fps));
775+
}

src/engine/internal/engine.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class Engine : public IEngine
3636
void initClone(libscratchcpp::Sprite *clone) override;
3737
void run() override;
3838

39+
double fps() const override;
40+
void setFps(double fps) override;
41+
3942
bool broadcastRunning(unsigned int index, VirtualMachine *sourceScript) override;
4043

4144
void breakFrame() override;
@@ -91,6 +94,8 @@ class Engine : public IEngine
9194
std::shared_ptr<Entity> getEntity(const std::string &id);
9295
std::shared_ptr<IBlockSection> blockSection(const std::string &opcode) const;
9396

97+
void updateFrameDuration();
98+
9499
std::unordered_map<std::shared_ptr<IBlockSection>, std::unique_ptr<BlockSectionContainer>> m_sections;
95100
std::vector<std::shared_ptr<Target>> m_targets;
96101
std::vector<std::shared_ptr<Broadcast>> m_broadcasts;
@@ -107,6 +112,8 @@ class Engine : public IEngine
107112

108113
std::unique_ptr<ITimer> m_defaultTimer;
109114
ITimer *m_timer = nullptr;
115+
double m_fps = 30; // default FPS
116+
std::chrono::milliseconds m_frameDuration; // will be computed in run()
110117

111118
bool m_breakFrame = false;
112119
bool m_skipFrame = false;

test/engine/engine_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ TEST(EngineTest, Clear)
3434
ASSERT_TRUE(engine.registeredSections().empty());
3535
}
3636

37+
TEST(EngineTest, Fps)
38+
{
39+
Engine engine;
40+
ASSERT_EQ(engine.fps(), 30);
41+
42+
engine.setFps(60.25);
43+
ASSERT_EQ(engine.fps(), 60.25);
44+
}
45+
3746
TEST(EngineTest, BreakFrame)
3847
{
3948
Engine engine;

test/mocks/enginemock.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ class EngineMock : public IEngine
2424
MOCK_METHOD(void, initClone, (Sprite *), (override));
2525
MOCK_METHOD(void, run, (), (override));
2626

27+
MOCK_METHOD(double, fps, (), (const, override));
28+
MOCK_METHOD(void, setFps, (double fps), (override));
29+
2730
MOCK_METHOD(bool, broadcastRunning, (unsigned int, VirtualMachine *), (override));
2831

2932
MOCK_METHOD(void, breakFrame, (), (override));

0 commit comments

Comments
 (0)