SDL本身只支持加載BMP文件,如果你想顯示其他類型的圖像文件,就要用到擴展類庫,擴展類庫里面還有其他一些組件,你可以在這里下載到
http://www.libsdl.org/projects/SDL_image/
下載完以后,按照教程一配置,我們就可以顯示一張PNG圖片了。
以下是具體的代碼,與教程二很相似,如有疑問請參考教程二。
#include?
"
SDL.h
"
#include?
<
SDL_image.h
>
#include?
<
string
>
SDL_Surface?
*
screen;
SDL_Surface?
*
background;
SDL_Surface?
*
load_image(?std::
string
?filename?)?

{
????
//
The?image?that's?loaded
????SDL_Surface
*
?loadedImage?
=
?NULL;

????
//
The?optimized?image?that?will?be?used
????SDL_Surface
*
?optimizedImage?
=
?NULL;

????
//
Load?the?image?using?SDL_image
????loadedImage?
=
?IMG_Load(?filename.c_str()?);

????
//
If?the?image?loaded
????
if
(?loadedImage?
!=
?NULL?)

????
{
????????
//
Create?an?optimized?image
????????optimizedImage?
=
?SDL_DisplayFormat(?loadedImage?);

????????
//
Free?the?old?image
????????SDL_FreeSurface(?loadedImage?);
????}
????
//
Return?the?optimized?image
????
return
?optimizedImage;
}
void
?apply_surface(
int
?x,
int
?y,SDL_Surface?
*
source?,SDL_Surface?
*
destinaion)

{
????SDL_Rect?rect;
????rect.x
=
x;
????rect.y
=
y;
????SDL_BlitSurface(source,NULL,destinaion,
&
rect);

}
int
?main(?
int
?argc,?
char
*
?args[]?)

{
????
//
Start?SDL
????SDL_Init(?SDL_INIT_EVERYTHING?);

????
//
Quit?SDL
????screen
=
SDL_SetVideoMode(
640
,
480
,
32
,SDL_SWSURFACE);
????background
=
load_image(
"
Dockxrt5.jpg
"
);

????apply_surface(
0
,
0
,background,screen);

????
if
(SDL_Flip(screen)
==-
1
)
????????
return
?
-
1
;
????SDL_FreeSurface(background);
????SDL_Delay(
2000
);
????SDL_FreeSurface(background);
????SDL_Quit();

????
return
?
0
;????
}
以下代碼用SDL_TTF擴展類庫加載一個TTF字體文件,并用該字體顯示一段文字。
下載完以后,按照教程一配置,我們就可以顯示一張PNG圖片了。


































































以下代碼用SDL_TTF擴展類庫加載一個TTF字體文件,并用該字體顯示一段文字。
?1
#include?
"
SDL.h
"
?2
#include?
<
SDL_ttf.h
>
?3
#include?
<
string
>
?4
SDL_Surface?
*
screen;
?5
SDL_Surface?
*
background;
?6
void
?apply_surface(
int
?x,
int
?y,SDL_Surface?
*
source?,SDL_Surface?
*
destinaion)
?7
{
?8
????SDL_Rect?rect;
?9
????rect.x
=
x;
10
????rect.y
=
y;
11
????SDL_BlitSurface(source,NULL,destinaion,
&
rect);
12
13
}
14
int
?main(?
int
?argc,?
char
*
?args[]?)
15
{
16
????
//
Start?SDL
17
18
????SDL_Init(?SDL_INIT_EVERYTHING?);
19
????
if
(TTF_Init()
==-
1
)
20
????????
return
?
-
1
;
21
????TTF_Font?
*
font
=
NULL;
22
????font
=
TTF_OpenFont(
"
verdana.ttf
"
,
14
);
23
????
if
(font?
==
?NULL)
24
????????
return
?
-
1
;
25
26
????
//
Quit?SDL
27
????SDL_Color?textColor
=
{
255
,
255
,
255
}
;
28
????SDL_Color?textBgColor
=
{
0
,
0
,
0
,}
;
29
????screen
=
SDL_SetVideoMode(
640
,
480
,
32
,SDL_SWSURFACE);
30
????background
=
TTF_RenderText_Blended(?font,?
"
why?it?can?not?display?chinese
"
,?textColor);
31
32
????apply_surface(
0
,
0
,background,screen);
33
34
????
if
(SDL_Flip(screen)
==-
1
)
35
????????
return
?
-
1
;
36
37
????SDL_Delay(
100000
);
38
????SDL_Quit();
39
40
????
return
?
0
;????
41
}
42

?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
