SDL Stuttering Problem

Started by
4 comments, last by Shadeirou 14 years, 9 months ago
I've been working on a rendering engine using SDL off and on for the past few months now and I've noticed a problem that I cannot find the solution to; Whenever I move an object across the screen, the object has a strange stuttering issue. The object moves smooth for a second or two and then seems to stall and stutter a few times and then continues moving smoothly. The process repeats. It is quite annoying... Here's loosely what I'm doing in pseudo code:

initialize my window class //tried with HWSURFACE, SWSURFACE, DOUBLEBUF and combinations of those
fps_mngr.set_cap(60);      //set the fps cap to 60
while(user doesn't quit) { 
  fps_mngr.start();        //start the fps timer

  handle_input();          //handle user input
  move object;             //move object across screen

  window->draw_children(); //draw all children to the screen
  window->refresh();       //flip the screen

  fps_mngr.delay();        //delay the frame to match fps cap
  fps_mngr.update_fps();   //update the fps
}
My only guess is that the stuttering problem stems from the inconsistentsy in the amount of time it takes to render the frame and the effect that that has on the amount of time to delay the frame for fps capping purposes. For instance, let's say that I set my fps cap to 50. If the frame takes 6 milliseconds to render, then the fps manager delays 14 milliseconds to ensure a 50fps cap. Then, if the frame takes 19 milliseconds to render, then the fps manager delays 1 millisecond. What this means is that, in the last frame, the scene stays the same for 14 ms and it only stays the same for 1 ms in this frame. This is only a theory, though... Here is my fps manager's code:

inline void cFpsManager::start() { 
  start_ticks = SDL_GetTicks();
}

inline void cFpsManager::delay() {
  int ticks = SDL_GetTicks() - start_ticks;
  if( 1000 / cap > ticks ) {
    SDL_Delay( (1000 / cap) - ticks );
  }
}

inline void cFpsManager::update_fps() { 
  fps = 1000 / (SDL_GetTicks()-start_ticks); 
}
And this the code to update my window class:

/* NOTE: canvas is the container class that I created
** to house a SDL_Surface and all the functions
** associated with it.
*/
void CWindow::draw_children() {
  /* clear the screen if background is on. */
  if(background) {
    canvas->fill_rect(NULL, clear_color.r, clear_color.g, clear_color.b);
  }

  /* draw children */
  for( int x = 0; x < children.size(); x++ ) {         //for all children in my children vector
    if( children[x]->visible ) {                       //if the child is visible
      children[x]->refresh();                          //refresh the child
      if( children[x]->get_active_canvas() != NULL ) { //make sure the child has an active canvas
        canvas->blit(children[x]->get_x(),             //blit the child at offset x
                     children[x]->get_y(),             //and offset y
                     children[x]->get_active_canvas(), //using the active canvas
                     children[x]->get_clip_rect());    //and clipping the canvas by the given rect
      }
    }
  }
}

void CWindow::refresh() { 
  canvas->update_rect(); //basically just calls SDL_UpdateRect() and updates the whole screen
                         //I've also used SDL_Flip();
}
If anyone sees any reason as to why the stuttering problem may be happening, it would be good to know. I have used my engine on 3 different computers it happened on all of them. One weird note: On an XP machine, the stutter wasn't there, but there was a strange line that would go up and down the screen... When I reformatted that computer to Windows 7 RC, it started... Also, the other computers I tried it on were Windows 7 and Vista machines... So it may just be Vista/Windows 7... I hope not, though...
Advertisement
I haven't gone into the specifics of your code, but capping FPS with SDL_Delay is a bad idea; it doesn't give guarantees about the delay length (it can't because of the variety of operating systems).

What I'd do is to just let FPS be as high as possible (i.e. always continue rendering). Then either A) have variable length frames, where a delta time value is used to scale all movements, or B) fixed length frames where you have the update function invoked as necessary to keep up with the target update FPS (which is different from the render FPS). Googling at these terms should give you some ideas.

Essentially, this highlights the difference:

main_loop:    //variable length variant; in practice, you'd like to use an average for delta rather than only the value since last frame    dt = time_since_last_frame()    variable_update(dt)    //fixed length variant, it's important that target frames are calculated over perhaps a second or a little more only    n = frames_needed_to_meet_target_fps()    if n > 0        fixed_update()    //always render    render()
Using variable length frames and getting a conversion rate definitely seems to help, except for when the frame rate gets closer to the simulated fps cap... for lack of better words.
 float conversion = 30.f*(delta_ticks/1000.f);object->shift_x( x_vel * conversion );

As my fps gets closer to that 30.f value, the stuttering begins again. This makes more sense, at least, now that I know it's a problem with using SDL_Delay.

As for using fixed length frames, I must do a little research on this as I have never used this method before to my knowledge.

Thanks for the speedy reply.
Could someone give me a bit of information on fixed length frames? I've tried looking it up, but I can't find anything about it. I don't really want to use frame independent movement
have a look at this,it might help:

http://lazyfoo.net/SDL_tutorials/lesson14/index.php
That's what I was doing originally that was causing the problem.

This topic is closed to new replies.

Advertisement