From 1f92c5920140d3de9337ee74cbaa95c4532c0abe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaakko=20Kera=CC=88nen?= Date: Tue, 6 Sep 2016 20:35:52 +0300 Subject: [PATCH] libgui: Added GUI-friendly app loop timer Activates the OpenGL context before triggering a loop iteration. --- doomsday/sdk/libcore/include/de/core/loop.h | 2 +- doomsday/sdk/libgui/include/de/GuiLoop | 1 + doomsday/sdk/libgui/include/de/gui/guiapp.h | 4 +- doomsday/sdk/libgui/include/de/gui/guiloop.h | 49 +++++++++++++++++ doomsday/sdk/libgui/src/canvaswindow.cpp | 4 +- doomsday/sdk/libgui/src/guiapp.cpp | 4 +- doomsday/sdk/libgui/src/guiloop.cpp | 58 ++++++++++++++++++++ 7 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 doomsday/sdk/libgui/include/de/GuiLoop create mode 100644 doomsday/sdk/libgui/include/de/gui/guiloop.h create mode 100644 doomsday/sdk/libgui/src/guiloop.cpp diff --git a/doomsday/sdk/libcore/include/de/core/loop.h b/doomsday/sdk/libcore/include/de/core/loop.h index 4a096bcf89..83040d146a 100644 --- a/doomsday/sdk/libcore/include/de/core/loop.h +++ b/doomsday/sdk/libcore/include/de/core/loop.h @@ -85,7 +85,7 @@ class DENG2_PUBLIC Loop : public QObject static Loop &get(); public slots: - void nextLoopIteration(); + virtual void nextLoopIteration(); private: DENG2_PRIVATE(d) diff --git a/doomsday/sdk/libgui/include/de/GuiLoop b/doomsday/sdk/libgui/include/de/GuiLoop new file mode 100644 index 0000000000..ed8de72e51 --- /dev/null +++ b/doomsday/sdk/libgui/include/de/GuiLoop @@ -0,0 +1 @@ +#include "gui/guiloop.h" diff --git a/doomsday/sdk/libgui/include/de/gui/guiapp.h b/doomsday/sdk/libgui/include/de/gui/guiapp.h index d71b6af108..80deac66b8 100644 --- a/doomsday/sdk/libgui/include/de/gui/guiapp.h +++ b/doomsday/sdk/libgui/include/de/gui/guiapp.h @@ -22,7 +22,7 @@ #include "libgui.h" #include #include -#include +#include /** * Macro for conveniently accessing the de::GuiApp singleton instance. @@ -66,7 +66,7 @@ class LIBGUI_PUBLIC GuiApp : public QApplication, public App, int execLoop(); void stopLoop(int code); - Loop &loop(); + GuiLoop &loop(); protected: NativePath appDataPath() const; diff --git a/doomsday/sdk/libgui/include/de/gui/guiloop.h b/doomsday/sdk/libgui/include/de/gui/guiloop.h new file mode 100644 index 0000000000..6c84f9151d --- /dev/null +++ b/doomsday/sdk/libgui/include/de/gui/guiloop.h @@ -0,0 +1,49 @@ +/** @file guiloop.h Continually triggered loop. + * + * @authors Copyright (c) 2016 Jaakko Keränen + * + * @par License + * LGPL: http://www.gnu.org/licenses/lgpl.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at your + * option) any later version. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser + * General Public License for more details. You should have received a copy of + * the GNU Lesser General Public License along with this program; if not, see: + * http://www.gnu.org/licenses + */ + +#ifndef GUILOOP_H +#define GUILOOP_H + +#include + +namespace de { + +class CanvasWindow; + +/** + * Continually triggered loop that activates a window when triggering iterations. + */ +class GuiLoop : public Loop +{ +public: + GuiLoop(); + + void setWindow(CanvasWindow *window); + + static GuiLoop &get(); + +protected slots: + void nextLoopIteration() override; + +private: + DENG2_PRIVATE(d) +}; + +} // namespace de + +#endif // GUILOOP_H diff --git a/doomsday/sdk/libgui/src/canvaswindow.cpp b/doomsday/sdk/libgui/src/canvaswindow.cpp index 4d3ad5f0b1..3e18b9b201 100644 --- a/doomsday/sdk/libgui/src/canvaswindow.cpp +++ b/doomsday/sdk/libgui/src/canvaswindow.cpp @@ -60,7 +60,8 @@ DENG2_PIMPL(CanvasWindow) { if (thisPublic == mainWindow) { - mainWindow = 0; + GuiLoop::get().setWindow(nullptr); + mainWindow = nullptr; } } @@ -180,6 +181,7 @@ CanvasWindow &CanvasWindow::main() void CanvasWindow::setMain(CanvasWindow *window) { mainWindow = window; + GuiLoop::get().setWindow(window); } } // namespace de diff --git a/doomsday/sdk/libgui/src/guiapp.cpp b/doomsday/sdk/libgui/src/guiapp.cpp index 98fcf1d4a0..d1804db1ee 100644 --- a/doomsday/sdk/libgui/src/guiapp.cpp +++ b/doomsday/sdk/libgui/src/guiapp.cpp @@ -35,7 +35,7 @@ namespace de { DENG2_PIMPL(GuiApp) { - Loop loop; + GuiLoop loop; Impl(Public *i) : Base(i) { @@ -120,7 +120,7 @@ void GuiApp::stopLoop(int code) return QApplication::exit(code); } -Loop &GuiApp::loop() +GuiLoop &GuiApp::loop() { return d->loop; } diff --git a/doomsday/sdk/libgui/src/guiloop.cpp b/doomsday/sdk/libgui/src/guiloop.cpp new file mode 100644 index 0000000000..be4d47f476 --- /dev/null +++ b/doomsday/sdk/libgui/src/guiloop.cpp @@ -0,0 +1,58 @@ +/** @file guiloop.cpp + * + * @authors Copyright (c) 2016 Jaakko Keränen + * + * @par License + * LGPL: http://www.gnu.org/licenses/lgpl.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation; either version 3 of the License, or (at your + * option) any later version. This program is distributed in the hope that it + * will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser + * General Public License for more details. You should have received a copy of + * the GNU Lesser General Public License along with this program; if not, see: + * http://www.gnu.org/licenses + */ + +#include "de/GuiLoop" +#include "de/CanvasWindow" + +namespace de { + +DENG2_PIMPL_NOREF(GuiLoop) +{ + CanvasWindow *window = nullptr; +}; + +GuiLoop::GuiLoop() + : d(new Impl) +{} + +void GuiLoop::setWindow(CanvasWindow *window) +{ + d->window = window; +} + +GuiLoop &GuiLoop::get() // static +{ + return static_cast(Loop::get()); +} + +void GuiLoop::nextLoopIteration() +{ + if (d->window) + { + d->window->glActivate(); + } + + Loop::nextLoopIteration(); + + if (d->window) + { + d->window->glDone(); + } +} + +} // namespace de