summaryrefslogtreecommitdiffstats
path: root/examples/opengl/hellowindow
diff options
context:
space:
mode:
Diffstat (limited to 'examples/opengl/hellowindow')
-rw-r--r--examples/opengl/hellowindow/CMakeLists.txt36
-rw-r--r--examples/opengl/hellowindow/hellowindow.cpp352
-rw-r--r--examples/opengl/hellowindow/hellowindow.h119
-rw-r--r--examples/opengl/hellowindow/hellowindow.pro9
-rw-r--r--examples/opengl/hellowindow/main.cpp172
5 files changed, 0 insertions, 688 deletions
diff --git a/examples/opengl/hellowindow/CMakeLists.txt b/examples/opengl/hellowindow/CMakeLists.txt
deleted file mode 100644
index cb86df3595..0000000000
--- a/examples/opengl/hellowindow/CMakeLists.txt
+++ /dev/null
@@ -1,36 +0,0 @@
-cmake_minimum_required(VERSION 3.16)
-project(hellowindow LANGUAGES CXX)
-
-set(CMAKE_AUTOMOC ON)
-
-if(NOT DEFINED INSTALL_EXAMPLESDIR)
- set(INSTALL_EXAMPLESDIR "examples")
-endif()
-
-set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/opengl/hellowindow")
-
-find_package(Qt6 REQUIRED COMPONENTS Core Gui OpenGL)
-
-qt_add_executable(hellowindow
- hellowindow.cpp hellowindow.h
- main.cpp
-)
-
-set_target_properties(hellowindow PROPERTIES
- WIN32_EXECUTABLE TRUE
- MACOSX_BUNDLE TRUE
-)
-
-target_link_libraries(hellowindow PUBLIC
- Qt::Core
- Qt::CorePrivate
- Qt::Gui
- Qt::GuiPrivate
- Qt::OpenGL
-)
-
-install(TARGETS hellowindow
- RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
- BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
- LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
-)
diff --git a/examples/opengl/hellowindow/hellowindow.cpp b/examples/opengl/hellowindow/hellowindow.cpp
deleted file mode 100644
index bde763dd70..0000000000
--- a/examples/opengl/hellowindow/hellowindow.cpp
+++ /dev/null
@@ -1,352 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the examples of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include "hellowindow.h"
-
-#include <QOpenGLContext>
-#include <QOpenGLFunctions>
-#include <QRandomGenerator>
-#include <qmath.h>
-#include <QElapsedTimer>
-
-Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *screen)
- : m_initialized(false)
- , m_format(format)
-{
- m_context = new QOpenGLContext(this);
- if (screen)
- m_context->setScreen(screen);
- m_context->setFormat(format);
- if (share)
- m_context->setShareContext(share->m_context);
- m_context->create();
-
- m_backgroundColor = QColor::fromRgbF(0.1f, 0.1f, 0.2f, 1.0f);
- m_backgroundColor.setRed(QRandomGenerator::global()->bounded(64));
- m_backgroundColor.setGreen(QRandomGenerator::global()->bounded(128));
- m_backgroundColor.setBlue(QRandomGenerator::global()->bounded(256));
-}
-
-HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer, QScreen *screen)
- : m_colorIndex(0), m_renderer(renderer)
-{
- setSurfaceType(QWindow::OpenGLSurface);
- setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
-
- setGeometry(QRect(10, 10, 640, 480));
-
- setFormat(renderer->format());
- if (screen)
- setScreen(screen);
-
- create();
-
- updateColor();
-
- connect(renderer.data(), &Renderer::requestUpdate, this, &QWindow::requestUpdate);
-}
-
-void HelloWindow::exposeEvent(QExposeEvent *)
-{
- if (isExposed())
- render();
-}
-
-bool HelloWindow::event(QEvent *ev)
-{
- if (ev->type() == QEvent::UpdateRequest && isExposed())
- render();
- return QWindow::event(ev);
-}
-
-void HelloWindow::render()
-{
- static QElapsedTimer timer;
- if (!timer.isValid())
- timer.start();
- qreal a = (qreal)(((timer.elapsed() * 3) % 36000) / 100.0);
- auto call = [this, r = m_renderer.data(), a, c = color()]() { r->render(this, a, c); };
- QMetaObject::invokeMethod(m_renderer.data(), call);
-}
-
-void HelloWindow::mousePressEvent(QMouseEvent *)
-{
- updateColor();
-}
-
-QColor HelloWindow::color() const
-{
- return m_color;
-}
-
-void HelloWindow::updateColor()
-{
- QColor colors[] =
- {
- QColor(100, 255, 0),
- QColor(0, 100, 255)
- };
-
- m_color = colors[m_colorIndex];
- m_colorIndex = 1 - m_colorIndex;
-}
-
-void Renderer::render(HelloWindow *surface, qreal angle, const QColor &color)
-{
- if (!m_context->makeCurrent(surface))
- return;
-
- QSize viewSize = surface->size();
-
- if (!m_initialized) {
- initialize();
- m_initialized = true;
- }
-
- QOpenGLFunctions *f = m_context->functions();
- f->glViewport(0, 0, viewSize.width() * surface->devicePixelRatio(), viewSize.height() * surface->devicePixelRatio());
- f->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- f->glClearColor(m_backgroundColor.redF(), m_backgroundColor.greenF(), m_backgroundColor.blueF(), m_backgroundColor.alphaF());
- f->glFrontFace(GL_CW);
- f->glCullFace(GL_FRONT);
- f->glEnable(GL_CULL_FACE);
- f->glEnable(GL_DEPTH_TEST);
-
- m_program->bind();
- m_vbo.bind();
-
- m_program->enableAttributeArray(vertexAttr);
- m_program->enableAttributeArray(normalAttr);
- m_program->setAttributeBuffer(vertexAttr, GL_FLOAT, 0, 3);
- const int verticesSize = vertices.count() * 3 * sizeof(GLfloat);
- m_program->setAttributeBuffer(normalAttr, GL_FLOAT, verticesSize, 3);
-
- QMatrix4x4 modelview;
- modelview.rotate(angle, 0.0f, 1.0f, 0.0f);
- modelview.rotate(angle, 1.0f, 0.0f, 0.0f);
- modelview.rotate(angle, 0.0f, 0.0f, 1.0f);
- modelview.translate(0.0f, -0.2f, 0.0f);
-
- m_program->setUniformValue(matrixUniform, modelview);
- m_program->setUniformValue(colorUniform, color);
-
- m_context->functions()->glDrawArrays(GL_TRIANGLES, 0, vertices.size());
-
- m_context->swapBuffers(surface);
-
- emit requestUpdate();
-}
-
-Q_GLOBAL_STATIC(QMutex, initMutex)
-
-void Renderer::initialize()
-{
- // Threaded shader compilation can confuse some drivers. Avoid it.
- QMutexLocker lock(initMutex());
-
- QOpenGLShader *vshader = new QOpenGLShader(QOpenGLShader::Vertex, this);
- vshader->compileSourceCode(
- "attribute highp vec4 vertex;"
- "attribute mediump vec3 normal;"
- "uniform mediump mat4 matrix;"
- "uniform lowp vec4 sourceColor;"
- "varying mediump vec4 color;"
- "void main(void)"
- "{"
- " vec3 toLight = normalize(vec3(0.0, 0.3, 1.0));"
- " float angle = max(dot(normal, toLight), 0.0);"
- " vec3 col = sourceColor.rgb;"
- " color = vec4(col * 0.2 + col * 0.8 * angle, 1.0);"
- " color = clamp(color, 0.0, 1.0);"
- " gl_Position = matrix * vertex;"
- "}");
-
- QOpenGLShader *fshader = new QOpenGLShader(QOpenGLShader::Fragment, this);
- fshader->compileSourceCode(
- "varying mediump vec4 color;"
- "void main(void)"
- "{"
- " gl_FragColor = color;"
- "}");
-
- m_program = new QOpenGLShaderProgram(this);
- m_program->addShader(vshader);
- m_program->addShader(fshader);
- m_program->link();
- m_program->bind();
-
- vertexAttr = m_program->attributeLocation("vertex");
- normalAttr = m_program->attributeLocation("normal");
- matrixUniform = m_program->uniformLocation("matrix");
- colorUniform = m_program->uniformLocation("sourceColor");
-
- createGeometry();
-
- m_vbo.create();
- m_vbo.bind();
- const int verticesSize = vertices.count() * 3 * sizeof(GLfloat);
- m_vbo.allocate(verticesSize * 2);
- m_vbo.write(0, vertices.constData(), verticesSize);
- m_vbo.write(verticesSize, normals.constData(), verticesSize);
-}
-
-void Renderer::createGeometry()
-{
- vertices.clear();
- normals.clear();
-
- qreal x1 = +0.06f;
- qreal y1 = -0.14f;
- qreal x2 = +0.14f;
- qreal y2 = -0.06f;
- qreal x3 = +0.08f;
- qreal y3 = +0.00f;
- qreal x4 = +0.30f;
- qreal y4 = +0.22f;
-
- quad(x1, y1, x2, y2, y2, x2, y1, x1);
- quad(x3, y3, x4, y4, y4, x4, y3, x3);
-
- extrude(x1, y1, x2, y2);
- extrude(x2, y2, y2, x2);
- extrude(y2, x2, y1, x1);
- extrude(y1, x1, x1, y1);
- extrude(x3, y3, x4, y4);
- extrude(x4, y4, y4, x4);
- extrude(y4, x4, y3, x3);
-
- const int NumSectors = 100;
- const qreal sectorAngle = 2 * qreal(M_PI) / NumSectors;
- for (int i = 0; i < NumSectors; ++i) {
- qreal angle = i * sectorAngle;
- qreal x5 = 0.30 * qSin(angle);
- qreal y5 = 0.30 * qCos(angle);
- qreal x6 = 0.20 * qSin(angle);
- qreal y6 = 0.20 * qCos(angle);
-
- angle += sectorAngle;
- qreal x7 = 0.20 * qSin(angle);
- qreal y7 = 0.20 * qCos(angle);
- qreal x8 = 0.30 * qSin(angle);
- qreal y8 = 0.30 * qCos(angle);
-
- quad(x5, y5, x6, y6, x7, y7, x8, y8);
-
- extrude(x6, y6, x7, y7);
- extrude(x8, y8, x5, y5);
- }
-
- for (int i = 0;i < vertices.size();i++)
- vertices[i] *= 2.0f;
-}
-
-void Renderer::quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4)
-{
- vertices << QVector3D(x1, y1, -0.05f);
- vertices << QVector3D(x2, y2, -0.05f);
- vertices << QVector3D(x4, y4, -0.05f);
-
- vertices << QVector3D(x3, y3, -0.05f);
- vertices << QVector3D(x4, y4, -0.05f);
- vertices << QVector3D(x2, y2, -0.05f);
-
- QVector3D n = QVector3D::normal
- (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(x4 - x1, y4 - y1, 0.0f));
-
- normals << n;
- normals << n;
- normals << n;
-
- normals << n;
- normals << n;
- normals << n;
-
- vertices << QVector3D(x4, y4, 0.05f);
- vertices << QVector3D(x2, y2, 0.05f);
- vertices << QVector3D(x1, y1, 0.05f);
-
- vertices << QVector3D(x2, y2, 0.05f);
- vertices << QVector3D(x4, y4, 0.05f);
- vertices << QVector3D(x3, y3, 0.05f);
-
- n = QVector3D::normal
- (QVector3D(x2 - x4, y2 - y4, 0.0f), QVector3D(x1 - x4, y1 - y4, 0.0f));
-
- normals << n;
- normals << n;
- normals << n;
-
- normals << n;
- normals << n;
- normals << n;
-}
-
-void Renderer::extrude(qreal x1, qreal y1, qreal x2, qreal y2)
-{
- vertices << QVector3D(x1, y1, +0.05f);
- vertices << QVector3D(x2, y2, +0.05f);
- vertices << QVector3D(x1, y1, -0.05f);
-
- vertices << QVector3D(x2, y2, -0.05f);
- vertices << QVector3D(x1, y1, -0.05f);
- vertices << QVector3D(x2, y2, +0.05f);
-
- QVector3D n = QVector3D::normal
- (QVector3D(x2 - x1, y2 - y1, 0.0f), QVector3D(0.0f, 0.0f, -0.1f));
-
- normals << n;
- normals << n;
- normals << n;
-
- normals << n;
- normals << n;
- normals << n;
-}
diff --git a/examples/opengl/hellowindow/hellowindow.h b/examples/opengl/hellowindow/hellowindow.h
deleted file mode 100644
index d4dcbcf3db..0000000000
--- a/examples/opengl/hellowindow/hellowindow.h
+++ /dev/null
@@ -1,119 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the examples of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include <QWindow>
-
-#include <QColor>
-#include <QMutex>
-#include <QOpenGLShaderProgram>
-#include <QOpenGLBuffer>
-#include <QSharedPointer>
-#include <QTimer>
-
-class HelloWindow;
-
-class Renderer : public QObject
-{
- Q_OBJECT
-
-public:
- explicit Renderer(const QSurfaceFormat &format, Renderer *share = nullptr,
- QScreen *screen = nullptr);
-
- QSurfaceFormat format() const { return m_format; }
-
-public slots:
- void render(HelloWindow *surface, qreal angle, const QColor &color);
-
-signals:
- void requestUpdate();
-
-private:
- void initialize();
-
- void createGeometry();
- void createBubbles(int number);
- void quad(qreal x1, qreal y1, qreal x2, qreal y2, qreal x3, qreal y3, qreal x4, qreal y4);
- void extrude(qreal x1, qreal y1, qreal x2, qreal y2);
-
- QList<QVector3D> vertices;
- QList<QVector3D> normals;
- int vertexAttr;
- int normalAttr;
- int matrixUniform;
- int colorUniform;
-
- bool m_initialized;
- QSurfaceFormat m_format;
- QOpenGLContext *m_context;
- QOpenGLShaderProgram *m_program;
- QOpenGLBuffer m_vbo;
- QColor m_backgroundColor;
-};
-
-class HelloWindow : public QWindow
-{
-public:
- explicit HelloWindow(const QSharedPointer<Renderer> &renderer, QScreen *screen = nullptr);
-
- QColor color() const;
- void updateColor();
-
-protected:
- bool event(QEvent *ev) override;
- void exposeEvent(QExposeEvent *event) override;
- void mousePressEvent(QMouseEvent *) override;
- void render();
-
-private:
- int m_colorIndex;
- QColor m_color;
- const QSharedPointer<Renderer> m_renderer;
-};
diff --git a/examples/opengl/hellowindow/hellowindow.pro b/examples/opengl/hellowindow/hellowindow.pro
deleted file mode 100644
index 27c274a378..0000000000
--- a/examples/opengl/hellowindow/hellowindow.pro
+++ /dev/null
@@ -1,9 +0,0 @@
-QT += gui-private core-private opengl
-
-HEADERS += hellowindow.h
-SOURCES += hellowindow.cpp main.cpp
-
-# install
-target.path = $$[QT_INSTALL_EXAMPLES]/opengl/hellowindow
-INSTALLS += target
-
diff --git a/examples/opengl/hellowindow/main.cpp b/examples/opengl/hellowindow/main.cpp
deleted file mode 100644
index 7f0be39f5d..0000000000
--- a/examples/opengl/hellowindow/main.cpp
+++ /dev/null
@@ -1,172 +0,0 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the examples of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:BSD$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** BSD License Usage
-** Alternatively, you may use this file under the terms of the BSD license
-** as follows:
-**
-** "Redistribution and use in source and binary forms, with or without
-** modification, are permitted provided that the following conditions are
-** met:
-** * Redistributions of source code must retain the above copyright
-** notice, this list of conditions and the following disclaimer.
-** * Redistributions in binary form must reproduce the above copyright
-** notice, this list of conditions and the following disclaimer in
-** the documentation and/or other materials provided with the
-** distribution.
-** * Neither the name of The Qt Company Ltd nor the names of its
-** contributors may be used to endorse or promote products derived
-** from this software without specific prior written permission.
-**
-**
-** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
-** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
-** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
-** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
-** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
-** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
-** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
-** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
-** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
-** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
-** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
-
-#include "hellowindow.h"
-
-#include <qpa/qplatformintegration.h>
-
-#include <QCommandLineParser>
-#include <QCommandLineOption>
-#include <QGuiApplication>
-#include <QScreen>
-#include <QThread>
-
-int main(int argc, char *argv[])
-{
- QGuiApplication app(argc, argv);
-
- QCoreApplication::setApplicationName("Qt HelloWindow GL Example");
- QCoreApplication::setOrganizationName("QtProject");
- QCoreApplication::setApplicationVersion(QT_VERSION_STR);
- QCommandLineParser parser;
- parser.setApplicationDescription(QCoreApplication::applicationName());
- parser.addHelpOption();
- parser.addVersionOption();
- QCommandLineOption multipleOption("multiple", "Create multiple windows");
- parser.addOption(multipleOption);
- QCommandLineOption multipleSampleOption("multisample", "Multisampling");
- parser.addOption(multipleSampleOption);
- QCommandLineOption multipleScreenOption("multiscreen", "Run on multiple screens");
- parser.addOption(multipleScreenOption);
- QCommandLineOption timeoutOption("timeout", "Close after 10s");
- parser.addOption(timeoutOption);
- parser.process(app);
-
- // Some platforms can only have one window per screen. Therefore we need to differentiate.
- const bool multipleWindows = parser.isSet(multipleOption);
- const bool multipleScreens = parser.isSet(multipleScreenOption);
-
- QScreen *screen = QGuiApplication::primaryScreen();
-
- QRect screenGeometry = screen->availableGeometry();
-
- QSurfaceFormat format;
- format.setDepthBufferSize(16);
- if (parser.isSet(multipleSampleOption))
- format.setSamples(4);
-
- QPoint center = QPoint(screenGeometry.center().x(), screenGeometry.top() + 80);
- QSize windowSize(400, 320);
- int delta = 40;
-
- QList<QWindow *> windows;
- QSharedPointer<Renderer> rendererA(new Renderer(format));
-
- HelloWindow *windowA = new HelloWindow(rendererA);
- windowA->setGeometry(QRect(center, windowSize).translated(-windowSize.width() - delta / 2, 0));
- windowA->setTitle(QStringLiteral("Thread A - Context A"));
- windowA->setVisible(true);
- windows.prepend(windowA);
-
- QList<QThread *> renderThreads;
- if (multipleWindows) {
- QSharedPointer<Renderer> rendererB(new Renderer(format, rendererA.data()));
-
- QThread *renderThread = new QThread;
- rendererB->moveToThread(renderThread);
- renderThreads << renderThread;
-
- HelloWindow *windowB = new HelloWindow(rendererA);
- windowB->setGeometry(QRect(center, windowSize).translated(delta / 2, 0));
- windowB->setTitle(QStringLiteral("Thread A - Context A"));
- windowB->setVisible(true);
- windows.prepend(windowB);
-
- HelloWindow *windowC = new HelloWindow(rendererB);
- windowC->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, windowSize.height() + delta));
- windowC->setTitle(QStringLiteral("Thread B - Context B"));
- windowC->setVisible(true);
- windows.prepend(windowC);
- }
- if (multipleScreens) {
- for (int i = 1; i < QGuiApplication::screens().size(); ++i) {
- QScreen *screen = QGuiApplication::screens().at(i);
- QSharedPointer<Renderer> renderer(new Renderer(format, rendererA.data(), screen));
-
- QThread *renderThread = new QThread;
- renderer->moveToThread(renderThread);
- renderThreads.prepend(renderThread);
-
- QRect screenGeometry = screen->availableGeometry();
- QPoint center = screenGeometry.center();
-
- QSize windowSize = screenGeometry.size() * 0.8;
-
- HelloWindow *window = new HelloWindow(renderer, screen);
- window->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, -windowSize.height() / 2));
-
- QChar id = QChar('B' + i);
- window->setTitle(QStringLiteral("Thread ") + id + QStringLiteral(" - Context ") + id);
- window->setVisible(true);
- windows.prepend(window);
- }
- }
-
- for (int i = 0; i < renderThreads.size(); ++i) {
- QObject::connect(qGuiApp, &QGuiApplication::lastWindowClosed, renderThreads.at(i), &QThread::quit);
- renderThreads.at(i)->start();
- }
-
- // Quit after 10 seconds. For platforms that do not have windows that are closeable.
- if (parser.isSet(timeoutOption))
- QTimer::singleShot(10000, qGuiApp, &QCoreApplication::quit);
-
- const int exitValue = app.exec();
-
- for (int i = 0; i < renderThreads.size(); ++i) {
- renderThreads.at(i)->quit(); // some platforms may not have windows to close so ensure quit()
- renderThreads.at(i)->wait();
- }
-
- qDeleteAll(windows);
- qDeleteAll(renderThreads);
-
- return exitValue;
-}