Game Loop Explanation?

Started by
2 comments, last by WaleedH95 12 years, 7 months ago
Hey guys, I'm new to SDL and the game loop etc. And I find the game loop incredibly confusing. Is it possible for you guys to write a game loop and explain it thoroughly? Thanks.
Advertisement
Games (and computers and programs in general) are always doing things. In a game, you want it to keep running until the user exits. But what do you do while the game is running? Well, you need to collect input from the user. You need to process that input and update the game world (move the player, take damage because someone has attacked you, etc.), and then you need to render it all to the screen. The thing about games though, is that you don't do this only once. You do it. Then you do it again. And again. And again. And again. You're constantly getting input from the user, acting on that input and updating the game world, and then rendering the game to the screen. If you stop doing that, the game freezes and hangs. Why? Because then it's doing nothing. So games will try to do this as fast as they can. Ever notice a game that lags and how it seems to jerk and jump and stutter? It's because it isn't going through this process of collecting input, responding to it, updating the game world, and then rendering the game world. So games try to do this at a fairly constant rate (you know, because updating quickly and then updating slowly would be bad, because it's like the game is going faster then slower... you want continuos, steady gameplay).

Umm.. I'm not sure what else to really explain about the game loop. The game loop is just the part of the program that does this. It collects input, process the input, updates the game world, and then renders it to the screen. And it loops this. It keeps doing it. Again. And again. And again. Until the input fromt he user says "Exit and Quit", at which point the game exits the game loop so it's no longer in this continuous cycle, and the program exits.

I have no idea if this site is of any particularly good quality, but this first one explains a little bit about the game loop: http://www.sdltutorials.com/sdl-tutorial-basics/

Other than that, google is awesome.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
For example, you want to draw a ball on the screen. How do you do it? You need a method to draw and and an x-y coordinate.

ball.draw(x, y);

There you have a ball in front you. That's not very exciting, is it, because the ball just sits there! Now, you want to improvise. You want this ball to move from the left side of the screen (x=0), to the right side (x=w, where w is the width) of the screen.


for (int x = 0; x < w; ++x) {
ball.draw(x, y);
}


Now you are excited and want more! You want the ball to keep bouncing left-right left-right infinitely until you tell it to stop by pressing the ESC button!


bool running = true;
int x = 0;
int dx = 1;
while (running) {
x += dx;
if (x == w) dx = -1;
if (x == 0) dx = 1;

ball.draw(x, y);

if (esc_pressed()) running = false;
}


There! Now you know why there is such a thing as a game loop. It keeps the game running!
Thanks for your answers. They've helped me out. I appreciate it.

This topic is closed to new replies.

Advertisement