How to: Smooth rendering mainloop ?!? [solved]



  • How is it possible to crate a main loop for a game that renders smoothly, and doesn't create an overheat? In Glut i'd use glutIdleFunc() but what to do in Qt?


  • Lifetime Qt Champion

    Hi,

    What version of Qt are you using ?



  • qt5.1



  • Something wrong with QTimer?



  • This example here witch i run on X11/Linux tends to create tiny framebloopers. Could it be that Qpainter is creating that bloopers?
    @
    Test::Test()
    :QWidget()
    ,timer(new QTimer())
    {
    connect(timer, SIGNAL(timeout()),
    this, SLOT(mainLoop()));
    resize(600, 600);
    timer->start(1000/60);
    }

    void Test::mainLoop()
    {
    repaint();
    }

    void Test::paintEvent(QPaintEvent *e)
    {
    static int x = 0;
    static int y = 0;
    QPainter painter(this);
    painter.setViewport(0, 0, width(), height());
    painter.setWindow(0, 0, width(), height());

        painter.setPen(QPen(Qt::black)); 
        painter.drawRect(++x, ++y, 50, 50); 
    

    }
    @


  • Lifetime Qt Champion

    There's something strange here, you were talking about OpenGL and now you're using the QPainter api. So what do you want to achieve ?


  • Moderators

    [quote author="SGaist" date="1380570030"]There's something strange here, you were talking about OpenGL and now you're using the QPainter api. So what do you want to achieve ?[/quote]I think the_scrabi said "If I were to use the OpenGL Utility Toolkit, I'd use glutIdleFunc(). How do I accomplish the same effect in Qt (not GLUT)?"

    [quote author="the_scrabi" date="1380559312"]
    timer->start(1000/60);
    [/quote]QTimer::start() takes an integer so 1000/60 == 17 (not 16.66667). Your repaints will be slower than your screen refresh rate.

    I don't do graphics programming so I don't know the best solution to your problem, but have you looked at Qt's OpenGL functions? See the "QOpenGL*" classes in http://qt-project.org/doc/qt-5.1/qtgui/qtgui-module.html This example might also be useful: http://qt-project.org/doc/qt-5.1/qtgui/openglwindow.html


  • Lifetime Qt Champion

    [quote author="JKSH" date="1380588497"][quote author="SGaist" date="1380570030"]There's something strange here, you were talking about OpenGL and now you're using the QPainter api. So what do you want to achieve ?[/quote]I think the_scrabi said "If I were to use the OpenGL Utility Toolkit, I'd use glutIdleFunc(). How do I accomplish the same effect in Qt (not GLUT)?"[/quote]
    Indeed that's what I understood, so I thought that he wanted to know what could replace glutIdleFunc when using OpenGL with Qt



  • I need to apologize. I'm learning OpenGL at the moment, so I didn't use it in my Code above. I thought Qpainter would do it … it didn't, hence i rewrote everything using OpenGL and Qtimer and yes there are no framebloopers anymore. But that kind of confuses me... isn't Qpainter based on OpenGL in Qt5 ?!?

    Here is the code: http://pastebin.com/AYQ3GkEV

    Also this example here (http://qt-project.org/doc/qt-5.1/qtgui/openglwindow.html) confused me a bit. It doesn't use Qtimer to generate an animation.


  • Lifetime Qt Champion

    QPainter can make use of OpenGL but not necessarily, and you can also use OpenGL code directly (it all depend on what you want to do)

    Anyway, the example shows another way to trigger the update of the window using QEvent::UpdateRequest. Event which will be handled in the event() function. Following the code path, the event handling will do a call to renderNow(), and at the end of it, renderLater() is called, which will queue a new UpdateRequest and all starts again


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.