Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update/render game objects using Qt

Tags:

c++

event-loop

qt

I'd like to know how to update and render game objects when using Qt. E.g. with a typical game you'd have an event loop, but Qt just has exec(). What is the correct way to update and render game objects using Qt? How do I do things like get the time between each frame to update my game objects? Do I use a separate thread for all of this?

My question is quite similar to this thread, except I'm not trying to avoid standard Qt processes.

Edit: I apologise for the incomplete question. I believe tmpearce has already answered my question, but here's a basic example of what I'm trying to do in case anyone else has the same issue:

int main(int argv, char **args)
{
    QApplication app(argv, args);

    ApplicationWindow window;
    window.show();

    // How and where do I update my Game object?

    return app.exec();
}

class Game
{
public:
    void timeUpdate()
    {
        // Update game entities, etc. Should be called every frame.
    }

    void render()
    {
        // Renders game entities - where should I render to?
        // Should be called 30 times a second, etc.
    }
};

Cheers.

like image 258
Mitch Avatar asked Mar 18 '12 07:03

Mitch


1 Answers

Qt has an event loop; it is started by QApplication::exec(). I have no idea what your requirements are as far as updating and rendering; but since you seem to want an event loop like in a "typical game", I'll assume you can take it from there.

In your application, create a QTimer. Connect it's timeout signal to a slot you want to call on each event loop. Call QTimer::start(0): this will trigger the timeout signal each time through the event loop.

As far as timing goes, check out QElapsedTimer - I suspect it is what you're looking for.

like image 86
tmpearce Avatar answered Sep 18 '22 20:09

tmpearce