偶然得知SDL這個游戲庫,趕忙迫不及待的學習了一下,正好最近在學習DELPHI,于是下了DELPHI版的。可以在http://www.libsdl.org http://www.delphi-jedi.org/這兩個站點了解到相關的信息。
SDL庫設計的十分的簡潔,非常容易使用。我的代碼實例,實現了BMP、PNG、JPG三種圖片格式的加載顯示,并加入了TTF字體的顯示,都是庫使用的例子,代碼不難,發出來共享。以下是截圖:

下面是代碼:
//Program: A simple delphi sdl demo
//Author: shaoyun
//Mail: shaoyun at yeah.net (please use '@' instead of 'at')
program SDLDemo;
uses SysUtils, Windows, SDL, SDL_Image, SDL_TTF;
var
screen: PSDL_Surface;
event: TSDL_Event;
isOver:boolean=false;
//*******************定義顯示位圖的函數draw_bmp()*************************
//BMP格式的圖片通常都上MB了,誰會用這種格式做游戲
procedure draw_bmp(surface:PSDL_Surface; img_path:PChar; x_pos:integer; y_pos:integer );
var
image : PSDL_Surface;
dest:TSDL_Rect;
begin
image := SDL_LoadBMP(img_path);
if ( image = nil ) then
begin
MessageBox(0, PChar(Format( ' Error:%s! ' #9, [SDL_GetError])), ' Error ' , MB_OK or MB_ICONHAND);
exit;
end;
if (image.format.palette<>nil) then
begin
SDL_SetColors(surface, @image.format.palette.colors[0], 0, image.format.palette.ncolors);
end;
dest.x:=x_pos;
dest.y:=y_pos;
dest.w:=0;
dest.h:=0;
if (SDL_BlitSurface(image, nil, surface, @dest) < 0) then
MessageBox(0, PChar(Format( ' BlitSurface error : %s ' , [SDL_GetError])), ' Error ' , MB_OK or MB_ICONHAND);
SDL_UpdateRect(surface, 0, 0, image.w, image.h);
SDL_FreeSurface(image);
end;
//*******************定義顯示圖片的函數draw_img()*************************
//這個函數的調用須有SDL_Image.dll、jpeg.dll、libpng1.dll的支持 ,可以顯示bmp、jpg、png三種格式
//文檔指明顯示png格式需要zlib.dll和libpng1.dll
procedure draw_img(surface:PSDL_Surface; img_path:PChar; x_pos:integer; y_pos:integer);
var
image : PSDL_Surface;
dest:TSDL_Rect;
begin
image:=IMG_Load(img_path);
if image=nil then begin
MessageBox(0, PChar(Format( ' Error:%s! ' #9, [SDL_GetError])), ' Error ' , MB_OK or MB_ICONHAND);
exit;
end;
dest.x:=x_pos;
dest.y:=y_pos;
dest.w:=0;
dest.h:=0;
SDL_BlitSurface ( image, nil, surface, @Dest );
SDL_FreeSurface ( image );
end;
//*******************定義顯示TTF字體的函數draw_text()*************************
//不能顯示中文,不過網上有人實現了中文的顯示
procedure draw_text(surface:PSDL_Surface; words:PChar; x_pos:integer; y_pos:integer );
var
text : PSDL_Surface;
font : PTTF_Font;
dest: TSDL_Rect;
textColor : TSDL_Color;
begin
textcolor.r:=$00;
textcolor.g:=$FF;
textcolor.b:=$00;
textcolor.unused:=0;
font:= TTF_OpenFont( ' simhei.ttf ' ,20);
if font=nil then
begin
MessageBox(0, PChar(Format( ' Error:%s! ' #9, [SDL_GetError])), ' Error ' , MB_OK or MB_ICONHAND);
exit;
end;
text:= TTF_RenderText_Blended(font,words, textColor);
dest.x:=x_pos;
dest.y:=y_pos;
SDL_BlitSurface ( text, nil, surface, @Dest );
SDL_Flip(screen);
SDL_FreeSurface ( text );
TTF_CloseFont( font );
end;
//*****************************Main***************************//
begin
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ) then
begin
exit;
end;
SDL_WM_SetCaption( ' Delphi SDL Simple Demo ' , nil );
screen:= SDL_SetVideoMode( 640,480,32, SDL_SWSURFACE );//設置分辨率
if ( screen = nil ) then
begin
SDL_Quit;
exit;
end;
//draw_bmp(screen,'bg.bmp',0,0);
draw_img(screen, ' bg.jpg ' ,0,0);
//TTF初始化
if TTF_Init()<0 then
begin
MessageBox(0, PChar(Format( ' Error:%s! ' #9, [SDL_GetError])), ' Error ' , MB_OK or MB_ICONHAND);
exit;
end;
draw_text(screen, ' A Delphi SDL Simple Demo ' ,30,30);
draw_text(screen, ' By shaoyun ' ,380,400); draw_text(screen, ' E-mail: shaoyun@yeah.net ' ,380,430);
//銷毀TTF
TTF_Quit();
while not isOver do
begin
while (SDL_PollEvent(@event)<>0) do //處理鍵盤按鍵
begin
case of
SDL_QUITEV: isOver:=true;
SDL_KEYDOWN:
begin
case event.key.keysym.sym of
SDLK_ESCAPE: isOver:= True;
end;
end;
end;
end;
end;
SDL_Quit;
end.