冒险解谜游戏中文网 ChinaAVG

标题: 在XNA中显示中文字符 [打印本页]

作者: shane007    时间: 2010-5-11 23:48
标题: 在XNA中显示中文字符
原文! n. a% }+ Z) J2 c7 U0 w
http://blog.csdn.net/lqr801225/archive/2009/05/26/4218688.aspx" m! u5 f) x( k9 ]; q* M- q
' P1 \* Y0 [5 B- e+ s8 e

8 N7 s( X3 `( @0 J  \6 l1 h7 Q! v- ]在XNA中显示中文字符或其它UNICODE字符(非GDI+)
8 i4 z, u- v2 EXNA3.0+VS2008SP1下调试通过
1 Z4 l) b+ i5 J4 y, h# I1 k由于XNA内置的DrawString方法并不能输出全角UNICODE字符,只能设定字符的起始和终止的内码,欧洲语系的字元不多,可以一次导入并生成字体,但像亚洲语系这样动辄上千的字元又是全角,好像设计者并没有考虑到这些情况。为了实现字符输出,已经有一些方法,例如利用.net中的GDI+。下面的实现方法是通过生成自定义的文字托管方式来实现的。2 R9 v. R: P! E" i- Q% R5 ^, m
步骤如下:2 H1 B, t( K  N
建立字体文件% _) X  `4 r! z) n6 f  P2 `( E
字典文件:
# u* n) l# |8 b- v* ~' f文字处理类:
1 H4 k; g( u5 J! g+ Q
DefaultFontProcessor.cs
using System.IO;
: q# S$ ~! S9 C+ s  s% F% r+ Vusing Microsoft.Xna.Framework.Content.Pipeline;6 P4 r: M3 v9 Z% F. Y
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
1 b9 b0 u! z- gusing Microsoft.Xna.Framework.Content.Pipeline.Processors;
( z$ w% C6 b0 z4 t& J; d! R0 k5 l6 y" X6 L

/ H- v! ^1 x: V5 Z- Qnamespace FontProcessors# g6 m6 G0 K+ Q5 M
{
/ X! g& U$ o5 O7 M. H* |5 V6 s    [ContentProcessor]
) m' W/ Z. o# q+ w+ A    public class DefaultFontProcessor : FontDescriptionProcessor8 K/ n/ L, ]$ g# f& Z( Y! ]
    {* a, h. z3 M5 r$ b! d
        public override SpriteFontContent Process(FontDescription input, ContentProcessorContext context)( f  d' Y' x; y$ \2 Y- ~
        {
/ h. ^8 K/ P9 l7 `            //载入文件
) b- v( O4 c$ w# z* ?9 L- N            string fullPath = Path.GetFullPath("message.txt");
+ ]/ D% j& Z+ Z- T/ f# F/ y            context.AddDependency(fullPath);( b2 ^+ m. ?: T: m, z+ q, K# M
            string letters = File.ReadAllText(fullPath, System.Text.Encoding.UTF8);
6 ^5 s8 Y  u( N6 J$ O# p* z, a0 Q: q' |4 i

" m0 w6 E) Y2 s6 \4 `  c            //导入字符
- m+ g1 M/ E$ A5 Y; f            foreach (char c in letters)
  B5 Z3 ^0 N( Z& ~            {
8 z* u: C  x$ z2 f6 g                input.Characters.Add(c);; q! A4 L2 H+ }& m
            }+ B6 u: K6 W* C% G
            return base.Process(input, context);
8 }% r5 _7 [0 u! W' t$ n* p3 q7 l        }
" }3 a% P. ^# X3 E7 m& @" X: n7 ^    }
" N6 O% v3 U0 G" s' y" V}
9 O4 [0 n+ }3 A6 O- }$ T$ y7 s5 l: z0 f! z

! q8 M- x: S8 q! E
- I& c& c+ c  A: j  t8 z5 Y添加项目引用
& [+ z7 |( Y1 E$ @2 O在项目用使用
' |. Q& C; G% e2 q: v
Game1.cs
using System;) T( e) o6 d6 S3 c: g6 U( S
using System.Collections.Generic;
9 y% S6 ^! U, \6 R7 X' j5 ]) dusing Microsoft.Xna.Framework;" a8 }4 p' u# D9 f/ y- e
using Microsoft.Xna.Framework.Audio;
" B9 a! G& E7 R# u2 N; D. h  Tusing Microsoft.Xna.Framework.Content;3 ?" Y  e( `0 l, {: O' t$ G5 i
using Microsoft.Xna.Framework.GamerServices;
' E* o9 \+ B; z- ?; zusing Microsoft.Xna.Framework.Graphics;
; ?8 f- S& W5 ~/ T- pusing Microsoft.Xna.Framework.Input;6 b% T1 q* w9 H: r0 n
using Microsoft.Xna.Framework.Net;
' K. w* j- C0 b( @using Microsoft.Xna.Framework.Storage;
7 h. B) |8 `9 x6 i+ l* I( v. y
2 w1 N1 P( j; `% O; O5 m  F& y1 \% x8 r, O
namespace HelloWorld {+ G1 I& q% x8 v# ]& S' k' l
    public class GameMain : Microsoft.Xna.Framework.Game {
$ n$ C" J. i2 k$ _8 k& p3 A        private GraphicsDeviceManager graphics;   1 H7 C4 R9 M9 g
        private SpriteBatch           spriteBatch;7 U! L' f% Z% x& J0 r
        private SpriteFont            font;         c" b  l( e: Q" x

/ L8 G4 z4 e! k. P4 m
, c; z, |; J1 H        public GameMain() {
0 O2 d1 M: g: a6 {( q5 A2 ?, a9 n2 r            graphics=new GraphicsDeviceManager(this);
4 M7 M. C  Z% ]            Content.RootDirectory="Content";
% D1 [1 Z0 i+ }) q$ k* c( A% j0 n        }+ C8 ]. a' f* }
0 n, g3 N) ~. Z7 o# ~

6 l6 U: r3 ], ~0 `$ z        protected override void Initialize() {
: c3 d% w% T9 [/ P  Z            base.Initialize();, Y6 y9 J! H0 a* I
        }$ }+ |$ A# B% e6 o' y" p5 N

5 j/ K9 N8 ]# e: t5 b
5 C; d% a; a; h8 t6 c6 B) y% O        protected override void LoadContent() {
% t; a9 X, e3 i- H  L            spriteBatch=new SpriteBatch(GraphicsDevice);- S( u* d; \8 a7 ]; w" h9 f
            font=Content.Load<SpriteFont>("DefaultFont");
2 A6 U4 S3 W$ f9 g. j" k, W+ b        }
8 J" j2 A" A& h' K* D+ t) J& k0 x4 t" o0 @' R

+ V/ C0 F/ b( W        protected override void UnloadContent() {# [7 V. _4 Q8 ^
        }9 j8 h+ ~* e/ K  m( r
* s/ I) C! D3 T2 M% K4 @

2 {) G; e4 ?5 V% s) B# q3 ^, K. y        protected override void Update(GameTime gameTime) {! d+ C& ?5 C$ a: N9 U5 P
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back==  A6 c; O5 V# D: X$ J, d
                ButtonState.Pressed) {
( S  o3 b! F0 f, s( Z                Exit();
- N5 f4 r/ K+ R# D& Z8 _. Q            }! U9 y8 J$ y& S% F1 P4 e

) ]* s* s  N$ @  x0 x( I; f
1 x2 x4 R+ |' p* {6 J* O3 D            base.Update(gameTime);% M* ~/ p$ S) Z& o. k+ g! u5 O
        }
8 w7 q2 S- Y; k% y) @, }% Z4 M3 _3 T, C+ O6 I
" C7 I+ ]1 }5 m. x7 P7 ], y, H
        protected override void Draw(GameTime gameTime) {$ q; E+ L! t+ @( ?4 b# [; ]9 i
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
8 G& w4 W, h7 X
) s# i9 X6 E2 X6 }4 Q  R: I; ^  J  j+ I: h
            spriteBatch.Begin();/ `( f! y. f' _+ Y) \! s
            DrawString("中文输入测试,日本語テストを入力する",50,50);
5 |! }1 \3 L: V* W            spriteBatch.End();# j! d  U% P9 z3 H" b
- K- Q! c  A, O& F  ?: q
. d/ L* ]/ G; R$ S2 j3 x9 m" a2 K; D
            base.Draw(gameTime);% G! F$ Z  v2 V3 e/ r; U
        }
5 z- f7 }9 d5 ]. i' `: |/ D# U; `$ Q0 O9 V
2 t9 e1 g" k; ]; t4 l9 p
        private void DrawString(String str,int x,int y) {
0 i# }8 H+ d8 g5 Y( {4 l            spriteBatch.DrawString(font,str,new Vector2(x,y),Color.White);3 z9 u/ f- J3 A5 N5 G6 l
        }( r! G* M/ R5 x! l4 W
    }
3 M$ A- m4 K( _( }) [+ S2 ]}
$ p% O' R, i& s
; p" Z( V5 @9 {* h& T8 E





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