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