Game loops, events etc confusion

Started by
2 comments, last by sand_man 19 years, 3 months ago
I'm unsure exactly how the game loo works in some cases, hopefully someone can help. I know the main game loop usually does the following things - While game_state = running 1)Get User Imput 2)Process input/game logic 3)Process AI 4)Ouput - render/update screen Repeat However, how does this work with event-driven things, like a specific event at any time happening? And what about multiple user input at the same time, for example moving and firing? Does the loop just see if there is any key being pressed at point (1) and if so act on it? At what point does event detection happen? I'm talking about using C++ specifically by the way, I'm just learning and have previously written application in VB.NET, which is very much event-driven and has no central loop as such. Does anyone have an easy-to-read example of a game loop that takes these into account?
Advertisement
Quote:how does this work with event-driven things
You'll probably end up with a very tight (and simplistic) "inner loop" that does what you listed (the basic checking/updating) that fires off events.

Our cross-platform game engine uses a variety of message passing and callbacks.

The "get input" code is a good example - on every iteration of the game you'd check for new input (this is very dependent on the API you use), and if something has happened (e.g. a key-press) you send off a "Keydown[LEFT_ARROW]" type message. Any other objects can then pick up and process this information if they want to.

Much like the standard Win32 msgproc/message queue.

Using callbacks you can set up a very clever heirachical system where an object (say the graphics) can tell the Input engine that its interested in everytime an "F1" key is pressed by handing it a function pointer. The input engine goes about its business and eventually detects an F1 key and calls the function... and if the graphics engine doesn't care about it (returns 'false' for example) then it can pass on the callback to the next object in the list...

Now, internally, its all handled as a well disciplined and simplistic loop. But when you get to the highlevel code (the part in the graphics engine thats waiting for an "F1" keypress) it becomes event-based - and much much more like the VB way you mention...

Quote:Does anyone have an easy-to-read example of a game loop that takes these into account?
I'm in a hurry now and don't have a clicky link for you, but have a read through Richard Fine (aka Superpig)'s "Enginuity" series on this website (go to the articles section and do a search for it), its a great read and I think it covers this topic.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Quote:Original post by cornflake
However, how does this work with event-driven things, like a specific event at any time happening?
And what about multiple user input at the same time, for example moving and firing?


Typically, you enqueue each input right as it happens, in a queue of some sort of struct (or class) which represents all possible events (with data indicating the type of event). Then the 'process' part of the loop (there is no longer an 'input' part to the main loop in a real event-driven system; it's handled by whatever system is telling you about events) interprets all inputs which occurred since the last iteration. That's what all that Peek/Translate/Dispatch Message stuff in the Windows API is all about. :)
When an event is true, depending on when it is checked apon, the action taking usually isnt until the next iteration. But this happens all so fast that when the program runs you dont even know it. Just try programming it and I think you will find it will just kind of fall into your lap.

This topic is closed to new replies.

Advertisement