偶然得知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')
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: [email protected] ', 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.