原文,附上代码。! _5 o' c9 c$ N; F1 [9 h
http://ubuntu-gamedev.pbworks.com/Using%20Bitmap%20Fonts%20with%20SDL
' @! g( r+ s- w5 o9 Y& `3 Z8 s4 ~# a# z
Using Bitmap Fonts with SDL. p" n3 h& r6 S
9 w2 V( d$ h4 n7 j% I
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. - C! K B" P3 g* j* q8 j. ?; G' V5 H
: a1 r2 G3 J7 P$ M# J9 m
CREDIT WHERE CREDIT IS DUE: 0 |* k2 C% e) \+ D# x! N
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. 5 k! P5 Z* j' ~1 L% w0 r3 s
+ k7 ]5 t6 m9 F2 ~
9 C. ? G; q1 W) A( \; W. _
Introduction
. W8 j- c9 Y! DA 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.
4 ?, M) N7 z0 ^4 D' u- L
! m7 X+ A W, bHandling the fonts using a class! C; h4 o1 m D0 O7 S: i6 N
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: & r3 M. T+ t$ p
4 A" {6 q4 F b, ?. ]; W9 r
Handle the loading of the font bitmap, u) q! ]9 n" r% R" G3 }
Exclusively deal with any information about the font
, r5 ^- @8 \$ X4 t7 {6 J: t6 I# PDeal with drawing/blitting the font to a Surface7 Z+ _! ]1 z, X/ P5 e. W9 @; E
# C& f* c) F ~( d5 d5 ~9 c1 p
So lets define our class:
1 ?. d* {) {8 D) _
2 Q% ^. c* n6 I0 |% d% c6 J* }4 E5 e% ]5 i6 H
class Font* `2 Q, R" z* z0 ^" e
{ Z1 {7 W( c* f3 Z' N }
SDL_Surface* m_pFontList; // this is our surface to store the bitmap font. H. T( V8 L% ^6 v& p2 w
public:
6 }8 c9 T+ Z. H' l4 H Font(std::string src_file);
& q, S0 m! h, z" D9 \9 r virtual ~Font(void);
2 c4 c* U2 K; G: |& a- d int ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen);
, S) k! ~- B- ~$ j% Y};
# c# _9 F S- H6 d 9 B7 |4 |: u" y+ S
Font(): constructs the class object and loads the specified file into memory., M5 h' T0 K% A6 `3 I; `
~Font(): destructs the class but currently contains no code. Its still worth having one for possible future changes9 Q/ p# N: e* v8 i- c1 e d7 W5 ~
ShowText(): handles translating and drawing a specified string onto our surface' }8 `0 ?/ A! N, D
. {% W, B4 J" y+ \' l; J; r
Loading the font surface
, c& d: x+ l% ]. E% 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.
+ b g$ t C" _7 o# Q0 j3 U
" W+ T- M/ U7 ]9 z1 N- H& ]Heres the code: : F# P+ y, f+ I5 \/ F; Y
# ?5 H, G0 W- @* a7 H% v3 x# g; R
6 E0 i& G8 T& }& p4 yFont::Font(std::string src_file)4 I5 S0 I- \2 O+ S+ l: C u9 P2 j; ]
{9 J; e; n0 [% b7 g) C- ?% ]$ n
m_pFontList = SDL_DisplayFormatAlpha(IMG_Load(src_file.c_str()));
# c9 K+ v7 }) h* _) O}8 }5 ^# V3 n" X8 t
6 C7 v( x* `/ Q' U% O
Lets go loopy!9 X# Y7 b6 H4 O3 P, h+ A
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: 3 `9 U I! Z* [# z8 j1 ^
7 W+ E1 ~9 _' F' s: P
; ~& A& f! W* L' o
int Font::ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen)
1 K- p% \9 j2 g! y! i# C{) X- g0 \( `; L" E9 F/ T# M z; v% v
/* TODO: We need to calculate the fonts height into the pos_y thing. */
, a/ f7 u/ V4 R/ b" e // Also, id like to see this stuff gathered from an ini file.
8 g9 e2 d5 R1 b! R# |+ W1 r // That way we can alter fonts without the need for recompilcation
( L' W) r# h } if(!pScreen) return 1;- ^: m. P+ I2 H1 _* b
// src_rect is the location of the character we need to fetch.
5 z2 `2 \/ h7 N6 @( }7 w$ q& ~ // rect will be the destenation/ F8 U: Y. H8 o( j! v! R9 c
SDL_Rect rect, src_rect; & y+ v @# P; r$ o7 G
rect.x = pos_x;
: D; q2 }$ S2 N5 C6 }( A. g7 L" B rect.y = pos_y;
/ T8 |" ^& b1 |. ^ j/ t5 E; I SDL_Rect tmp_rect;) p u k: `3 m' D2 `" B" ~
tmp_rect.y = 0*type;
/ T q& |) f6 \ X" I5 _" F for(int i=0; i < text.size(); i++) {
) M0 @9 u) R/ y% Q: k% k5 z, d tmp_rect.y = 0; // set right y axe' X, a! S( j% w# s
switch(text) {2 F; I1 d" O4 t) p4 ^& i5 Q% U$ V: F
case 0x20:" U% e# M, m) h/ j: `) |
rect.x += 10;( E7 M; x9 A+ u2 v. z) `
break;% e, }# ^5 w$ }/ ?0 Q* q+ {* T
case 0x21: // !8 V0 G# }* G, C, a( U
tmp_rect.x = 4;
i; J. f% O( A tmp_rect.w = 6;
4 ?1 x% N$ E) [; K( u& H# u7 f tmp_rect.h = 19;
# k2 j( \2 y1 y5 s" n tmp_rect.y = 0;
* c7 Z1 \- j0 }' A SDL_BlitSurface( m_pFontList, &tmp_rect, pScreen, &rect);& ~, V8 ^/ i% e" N$ O& ^# C
rect.x += tmp_rect.w;
6 [0 a2 W# o/ B) B ? break;
3 ?0 d$ H; a0 C, q/ l/ L. Z9 k . m+ N( a3 f5 Z1 w
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!
. y: q4 H' E" X4 D : [3 w+ j+ @5 |2 g
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. + x9 R5 g- }$ u5 I& W- ~4 J: q3 J
$ [/ g4 ~9 D& V
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
" b+ a$ o, E) x# J# z/ t* P 2 Z0 L H I( l- E
Click Here To Download the code sdl-fontfun.tar.gz ! m9 P. M1 v3 ]
& R, |& y, {# ^To compile and run the code: # H# m) }7 J, }% T2 D% ^0 d
' o4 T) q4 v( ]0 s) a/ x4 ]& B
" e) W# x$ R/ U
g++ -o sdl-funfonts font.cpp sdlfontsfun.cpp `sdl-config --cflags --libs` -lSDL_image
2 G$ j( R# t6 z: D' D1 J# ?./sdl-funfonts
9 [4 B' _. @. ^2 Y. h: k& _ : k5 f$ P4 x& S- j
Final thoughts
" k, D3 l3 [$ p4 w) e7 jThats 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. |