Program SDL_ImageDemo2

Program SDL_ImageDemo2 uses system memory for the surface, calculates where the sprite is to be positioned, blits the sprite, then restores the rectangle of the background that contained the sprite before blitting the sprite to its next position. To compile this program, you need to download and unzip the zip file containing the two images and put them in your program folder.

program SDL_ImageDemo2;
  {$Apptype Console}
uses
  SDL;
var
  Display, Image, ImageVox : PSDL_Surface;
  Filename : PChar = 'ball.bmp';
  Filename2 : PChar = 'voxel.bmp';
  DestinationRect : TSDL_Rect;
  Fmt : PSDL_PixelFormat;
  WhiteKey : UInt32;
begin
  Image := SDL_LoadBMP(Filename);
  ImageVox := SDL_LoadBMP(Filename2);
  DestinationRect.x := 550;
  DestinationRect.y := 235;
  DestinationRect.w := Image^.w;
  DestinationRect.h := Image^.h;
  //Initialise Video
  SDL_Init(SDL_INIT_VIDEO);
  Display := SDL_SetVideoMode(ImageVox^.w, ImageVox^.h, 24, SDL_SWSURFACE);
  SDL_WM_SetCaption('Rolling', nil);
  SDL_ShowCursor(SDL_DISABLE);
  //Calculate WhiteKey, needed to set transparent colour
  Fmt := Image^.format;
  WhiteKey:= SDL_MapRGB(Fmt, 255, 255, 255);
  //Set transparent colour
  SDL_SetColorKey(Image, SDL_SRCCOLORKEY or SDL_RLEACCEL, WhiteKey);
  //Create background
  SDL_BlitSurface(ImageVox, nil, Display, nil);
  SDL_UpdateRect (Display, 0, 0, 0, 0);
  repeat
    //Calculate new position.
    inc(DestinationRect.x, 3);
    inc(DestinationRect.y, 1);
    //copy sprite to Display
    SDL_BlitSurface(Image, nil, Display, @DestinationRect);
    //Update
    SDL_UpdateRect (Display, DestinationRect.x, DestinationRect.y,
     DestinationRect.h, DestinationRect.w);
    //wait 30 milliseconds
    SDL_Delay(30);
    //replace sprite with background.
    SDL_BlitSurface(ImageVox,@DestinationRect , Display, @DestinationRect);
    SDL_UpdateRect (Display, DestinationRect.x, DestinationRect.y,
    DestinationRect.h, DestinationRect.w);
  until DestinationRect.x > (Display^.w - 20);
  //Leave the ball showing at the border.
  SDL_BlitSurface(Image, nil, Display, @DestinationRect);
  SDL_UpdateRect(Display, 0, 0, 0, 0);
  readln;
  SDL_ShowCursor(SDL_ENABLE);
  SDL_Freesurface(Image);
  SDL_Freesurface(ImageVox);
  SDL_Quit;
end.      
Programming - a skill for life!

Descriptions and demonstrations of SDL procedures and functions