SDL学习笔记二 中文字体的显示
曾今一段时间热心的想学习游戏编程,于是研究起了SDL,第一篇文章《SDL学习笔记一 图片和字体显示》是07年7月份写的,至此之后,就停了下来。
最近的一段时间有热心起来,拾起以前的代码,继续研究!写《SDL学习笔记一 图片和字体显示》一文时,我没有找到中文的显示方法,今天参照《SDL & Object Pascal (Delphi) [2] 显示中文字符》一文,做了个小鬼搬家,也算重新学习的开始吧!
下面是代码,同样使用Delphi7,配合JEDI-SDL:
1program Project1;
2
3uses
4 SysUtils,
5 SDL,
6 SDL_TTF;
7
8var
9 screen:PSDL_Surface;
10 event:TSDL_Event;
11 font: PTTF_Font;
12 outtxt: WideString;
13procedure draw_unicode_text(word: WideString ; x_pos,y_pos : Integer;
14 color :Cardinal);
15var
16 text : PSDL_Surface;
17 dest : TSDL_Rect;
18 clr : TSDL_color;
19begin
20 clr.r := Color and $FF;
21 clr.g := (Color shr 8) and $FF;
22 clr.b := (Color shr 16) and $FF;
23 // 用到了两个全局对象screen和font
24 text:= TTF_RenderUNICODE_Blended(font,@word[1],clr);
25 dest.x:=x_pos;
26 dest.y:=y_pos;
27 SDL_BlitSurface(text,nil,screen,@dest);
28 SDL_FreeSurface(text);
29end;
30begin
31 if SDL_Init(SDL_INIT_VIDEO)< 0 then Exit;
32 if TTF_Init()<0 then Exit;
33 SDL_WM_SetCaption('Delphi SDL Demo',nil);
34 screen:=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
35 if (screen=nil) then
36 begin
37 SDL_Quit;
38 exit;
39 end;
40 font:=TTF_OpenFont('msyh.ttf',24);
41 outtxt:='JEDI-SDL 演示程序';
42 draw_unicode_text(outtxt,220,160,$0000FF);
43 draw_unicode_text('SDL中文输出测试',220,200,$336699);
44 draw_unicode_text('Code By Shaoyun',220,240,$00FF00);
45 SDL_Flip(screen);
46 while SDL_PollEvent(@event)>=0 do
47 begin
48 case event.type_ of
49 SDL_QUITEV: Break;
50 SDL_KEYDOWN:
51 case event.key.keysym.sym of
52 SDLK_ESCAPE: Break;
53 end;
54 end;
55 end;
56 TTF_CloseFont(font);
57 TTF_Quit;
58 SDL_Quit;
59 exit;
60end.
2
3uses
4 SysUtils,
5 SDL,
6 SDL_TTF;
7
8var
9 screen:PSDL_Surface;
10 event:TSDL_Event;
11 font: PTTF_Font;
12 outtxt: WideString;
13procedure draw_unicode_text(word: WideString ; x_pos,y_pos : Integer;
14 color :Cardinal);
15var
16 text : PSDL_Surface;
17 dest : TSDL_Rect;
18 clr : TSDL_color;
19begin
20 clr.r := Color and $FF;
21 clr.g := (Color shr 8) and $FF;
22 clr.b := (Color shr 16) and $FF;
23 // 用到了两个全局对象screen和font
24 text:= TTF_RenderUNICODE_Blended(font,@word[1],clr);
25 dest.x:=x_pos;
26 dest.y:=y_pos;
27 SDL_BlitSurface(text,nil,screen,@dest);
28 SDL_FreeSurface(text);
29end;
30begin
31 if SDL_Init(SDL_INIT_VIDEO)< 0 then Exit;
32 if TTF_Init()<0 then Exit;
33 SDL_WM_SetCaption('Delphi SDL Demo',nil);
34 screen:=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
35 if (screen=nil) then
36 begin
37 SDL_Quit;
38 exit;
39 end;
40 font:=TTF_OpenFont('msyh.ttf',24);
41 outtxt:='JEDI-SDL 演示程序';
42 draw_unicode_text(outtxt,220,160,$0000FF);
43 draw_unicode_text('SDL中文输出测试',220,200,$336699);
44 draw_unicode_text('Code By Shaoyun',220,240,$00FF00);
45 SDL_Flip(screen);
46 while SDL_PollEvent(@event)>=0 do
47 begin
48 case event.type_ of
49 SDL_QUITEV: Break;
50 SDL_KEYDOWN:
51 case event.key.keysym.sym of
52 SDLK_ESCAPE: Break;
53 end;
54 end;
55 end;
56 TTF_CloseFont(font);
57 TTF_Quit;
58 SDL_Quit;
59 exit;
60end.