Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


basic arcade game timing problem

This topic is 5985 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

my game right now has a main loop like this:

       //check input functions//

       //update player position//

       //update computer logic (moveX, moveY)//

       //update computer position (xPos+=moveX, etc.//

       //clear screen//

       //draw background//

       //draw player//

       //draw computer//

       //flip screen//

} while(!gameOver);

Where in all that do i put in something to control speed? How can i do this? (I''ve never made a large arcade type game before, so I''ve never really faced this problem)

Share this post

Link to post
Share on other sites
Id put the delay after the Flip screen.

How ?
Well you could try a usleep(41);
but I''d have a look at http://nehe.gamedev.net/tutorials/lesson.asp?l=21

It has a nice and easy timer.
the line that dose it is:

Hope that helps.

It is a good day to code.

Share this post

Link to post
Share on other sites
i''ll give you another alternative:

i''ll have a global variable that controls the speed of sprites. this variable will be fps dependent. so if the fps goes big, it will be big, if fps goes small, it will be small also.

float g_speedcontrol;

// assuming that 60.0 is the normal speed

g_speedcontrol = fps / 60.0f;

sprite.speed = 5.0f / g_speedcontrol;
sprite.xpos += speed;

i''ve tested this and the movement of the sprites is pretty much the same when the game is running at 300fps or 60fps.
however, beware, it will go off limits when fps is very small (1 or 2 fps). if fps = 1, g_speedcontrol = 1/60 -> speed = 50*60; that''s too fast. to solve this, i''ll put an if statement to limit g_speedcontrol to a certain boundary.

Share this post

Link to post
Share on other sites
ok so my speeds were real low (originally pixel by pixel so they were like 10) and I got the following suggestion:

xPos += (((SDL_GetTicks()-last_time)/1000.0)*xVel);

but b/c of my speeds being low it was real slow so instead of multipling each speed by about 200 (which i found gave me a desired speed) i did this:

xPos += (((SDL_GetTicks()-last_time)/1000.0)*xVel*200);

which i later simplified to this:

xPos += (((SDL_GetTicks()-last_time)/50.0)*xVel);

is this a decent solution?

Share this post

Link to post
Share on other sites
The calculation is being done everytime you want to add move your object. When I did this, I found a bug. We''re dealing with floating point variables, so I got this round-off bugs. When I ran my game, and I''m not moving my object but the fps changing, the object moves by itself. So, I fixed it by doing the calculation on the speed (xVel) and then do the movement as usual:

xVel = (SDL_GetTicks()-last_time)/50.0f;
xpos += xVel;

I think this is more reliable, but if you find your way works better, do it so.

The hardest part of a project is to finish it as planned.
-Albert Tedja-

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!