Jump to content

  • Log In with Google      Sign In   
  • Create Account


Making a fast, efficient main game loop for a 2D platformer?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 nicksaiz65   Members   -  Reputation: 200

Like
0Likes
Like

Posted 04 November 2012 - 08:29 PM

I have recently finished a level editor that I plan to use to create levels for a 2D platformer that I am working on. Now, I am working on a program that will actually play the levels and let the user play them. However, I intend to sell this game, so it will need to be fast, and run the same speed on all computers. For it to do this, the main game loop will need to be structured well. I understand I will need to use timers, especially for rendering the game, but what would be the best way to go about this? Also, does it matter what order I do the tasks in? For example, should I take their input before I draw them? When should I test if they are on solid ground? Also, I am programming in Blitz Plus, so pseudocode would be appreciated if possible. Thanks.
I am the man who will become the pirate king!

Sponsor:

#2 lride   Members   -  Reputation: 633

Like
3Likes
Like

Posted 04 November 2012 - 09:44 PM

You should calculate how much time has passed between each frame and use that time to update your game
And if you are running some physics, it is good idea to use fixed fps. To do this you can create some delay between each frame if a frame is finished early.
Every game loop will look similar to this
[source lang="cpp"]while(GameIsOn){ handleEvent(); update(delta_time); draw();}[/source]
An invisible text.

#3 jbadams   Senior Staff   -  Reputation: 17248

Like
8Likes
Like

Posted 04 November 2012 - 09:55 PM

Try reading through "fix your timestep" and "deWiTTERS game loop". Posted Image

#4 kloffy   Members   -  Reputation: 865

Like
0Likes
Like

Posted 05 November 2012 - 09:19 PM

Try reading through "fix your timestep" and "deWiTTERS game loop". Posted Image


Classic articles, especially the Gaffer one.

After reading deWitter, I am wondering: Is it really fair to say that the "constant game speed" solutions help with input response time on slower machines? The article certainly seems to suggest that, stating it as a flaw of the other approaches. Furthermore, this idea is reinforced by the notion that your game logic will be independent from your rendering. However, what you are really doing is giving your physics simulation time to catch up if the rendering is running slow. The catching up is done in a rapid sequence of fixed-length incremental steps. If I am not mistaken, the main purpose of this is to make sure that your simulation is well behaved. However, this should not really affect input response time, right? In a sense, the simulation is running in virtual time, whereas user input happens in real time.

Either way, it is a good article on an interesting topic. The intent of my post is to verify that my understanding is correct and to help others that may be confused.

Edited by kloffy, 05 November 2012 - 09:26 PM.


#5 doyleman77   Members   -  Reputation: 347

Like
0Likes
Like

Posted 07 November 2012 - 08:02 PM

this is where it confuses me. at a certain point, the articles lose me on exactly what model I should be using on certain scenarios. from what I gather, though, it's almost always better to just use a fixed frame rate (ie 30 - 60)?

#6 L. Spiro   Crossbones+   -  Reputation: 12270

Like
1Likes
Like

Posted 07 November 2012 - 09:26 PM

Fixed frame rate or fixed logical-update rate?
They are not the same thing. The frame rate should be whatever it can be. Logical updates should be fixed (usually to around 30 times per second).


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS