Jump to content
  • Advertisement
Sign in to follow this  
Shabla0

[Qt][OpenGL] Game loop in Qt ?

This topic is 2533 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've started a project to make a small RPG game with Qt and OpenGL.
My problem is that I don't get how to incorporate the usual game loop with Qt, which would look something like that :
while( game->running() )
{
game->updatestuff();
game->dosomethingelse();
[...]
game->drawGL();
}


Here's a simplified version of what I have right now :
int main(int argc, char *argv[])
{
QApplication app(argc, argv);

Engine engine;
engine.run();

return app.exec();
}

Engine::Engine()
{
window = new QMainWindow();
window->setFixedSize(800, 600);

glWidget = new QGLWidget(window);
glWidget->setGeometry(0, 0, 800, 600);
resizeGL(glWidget->width(), glWidget->height());
initGL();

window->show();
}

void Engine::run()
{
while(true)
{
qApp->processEvents();

}
}


So I kinda want the run method replace the app.exec() loop (Qt's loop) by my own loop, so that I can update what needs to be updated, draw what needs to be drawn (in the OpenGL widget) and so on.

Is this a good way to go or is there a way to customize Qt's loop, or something like that ?

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
You can use for that QTimer. If timer has timeout of zero then it will activate signal timeout() when the event queue is empty - exactly what you need.

Share this post


Link to post
Share on other sites
One way of doing this would be to create a QTimer which when triggered calls the updateGL method on the QGLWidget object. The method your timer calls should do all your scene update stuff and then call "UpdateGL" to trigger a repaint (which will call your paintGL method in the widget).

Share this post


Link to post
Share on other sites
Thanks guys, that works great, but I have a question. Here's my code now :

GLWidget::GLWidget( QWidget* parent, int timerInterval )
{
this->setParent( parent );
this->setGeometry(0, 0, 800, 600);

initializeGL();

fps = 0;

QTimer* fpsTimer = new QTimer();
QObject::connect(fpsTimer, SIGNAL(timeout()), this, SLOT(showFps()));
fpsTimer->start(1000);

this->timer = new QTimer();
QObject::connect(timer, SIGNAL(timeout()), this, SLOT(processing()));
timer->start( timerInterval );
}

void GLWidget::showFps()
{
qDebug() << fps << "fps";
fps = 0;
}

void GLWidget::processing()
{
paintGL();
fps++;
}

What's the difference between updateGL and paintGL ?
With paintGL instead of updateGL and a timerInterval of 0 I get 130 000 fps (I'm only drawing a white triangle and a white square).
With updateGL and a timerInterval of 0, I get 60 fps, my guess is because VSync is probably on by default or something like that. Is this the only difference ?

Thanks again!

Share this post


Link to post
Share on other sites
Yes it seems likely that updateGL is blocking to sync with the vertical refresh rate. By calling paintgl directly you circumvent Qt's event handling, so it's likely that things like resizing the window won't work correctly. Also updategl is a public slot, so you can signal it from outside your widget whereas paintgl its protected. Using updategl is the right thing to do here I think.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!