您的位置:首页 > 其它

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

2009-05-16 16:32 344 查看
这篇文章最早写于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字体的显示,都是库使用的例子,代码不难,发出来共享。以下是截图:

//Program: A simple delphi sdl demo

2//Author: shaoyun

3//Mail: shaoyun at yeah.net (please use '@' instead of 'at')

4program SDLDemo;

5uses SysUtils, Windows, SDL, SDL_Image, SDL_TTF;

6var

7 screen: PSDL_Surface;

8 event: TSDL_Event;

9 isOver: boolean = false;

10

11//*******************定义显示位图的函数draw_bmp()*************************

12//BMP格式的图片通常都上MB了,谁会用这种格式做游戏

13

14procedure draw_bmp(surface: PSDL_Surface; img_path: PChar; x_pos: integer; y_pos: integer);

15var

16 image: PSDL_Surface;

17 dest: TSDL_Rect;

18begin

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);

37end;

38

39//*******************定义显示图片的函数draw_img()*************************

40//这个函数的调用须有SDL_Image.dll、jpeg.dll、libpng1.dll的支持 ,可以显示bmp、jpg、png三种格式

41//文档指明显示png格式需要zlib.dll和libpng1.dll

42

43procedure draw_img(surface: PSDL_Surface; img_path: PChar; x_pos: integer; y_pos: integer);

44var

45 image: PSDL_Surface;

46 dest: TSDL_Rect;

47

48begin

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);

61end;

62//*******************定义显示TTF字体的函数draw_text()*************************

63//不能显示中文,不过网上有人实现了中文的显示

64

65procedure draw_text(surface: PSDL_Surface; words: PChar; x_pos: integer; y_pos: integer);

66var

67 text: PSDL_Surface;

68 font: PTTF_Font;

69 dest: TSDL_Rect;

70 textColor: TSDL_Color;

71begin

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);

89end;

90//*****************************Main***************************

91// begin

92if (SDL_Init(SDL_INIT_VIDEO) < 0) then exit;

93SDL_WM_SetCaption(' Delphi SDL Simple Demo ', nil);

94screen := SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); //设置分辨率

95if (screen = nil) then

96begin

97 SDL_Quit;

98 exit;

99end;

100//draw_bmp(screen,'bg.bmp',0,0);

101draw_img(screen, ' bg.jpg ', 0, 0);

102//TTF初始化

103if TTF_Init() < 0 then

104begin

105 MessageBox(0, PChar(Format(' Error:%s! ' #9, [SDL_GetError])), ' Error ', MB_OK or MB_ICONHAND);

106 exit;

107end;

108draw_text(screen, ' A Delphi SDL Simple Demo ', 30, 30);

109draw_text(screen, ' By shaoyun ', 380, 400);

110draw_text(screen, ' E-mail: shaoyun@yeah.net ', 380, 430);

111//销毁TTF

112TTF_Quit();

113while not isOver do

114begin

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;

128end;

129SDL_Quit;

130end.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: