设为首页收藏本站官方微博

汉化资料 在SDL里使用图片字库

[复制链接]
查看: 1896|回复: 0
打印 上一主题 下一主题

[汉化资料] 在SDL里使用图片字库

跳转到指定楼层
楼主
发表于 2010-11-4 23:45 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

在SDL里使用图片字库

原文,附上代码。
0 r9 d* q1 I3 H* Zhttp://ubuntu-gamedev.pbworks.com/Using%20Bitmap%20Fonts%20with%20SDL$ X% _1 C% z& {) ~2 E
( b: _$ ~2 D9 y
Using Bitmap Fonts with SDL  x- u8 F& }; k+ P0 e/ ^' W  ^2 Y

9 |  |+ L# W  M, YIn 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$ }) b9 f* q ( g9 ?; h6 q0 w" d% h7 F" i
CREDIT WHERE CREDIT IS DUE:
1 r9 [: \0 R8 q. kThe 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. 2 p6 E$ y4 o2 a
- L. t6 Y" R, ]! z
6 Q9 T: _6 p% U8 ?0 @& p" E/ }$ t
Introduction
3 ~( R# ~5 j: E9 J) 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. . L8 H4 X: I7 a8 y

' V+ R! B1 Y5 c2 j! y, H; fHandling the fonts using a class
+ U5 I7 i" Y/ y# qAs 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 s5 ?- y& n1 ^ * k: u. y' ~* n2 [. K, d+ W5 s
Handle the loading of the font bitmap: F0 D& ~; u; }1 A+ F& d( Y9 e0 L
Exclusively deal with any information about the font
! @4 @" b- {- SDeal with drawing/blitting the font to a Surface
- _7 b+ W% D5 U& _  P" _) ^ / y$ [" P$ u) X8 X& u: M* g
So lets define our class:
/ n$ v7 t! u; J( g$ X. o ; i* Y$ [) {) @4 ]/ y7 m( d
* {4 b9 F# H" r1 m7 z% c( m
class Font& X& I* K: b5 \$ @/ j5 o7 O
{  p* Q* s# J: k+ Q2 S9 V! b/ l3 `
SDL_Surface* m_pFontList; // this is our surface to store the bitmap font
" m6 `6 {8 K2 qpublic:
4 H# u3 L2 _$ H' [" J, m, s Font(std::string src_file);# E& q; b8 O6 L
virtual ~Font(void);
3 ?0 {2 x3 b, h, g+ U3 s$ E1 V int ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen);
4 V5 S/ B8 m3 m. G% M};# C$ b5 A( [) |8 u

% b/ \$ w! x  Q) l% k0 `. }Font(): constructs the class object and loads the specified file into memory.0 u( A8 q' M- j  c  C4 |
~Font(): destructs the class but currently contains no code. Its still worth having one for possible future changes
; y- o3 |4 D6 f( I; ?" j1 w  hShowText(): handles translating and drawing a specified string onto our surface
( [6 s8 `, e2 G  F& \ 9 ~) ~! e: T( O5 Q" w; R9 u$ A
Loading the font surface
9 G8 `7 V) B$ j' g" xThis 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.
. n/ r1 w/ H' ]  J/ I- C
8 K2 }- I5 ~% {& dHeres the code: 7 y/ r; I4 s, b

& U! K* A6 I- p" G1 z6 n
, z4 n2 C& A; q2 A* F6 r9 h' uFont::Font(std::string src_file)
( Z/ p% n% P; V8 x" F{$ S$ d2 K' x# V' Y7 E/ v( ]
m_pFontList = SDL_DisplayFormatAlpha(IMG_Load(src_file.c_str()));
5 _# K( M1 r7 R- u}
" y+ ]  g5 F4 M) z% ?1 Q
; _" X$ Q/ [3 N+ t. T8 MLets go loopy!3 Z. Q- M' ?% L6 K" V" t
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" L8 H" k; h' T1 Y4 T( n& S ' a' c. B0 A: c% Y! ^

4 b" A  K3 w6 [# H& @int Font::ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen)" i# b! `/ K% B$ r1 B
{" j% v/ }' v2 p/ {: w
/* TODO: We need to calculate the fonts height into the pos_y thing. */  d- N$ H; K! h$ f' m9 h1 X
// Also, id like to see this stuff gathered from an ini file.
, s5 I* }$ F9 \: t // That way we can alter fonts without the need for recompilcation
  [; C1 `+ L3 K5 L0 G if(!pScreen) return 1;
( ?4 D+ k3 h3 T& G7 o5 d, G        // src_rect is the location of the character we need to fetch. 8 C# n. w% L  \3 K& X. M
        // rect will be the destenation
; _! M/ s3 d' v; I9 K. l- S. c% I SDL_Rect rect, src_rect;
; t5 e1 u6 K/ ]4 ~ rect.x = pos_x;* ^- R0 `9 n& h
rect.y = pos_y;: T) g+ f- F9 ^# d: c3 M& a
SDL_Rect tmp_rect;
0 d6 z1 J" g% R) Q+ l; H7 C tmp_rect.y = 0*type;
. ?: ?  i9 |2 l/ z* { for(int i=0; i < text.size(); i++) {0 m8 X9 I0 d5 N" e: g7 {) z3 w
  tmp_rect.y = 0; // set right y axe
. L' M9 s* \# f4 ]  switch(text) {: p; Z! P2 h# ^% {$ q' \% |$ X
   case 0x20:2 v3 B: z$ U) {1 V  F
    rect.x += 10;, L6 @' Y5 z4 i% Z( i/ w
    break;. a7 r6 T& A5 k( {; @
   case 0x21: // !/ h" X# v+ I. |& w7 o/ ~8 t# T
    tmp_rect.x = 4;
7 _$ ~6 G# R  ]* E/ u    tmp_rect.w = 6;& i1 \- m) t4 K# ^$ t  S! j
    tmp_rect.h = 19;' k; f% p& n( ?3 D7 g4 U' Y
    tmp_rect.y = 0;. d- S# t! X) q* `6 w: j& c, }
    SDL_BlitSurface( m_pFontList, &tmp_rect, pScreen, &rect);0 }  ~, R1 Q# b$ s1 a3 [9 a& r0 ^+ k1 E1 h
    rect.x += tmp_rect.w;* i9 Z. I1 H" Z& p6 p9 d* \* \
    break;! y4 ^8 t! a/ C8 r
$ }5 R9 _  k9 Y! d) K  b2 R
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" I" _9 O! _5 m9 n
0 B2 P! a8 G/ g) N9 b  o
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.
  _# E- Z. n, W& n& R  b% [ . s7 w2 ^6 `( J2 A9 j+ f% N
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 1 Q* v) O+ P1 e
- ~  u0 `! y1 k9 m& i
Click Here To Download the code sdl-fontfun.tar.gz
0 b1 ~* d6 P' ^! |" I 4 U9 C: C8 v) q# K  \" `
To compile and run the code:
: O5 {7 B0 b& H' i/ z" Y  I
. d" J( f7 w  E8 h. V
3 r' T* G4 S: p8 O: X6 ]2 q! p! mg++ -o sdl-funfonts font.cpp sdlfontsfun.cpp `sdl-config --cflags --libs` -lSDL_image) i4 S/ z. m5 U1 f8 F- J  \
./sdl-funfonts* h$ i1 t8 X# b9 i+ h! v9 j

* e# E6 C: U4 D" BFinal thoughts
( |+ F) `/ y5 o& C7 L# r; ]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.

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 很美好很美好 很差劲很差劲
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

冒险解谜游戏中文网 ChinaAVG

官方微博官方微信号小黑屋 微信玩家群  

(C) ChinaAVG 2004 - 2019 All Right Reserved. Powered by Discuz! X3.2
辽ICP备11008827号 | 桂公网安备 45010702000051号

冒险,与你同在。 冒险解谜游戏中文网ChinaAVG诞生于2004年9月9日,是全球华人共同的冒险解谜类游戏家园。我们致力于提供各类冒险游戏资讯供大家学习交流。本站所有资源均不用于商业用途。

快速回复 返回顶部 返回列表