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

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

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

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

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

在SDL里使用图片字库

原文,附上代码。% P/ e6 ]7 I! f4 |4 Z( O3 ?
http://ubuntu-gamedev.pbworks.com/Using%20Bitmap%20Fonts%20with%20SDL$ D4 J: g' c5 v# q- Y9 K9 Q/ h, A
9 }$ p- R: S/ ^% g7 w
Using Bitmap Fonts with SDL
% ~, B( V8 Z' S9 x 8 f1 {. Y2 Y* v7 W+ q6 i3 H4 \
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 Q9 ]* N+ G: X$ o
' h& |! U6 c! qCREDIT WHERE CREDIT IS DUE: # [/ g3 [; m4 R' T1 x; L
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.
" W8 l7 K$ t$ Q
) |- f2 G" B5 Z4 Q* g0 u  z 5 c% W3 \3 b/ A& w. r! a% N* `
Introduction3 @4 U; ?$ M3 l  i5 P, g
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. ; t, ~  {# s2 E. f! J2 E! D

" Q; D7 k( p: |$ D& rHandling the fonts using a class. K0 L  c: e( f  C
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: 2 W' L+ B. `: W4 [& n* C

, r1 Z, K5 |7 T2 `Handle the loading of the font bitmap
) [2 V$ j0 k" B, g+ P/ e- l+ s+ \Exclusively deal with any information about the font
0 r& L! ^* \2 [8 C' l& p! A4 n1 ODeal with drawing/blitting the font to a Surface
; S/ q/ L3 k# W( w
3 t; i! }! v4 P4 S6 R2 {7 qSo lets define our class: 3 c7 ^% X' ?! x) J- T9 t  x; ~6 }  B
1 x  r% {2 U( N: t* |2 S
7 P% ^4 [( |6 b# p7 |: S3 y
class Font
3 O- t  ^: N; Z. o{
4 h+ r1 _; _9 c SDL_Surface* m_pFontList; // this is our surface to store the bitmap font* d8 k1 b+ B' J/ \0 j$ `  t
public:
1 r3 e+ N2 M2 N* c* b Font(std::string src_file);
0 E  Q, H1 J# k+ l: y' a+ N1 k virtual ~Font(void);$ C# E, E* s8 [
int ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen);
$ c% M2 Z) ^! Y2 b+ J) C};- y5 q( V  a7 v4 a: }
9 \6 p- g, f6 ]( c! Z
Font(): constructs the class object and loads the specified file into memory.
! `3 [6 J2 o' T) k+ e~Font(): destructs the class but currently contains no code. Its still worth having one for possible future changes
) k5 t7 J' @) W- P) ?5 |ShowText(): handles translating and drawing a specified string onto our surface
  y2 i5 X; O5 k; K# x$ H) v" I ; j7 x  q" F, X/ @
Loading the font surface
, ~: K9 A* J3 X6 p# ?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.
; o5 p& X8 Q4 z3 N: u! W9 _9 q ! a/ v: T! u: j1 p
Heres the code:
4 q  x' C! ?8 z# q' W) C 3 }3 Q4 i) N/ ?
7 ]3 c# {) X: A
Font::Font(std::string src_file)
: f& f* F5 d6 h( a2 Y) h2 b{5 {* g/ g, Z! {$ {; c, D9 b) ~
m_pFontList = SDL_DisplayFormatAlpha(IMG_Load(src_file.c_str()));
" b2 w# h8 X' `5 `5 B, ]8 I}
8 }# C- O% G2 }: l - U7 A: [7 M& t6 T- s6 P# r
Lets go loopy!* `* J( ~6 {) N! v  M- x
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:
! |: v( f  w7 L6 i* z; Z0 m5 B+ }
0 j3 L0 y1 ]! u9 c% ^
/ p' W8 O$ U! ]0 P' x6 Iint Font::ShowText(std::string text, int type, int pos_x, int pos_y, SDL_Surface* pScreen)
  }. u% I. A! k# D{# j7 v0 b+ n" o# J+ ~: J
/* TODO: We need to calculate the fonts height into the pos_y thing. */
7 ^& F. z* S- I& ?) Q& k2 c# \2 b // Also, id like to see this stuff gathered from an ini file.
: N2 c3 v5 I" U0 s/ `7 | // That way we can alter fonts without the need for recompilcation" t; [1 h/ t2 Y( K/ x
if(!pScreen) return 1;
, ?" S1 ^: E( L! o9 t0 f' p        // src_rect is the location of the character we need to fetch.
9 J2 s$ O! H; ]4 `8 m        // rect will be the destenation
, i' Z7 G" a0 J8 s SDL_Rect rect, src_rect;
. A$ T# \" O% P4 h rect.x = pos_x;1 m9 x/ P6 f4 M3 L
rect.y = pos_y;: x) t2 E6 D) A  X1 D2 _7 w6 f
SDL_Rect tmp_rect;8 ?5 ]2 L) q9 Q' G: g
tmp_rect.y = 0*type;5 v" {: O, y" b5 L- Q
for(int i=0; i < text.size(); i++) {' W2 n% e0 j" g0 _; o$ _
  tmp_rect.y = 0; // set right y axe0 D* X. ]7 K) s7 `) p% x
  switch(text) {
- l- c1 Y# R% e' x; J0 P+ \   case 0x20:
! d0 R+ W+ Y3 q* a1 S2 s    rect.x += 10;+ q+ e0 B. g- O
    break;
" u# L8 A9 q* X" B; a   case 0x21: // !! C# [" p8 i7 s  s* c: D
    tmp_rect.x = 4;
: Z, q2 c/ Y. u) L& c0 D/ j8 q    tmp_rect.w = 6;
3 j& a. S0 }( J+ R/ U" E    tmp_rect.h = 19;# Q" q3 T  e/ m+ l
    tmp_rect.y = 0;  ]- {% Q% M% g% e) @" b0 z
    SDL_BlitSurface( m_pFontList, &tmp_rect, pScreen, &rect);
/ m# Q- a; Q- B" f' I$ c& c$ ~    rect.x += tmp_rect.w;* p5 F8 D1 _  l/ A: h. s6 A, Q5 M  c
    break;
2 x0 w: R  O' v; o4 k% O $ e$ O7 f; l: \3 M" y$ r* K) Q
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!
; O3 O' M- I3 z# n1 d* K
6 E( P/ D2 O" T2 wyou 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. : q" `6 }; J) S: X+ b
1 l8 ^5 C1 n/ m6 `
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 ! e* g4 T6 H: l) `9 H
7 a9 J5 P! e6 Y" F
Click Here To Download the code sdl-fontfun.tar.gz 6 t& l/ W) g- U9 {' o9 X9 Y
; `4 \! A; d0 s
To compile and run the code:
( R/ C, l) k1 z # s$ _8 S8 [/ W* d* f  l, Q
4 {* Z) n9 N* O) ?
g++ -o sdl-funfonts font.cpp sdlfontsfun.cpp `sdl-config --cflags --libs` -lSDL_image. @0 S1 y* p4 Y. ~8 C
./sdl-funfonts5 ]) c/ ]5 j& F6 V

1 H' a4 o% B1 m: ], f+ }( @. hFinal thoughts
6 k4 u+ k' t8 ?0 M* dThats 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日,是全球华人共同的冒险解谜类游戏家园。我们致力于提供各类冒险游戏资讯供大家学习交流。本站所有资源均不用于商业用途。

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