Skip to content

Commit cb9511b

Browse files
committed
Use Clock in Engine
1 parent 2b893ac commit cb9511b

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/engine/internal/engine.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@
1717
#include <scratchcpp/keyevent.h>
1818
#include <cassert>
1919
#include <iostream>
20-
#include <thread>
2120

2221
#include "engine.h"
2322
#include "blocksectioncontainer.h"
2423
#include "timer.h"
24+
#include "clock.h"
2525
#include "../../blocks/standardblocks.h"
2626

2727
using namespace libscratchcpp;
2828

2929
Engine::Engine() :
3030
m_defaultTimer(std::make_unique<Timer>()),
31-
m_timer(m_defaultTimer.get())
31+
m_timer(m_defaultTimer.get()),
32+
m_clock(Clock::instance().get())
3233
{
3334
}
3435

@@ -354,7 +355,7 @@ void Engine::run()
354355
start();
355356

356357
while (true) {
357-
auto lastFrameTime = std::chrono::steady_clock::now();
358+
auto lastFrameTime = m_clock->currentSteadyTime();
358359
m_skipFrame = false;
359360

360361
// Execute the frame
@@ -363,13 +364,13 @@ void Engine::run()
363364
break;
364365

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

371372
if (!timeOut && !m_skipFrame)
372-
std::this_thread::sleep_for(sleepTime);
373+
m_clock->sleep(sleepTime);
373374

374375
if ((m_skipFrame && timeOut) || !m_skipFrame) {
375376
// TODO: Repaint here

src/engine/internal/engine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ namespace libscratchcpp
1515
{
1616

1717
class Entity;
18+
class IClock;
1819

1920
class Engine : public IEngine
2021
{
@@ -116,6 +117,8 @@ class Engine : public IEngine
116117
BlockSectionContainer *blockSectionContainer(const std::string &opcode) const;
117118
BlockSectionContainer *blockSectionContainer(IBlockSection *section) const;
118119

120+
IClock *m_clock = nullptr;
121+
119122
private:
120123
void finalize();
121124
void deleteClones();

test/engine/engine_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010
#include "../common.h"
1111
#include "testsection.h"
1212
#include "engine/internal/engine.h"
13+
#include "engine/internal/clock.h"
1314

1415
using namespace libscratchcpp;
1516

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

19+
TEST(EngineTest, Clock)
20+
{
21+
Engine engine;
22+
ASSERT_EQ(engine.m_clock, Clock::instance().get());
23+
}
24+
1825
TEST(EngineTest, Clear)
1926
{
2027
Engine engine;

0 commit comments

Comments
 (0)