Results 1 to 1 of 1

Thread: Game Engine

  1. #1
    Join Date
    Dec 2009
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Game Engine

    Hi everyone,

    I'm writing a game engine and I wanted to get some input about my design. First of all, here's a quick outline of how I want the engine to behave. This isn't a graphics engine (I'll be using QGraphicsView/Scene for that), it's just the engine that will be updating the properties of the objects that are in the game. The game engine will run in a separate thread so that if there are too many objects for the system to handle, the application's interface will remain responsive and only the updating of the game world will lag. The game engine should allow a maximum frames per second (in this case, a frame isn't updating the graphics on the screen, but calling the update function for every object in the game), and if the system can't maintain that fps, time in the game world shouldn't slow down, only the updating of objects in the game world. For example, if an object is moving 5 pixels per second, but isn't updated for 3 seconds, then it should move 15 pixels when it's updated.

    All the objects in the game will inherit from a class I'll be calling MyObject in the code I show here. The MyObject class has a virtual function called tick() which the engine will call to update the object. Subclasses will implement this function to do things like update the object's position, speed, etc. I've gone through a few different ways to implement this engine, and run into problems with each of them. Here is my current iteration:

    Qt Code:
    1. class GameEngine : public QThread
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit Ticker(QObject *parent = 0);
    7.  
    8. void add(MyObject* object) {
    9. done_->enqueue(object);
    10. }
    11.  
    12. void doFrame() {
    13. qSwap(queue_, done_);
    14. while (!queue_->empty()) {
    15. MyObject *object = queue_->dequeue();
    16. object->tick();
    17. add(object);
    18. }
    19. }
    20.  
    21. private:
    22. QQueue<MyObject*> *queue_;
    23. QQueue<MyObject*> *done_;
    24. };
    25.  
    26. class MyObject : public QObject
    27. {
    28. Q_OBJECT
    29.  
    30. public:
    31. explicit MyObject(QObject *parent) : QObject(parent) {
    32. timer_.start();
    33. }
    34.  
    35. protected slots:
    36. virtual void tick() = 0;
    37.  
    38. protected:
    39. //Implementations of tick() can use this to know the time between calls.
    40. int elapsed() {
    41. int time = timer_.elapsed();
    42. timer_.restart();
    43. return time;
    44. }
    45.  
    46. private:
    47. QTimer timer_;
    48. };
    49.  
    50. class MainWindow : public QMainWindow
    51. {
    52. Q_OBJECT
    53.  
    54. public:
    55. explicit MainWindow(QWidget *parent = 0) : MainWindow(parent) {
    56. engine = new GameEngine;
    57. engine->start();
    58. startTimer(20);
    59. }
    60.  
    61. protected:
    62. void timerEvent(QTimerEvent *event) {
    63. engine->doFrame();
    64. }
    65.  
    66. private:
    67. GameEngine *engine;
    68. };
    To copy to clipboard, switch view to plain text mode 

    It's pretty simple, but gets most of the job done. My main question is what will happen if the system can't process the tick() function for every object in the 20 msecs before the next frame. Since the objects keep track of how long it's been since the last time they updated, I don't see a problem there. What happens with the event queue from the timer, though? If a timer event is being posted every 20 msecs, but it takes say 30 msecs to process each frame, will the event queue just keep growing and growing?

    Another thing I'm not sure about is having a separate timer for each object to track the time between updates. Is there a better way to do this? Maybe pass an int to the tick function that indicates the current time, and each object just stores an int which is the time of its last update?

    Any other comments about this? Other issues you think I might run in to? Better ways of going about this problem? Thanks!
    Last edited by kernco; 26th February 2010 at 03:24.

Similar Threads

  1. Paint Engine
    By ^NyAw^ in forum Qt Programming
    Replies: 9
    Last Post: 22nd January 2010, 01:44
  2. Using webkit engine for QTextTable?
    By Kumosan in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2009, 15:06
  3. Can you specify a file engine?
    By skimber in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2008, 16:54
  4. Form Engine?
    By WinchellChung in forum Newbie
    Replies: 1
    Last Post: 10th January 2008, 15:51
  5. Scripting engine
    By stevey in forum Qt Programming
    Replies: 2
    Last Post: 27th October 2006, 12:36

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.