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
3 changes: 3 additions & 0 deletions include/scratchcpp/iengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class LIBSCRATCHCPP_EXPORT IEngine
*/
virtual void runEventLoop() = 0;

/*! Stops the event loop which is running in another thread. */
virtual void stopEventLoop() = 0;

/*! Sets the function which is called on every frame. */
virtual void setRedrawHandler(const std::function<void()> &handler) = 0;

Expand Down
18 changes: 18 additions & 0 deletions src/engine/internal/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,13 @@ void Engine::runEventLoop()
eventLoop();
}

void Engine::stopEventLoop()
{
m_stopEventLoopMutex.lock();
m_stopEventLoop = true;
m_stopEventLoopMutex.unlock();
}

void Engine::setRedrawHandler(const std::function<void()> &handler)
{
m_redrawHandler = handler;
Expand All @@ -331,6 +338,7 @@ void Engine::eventLoop(bool untilProjectStops)
{
updateFrameDuration();
m_newScripts.clear();
m_stopEventLoop = false;

while (true) {
auto frameStart = m_clock->currentSteadyTime();
Expand Down Expand Up @@ -369,6 +377,16 @@ void Engine::eventLoop(bool untilProjectStops)
}
}

// Stop the event loop if stopEventLoop() was called
m_stopEventLoopMutex.lock();

if (m_stopEventLoop) {
stop = true;
break;
}

m_stopEventLoopMutex.unlock();

currentTime = m_clock->currentSteadyTime();
elapsedTime = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - frameStart);
sleepTime = m_frameDuration - elapsedTime;
Expand Down
5 changes: 5 additions & 0 deletions src/engine/internal/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <unordered_map>
#include <memory>
#include <chrono>
#include <mutex>

#include "blocksectioncontainer.h"

Expand Down Expand Up @@ -37,8 +38,10 @@ class Engine : public IEngine
void stopTarget(Target *target, VirtualMachine *exceptScript) override;
void initClone(libscratchcpp::Sprite *clone) override;
void deinitClone(libscratchcpp::Sprite *clone) override;

void run() override;
void runEventLoop() override;
void stopEventLoop() override;

void setRedrawHandler(const std::function<void()> &handler) override;

Expand Down Expand Up @@ -183,6 +186,8 @@ class Engine : public IEngine
bool m_running = false;
bool m_redrawRequested = false;
std::function<void()> m_redrawHandler = nullptr;
bool m_stopEventLoop = false;
std::mutex m_stopEventLoopMutex;
};

} // namespace libscratchcpp
11 changes: 11 additions & 0 deletions test/engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <scratchcpp/list.h>
#include <timermock.h>
#include <clockmock.h>
#include <thread>

#include "../common.h"
#include "testsection.h"
Expand Down Expand Up @@ -68,6 +69,16 @@ TEST(EngineTest, IsRunning)
ASSERT_FALSE(engine.isRunning());
}

TEST(EngineTest, EventLoop)
{
Engine engine;

std::thread th([&engine]() { engine.runEventLoop(); });
std::this_thread::sleep_for(std::chrono::milliseconds(100));
engine.stopEventLoop();
th.join(); // should return immediately
}

TEST(EngineTest, Fps)
{
Engine engine;
Expand Down
2 changes: 2 additions & 0 deletions test/mocks/enginemock.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ class EngineMock : public IEngine
MOCK_METHOD(void, stopTarget, (Target *, VirtualMachine *), (override));
MOCK_METHOD(void, initClone, (Sprite *), (override));
MOCK_METHOD(void, deinitClone, (Sprite *), (override));

MOCK_METHOD(void, run, (), (override));
MOCK_METHOD(void, runEventLoop, (), (override));
MOCK_METHOD(void, stopEventLoop, (), (override));

MOCK_METHOD(void, setRedrawHandler, (const std::function<void()> &), (override));

Expand Down