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
4 changes: 4 additions & 0 deletions include/scratchcpp/iengine.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memory>
#include <vector>
#include <unordered_map>
#include <functional>

#include "global.h"

Expand Down Expand Up @@ -90,6 +91,9 @@ class LIBSCRATCHCPP_EXPORT IEngine
*/
virtual void runEventLoop() = 0;

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

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

Expand Down
8 changes: 7 additions & 1 deletion src/engine/internal/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ void Engine::runEventLoop()
eventLoop();
}

void Engine::setRedrawHandler(const std::function<void()> &handler)
{
m_redrawHandler = handler;
}

void Engine::eventLoop(bool untilProjectStops)
{
updateFrameDuration();
Expand Down Expand Up @@ -374,7 +379,8 @@ void Engine::eventLoop(bool untilProjectStops)
break;

// Redraw
// TODO: Redraw here
if (m_redrawHandler)
m_redrawHandler();

// If the timeout hasn't been reached yet (redraw was requested), sleep
if (!timeout)
Expand Down
3 changes: 3 additions & 0 deletions src/engine/internal/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class Engine : public IEngine
void run() override;
void runEventLoop() override;

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

bool isRunning() const override;

double fps() const override;
Expand Down Expand Up @@ -180,6 +182,7 @@ class Engine : public IEngine

bool m_running = false;
bool m_redrawRequested = false;
std::function<void()> m_redrawHandler = nullptr;
};

} // namespace libscratchcpp
10 changes: 10 additions & 0 deletions test/engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ using ::testing::Return;

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

class RedrawMock
{
public:
MOCK_METHOD(void, redraw, ());
};

TEST(EngineTest, Clock)
{
Engine engine;
Expand Down Expand Up @@ -98,6 +104,9 @@ TEST(EngineTest, FpsProject)
p.run();

engine->setFps(10);
RedrawMock redrawMock;
auto handler = std::bind(&RedrawMock::redraw, &redrawMock);
engine->setRedrawHandler(std::function<void()>(handler));
std::chrono::steady_clock::time_point time5(std::chrono::milliseconds(100));
std::chrono::steady_clock::time_point time6(std::chrono::milliseconds(115));
std::chrono::steady_clock::time_point time7(std::chrono::milliseconds(200));
Expand All @@ -113,6 +122,7 @@ TEST(EngineTest, FpsProject)
.WillOnce(Return(time8));
EXPECT_CALL(clock, sleep(std::chrono::milliseconds(100)));
EXPECT_CALL(clock, sleep(std::chrono::milliseconds(15)));
EXPECT_CALL(redrawMock, redraw()).Times(4);
p.run();

engine->setTurboModeEnabled(true);
Expand Down
2 changes: 2 additions & 0 deletions test/mocks/enginemock.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class EngineMock : public IEngine
MOCK_METHOD(void, run, (), (override));
MOCK_METHOD(void, runEventLoop, (), (override));

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

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

MOCK_METHOD(double, fps, (), (const, override));
Expand Down