SDL学习笔记一 图片和字体显示

SDL学习笔记一 图片和字体显示

偶然得知SDL这个游戏库,赶忙迫不及待的学习了一下,正好最近在学习DELPHI,于是下了DELPHI版的。可以在http://www.libsdl.org  http://www.delphi-jedi.org/这两个站点了解到相关的信息。

SDL库设计的十分的简洁,非常容易使用。我的代码实例,实现了BMP、PNG、JPG三种图片格式的加载显示,并加入了TTF字体的显示,都是库使用的例子,代码不难,发出来共享。以下是截图:
SDL学习笔记一 图片和字体显示_第1张图片

下面是代码:
//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: [email protected] ' ,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.

你可能感兴趣的:(SDL学习笔记一 图片和字体显示)