Chapter 2. Graphics and Video

Table of Contents
Introduction to SDL Video
Using OpenGL With SDL

Introduction to SDL Video

Video is probably the most common thing that SDL is used for, and so it has the most complete subsystem. Here are a few examples to demonstrate the basics.

Initializing the Video Display

This is what almost all SDL programs have to do in one way or another.

Example 2-1. Initializing the Video Display

var    
screen_ : PSDL_Surface;
begin
// Initialize the SDL library
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) then
begin
        MessageBox(0, PChar(Format('Couldn''t initialize SDL : %s',
[SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);
        // Clean up on exit
SDL_Quit;
exit;
end;

(*
* Initialize the display in a 640x480 8-bit palettized mode,
* requesting a software surface
*)
screen_ := SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
if ( screen_ = nil ) then
begin
        MessageBox(0, PChar(Format('Couldn''t set 840x480x8 video mode : %s',
[SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);
SDL_Quit;
exit;
end;
end;

Initializing the Best Video Mode

If you have a preference for a certain pixel depth but will accept any other, use SDL_SetVideoMode with SDL_ANYFORMAT as below. You can also use SDL_VideoModeOK() to find the native video mode that is closest to the mode you request.

Example 2-2. Initializing the Best Video Mode

    // Have a preference for 8-bit, but accept any depth
screen_ := SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE or SDL_ANYFORMAT);
if ( screen_ = nil ) then
begin
        MessageBox(0, PChar(Format('Couldn''t set 640x480x8 video mode : %s',
[SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);
        SDL_Quit;
halt(1);
end;
    MessageBox(0, PChar(Format('Set 640x480 at %d bits-per-pixel mode',
[screen_.format.BitsPerPixel])), 'Error', MB_OK or MB_ICONHAND);

Loading and Displaying a BMP File

The following function loads and displays a BMP file given as argument, once SDL is initialised and a video mode has been set.

Example 2-3. Loading and Displaying a BMP File

procedure display_bmp( file_name : PChar );
var
image : PSDL_Surface;
begin
// Load the BMP file into a surface
image := SDL_LoadBMP(file_name);
if ( image = nil ) then
begin
        MessageBox(0, PChar(Format('Couldn''t load %s : %s',
[file_name, SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);
exit;
end;

(*
* Palettized screen modes will have a default palette (a standard
* 8*8*4 colour cube), but if the image is palettized as well we can
* use that palette for a nicer colour matching
*)
if (image.format.palette and screen_.format.palette) then
begin
SDL_SetColors(screen_, @image.format.palette.colors[0], 0, image.format.palette.ncolors);
end;

// Blit onto the screen surface
if (SDL_BlitSurface(image, nil, screen_, nil) < 0) then
MessageBox(0, PChar(Format('BlitSurface error : %s', [SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);

SDL_UpdateRect(screen_, 0, 0, image.w, image.h);

// Free the allocated BMP surface
SDL_FreeSurface(image);
end;

Drawing Directly to the Display

The following two functions can be used to get and set single pixels of a surface. They are carefully written to work with any depth currently supported by SDL. Remember to lock the surface before calling them, and to unlock it before calling any other SDL functions.

To convert between pixel values and their red, green, blue components, use SDL_GetRGB() and SDL_MapRGB().

Example 2-4. getpixel()

(*
* Return the pixel value at (x, y)
* NOTE: The surface must be locked before calling this!
*)
function getpixel( surface : PSDL_Surface; x : integer; y : integer ) : Uint32;
type
TByteArray = array[0..2] of Byte;
PByteArray = ^TByteArray;
var
bpp : integer;
p : PInteger;
begin
bpp := surface.format.BytesPerPixel;
// Here p is the address to the pixel we want to retrieve
p := Pointer(Uint32(surface_.pixels) + y * surface_.pitch + x * bpp);


case bpp of
1:
result := LongWord(p^);

2:
result := PUint16(p)^;

3:
if (SDL_BYTEORDER = SDL_BIG_ENDIAN) then
result := PByteArray(p)[0] shl 16 or PByteArray(p)[1] shl 8 or PByteArray(p)[2]
else
result := PByteArray(p)[0] or PByteArray(p)[1] shl 8 or PByteArray(p)[2] shl 16;

4:
result := PUint32(p)^;

else
result := 0; // shouldn't happen, but avoids warnings
end;
end;

Example 2-5. putpixel()

 (*
* Set the pixel at (x, y) to the given value
* NOTE: The surface must be locked before calling this!
*)
procedure putpixel( surface_ : PSDL_Surface; x : integer; y : integer; pixel : Uint32 );
type
TByteArray = array[0..2] of Byte;
PByteArray = ^TByteArray;
var
bpp : integer;
p : PInteger;
begin
bpp := surface.format.BytesPerPixel;
// Here p is the address to the pixel we want to set
p := Pointer(Uint32(surface_.pixels) + y * surface_.pitch + x * bpp);

case bpp of
1: LongWord(p^) := pixel;

2:
PUint16(p)^ := pixel;

3:
if (SDL_BYTEORDER = SDL_BIG_ENDIAN) then
begin
PByteArray(p)[0] := (pixel shr 16) and $FF;
PByteArray(p)[1] := (pixel shr 8) and $FF;
PByteArray(p)[2] := pixel and $FF;
end
else
begin
PByteArray(p)[0] := pixel and $FF;
PByteArray(p)[1] := (pixel shr 8) and $FF;
PByteArray(p)[2] := (pixel shr 16) and $FF;
end;

4:
PUint32(p)^ := pixel;
end;
end;

The following code uses the putpixel() function above to set a yellow pixel in the middle of the screen_.

Example 2-6. Using putpixel()


// Code to set a yellow pixel at the center of the screen
var
x, y : integer;
yellow : Uint32;
begin
(* Map the color yellow to this display (R := $ff, G := $FF, B := $00)
Note: If the display is palettized, you must set the palette first.
*)
yellow := SDL_MapRGB(screen_.format, $ff, $ff, $00);

x := screen_.w div 2;
y := screen_.h div 2;

// Lock the screen for direct access to the pixels
if ( SDL_MustLock(screen_) ) then
begin
if ( SDL_LockSurface(screen_) < 0 ) then
begin
            MessageBox(0, PChar(Format('Can''t lock screen : %s', [SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);
exit;
end;
end;

putpixel(screen_, x, y, yellow);

if ( SDL_MustLock(screen_) > 0 ) then
begin
SDL_UnlockSurface(screen_);
end;
// Update just the part of the display that we've changed
SDL_UpdateRect(screen_, x, y, 1, 1);
end;