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

这篇文章最早写于07年,我的cppblog里,现在转过来,地址:http://www.cppblog.com/shaoyun/archive/2007/07/28/28876.html

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

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

下面是代码:

  1 // Program: A simple delphi sdl demo
  2 // Author: shaoyun
  3 // Mail: shaoyun at yeah.net (please use  ' @ '  instead  of   ' at ' )
  4 program  SDLDemo;
  5 uses  SysUtils, Windows, SDL, SDL_Image, SDL_TTF;
  6 var
  7     screen: PSDL_Surface;
  8     event: TSDL_Event;
  9     isOver: boolean  =  false;
 10
 11 //******************* 定义显示位图的函数draw_bmp() *************************
 12 // BMP格式的图片通常都上MB了,谁会用这种格式做游戏
 13
 14 procedure  draw_bmp(surface: PSDL_Surface; img_path: PChar; x_pos: integer; y_pos: integer);
 15 var
 16     image: PSDL_Surface;
 17     dest: TSDL_Rect;
 18 begin
 19     image : =  SDL_LoadBMP(img_path);
 20      if  (image  =   nil then
 21      begin
 22     MessageBox( 0 , PChar(Format( '  Error:%s!  '  # 9 , [SDL_GetError])),  '  Error  ' , MB_OK  or  MB_ICONHAND);
 23     exit;
 24      end ;
 25      if  (image.format.palette  <>   nil then
 26      begin
 27         SDL_SetColors(surface, @image.format.palette.colors[ 0 ],  0 ,image.format.palette.ncolors);
 28      end ;
 29     dest.x : =  x_pos;
 30     dest.y : =  y_pos;
 31     dest.w : =   0 ;
 32     dest.h : =   0 ;
 33      if  (SDL_BlitSurface(image,  nil , surface, @dest)  <   0 then
 34     MessageBox( 0 , PChar(Format( '  BlitSurface error : %s  ' , [SDL_GetError])), '  Error  ' , MB_OK  or  MB_ICONHAND);
 35     SDL_UpdateRect(surface,  0 0 , image.w, image.h);
 36     SDL_FreeSurface(image);
 37 end ;
 38
 39 //******************* 定义显示图片的函数draw_img() *************************
 40 // 这个函数的调用须有SDL_Image.dll、jpeg.dll、libpng1.dll的支持 ,可以显示bmp、jpg、png三种格式
 41 // 文档指明显示png格式需要zlib.dll和libpng1.dll
 42
 43 procedure  draw_img(surface: PSDL_Surface; img_path: PChar; x_pos: integer; y_pos: integer);
 44 var
 45     image: PSDL_Surface;
 46     dest: TSDL_Rect;
 47
 48 begin
 49     image : =  IMG_Load(img_path);
 50      if  image  =   nil   then
 51      begin
 52     MessageBox( 0 , PChar(Format( '  Error:%s!  '  # 9 , [SDL_GetError])),  '  Error  ' , MB_OK  or  MB_ICONHAND);
 53     exit;
 54      end ;
 55     dest.x : =  x_pos;
 56     dest.y : =  y_pos;
 57     dest.w : =   0 ;
 58     dest.h : =   0 ;
 59     SDL_BlitSurface(image,  nil , surface, @Dest);
 60     SDL_FreeSurface(image);
 61 end ;
 62 //******************* 定义显示TTF字体的函数draw_text() *************************
 63 // 不能显示中文,不过网上有人实现了中文的显示
 64
 65 procedure  draw_text(surface: PSDL_Surface; words: PChar; x_pos: integer; y_pos: integer);
 66 var
 67     text: PSDL_Surface;
 68     font: PTTF_Font;
 69     dest: TSDL_Rect;
 70     textColor: TSDL_Color;
 71 begin
 72     textcolor.r : =  $ 00 ;
 73     textcolor.g : =  $FF;
 74     textcolor.b : =  $ 00 ;
 75     textcolor.unused : =   0 ;
 76     font : =  TTF_OpenFont( '  simhei.ttf  ' 20 );
 77      if  font  =   nil   then
 78      begin
 79         MessageBox( 0 , PChar(Format( '  Error:%s!  '  # 9 , [SDL_GetError])),  '  Error  ' ,MB_OK  or  MB_ICONHAND);
 80     exit;
 81      end ;
 82     text : =  TTF_RenderText_Blended(font, words, textColor);
 83     dest.x : =  x_pos;
 84     dest.y : =  y_pos;
 85     SDL_BlitSurface(text,  nil , surface, @Dest);
 86     SDL_Flip(screen);
 87     SDL_FreeSurface(text);
 88     TTF_CloseFont(font);
 89 end ;
 90 //***************************** Main ***************************
 91 //   begin
 92 if  (SDL_Init(SDL_INIT_VIDEO)  <   0 then  exit;
 93 SDL_WM_SetCaption( '  Delphi SDL Simple Demo  ' nil );
 94 screen : =  SDL_SetVideoMode( 640 480 32 , SDL_SWSURFACE);  // 设置分辨率
 95 if  (screen  =   nil then
 96 begin
 97     SDL_Quit;
 98     exit;
 99 end ;
100 // draw_bmp(screen, ' bg.bmp ' , 0 , 0 );
101 draw_img(screen,  '  bg.jpg  ' 0 0 );
102 // TTF初始化
103 if  TTF_Init()  <   0   then
104 begin
105     MessageBox( 0 , PChar(Format( '  Error:%s!  '  # 9 , [SDL_GetError])),  '  Error  ' , MB_OK  or  MB_ICONHAND);
106     exit;
107 end ;
108 draw_text(screen,  '  A Delphi SDL Simple Demo  ' 30 30 );
109 draw_text(screen,  '  By shaoyun  ' 380 400 );
110 draw_text(screen,  '  E-mail: shaoyun@yeah.net  ' 380 430 );
111 // 销毁TTF
112 TTF_Quit();
113 while   not  isOver  do
114 begin
115      while  (SDL_PollEvent(@event)  <>   0 do        // 处理键盘按键
116      begin
117      case   of
118       SDL_QUITEV:
119         isOver : =  true;
120       SDL_KEYDOWN:
121          begin
122            case  event.key.keysym.sym  of
123             SDLK_ESCAPE: isOver : =  True;
124            end ;
125          end ;
126      end ;
127      end ;
128 end ;
129 SDL_Quit;
130 end .

转载于:https://www.cnblogs.com/shaoyun/archive/2009/05/16/1458027.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值