若水居

shaoyun的编程Blog
随笔 - 1, 文章 - 0, 评论 - 5, 引用 - 0
数据加载中……

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

偶然得知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.

posted on 2007-07-27 18:04 shaoyun 阅读(3064) 评论(5)  编辑 收藏 引用 所属分类: Delphi

评论

# re: SDL学习笔记一 图片和字体显示  回复  更多评论   

对SDL感兴趣可以看看
http://lazyfoo.net/SDL_tutorials/index.php

http://sol.gfxile.net/gp/
2007-07-27 21:23 | zhongwx

# re: SDL学习笔记一 图片和字体显示  回复  更多评论   

我在C++里是这样的直接用中文的
message = TTF_RenderUNICODE_Blended( font, (const Uint16 *)L"嗨,世界~", textColor );
2007-07-28 03:30 | func

# re: SDL学习笔记一 图片和字体显示[未登录]  回复  更多评论   

代码可以用插入代码,这样看起来比较好!
2007-07-28 10:01 | 梦在天涯

# re: SDL学习笔记一 图片和字体显示  回复  更多评论   

怎么没有接着写下去,挺好的
2008-06-26 00:55 | Onet

# re: SDL学习笔记一 图片和字体显示  回复  更多评论   

delhi版的SDL库我一直没找着,你说的那个网站我也去看了,还是没有找到,麻烦你给个下载地址,和在DELPHI里的设置方法,谢谢。
2008-07-07 09:45 | DELPHIGAMER

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理