Skip to content

Commit

Permalink
libgui: Added GUI-friendly app loop timer
Browse files Browse the repository at this point in the history
Activates the OpenGL context before triggering a loop iteration.
  • Loading branch information
skyjake committed Sep 6, 2016
1 parent db20771 commit 1f92c59
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 6 deletions.
2 changes: 1 addition & 1 deletion doomsday/sdk/libcore/include/de/core/loop.h
Expand Up @@ -85,7 +85,7 @@ class DENG2_PUBLIC Loop : public QObject
static Loop &get();

public slots:
void nextLoopIteration();
virtual void nextLoopIteration();

private:
DENG2_PRIVATE(d)
Expand Down
1 change: 1 addition & 0 deletions doomsday/sdk/libgui/include/de/GuiLoop
@@ -0,0 +1 @@
#include "gui/guiloop.h"
4 changes: 2 additions & 2 deletions doomsday/sdk/libgui/include/de/gui/guiapp.h
Expand Up @@ -22,7 +22,7 @@
#include "libgui.h"
#include <QApplication>
#include <de/App>
#include <de/Loop>
#include <de/GuiLoop>

/**
* Macro for conveniently accessing the de::GuiApp singleton instance.
Expand Down Expand Up @@ -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;
Expand Down
49 changes: 49 additions & 0 deletions 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 <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>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</small>
*/

#ifndef GUILOOP_H
#define GUILOOP_H

#include <de/Loop>

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
4 changes: 3 additions & 1 deletion doomsday/sdk/libgui/src/canvaswindow.cpp
Expand Up @@ -60,7 +60,8 @@ DENG2_PIMPL(CanvasWindow)
{
if (thisPublic == mainWindow)
{
mainWindow = 0;
GuiLoop::get().setWindow(nullptr);
mainWindow = nullptr;
}
}

Expand Down Expand Up @@ -180,6 +181,7 @@ CanvasWindow &CanvasWindow::main()
void CanvasWindow::setMain(CanvasWindow *window)
{
mainWindow = window;
GuiLoop::get().setWindow(window);
}

} // namespace de
4 changes: 2 additions & 2 deletions doomsday/sdk/libgui/src/guiapp.cpp
Expand Up @@ -35,7 +35,7 @@ namespace de {

DENG2_PIMPL(GuiApp)
{
Loop loop;
GuiLoop loop;

Impl(Public *i) : Base(i)
{
Expand Down Expand Up @@ -120,7 +120,7 @@ void GuiApp::stopLoop(int code)
return QApplication::exit(code);
}

Loop &GuiApp::loop()
GuiLoop &GuiApp::loop()
{
return d->loop;
}
Expand Down
58 changes: 58 additions & 0 deletions doomsday/sdk/libgui/src/guiloop.cpp
@@ -0,0 +1,58 @@
/** @file guiloop.cpp
*
* @authors Copyright (c) 2016 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* LGPL: http://www.gnu.org/licenses/lgpl.html
*
* <small>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</small>
*/

#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<GuiLoop &>(Loop::get());
}

void GuiLoop::nextLoopIteration()
{
if (d->window)
{
d->window->glActivate();
}

Loop::nextLoopIteration();

if (d->window)
{
d->window->glDone();
}
}

} // namespace de

0 comments on commit 1f92c59

Please sign in to comment.