• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Shadeirou

SDL Stuttering Problem

5 posts in this topic

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...
0

Share this post


Link to post
Share on other sites
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()



0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
have a look at this,it might help:

http://lazyfoo.net/SDL_tutorials/lesson14/index.php
0

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  
Followers 0