Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

tearing in SDL

Started by chrono_one Nov 12, 2009 at 12:31 AM 4 replies 2.6k views
Original Post
chrono_one
chrono_one
Hi folks. I'm currently learning SDL. It's going well except that I can't seem to eliminate this tearing problem whenever my sprite moves. The program that I'm working with right now is just a simple sprite that can move left and right on the screen. It's such a simple program, it's weird that I should get tearing. I setup the screen like this:

    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_FULLSCREEN | SDL_DOUBLEBUF );



and this is the main loop:

    //While the user hasn't quit
    while( quit == false )
    {
        //Start the frame timer
        fps.start();

        //While there's events to handle
        while( SDL_PollEvent( &event ) )
        {
            //Handle events for the stick figure
            walk.handle_events();

            //If the user has Xed out the window
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }

        //Move the stick figure
        walk.move();

        //Fill the screen white
        SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );

        //Show the stick figure on the screen
        walk.show();

        //Update the screen
        if( SDL_Flip( screen ) == -1 )
        {
            return 1;
        }

        //Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
    }


Is it the way that I'm controlling the frame rate, maybe? Any help would be appreaciated. Thanks. ~chrono
Kylotan
Kylotan
Is event a global? I think it would be better to have it as a local and just pass it in to the object handling it.

The only reason I can imagine you would get tearing is if you have turned vsync off on your graphics card.
chrono_one
chrono_one
Yeah it's a global. Hmm.. well I'll try the vsync thing. But yeah, strange. I've always had this problem with SDL. It's infuriating.
Erik Rufelt
Erik Rufelt
You should really avoid capping the framerate whenever possible, as it can result in missing a screen refresh, which will either cause tearing or stuttering if the flip waits all the way to the next vsync. The refresh rate isn't always the same on all computers, so framerate-independent movement is better (either fixed timestep with interpolated redraws, or time-based movement).
signal_
signal_
As Kylotan said, be sure to check yr vsync settings. In addition to that, you are using an SDL delay to limit the frame rate. Disable this. Turn it off. Does the tearing persist?

I think I should quote a poster here, rip-off, since he put it nicely in this thread:

Quote:
Something like SDL_Delay() is a really brittle solution. For example, SDL_Delay() typically has a granularity of about 10 milliseconds - this is defined by the operating system scheduler, not SDL. This means that if you sleep for 1 millisecond, you might end up sleeping for *at least* 10 milliseconds. So, if your total frame time exceeds 6 milliseconds you will probably miss your 60 FPS deadline. Not too bad a problem - until you are running vsynced and drop down to 30 FPS!


In that thread, I recommended the approach you took since it is simplest; however it is prone to problems. Perhaps you are experiencing a problem case.

Check out these links (especially the last one). Frame independent physics may solve this problem, if the SDL_Delay() thing is the culprit. So try turning it off as a test.

Lazy Foo Lesson on Timers
Lazy Foo Lesson on Frame Rate Regulation
Lazy Foo Lesson on frame rate independent movement
Gaffer on Games: Fix Your Timestep!
chrono_one
chrono_one
Cool thanks for the suggestions guys. I'm pretty sure that vsync is enabled, and I've got plenty of memory and clock-cycles to spare. I've read that frame capping isn't a great way to control the game speed, so I'll try to make it all frame independent. Hopefully that is the culprit, because I actually haven't tried that yet.

~chrono

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.