Skip to content

Commit aec4178

Browse files
committed
Replace redraw handler with a signal
1 parent 83abb6b commit aec4178

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

include/scratchcpp/iengine.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <vector>
77
#include <unordered_map>
88
#include <functional>
9+
#include <sigslot/signal.hpp>
910

1011
#include "global.h"
1112

@@ -109,8 +110,8 @@ class LIBSCRATCHCPP_EXPORT IEngine
109110
/*! Stops the event loop which is running in another thread. */
110111
virtual void stopEventLoop() = 0;
111112

112-
/*! Sets the function which is called on every frame. */
113-
virtual void setRedrawHandler(const std::function<void()> &handler) = 0;
113+
/*! Emits when rendering should occur. */
114+
virtual sigslot::signal<> &aboutToRender() = 0;
114115

115116
/*! Returns true if the project is currently running. */
116117
virtual bool isRunning() const = 0;

src/engine/internal/engine.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,7 @@ void Engine::step()
486486
stepThreads();
487487

488488
// Render
489-
if (m_redrawHandler)
490-
m_redrawHandler();
489+
m_aboutToRedraw();
491490
}
492491

493492
void Engine::run()
@@ -509,9 +508,9 @@ void Engine::stopEventLoop()
509508
m_stopEventLoopMutex.unlock();
510509
}
511510

512-
void Engine::setRedrawHandler(const std::function<void()> &handler)
511+
sigslot::signal<> &Engine::aboutToRender()
513512
{
514-
m_redrawHandler = handler;
513+
return m_aboutToRedraw;
515514
}
516515

517516
std::vector<std::shared_ptr<VirtualMachine>> Engine::stepThreads()

src/engine/internal/engine.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class Engine : public IEngine
4949
void runEventLoop() override;
5050
void stopEventLoop() override;
5151

52-
void setRedrawHandler(const std::function<void()> &handler) override;
52+
sigslot::signal<> &aboutToRender() override;
5353

5454
bool isRunning() const override;
5555

@@ -268,7 +268,7 @@ class Engine : public IEngine
268268

269269
bool m_running = false;
270270
bool m_redrawRequested = false;
271-
std::function<void()> m_redrawHandler = nullptr;
271+
sigslot::signal<> m_aboutToRedraw;
272272
bool m_stopEventLoop = false;
273273
std::mutex m_stopEventLoopMutex;
274274

test/engine/engine_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ TEST(EngineTest, FpsProject)
341341
engine->setFps(10);
342342
RedrawMock redrawMock;
343343
auto handler = std::bind(&RedrawMock::redraw, &redrawMock);
344-
engine->setRedrawHandler(std::function<void()>(handler));
344+
engine->aboutToRender().connect(handler);
345345
std::chrono::steady_clock::time_point time7(std::chrono::milliseconds(100));
346346
std::chrono::steady_clock::time_point time8(std::chrono::milliseconds(200));
347347
std::chrono::steady_clock::time_point time9(std::chrono::milliseconds(300));

test/mocks/enginemock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class EngineMock : public IEngine
3232
MOCK_METHOD(void, runEventLoop, (), (override));
3333
MOCK_METHOD(void, stopEventLoop, (), (override));
3434

35-
MOCK_METHOD(void, setRedrawHandler, (const std::function<void()> &), (override));
35+
MOCK_METHOD(sigslot::signal<> &, aboutToRender, (), (override));
3636

3737
MOCK_METHOD(bool, isRunning, (), (const, override));
3838

0 commit comments

Comments
 (0)