冒险解谜游戏中文网 ChinaAVG

标题: 在SDL里使用图片字库 [打印本页]

作者: shane007    时间: 2010-11-4 23:45
标题: 在SDL里使用图片字库
原文,附上代码。) s( e1 F6 a6 ^" Y! n, @. g7 O
http://ubuntu-gamedev.pbworks.com/Using%20Bitmap%20Fonts%20with%20SDL7 B" P' C$ i% i

' I' s# {0 A' m7 P& I+ {! [( ?Using Bitmap Fonts with SDL
& s& p: |. b* F; I. a  C " r, \) a3 G3 B* t: M6 _/ }  B, q' \( O
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. 5 z1 p) w! K* k3 Z2 l& }- A
) r% `1 ]+ b1 x$ P
CREDIT WHERE CREDIT IS DUE: 1 q" Z$ A2 R  ^5 C% [
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. : v( y) f4 T% a( V
' T/ i1 B: }1 E) R8 u0 n* i( _

/ h- M6 x9 _0 l8 iIntroduction+ Q& F) \3 Z" i
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. & ]* j, l  d; S# G' F
0 r: s2 `1 Y4 M0 q
Handling the fonts using a class
' M# G6 [% y, MAs 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:
2 t' t) ^; c6 f7 \1 `  L ' w$ A5 Z( U" Y* v& F9 t
Handle the loading of the font bitmap
7 I6 ^5 P' j$ e- E) }) w4 j. D  CExclusively deal with any information about the font. @, Z1 r% z4 s- V
Deal with drawing/blitting the font to a Surface
* J/ l3 @4 y+ X( u & z# [6 z3 P% ^' r
So lets define our class: " \4 t4 t2 c7 H$ X5 ~1 g7 g

  ?* i3 n& B; R2 z5 i, I" m; p' ?. ?+ h! Y$ Y
class Font, l' E  d7 `; k! I/ ~% f  V" L
{' @8 |) Z* d9 q3 ^5 a9 I
SDL_Surface* m_pFontList; // this is our surface to store the bitmap font$ {+ G; [' f: H! V3 q* x
public:
3 \. m  F0 n6 ^6 _0 B  ^ Font(std::string src_file);
" g3 _+ c$ K) Q" R virtual ~Font(void);
8 Q5 j. P  q. t4 X8 v5 M int ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen);
; r0 q( G: k. Q8 u3 V3 a$ j4 f};
1 p# q4 B, y. j+ L: u7 l* ]4 G 7 \( j5 \! Q( i3 `; u  g: W" ?- D
Font(): constructs the class object and loads the specified file into memory.
) j1 }4 F4 C! }$ |0 ^: C5 S+ h0 ]/ I~Font(): destructs the class but currently contains no code. Its still worth having one for possible future changes+ q, P& B4 x) e4 Q* u
ShowText(): handles translating and drawing a specified string onto our surface2 y6 @3 I: M( [0 T

" f5 F1 N5 z# c% d( b6 R; o, mLoading the font surface
$ C8 F0 q( N9 p7 W9 A; o) }/ SThis 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.
/ z+ j% _! x! q) k7 P 7 j% ^) N( y- v5 ]
Heres the code:
2 |; O' K! w5 f& s0 E# r % d3 f7 k7 |0 @# }! g, T; Z
& r$ C" l: a, N% `- B; m% Z
Font::Font(std::string src_file)
. L$ R; C5 k5 ~, @3 R  t8 c9 L7 [{
0 f4 K' m5 J0 ?" F# s m_pFontList = SDL_DisplayFormatAlpha(IMG_Load(src_file.c_str()));7 ?* _" F; \, a. b0 ]. A  e) q
}
/ N" b* ?/ w+ m5 Z9 k% @6 x: a9 o 2 u5 V8 v( Q$ ]: b3 r5 L; Q
Lets go loopy!
3 v$ S$ y5 ~: s% G; R! QNow 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: 5 c: N! F9 i3 `/ G4 s( M: P
0 P( E5 D% r3 Z2 A$ _
3 f: d3 C& J, Z! p$ ?( o  W3 w
int Font::ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen)& s7 ]; y' ?0 x8 g' h
{
# `& ~  M6 U8 S- A* ^8 l2 A# T; } /* TODO: We need to calculate the fonts height into the pos_y thing. */1 p3 a5 K; j* }  i: Y  Q: M( V  B
// Also, id like to see this stuff gathered from an ini file.. S; B8 f9 F: P. Z6 p; e
// That way we can alter fonts without the need for recompilcation2 d3 F; }$ K( i/ m7 S
if(!pScreen) return 1;: d- `/ R; o! z9 ?
        // src_rect is the location of the character we need to fetch. . [' m. m! r8 B  ^" o9 |+ c
        // rect will be the destenation2 o5 l3 v  {2 o% e  C2 p+ z
SDL_Rect rect, src_rect;
" g4 P, S$ ^) s; |% H4 }( E rect.x = pos_x;2 y/ @" s4 w7 n7 U* k$ g
rect.y = pos_y;
8 a7 c& a; r/ b  C7 j$ o SDL_Rect tmp_rect;4 ?) P# `' U5 c+ |
tmp_rect.y = 0*type;
' G; g& C1 }% ?+ H- \ for(int i=0; i < text.size(); i++) {4 |" k% q5 E2 t1 }4 n
  tmp_rect.y = 0; // set right y axe& x# D8 d+ R0 a& M, o: \+ |: m
  switch(text) {
" v3 W  K6 `# I8 ]# r7 i( o   case 0x20:0 L1 a- I/ y8 |: s7 y
    rect.x += 10;
  j' ]1 d% f- F! b6 `  e$ I    break;1 p+ g  h( T6 U) F
   case 0x21: // !
$ z  n: }/ Y* p" f9 }    tmp_rect.x = 4;
) \  e$ s# u0 K! f    tmp_rect.w = 6;% k+ U- L, q! {# W+ D
    tmp_rect.h = 19;$ k* \. q2 h; |1 `6 ]2 E
    tmp_rect.y = 0;
# N* p. o! V; e$ `    SDL_BlitSurface( m_pFontList, &tmp_rect, pScreen, &rect);# G$ m+ j7 e' l: G5 b# O  U
    rect.x += tmp_rect.w;
8 y* |! {2 |6 r- R7 p* ~    break;
9 o- m8 I6 ]/ \* n5 }7 E
1 H4 M& w; R! _( |. {% ]; r) I% bYou'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! 8 O* M' k. A0 @6 z, _
; R0 L5 c& P" e6 s! `0 p2 M
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. 8 @- z% V) k9 }+ t" y# r7 G  H

- o; M$ U2 }, M) N/ e0 l! {3 GVDM 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 ! p" u# R7 x7 x9 p4 \, O5 Z) s& y, t5 Z
3 [% ~( e2 Y6 R2 a& W1 l1 c; T2 i! o8 K
Click Here To Download the code sdl-fontfun.tar.gz 2 }7 v' R! O! d5 `' L

: z+ ?5 b5 `8 Z8 ~To compile and run the code:
% J) |# [) C) M7 t
$ X" S1 X# b$ L( g( k
; L- v9 b4 R7 J- [3 Ag++ -o sdl-funfonts font.cpp sdlfontsfun.cpp `sdl-config --cflags --libs` -lSDL_image
3 }# O0 ^) z: }) p! F./sdl-funfonts
! x- n. D0 w6 |/ b * W  Y  N0 O3 S: j# y* B  {
Final thoughts
/ D4 Y- I% I5 U- H& `6 Q- WThats 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.




欢迎光临 冒险解谜游戏中文网 ChinaAVG (https://chinaavg.com/) Powered by Discuz! X3.2