How to implement a game loop with C ++ and QtQuick

I am developing a game with QtQuick 2 (Qt5.2) QML and C ++. I want most of the game logic in C ++ (I don't want to do this with JS), and I'm trying to use QStateMachines for more logic.

Now my question is: how do I implement the correct game loop in this scenario?

In the game there are, for example, objects that move from and to certain waypoints on the screen, so I think I canโ€™t do everything based on state / events. At some point I need to say: "My object should move 10 pixels in the x direction every second." For example, when my object is in the "move" state, it should move by a certain amount every second, and then, of course, check some conditions if it should switch the state (for example, to "attack").

Now all the demos in the Qt examples and on the Internet seem either completely event-based (for example, looking like four in a row), or written in JavaScript. So I got a little lost here.

One idea I could find was to create a QTimer with a timer, for example. 30 ms and connect the QTimer timeout () signal to the advance () interval of each moving object and start this timer until "return app.exec ();". Like this:

QTimer timer; QObject::connect(&timer, SIGNAL(timeout()), &scene, SLOT(advance())); timer.start(1000 / 33); return app.exec(); 

and then every object (like a mouse) has

 void Mouse::advance(int step) 

However, this requires a QGraphicsScene , and I'm not sure how well this works with the QtQuick / QML project on Android / iOS.

This is a good decision? Or is my opinion about the problem somehow wrong, and I donโ€™t need a game loop to achieve my goal?

The solution should not use any desktop applications from Qt, i.e. they should work on Android, iOS and desktop computers.

+7
c ++ qt game-engine qtquick2
source share
2 answers

This is the way: QTimer. Here you will find a detailed example:

  • Typical Qt Loop:

     int main(]int argc, char* argv[]) { // init stuff while(game.isRunning()) { a.processEvents(); //(a is a QApplication created during the init, should use a better name i guess) QTime currentTime= QTime::currentTime(); int timeSinceLastUpdate = lastUpdate.msecsTo(currentTime); while(timeSinceLastUpdate>updateTimeStep){ game.update(); timeSinceLastUpdate-=updateTimeStep; lastUpdate=lastUpdateaddMSecs(updateTimeStep); } renderer.setInterpolateFraction(static_cast<float>(timeSinceLastUpdate)/static_cast<float>updateTimeStep); renderer.renderGameObjects(); renderer.renderGUI(); renderer.swap(); } a.exit(); return 0; } 

Source: Qt Game Cycle

This should be enough information for you to get started.

+6
source share

A regular game loop in a simple game might look like this (not sure if I understand you correctly).

Each class representing a game object has two public methods: update (); and render (); Each time you call the QTimer object, you iterate over all the game objects and call their update method. After that, you repeat the same for the render () method;

In the update methods, each object decides what to do on the game map (move / shot / stand / ...) and changes its coordinates / properties. In visualization methods, each object simply displays itself on the screen.

+1
source share

All Articles