Jumps in scrolling animation

Hi,

I am using SDL in a utility which scrolls banner horizontally, this utility will be executed in window mode. Sample code is at bottom.

During scrolling, a lot of jumps can be observed by looking closely. The frame rate is around 550-600 frames/s, but it seems frames are skipping which is causing the jerks. I have event tried controlling the scrolling speed to 60 frames/s, but it makes no difference. Is there any way to achieve smooth scrolling ? - an initialization flag or a different approach.

Thank you.

Regards,
Anees Haider

#include
#include
#include
#include “SDL.h”
#include “SDL_events.h”

//The attributes of the screen
const int SCREEN_WIDTH = 1024;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces that will be used
SDL_Surface *message= NULL;
SDL_Surface *screen = NULL;

//The event structure
SDL_Event event;

SDL_Surface *load_image( std::string filename ) {

//The image that’s loaded
SDL_Surface* loadedImage = NULL;

//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;

//Load the image
loadedImage = SDL_LoadBMP( filename.c_str() );

//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );

//Free the old surface
SDL_FreeSurface( loadedImage );
}

//Return the optimized image
return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;

//Get offsets
offset.x = x;
offset.y = y;

//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}

bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}

//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE );

//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}

//Set the window caption
SDL_WM_SetCaption( “Hello World”, NULL );

//If everything initialized fine
return true;
}

bool load_files()
{

message = load_image(“hello.bmp”);

if(message == NULL) {

return false;

}

//If everything loaded fine
return true;
}

void clean_up()
{
//Free the surfaces
SDL_FreeSurface( message );

//Quit SDL
SDL_Quit();
}

void CalculateFrameRate() {
static time_t lastTime = 0;
static int frameRateCount=0;

frameRateCount++;
if(0 == lastTime) {
lastTime=time(NULL);
} else {
time_t currTime=time(NULL);
if(currTime - lastTime > 0){
FILE *fp = fopen(“framesCount.txt”, “a+”);
if(fp) fprintf(fp, “%d\n”, frameRateCount);
fclose(fp);

frameRateCount = 0;
lastTime=time(NULL);
}
}
}

bool IsCurrentTimeDifferenceInMillisecond(int msec) {
static SYSTEMTIME lastSysTime;
static bool isSetLastSysTimeInitial=false;

if(!isSetLastSysTimeInitial) {
GetSystemTime(&lastSysTime);
isSetLastSysTimeInitial = true;
return true;
} else {
SYSTEMTIME currTime;
GetSystemTime(&currTime);
long lTime = lastSysTime.wYear * 365 * 24 * 60 * 60 * 1000 +
lastSysTime.wMonth * 30 * 24 * 60 * 60 * 1000 +
lastSysTime.wDay * 24 * 60 * 60 * 1000 +
lastSysTime.wHour * 60 * 60 * 1000 +
lastSysTime.wMinute * 60 * 1000 +
lastSysTime.wSecond * 1000 +
lastSysTime.wMilliseconds;

long cTime = currTime.wYear * 365 * 24 * 60 * 60 * 1000 +
currTime.wMonth * 30 * 24 * 60 * 60 * 1000 +
currTime.wDay * 24 * 60 * 60 * 1000 +
currTime.wHour * 3600 * 1000 +
currTime.wMinute * 60 * 1000 +
currTime.wSecond * 1000 +
currTime.wMilliseconds;

if(cTime - lTime > msec) {
GetSystemTime(&lastSysTime);
return true;
}
else return false;
}
}

int main( int argc, char* args[] ) {

//Quit flag
bool quit = false;

//Initialize
if( init() == false )
{
return 1;
}

//Load the files
if( load_files() == false )
{
return 1;
}

int x=0, y=0, xinc=1, yinc=1;
while(!quit) {

//If there’s an event to handle
SDL_Event event;
if( SDL_PollEvent( &event ) ) {
//If a key was pressed
if( event.type == SDL_KEYDOWN ) {
//Set the proper message surface
switch( event.key.keysym.sym ) {
case SDLK_ESCAPE: quit=true; break;
}
}
//If the user has Xed out the window
else if( event.type == SDL_QUIT ) {
//Quit the program
quit = true;
}
}

//If a message needs to be displayed
if( message != NULL )
{
SDL_FillRect( SDL_GetVideoSurface(), NULL, 0 );

//Apply the message centered on the screen
apply_surface(x, (SCREEN_HEIGHT - message->h ) / 2, message, screen );
}

if(IsCurrentTimeDifferenceInMillisecond(16)) {
if(x > SCREEN_WIDTH) xinc = -1;
if(x < -(( SCREEN_WIDTH - message->w ) / 2)) xinc = 1;
if(y > SCREEN_HEIGHT) yinc = -1;
if(y < 0) yinc = 1;

x+=xinc;
y+=yinc;
}

CalculateFrameRate();

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

//Clean up
clean_up();

return 0;

}