Skip to content

Commit 87f8831

Browse files
authored
Merge pull request #101 from scratchcpp/initialization_api
Add library initialization API
2 parents a5ab6f8 + 445b542 commit 87f8831

File tree

10 files changed

+77
-17
lines changed

10 files changed

+77
-17
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ target_compile_definitions(scratchcpp-render PRIVATE SCRATCHCPPRENDER_LIBRARY)
2020

2121
linkQt(scratchcpp-render)
2222

23+
target_sources(scratchcpp-render
24+
PUBLIC
25+
include/scratchcpp-render/scratchcpp-render.h
26+
)
27+
28+
target_include_directories(scratchcpp-render PUBLIC include)
29+
2330
include(build/SetUpLibscratchcpp.cmake)
2431
target_link_libraries(scratchcpp-render PRIVATE scratchcpp)
2532

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,15 @@ Button {
114114
}
115115
```
116116

117-
Please note that the ScratchCPP renderer only works with the basic scene graph render loop.
118-
Qt 6 uses the threaded render loop by default, so you'll have to disable it by calling this
119-
before constructing your application object:
117+
**The library must be initialized by the application.**
118+
**To initialize the library, call `scratchcpprender::init()` before constructing the Q(Gui)Application object.**
120119
```cpp
121-
qputenv("QSG_RENDER_LOOP", "basic");
120+
#include <scratchcpp-render/scratchcpp-render.h>
121+
int main(int argc, char **argv) {
122+
scratchcpprender::init();
123+
QApplication a(argc, argv);
124+
...
125+
}
122126
```
123127
124128
<p align="right">(<a href="#readme-top">back to top</a>)</p>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#pragma once
4+
5+
/*! \brief The main namespace of the library. */
6+
namespace scratchcpprender
7+
{
8+
9+
/*! Initializes the library. Call this from main before constructing your Q(Gui)Application object. */
10+
void init();
11+
12+
} // namespace scratchcpprender

src/CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ qt_add_qml_module(scratchcpp-render
1818
shaders/sprite.frag
1919
icons/enter.svg
2020
SOURCES
21-
global.h
21+
global.h
22+
global_functions.cpp
2223
projectloader.cpp
2324
projectloader.h
24-
projectscene.cpp
25-
projectscene.h
25+
projectscene.cpp
26+
projectscene.h
2627
stagemodel.cpp
2728
stagemodel.h
2829
spritemodel.cpp
@@ -44,8 +45,8 @@ qt_add_qml_module(scratchcpp-render
4445
bitmapskin.h
4546
svgskin.cpp
4647
svgskin.h
47-
renderedtarget.cpp
48-
renderedtarget.h
48+
renderedtarget.cpp
49+
renderedtarget.h
4950
targetpainter.cpp
5051
targetpainter.h
5152
scenemousearea.cpp

src/global_functions.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: LGPL-3.0-or-later
2+
3+
#include <QQuickWindow>
4+
#include <scratchcpp-render/scratchcpp-render.h>
5+
6+
void scratchcpprender::init()
7+
{
8+
qputenv("QSG_RENDER_LOOP", "basic");
9+
QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);
10+
}

src/penlayerpainter.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ PenLayerPainter::PenLayerPainter(QOpenGLFramebufferObject *fbo)
1212

1313
void PenLayerPainter::paint(QNanoPainter *painter)
1414
{
15-
if (QThread::currentThread() != qApp->thread()) {
16-
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 "
17-
"application object.");
18-
}
15+
if (QThread::currentThread() != qApp->thread())
16+
qFatal("Error: Rendering must happen in the GUI thread to work correctly. Did you initialize the library using scratchcpprender::init()?");
1917

2018
QOpenGLContext *context = QOpenGLContext::currentContext();
2119
Q_ASSERT(context);

src/targetpainter.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ TargetPainter::~TargetPainter()
2323

2424
void TargetPainter::paint(QNanoPainter *painter)
2525
{
26-
if (QThread::currentThread() != qApp->thread()) {
27-
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 "
28-
"application object.");
29-
}
26+
if (QThread::currentThread() != qApp->thread())
27+
qFatal("Error: Rendering must happen in the GUI thread to work correctly. Did you initialize the library using scratchcpprender::init()?");
3028

3129
QOpenGLContext *context = QOpenGLContext::currentContext();
3230
Q_ASSERT(context);

test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ endfunction()
2020

2121
add_subdirectory(mocks)
2222

23+
add_subdirectory(global_functions)
2324
add_subdirectory(projectloader)
2425
add_subdirectory(renderedtarget)
2526
add_subdirectory(targetpainter)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
add_executable(
2+
global_functions_test
3+
global_functions_test.cpp
4+
)
5+
6+
target_link_libraries(
7+
global_functions_test
8+
GTest::gtest_main
9+
GTest::gmock_main
10+
scratchcpp-render
11+
${QT_LIBS}
12+
)
13+
14+
add_test(global_functions_test)
15+
gtest_discover_tests(global_functions_test)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <scratchcpp-render/scratchcpp-render.h>
2+
#include <QQuickWindow>
3+
4+
#include "../common.h"
5+
6+
TEST(GlobalFunctionsTest, Init)
7+
{
8+
qputenv("QSG_RENDER_LOOP", "threaded");
9+
QQuickWindow::setGraphicsApi(QSGRendererInterface::Software);
10+
11+
scratchcpprender::init();
12+
ASSERT_EQ(qgetenv("QSG_RENDER_LOOP"), "basic");
13+
ASSERT_EQ(QQuickWindow::graphicsApi(), QSGRendererInterface::OpenGL);
14+
}

0 commit comments

Comments
 (0)