Sign in to follow this  
Xanas

Consistent game speed

Recommended Posts

What is the best way to achieve this? In Chromium it's attempted by trying to keep the framerate at 50.0. I'm doing it differently by trying to base movement updates on time. For example
void drawGL() {
  static int Ticks = 0; // for tracking frame-rate
  static int Ticks2 = 0; // for keeping the game at a consistent speed
  static int Frames = 0;
  static GLfloat backgroundscroll = 0.0f;
  // Clear the colors & depth
  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  // Reset the view
  glLoadIdentity();
  glTranslatef(0.0,0.0,-10.0f);
  drawBackgroundGL(backgroundscroll);
	hero.drawGL();
	HeroProjectileBase.drawGL();
  int Time = SDL_GetTicks();
	if((Time - Ticks2) > 8) { // every 8 ticks we want to adjust the background
    backgroundscroll += speedSelect[gameSpeed];
    if(backgroundscroll >= 1.0f)
      backgroundscroll = 0.0f;
    Ticks2 = Time;
  }
  Frames++; // Increase the frame count
  if(Time - Ticks > 5000) {
    cout<<"5 seconds = "<<Frames<<" total frames"<<endl;
    cout<<Frames/5<<" per second"<<endl;
    Ticks = Time; // update Ticks
    Frames = 0; // reset frames
  }
  SDL_GL_SwapBuffers(); // swap the buffers to display the current view
Should I be doing it this way? Later on of course that section of code will not just be scrolling the 512x512 png background, but it will probably include updates for enemy positions so that they move randomly but a consistent speed. It will also include some transparent clouds (nebulae) or other such things in the background.

Share this post


Link to post
Share on other sites
Basing movement off of real-time is generally easier and more effective than trying to lock the frame rate.

The major problem that I see with your code, however, is that you are updating the state of the game inside the rendering code. Move the timing and game logic code to outside of the rendering function. I'm not sure how a game is structured in SDL, but this is basically how I would change your code.
static int Ticks = 0; // for tracking frame-rate
static int Ticks2 = 0; // for keeping the game at a consistent speed
static int Frames = 0;
static float backgroundscroll = 0.0f;

while( !done )
{
int Time = SDL_GetTicks();
int frame_duration = Time - Ticks2;

Update( frame_duration );
DrawGL();

Ticks2 = Time;
Frames++; // Increase the frame count
if(Time - Ticks > 5000)
{
cout<<"5 seconds = "<<Frames<<" total frames"<<endl;
cout<<Frames/5<<" per second"<<endl;
Ticks = Time; // update Ticks
Frames = 0; // reset frames
}
}

void Update( int frame_duration )
{
backgroundscroll += speedSelect[gameSpeed]/8.0f*frame_duration;
if(backgroundscroll >= 1.0f)
backgroundscroll -= 1.0f;
}
void drawGL()
{
// Clear the colors & depth
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// Reset the view
glLoadIdentity();
glTranslatef(0.0,0.0,-10.0f);
drawBackgroundGL(backgroundscroll);
hero.drawGL();
HeroProjectileBase.drawGL();

SDL_GL_SwapBuffers(); // swap the buffers to display the current view
}

Share this post


Link to post
Share on other sites
Oh you are right, it's messy right now and I've already moved some of the update functionality outside of the rendering function for that very reason.

At the time I first made that I was just trying to make a scrolling background.

I also started out using functions and you'll notice I've gradually thrown in some classes/objects (HeroProjectileBase, hero)

I am just playing here, though I would like to end up with something that is a neat top scrolling shooter so all suggestions are appreciated.

EDIT: Ok I noticed a subtle edit you have that might make things a bit smoother. Rather than only updating every 8 ticks you are just dividing the motion speed by 8 (essentially the same thing) and multiplying that by the frame speed to factor for the difference in rendering speed of other machines. Cool.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this