Results 1 to 2 of 2

Thread: [OpenGL + Qt] Implementing real time loop

  1. #1
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default [OpenGL + Qt] Implementing real time loop

    Hello,

    I want to implement a "real-time" loop, which will check and execute pending events, and after that fire up my QGLWidget::updateGL(). In short, it should follow the common pattern for game engine loop, which looks somehow like this:

    Qt Code:
    1. (...) // initialize qApp and my custom QGLWidget
    2.  
    3. while (application not closed) {
    4. if (has pending events)
    5. execute them;
    6.  
    7. w.updateGL();
    8. }
    9. return 0;
    To copy to clipboard, switch view to plain text mode 

    I've read lots of threads about this problem, but all of them either was outdated or not applicable.

    [solution 1]
    I found a solution with using a QTimer with timeout 0, which fires up every time the application is idle. It's very simple and attractive solution: I only need to create QTimer in my custom QGLWidget constructor and run the application with standard QApplication::exec() in main(). The problem is, that in this approach the gui is not "snappy", e.g. moving/resizing the application window is very "choppy", which is not very visual attractive. I've also read, that employing a QTimer (or using QObject::startTimer() / QObject::timerEvent() ) is not very efficient in this scenario.

    [solution 2]
    I tried to solve the problem in this way:

    Qt Code:
    1. (...) // initialize qApp and my custom QGLWidget
    2.  
    3. while (true) {
    4. if (app.hasPendingEvents())
    5. app.processEvents();
    6.  
    7. w.updateGL();
    8. }
    9.  
    10. return 0; // which is not really reachable
    To copy to clipboard, switch view to plain text mode 

    But I can't exit the application in nice way. I mean, when I click the X button, the app window is hid (closed?), but the process is still running (and consuming lots of cpu). I overridden the QGLWidget::closeEvent() so it calls qApp->quit(), but it doesn't seem to work (it seems to do nothing actually). I also set some window attributes in QGLWidget constructor: WA_QuitOnClose and WA_DeleteOnClose, but neither gives any result.

    [solution 3]
    I overcame the problem with a bool isFinished in my derived QGLWidget, which is set to "false" in the constructor, and to "true" in QGLWidget::closeEvent(). Now the main() looks like this:

    Qt Code:
    1. (...) // initialize qApp and my custom QGLWidget
    2.  
    3. while (!w.finished()) {
    4. if (app.hasPendingEvents())
    5. app.processEvents();
    6.  
    7. w.updateGL();
    8. }
    9.  
    10. return 0;
    To copy to clipboard, switch view to plain text mode 

    It does the job perfectly. But it doesn't seem to do the job "in the way it should be done".

    Well, my question is: how to implement a real-time loop in nice and smooth way. The solution 1 is nice but not smooth and probably not efficient. The second one is somehow nice, but doesn't work perfectly. The last one doesn't seem nice, but works perfectly.

    Thanks for any replies.

    OS: Windows XP SP3, Qt 4.7.0

    Regards,
    phan

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [OpenGL + Qt] Implementing real time loop

    The game loop should fire at constant intervals and not "as often as possible". You should assume your loop spins at say... 10Hz and start a timer matching this frequency -- that is 100ms in this case. Then you'll know you'll have more or less 10 spins of the game loop per second. This has nothing to do with updateGL() though. The latter causes a redraw. You shouldn't do any computations in painting code as you never know if something else than the timer wouldn't cause a repaint of your window.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Real time data plotting using Qwt
    By nagabathula in forum Qwt
    Replies: 8
    Last Post: 7th July 2011, 16:47
  2. Real time rendering
    By kaszewczyk in forum Newbie
    Replies: 1
    Last Post: 7th July 2010, 17:26
  3. real time plotting
    By agostain in forum Qwt
    Replies: 0
    Last Post: 10th August 2009, 10:47
  4. real time plotting
    By gyre in forum Qwt
    Replies: 4
    Last Post: 11th December 2007, 16:13
  5. Making 3D controls in QT, implementing a game loop in QT
    By smurrish in forum Qt Programming
    Replies: 10
    Last Post: 26th April 2006, 04:37

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.