Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ target_compile_definitions(scratchcpp-render PRIVATE SCRATCHCPPRENDER_LIBRARY)

linkQt(scratchcpp-render)

target_sources(scratchcpp-render
PUBLIC
include/scratchcpp-render/scratchcpp-render.h
)

target_include_directories(scratchcpp-render PUBLIC include)

include(build/SetUpLibscratchcpp.cmake)
target_link_libraries(scratchcpp-render PRIVATE scratchcpp)

Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,15 @@ Button {
}
```

Please note that the ScratchCPP renderer only works with the basic scene graph render loop.
Qt 6 uses the threaded render loop by default, so you'll have to disable it by calling this
before constructing your application object:
**The library must be initialized by the application.**
**To initialize the library, call `scratchcpprender::init()` before constructing the Q(Gui)Application object.**
```cpp
qputenv("QSG_RENDER_LOOP", "basic");
#include <scratchcpp-render/scratchcpp-render.h>
int main(int argc, char **argv) {
scratchcpprender::init();
QApplication a(argc, argv);
...
}
```

<p align="right">(<a href="#readme-top">back to top</a>)</p>
Expand Down
12 changes: 12 additions & 0 deletions include/scratchcpp-render/scratchcpp-render.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

#pragma once

/*! \brief The main namespace of the library. */
namespace scratchcpprender
{

/*! Initializes the library. Call this from main before constructing your Q(Gui)Application object. */
void init();

} // namespace scratchcpprender
11 changes: 6 additions & 5 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ qt_add_qml_module(scratchcpp-render
shaders/sprite.frag
icons/enter.svg
SOURCES
global.h
global.h
global_functions.cpp
projectloader.cpp
projectloader.h
projectscene.cpp
projectscene.h
projectscene.cpp
projectscene.h
stagemodel.cpp
stagemodel.h
spritemodel.cpp
Expand All @@ -44,8 +45,8 @@ qt_add_qml_module(scratchcpp-render
bitmapskin.h
svgskin.cpp
svgskin.h
renderedtarget.cpp
renderedtarget.h
renderedtarget.cpp
renderedtarget.h
targetpainter.cpp
targetpainter.h
scenemousearea.cpp
Expand Down
10 changes: 10 additions & 0 deletions src/global_functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <QQuickWindow>
#include <scratchcpp-render/scratchcpp-render.h>

void scratchcpprender::init()
{
qputenv("QSG_RENDER_LOOP", "basic");
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
}
6 changes: 2 additions & 4 deletions src/penlayerpainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ PenLayerPainter::PenLayerPainter(QOpenGLFramebufferObject *fbo)

void PenLayerPainter::paint(QNanoPainter *painter)
{
if (QThread::currentThread() != qApp->thread()) {
qFatal("Error: Rendering must happen in the GUI thread to work correctly. Please disable threaded render loop using qputenv(\"QSG_RENDER_LOOP\", \"basic\") before constructing your "
"application object.");
}
if (QThread::currentThread() != qApp->thread())
qFatal("Error: Rendering must happen in the GUI thread to work correctly. Did you initialize the library using scratchcpprender::init()?");

QOpenGLContext *context = QOpenGLContext::currentContext();
Q_ASSERT(context);
Expand Down
6 changes: 2 additions & 4 deletions src/targetpainter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,8 @@ TargetPainter::~TargetPainter()

void TargetPainter::paint(QNanoPainter *painter)
{
if (QThread::currentThread() != qApp->thread()) {
qFatal("Error: Rendering must happen in the GUI thread to work correctly. Please disable threaded render loop using qputenv(\"QSG_RENDER_LOOP\", \"basic\") before constructing your "
"application object.");
}
if (QThread::currentThread() != qApp->thread())
qFatal("Error: Rendering must happen in the GUI thread to work correctly. Did you initialize the library using scratchcpprender::init()?");

QOpenGLContext *context = QOpenGLContext::currentContext();
Q_ASSERT(context);
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ endfunction()

add_subdirectory(mocks)

add_subdirectory(global_functions)
add_subdirectory(projectloader)
add_subdirectory(renderedtarget)
add_subdirectory(targetpainter)
Expand Down
15 changes: 15 additions & 0 deletions test/global_functions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
add_executable(
global_functions_test
global_functions_test.cpp
)

target_link_libraries(
global_functions_test
GTest::gtest_main
GTest::gmock_main
scratchcpp-render
${QT_LIBS}
)

add_test(global_functions_test)
gtest_discover_tests(global_functions_test)
14 changes: 14 additions & 0 deletions test/global_functions/global_functions_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <scratchcpp-render/scratchcpp-render.h>
#include <QQuickWindow>

#include "../common.h"

TEST(GlobalFunctionsTest, Init)
{
qputenv("QSG_RENDER_LOOP", "threaded");
QQuickWindow::setGraphicsApi(QSGRendererInterface::Software);

scratchcpprender::init();
ASSERT_EQ(qgetenv("QSG_RENDER_LOOP"), "basic");
ASSERT_EQ(QQuickWindow::graphicsApi(), QSGRendererInterface::OpenGL);
}