summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/opengl/hellowindow/hellowindow.cpp56
-rw-r--r--examples/opengl/hellowindow/hellowindow.h25
2 files changed, 55 insertions, 26 deletions
diff --git a/examples/opengl/hellowindow/hellowindow.cpp b/examples/opengl/hellowindow/hellowindow.cpp
index bcb28e9d96..b5ffbd63a2 100644
--- a/examples/opengl/hellowindow/hellowindow.cpp
+++ b/examples/opengl/hellowindow/hellowindow.cpp
@@ -46,6 +46,7 @@
Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *screen)
: m_initialized(false)
, m_format(format)
+ , m_currentWindow(0)
{
m_context = new QOpenGLContext(this);
if (screen)
@@ -57,7 +58,7 @@ Renderer::Renderer(const QSurfaceFormat &format, Renderer *share, QScreen *scree
}
HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer)
- : m_colorIndex(0), m_renderer(renderer), m_timer(0)
+ : m_colorIndex(0), m_renderer(renderer)
{
setSurfaceType(QWindow::OpenGLSurface);
setFlags(Qt::Window | Qt::WindowSystemMenuHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint);
@@ -68,21 +69,12 @@ HelloWindow::HelloWindow(const QSharedPointer<Renderer> &renderer)
create();
- connect(this, SIGNAL(needRender(QSurface *, const QColor &, const QSize &)),
- renderer.data(), SLOT(render(QSurface *, const QColor &, const QSize &)));
-
updateColor();
}
void HelloWindow::exposeEvent(QExposeEvent *)
{
- render();
-
- if (!m_timer) {
- m_timer = new QTimer(this);
- connect(m_timer, SIGNAL(timeout()), this, SLOT(render()));
- m_timer->start(10);
- }
+ m_renderer->setAnimating(this, isExposed());
}
void HelloWindow::mousePressEvent(QMouseEvent *)
@@ -90,14 +82,16 @@ void HelloWindow::mousePressEvent(QMouseEvent *)
updateColor();
}
-void HelloWindow::render()
+QColor HelloWindow::color() const
{
- if (isExposed())
- emit needRender(this, m_color, size());
+ QMutexLocker locker(&m_colorLock);
+ return m_color;
}
void HelloWindow::updateColor()
{
+ QMutexLocker locker(&m_colorLock);
+
QColor colors[] =
{
QColor(100, 255, 0),
@@ -108,11 +102,41 @@ void HelloWindow::updateColor()
m_colorIndex = 1 - m_colorIndex;
}
-void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewSize)
+void Renderer::setAnimating(HelloWindow *window, bool animating)
+{
+ QMutexLocker locker(&m_windowLock);
+ if (m_windows.contains(window) == animating)
+ return;
+
+ if (animating) {
+ m_windows << window;
+ if (m_windows.size() == 1)
+ QTimer::singleShot(0, this, SLOT(render()));
+ } else {
+ m_currentWindow = 0;
+ m_windows.removeOne(window);
+ }
+}
+
+void Renderer::render()
{
+ QMutexLocker locker(&m_windowLock);
+
+ if (m_windows.isEmpty())
+ return;
+
+ HelloWindow *surface = m_windows.at(m_currentWindow);
+ QColor color = surface->color();
+
+ m_currentWindow = (m_currentWindow + 1) % m_windows.size();
+
if (!m_context->makeCurrent(surface))
return;
+ QSize viewSize = surface->size();
+
+ locker.unlock();
+
if (!m_initialized) {
initialize();
m_initialized = true;
@@ -146,6 +170,8 @@ void Renderer::render(QSurface *surface, const QColor &color, const QSize &viewS
m_context->swapBuffers(surface);
m_fAngle += 1.0f;
+
+ QTimer::singleShot(0, this, SLOT(render()));
}
void Renderer::paintQtLogo()
diff --git a/examples/opengl/hellowindow/hellowindow.h b/examples/opengl/hellowindow/hellowindow.h
index 3d6ac80cfe..6d66f0204a 100644
--- a/examples/opengl/hellowindow/hellowindow.h
+++ b/examples/opengl/hellowindow/hellowindow.h
@@ -41,10 +41,13 @@
#include <QWindow>
#include <QColor>
+#include <QMutex>
#include <QOpenGLShaderProgram>
#include <QSharedPointer>
#include <QTimer>
+class HelloWindow;
+
class Renderer : public QObject
{
Q_OBJECT
@@ -54,8 +57,10 @@ public:
QSurfaceFormat format() const { return m_format; }
-public slots:
- void render(QSurface *surface, const QColor &color, const QSize &viewSize);
+ void setAnimating(HelloWindow *window, bool animating);
+
+private slots:
+ void render();
private:
void initialize();
@@ -78,30 +83,28 @@ private:
QSurfaceFormat m_format;
QOpenGLContext *m_context;
QOpenGLShaderProgram *m_program;
+
+ QList<HelloWindow *> m_windows;
+ int m_currentWindow;
+
+ QMutex m_windowLock;
};
class HelloWindow : public QWindow
{
- Q_OBJECT
-
public:
explicit HelloWindow(const QSharedPointer<Renderer> &renderer);
+ QColor color() const;
void updateColor();
void exposeEvent(QExposeEvent *event);
-signals:
- void needRender(QSurface *surface, const QColor &color, const QSize &viewSize);
-
-private slots:
- void render();
-
private:
void mousePressEvent(QMouseEvent *);
int m_colorIndex;
QColor m_color;
const QSharedPointer<Renderer> m_renderer;
- QTimer *m_timer;
+ mutable QMutex m_colorLock;
};