Skip to content

Commit 5c5eb05

Browse files
committed
Add timer to Engine
1 parent dc18ede commit 5c5eb05

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

include/scratchcpp/iengine.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Sprite;
1919
class Variable;
2020
class List;
2121
class Script;
22+
class ITimer;
2223

2324
/*!
2425
* \brief The IEngine interface provides an API for running Scratch projects.
@@ -106,6 +107,9 @@ class LIBSCRATCHCPP_EXPORT IEngine
106107
/*! Call this from a block implementation to ignore calls to skipFrame() until the current frame ends. */
107108
virtual void lockFrame() = 0;
108109

110+
/*! Returns the timer of the project. */
111+
virtual ITimer *timer() const = 0;
112+
109113
/*!
110114
* Registers the given block section.
111115
* \see <a href="blockSections.html">Block sections</a>

src/engine/internal/engine.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
#include "engine.h"
2020
#include "blocksectioncontainer.h"
21+
#include "timer.h"
2122
#include "../../blocks/standardblocks.h"
2223

2324
using namespace libscratchcpp;
2425

25-
Engine::Engine()
26+
Engine::Engine() :
27+
m_defaultTimer(std::make_unique<Timer>()),
28+
m_timer(m_defaultTimer.get())
2629
{
2730
srand(time(NULL));
2831
}
@@ -165,6 +168,8 @@ void Engine::frame()
165168

166169
void Engine::start()
167170
{
171+
m_timer->reset();
172+
168173
for (auto target : m_targets) {
169174
auto gfBlocks = target->greenFlagBlocks();
170175
for (auto block : gfBlocks)
@@ -377,6 +382,16 @@ void Engine::lockFrame()
377382
m_lockFrame = true;
378383
}
379384

385+
ITimer *Engine::timer() const
386+
{
387+
return m_timer;
388+
}
389+
390+
void Engine::setTimer(ITimer *timer)
391+
{
392+
m_timer = timer;
393+
}
394+
380395
void Engine::registerSection(std::shared_ptr<IBlockSection> section)
381396
{
382397
if (section) {

src/engine/internal/engine.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <scratchcpp/iengine.h>
66
#include <scratchcpp/target.h>
7+
#include <scratchcpp/itimer.h>
78
#include <unordered_map>
89
#include <memory>
910
#include <chrono>
@@ -43,6 +44,9 @@ class Engine : public IEngine
4344
void skipFrame() override;
4445
void lockFrame() override;
4546

47+
ITimer *timer() const override;
48+
void setTimer(ITimer *timer);
49+
4650
void registerSection(std::shared_ptr<IBlockSection> section) override;
4751
std::vector<std::shared_ptr<IBlockSection>> registeredSections() const;
4852
unsigned int functionIndex(BlockFunc f) override;
@@ -101,6 +105,9 @@ class Engine : public IEngine
101105
std::unordered_map<Variable *, Target *> m_variableOwners;
102106
std::unordered_map<List *, Target *> m_listOwners;
103107

108+
std::unique_ptr<ITimer> m_defaultTimer;
109+
ITimer *m_timer = nullptr;
110+
104111
bool m_breakFrame = false;
105112
bool m_skipFrame = false;
106113
bool m_lockFrame = false;

test/engine/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ add_executable(
88
target_link_libraries(
99
engine_test
1010
GTest::gtest_main
11+
GTest::gmock_main
1112
scratchcpp
13+
scratchcpp_mocks
1214
)
1315

1416
gtest_discover_tests(engine_test)

test/engine/engine_test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <scratchcpp/project.h>
44
#include <scratchcpp/variable.h>
55
#include <scratchcpp/list.h>
6+
#include <timermock.h>
67

78
#include "../common.h"
89
#include "testsection.h"
@@ -42,6 +43,26 @@ TEST(EngineTest, BreakFrame)
4243
ASSERT_TRUE(engine.breakingCurrentFrame());
4344
}
4445

46+
TEST(EngineTest, Timer)
47+
{
48+
Engine engine;
49+
ASSERT_TRUE(engine.timer());
50+
engine.timer()->reset(); // shouldn't crash
51+
52+
TimerMock timer;
53+
engine.setTimer(&timer);
54+
ASSERT_EQ(engine.timer(), &timer);
55+
56+
EXPECT_CALL(timer, reset()).Times(1);
57+
engine.start();
58+
59+
EXPECT_CALL(timer, reset()).Times(0);
60+
engine.stop();
61+
62+
EXPECT_CALL(timer, reset()).Times(1);
63+
engine.run();
64+
}
65+
4566
TEST(EngineTest, Sections)
4667
{
4768
Engine engine;

test/mocks/enginemock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class EngineMock : public IEngine
3232
MOCK_METHOD(void, skipFrame, (), (override));
3333
MOCK_METHOD(void, lockFrame, (), (override));
3434

35+
MOCK_METHOD(ITimer *, timer, (), (const, override));
36+
3537
MOCK_METHOD(void, registerSection, (std::shared_ptr<IBlockSection>), (override));
3638
MOCK_METHOD(unsigned int, functionIndex, (BlockFunc), (override));
3739

0 commit comments

Comments
 (0)