原文,附上代码。
B- q* m9 L S4 ~7 B! u. Nhttp://ubuntu-gamedev.pbworks.com/Using%20Bitmap%20Fonts%20with%20SDL
9 V6 b3 c- N, W, n5 L: F7 ^' |' C w* X
Using Bitmap Fonts with SDL
6 {+ ]9 S s: B2 a q 0 b. a5 ?; y+ d. Q! }, l3 j# }. Q$ n( \
In this article were going to demonstrate how to display text on a SDL Surface in much the same way as we would draw a sprite. 4 A7 y4 a: `# c# R
( _2 F4 D- H% B$ bCREDIT WHERE CREDIT IS DUE: : c' }- J/ y4 S& A# e# U# Z
The code for this How-To was kindly donated to us by VDM of www.ubuntuforums.org who is currently acting as an adviser for us on this project.
8 j8 G# O% U% Z
$ R/ g; s$ m- w8 f6 ?8 }3 r
* g$ k5 x4 \- M* _6 E) o- jIntroduction- r7 H: {5 e4 l, c
A lot of the time its much nicer to display text in a font that is themed the same as your game. This means you are faced with using a specially designed True Type Font or you can create one on a bitmap. Its often better to create a bitmap font because it can be more pleasing to the eye and so add to you games look and feel value.
) @' [1 X c3 J) ^ E) H/ Y" }
3 e t- C+ F- mHandling the fonts using a class
1 t: a/ a- G' q+ W0 W9 J9 z, `/ wAs we have said before in previous articles, C++ classes handle this kind of job really well, so we're going to stick with the paradigm and create a class that will do the following:
1 m. b! m( _- d# _5 V7 [3 w3 Y
Z) ^& @- t' z8 A- x+ ~Handle the loading of the font bitmap4 d' f$ e0 E4 W8 E: b" h# c
Exclusively deal with any information about the font
8 @( j7 h# t, V! G9 nDeal with drawing/blitting the font to a Surface
- f" v1 Q( L( h9 ?4 E * B: [ n, P+ u4 R7 G9 O
So lets define our class: 7 T+ _2 y H% E6 `
/ G; E2 Y$ A' f
* X* H! [- y& i( g$ t
class Font
0 ~& Y' D+ d- ~( H! w7 }{1 D. j0 y) E9 N% C
SDL_Surface* m_pFontList; // this is our surface to store the bitmap font h# F& i3 a* z9 R. f) R# E% v
public:
. J9 S @4 ^9 p( I& J7 n3 @% C Font(std::string src_file);
7 ]* h& T7 ]) F! ~ virtual ~Font(void);- ^, P* U) _. c7 Y% k" ?. Y
int ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen);; L+ s8 v% P4 S% y S P& q) |2 u0 C
};
0 A; f- U/ l/ g
- m K7 b8 |# r3 ~" Z9 eFont(): constructs the class object and loads the specified file into memory.5 }6 X* v: W: Y/ S5 H1 J
~Font(): destructs the class but currently contains no code. Its still worth having one for possible future changes- p+ `3 n; t5 W
ShowText(): handles translating and drawing a specified string onto our surface
; m9 V; @6 A; O* t2 c# C+ Q7 s
' F& \6 _( L$ V3 NLoading the font surface+ P) O1 U8 e4 S' Y& q$ C
This is the easy part! we use the IMG_Load() which we have decided to 'nest' inside thae arguments of our SDL_DisplayFormatAlpha() function. SDL_DisplayFormatAlpha() is the same as the SDL_DisplayFormat() we have used previously but also provides an alpha channel which will be handy to have in future How-Tos.
9 A+ { I2 j4 x0 ~ N. m5 Y % F3 r# W4 F* ?% U: d
Heres the code:
+ ?) ?4 ]$ g1 K8 c3 D
5 v$ [9 U3 h* o0 j. v6 j7 b: @
( \; W& o: @1 ?- k5 e) n, [5 @Font::Font(std::string src_file), q. K* p; p% b( J: c
{
5 Q, H9 y" ~) v6 j m_pFontList = SDL_DisplayFormatAlpha(IMG_Load(src_file.c_str()));
+ T- C" f e. o' O}
1 a$ A9 Z, x. }8 m; A1 D, I. R3 k
~+ v7 D' c" V( SLets go loopy!* o0 k; g0 D3 R& q) z5 Y
Now comes the complex part of our Font class, we need to loop through every letter of the string and process which bitmapped letter to draw:
$ _9 j+ ?# r, X- U) |2 t
$ u9 r+ W) w, K7 s$ G/ S7 ]$ R# W
; W' M7 t1 c. \0 r+ F! qint Font::ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen)
# y0 r; ]( F* K0 \7 d6 p( C0 a{
8 G1 f3 F# m" e+ W1 k4 ] /* TODO: We need to calculate the fonts height into the pos_y thing. */5 L" d. ]9 j1 i" ?6 }0 j6 Z
// Also, id like to see this stuff gathered from an ini file.
0 M4 g5 x5 h6 f: J5 M- o; M // That way we can alter fonts without the need for recompilcation8 |: `2 a$ ?5 F% Q8 _+ z. p7 Z
if(!pScreen) return 1;
, ? U% R3 ^5 c. h // src_rect is the location of the character we need to fetch. , H2 S0 f6 |! _3 v8 C1 D
// rect will be the destenation! o4 |" @; u5 [3 m
SDL_Rect rect, src_rect; # U" D! _, f5 Q7 c& @0 k" }
rect.x = pos_x;
& }0 D4 t( n. s! i% W rect.y = pos_y;4 G) V+ Q5 o7 ]. J
SDL_Rect tmp_rect;/ d' K0 {# Q- R! }! ?4 s6 K8 w. S4 F
tmp_rect.y = 0*type;5 S) J2 }" W# A6 C. d, f. q- V1 {
for(int i=0; i < text.size(); i++) {
# P+ h0 F( |8 T* X" d+ D" y3 ? tmp_rect.y = 0; // set right y axe" ^# Z' N; c/ ^ r+ L" C
switch(text) {
" ~3 j Y: B8 t! s- V( k case 0x20:; b. D$ ?0 f! D# P r! u& _# X
rect.x += 10;
2 s. J) L, g5 |4 U- \ break; Z0 _: U9 z. R9 y" R) S; q, J
case 0x21: // !' Q( F5 c* }8 f0 D
tmp_rect.x = 4;1 M6 I' I2 p6 {7 E: n/ p. A
tmp_rect.w = 6;
( d$ N/ N, ]( w: k8 X tmp_rect.h = 19;
2 T" B" N( q+ v tmp_rect.y = 0;- g; l5 G. @; i: T s9 r
SDL_BlitSurface( m_pFontList, &tmp_rect, pScreen, &rect);
' `4 V) P0 O# L rect.x += tmp_rect.w;4 o }" j" l5 R( b' N! i" ^. O/ F
break;- P( m0 A& @# N# R; [! {
9 L Y# x' w' w" l
You'll notice that I didn't put the whole thing up for viewing! If would like to see it in its entirety you can download the source code and look at that! : g y" A5 |' a' `4 @
0 f! {* k O1 h
you can see from the code snippet that we use a switch to test the current letter and then act upon what we find. This involves setting the values of a SDL_Rect and passing it to the SDL_BlitSurface() function which, if you have read the earlier How-Tos, is exactly the same method we have used to draw sprites and background images. - o& S( m) |- X5 D0 z
& W9 f. z8 x# n; }4 k1 u7 }VDM and myself both agree that this could be done more elegantly by using some form of initialization file hat could be loaded at the start instead of setting the values in code. Perhaps that an idea for another How-To! ;-D " @6 z9 V8 J( Q3 d
! d, d' ]! a6 q. }Click Here To Download the code sdl-fontfun.tar.gz
. [$ y9 S3 `9 {0 K/ P1 R' v/ z , O# r# x$ x3 U* w% u% Z+ R' z5 L
To compile and run the code: M! V) S, z3 q* {; L/ i, r+ \
* _8 L5 A3 `' g& Q8 n4 N' g! H$ o
, N3 r6 W& U3 G" T' @0 A
g++ -o sdl-funfonts font.cpp sdlfontsfun.cpp `sdl-config --cflags --libs` -lSDL_image) _7 u v5 G1 N
./sdl-funfonts! b H& M+ {4 }0 P( U
i- d7 Y, P+ c& d
Final thoughts7 O1 S6 b* D4 s$ ]
Thats the end of this How-To, we both hope this sheds some light on the game development process. If you have any comments or contribution please feel free to contact us on the ubuntu forums. |