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.
偶然得知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.
相关文章推荐
- SDL学习笔记二(图片的加载和显示)
- SDL库的学习笔记之三—创建窗口与显示图片
- ios学习笔记:设置UIButton的文字显示位置、字体的大小、字体的颜色
- 【Qt学习笔记】.显示图片
- (源码实例)通过层DIV实现,当鼠标放在链接上面,显示图片及文字 - 流星絮语 JAVA学习笔记 - CSDNBlog
- flex 学习笔记 list显示不同字体
- html学习笔记-解决ie6中png图片透明不能正常显示问题
- zynq-7000学习笔记(十五)——在QT上调用opencv加载图片并显示
- 开源jeebbs学习笔记2——首页图片不显示问题解决
- OpenCV学习笔记(1)——显示图片
- openCV Python学习笔记(一)图片的读入,显示与保存
- 黑马程序员之WinForm编程基础学习笔记:页面上有一张图片,默认是隐藏的,用户在文本框中输入身份证号(131226198105223452),点击按钮,如果年龄大于18岁则显示图片。
- Opencv学习笔记_载入显示图片
- iOS学习笔记--如何使九宫格布局图片的显示自适应大小
- Silverlight学习笔记十二动态加载图片和显示提示(ToolTip)
- IDL学习笔记(3)--hdf文件SDS读取并显示为图片
- 如何使用汇编在dos界面上显示字体和颜色--学习心得笔记
- iOS学习笔记-126.SDWebImage2——显示gif图片
- python3.4学习笔记(十八) pycharm 安装使用、注册码、显示行号和字体大小等常用设置
- STM32学习笔记——TFT2.4彩屏显示图片