Program SDL_ImageDemo1

Program SDL_ImageDemo1 uses video memory and, to simulate movement, simply blits all of the background image and then the sprite in its new 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_ImageDemo1;
  {$Apptype GUI}
uses
  SDL;
var
  Display, Image, TempImage, ImageVox : PSDL_Surface;
  Filename : PChar = 'ball.bmp';
  Filename2 : PChar = 'voxel.bmp';
  DestinationRect : TSDL_Rect;
  Fmt : PSDL_PixelFormat;
  WhiteKey : UInt32;
  MyEvent : TSDL_Event;
begin
  Image := SDL_LoadBMP(Filename);
  TempImage := SDL_LoadBMP(Filename);
  ImageVox := SDL_LoadBMP(Filename2);
  DestinationRect.x := 550;
  DestinationRect.y := 235;
  DestinationRect.w := TempImage^.w;
  DestinationRect.h := TempImage^.h;
  //Initialise Video
  SDL_Init(SDL_INIT_VIDEO);
  Display := SDL_SetVideoMode(ImageVox^.w, ImageVox^.h, 24, SDL_HWSURFACE OR SDL_DOUBLEBUF);
  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);
  repeat
    //copy background without sprite
    SDL_BlitSurface(ImageVox, nil, Display, nil);
     //Calculate position of sprite.
    inc(DestinationRect.x, 3);
    inc(DestinationRect.y, 1);
    //copy sprite to Display
    SDL_BlitSurface(Image, nil, Display, @DestinationRect);
    //Update
    SDL_Flip(Display);
    //wait 30 milliseconds
    SDL_Delay(30);
  until DestinationRect.x > (Display^.w - 20);
  SDL_ShowCursor(SDL_DISABLE);
  repeat
    while SDL_PollEvent(@MyEvent) > 0 do
      begin
        if MyEvent.type_ = SDL_KeyDown then //Trailing underscore because type is a keyword
          begin
            if MyEvent.key.keysym.sym = SDLK_ESCAPE then
              begin
                SDL_ShowCursor(SDL_ENABLE);
                SDL_Freesurface(Image);
                SDL_Freesurface(TempImage);
                SDL_Freesurface(ImageVox);
                SDL_Quit;
                halt;
              end;
          end;
      end;
  until false;
end.    
Programming - a skill for life!

Descriptions and demonstrations of SDL procedures and functions