这篇文章最早写于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
.

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130
