原文,附上代码。6 R o. [* q6 n5 c/ P
http://ubuntu-gamedev.pbworks.com/Using%20Bitmap%20Fonts%20with%20SDL$ B B3 v8 E4 @2 K+ m- M0 P
7 l; s$ z& G# b* H+ n$ N/ g* t
Using Bitmap Fonts with SDL
) |! a: a7 `2 O# p3 m/ Y, ?5 m ( ~; ] J9 ?7 n" |0 y) y' Y. 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.
, ^& P8 G1 a* K$ p A, ` 1 u1 V9 M5 y" V- H$ J
CREDIT WHERE CREDIT IS DUE: , f/ Z: W2 f; g; z) o
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. , B9 I1 o, U- W6 x- h7 w
" m- A5 `! \. d* ]: x2 r
- R" d3 _1 s8 _" B! P1 LIntroduction" ~2 H+ ?1 K+ v5 F" A
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.
* o. ~; U! J6 x1 }7 v1 w
3 I1 E0 ~& W. i$ ?8 O: @Handling the fonts using a class. [( C7 y( D+ B% a& w3 D
As 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: % V& Z* [( C( D
3 U2 G% g# V( e+ n4 ] ]5 Z/ E
Handle the loading of the font bitmap
; Y% c6 d% M( r+ j/ EExclusively deal with any information about the font' k, ~* p$ g1 S% Y. v" ~2 c% ~
Deal with drawing/blitting the font to a Surface3 Y& U C9 y5 C% U$ @* N0 i3 [+ O$ f
: y& x6 j, S$ K/ S6 P/ ]
So lets define our class: 3 \% f( v% B# V# M+ c
8 k8 F" b. F; k1 r. s$ b U" Q3 ^
7 `) B1 y5 a) D# X4 jclass Font$ Q4 e3 h* t! \$ E; d6 O3 l; i2 f
{' @* S) a7 }) y7 T9 p
SDL_Surface* m_pFontList; // this is our surface to store the bitmap font
+ w; R2 B( {2 ]2 b# N$ U8 Upublic:3 B# ~# d0 [" d' T! n. h
Font(std::string src_file);) M O- o5 y @& j6 y, j! {0 Z7 n
virtual ~Font(void);
5 {( o3 r- X5 Y1 S" ?6 x int ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen);5 y! K$ N' F. I- f# e
};
. Y, I; X. Z8 F! V8 Q! l* i
2 Q( L- O0 F+ d3 `# cFont(): constructs the class object and loads the specified file into memory.
( f) I4 ^0 U4 j7 K~Font(): destructs the class but currently contains no code. Its still worth having one for possible future changes: m9 X! r8 e8 C& g- S' }) l
ShowText(): handles translating and drawing a specified string onto our surface
; z3 d( E2 ^ i' s3 w 0 L5 N6 _7 q$ P1 t- b2 l, i0 e
Loading the font surface
0 [) R4 O$ {) j$ I7 d- qThis 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. " l2 G0 {% t5 y. O
9 ?! l* y' K: S& k4 t- q
Heres the code: ! _- m9 M* y: U& s
1 M0 G0 h+ g2 L& X) } j
' y# k7 y* i5 R* s7 dFont::Font(std::string src_file)4 E7 R) u; h8 R$ p, n; a( M
{
0 B+ c2 ~1 v( w: f! L5 D7 } m_pFontList = SDL_DisplayFormatAlpha(IMG_Load(src_file.c_str()));# a* l2 Z7 r, ^- C3 M! G# m
}
+ a2 ?7 h. L6 M1 c( X' Q8 V& j 5 H* [$ W( P1 ?* Q! v
Lets go loopy!
, M; ]+ o% B. _# n1 |1 \" p+ [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:
; c. \6 [ k) d U6 ^+ C( x
1 X; L4 _: E) @& c! n* |) A: u/ V2 d
int Font::ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen)1 c. G% x( p# t
{
# f$ {( h0 E5 c! L8 n( H# Q /* TODO: We need to calculate the fonts height into the pos_y thing. */
& P# R8 M1 A5 `) [ // Also, id like to see this stuff gathered from an ini file. s5 Q" H- Z+ x5 U, b1 y5 V2 x' [ [, W
// That way we can alter fonts without the need for recompilcation0 I8 O0 n- }) a) x: [0 A
if(!pScreen) return 1;
2 d) \# E# ~' C5 h# v // src_rect is the location of the character we need to fetch.
! ?; p# M2 ?% I. Z2 D ]5 J // rect will be the destenation' u: a( {0 T) D% I) D& w6 a! Q3 n
SDL_Rect rect, src_rect; * \) q& ~1 x" Z7 K; Q$ h
rect.x = pos_x;
+ n& S: x" Q) D2 g$ ] rect.y = pos_y;
# `5 ?3 e- g" [3 M SDL_Rect tmp_rect;
! l. |; K0 X# q tmp_rect.y = 0*type;
3 E5 I5 B" u9 `0 \1 o for(int i=0; i < text.size(); i++) {
( o% ?. H9 q( j1 B* E$ |7 K! X/ R tmp_rect.y = 0; // set right y axe
; ~, K4 t. O9 b" \# e$ U switch(text) {) L2 Q) Y! y+ L4 R M0 V) L
case 0x20:
) @6 i* q) ?( p5 l4 F rect.x += 10;
Y6 p& d8 Y( U0 V; y# T8 v( o& d break;0 h! v( S# @+ M+ x. }2 N
case 0x21: // !
+ r9 [1 {% F- ]* k, Z0 A tmp_rect.x = 4;% I& L, w. B5 ^+ q
tmp_rect.w = 6;
/ K7 ?" z" k ` tmp_rect.h = 19;
7 y! v* S& S' o- g tmp_rect.y = 0;# U P1 l0 Q. m4 @) e4 s
SDL_BlitSurface( m_pFontList, &tmp_rect, pScreen, &rect);
; {' Q9 F1 ~' c5 v" b( i* m7 u) O6 S rect.x += tmp_rect.w;/ H# X4 @" T2 w# j( e. w
break;. y( i- R& A: ]. D
9 u* r6 k3 N) @/ P8 O9 i/ s+ C
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!
( Q- ?/ }$ e8 w% A9 {/ \ 7 P! @8 O( V( ?+ ]7 `* h3 ^6 x9 C
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. " W, `- ]& Y7 @/ f
' _ `6 }' U# u5 z7 D
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 * G( u4 P ^( n Z6 [2 Q/ h
% A! A- f1 q9 T& d8 Q* t
Click Here To Download the code sdl-fontfun.tar.gz
: w( w7 R! v- v4 f" j3 N* d, j$ b7 F4 T
; {3 e* S+ Q7 S# h5 [ E, E/ yTo compile and run the code:
' [, B( z K5 ?: g; g) }( v
7 P' D( T& b/ q% u5 K: W5 |/ t8 `/ }# s
g++ -o sdl-funfonts font.cpp sdlfontsfun.cpp `sdl-config --cflags --libs` -lSDL_image
/ F1 D3 I4 t8 s/ U& T3 K2 m' }./sdl-funfonts; G; Q7 g: f- U% _) x
' z8 e- ^3 _: L+ |& M5 I9 j* H# E
Final thoughts
& x3 X5 p) ?' z0 K# x3 o: Y4 AThats 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. |