冒险解谜游戏中文网 ChinaAVG

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

作者: shane007    时间: 2010-5-11 23:48
标题: 在XNA中显示中文字符
原文
" K- H: U) n0 ehttp://blog.csdn.net/lqr801225/archive/2009/05/26/4218688.aspx
7 r3 t3 P/ _/ n
4 p1 ]- A. V' X* Y) d 7 x1 n, S; @5 ?* c2 m
在XNA中显示中文字符或其它UNICODE字符(非GDI+)4 L5 V' x# d4 \# {! X2 T4 f, l" K7 O
XNA3.0+VS2008SP1下调试通过
- E% X) `7 `' a6 D; X9 u由于XNA内置的DrawString方法并不能输出全角UNICODE字符,只能设定字符的起始和终止的内码,欧洲语系的字元不多,可以一次导入并生成字体,但像亚洲语系这样动辄上千的字元又是全角,好像设计者并没有考虑到这些情况。为了实现字符输出,已经有一些方法,例如利用.net中的GDI+。下面的实现方法是通过生成自定义的文字托管方式来实现的。
+ \) j& n% [) J0 G  M0 V9 Q$ |步骤如下:; U& d  H4 h5 t. W
建立字体文件
2 F( i9 a7 {6 s7 G6 Y字典文件:6 c. O- D+ o8 {
文字处理类:
) ?1 G' q6 D2 \) p+ T
DefaultFontProcessor.cs
using System.IO;% ^, ^/ u" ~0 C: _4 }5 O9 m- W0 I+ r
using Microsoft.Xna.Framework.Content.Pipeline;: e8 B* f/ ]5 h: K/ N. W0 q7 i5 l
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
. ^2 A/ G& h2 n; t2 t* Rusing Microsoft.Xna.Framework.Content.Pipeline.Processors;
' w- {1 I7 g2 U8 o+ D( S6 ]7 Y. F6 P: q; V3 G3 z& e! E1 }
0 g* B+ l" R3 R; X+ ^* c0 W. f
namespace FontProcessors
4 H8 T% m' f7 K{
' X  p, a2 l1 R( w5 w4 w    [ContentProcessor]$ u. O/ {" u- M* f) X
    public class DefaultFontProcessor : FontDescriptionProcessor. P3 E8 T6 R* [( N
    {
, m) Y6 _0 l3 Z- s7 v9 F" b        public override SpriteFontContent Process(FontDescription input, ContentProcessorContext context)2 p; C4 d1 m' j7 X* E
        {1 d0 d1 x9 v# J  j: U3 \
            //载入文件
# n9 m% G9 `4 \' c! v% h0 M            string fullPath = Path.GetFullPath("message.txt");
5 Q# N" C8 i) c6 L3 Q( k            context.AddDependency(fullPath);& c0 N7 q6 X. t8 m+ B8 Y" [( y
            string letters = File.ReadAllText(fullPath, System.Text.Encoding.UTF8);" T% s: H) F6 U& X

5 L( B( O- |" E  ?5 V& W) d9 N8 u! L" S
            //导入字符
3 J% w6 J# Y% Q' ]4 j( X5 R            foreach (char c in letters)
; T/ U6 V. o+ V* C' M/ v            {
% i& ~0 Q; @2 G3 G& b4 _1 u                input.Characters.Add(c);% I  P9 B% V; a1 ?* I& `2 }
            }
2 M# V7 l0 E0 u3 f, x$ @  W/ N: L            return base.Process(input, context);8 s# g( b, y# E8 b
        }* k0 c) K: s9 x) ~$ b. v
    }8 \, ~, Y$ `( p  \0 }# Z" H
}
" p% ]+ X; B- m7 w; Q5 \
* I' i- D4 i* m

# Q$ T& S" N0 d7 P0 [: ]' w4 @9 _$ L! S1 R: `5 h3 v: g1 ]( g
添加项目引用  q6 O6 i3 K- r9 D( |( q
在项目用使用/ r0 E$ ]9 L! E* O8 i5 r
Game1.cs
using System;
. I! ]% K" z1 Yusing System.Collections.Generic;
; F) b' ]3 z4 V4 P7 j: Ausing Microsoft.Xna.Framework;( r6 d8 C' F0 G
using Microsoft.Xna.Framework.Audio;
/ `0 g$ k# R$ Y" ]: k- ousing Microsoft.Xna.Framework.Content;  ?. N  Y) h5 ]
using Microsoft.Xna.Framework.GamerServices;
, e/ T  q# k4 h4 _3 S2 cusing Microsoft.Xna.Framework.Graphics;9 J1 b/ I4 f" t
using Microsoft.Xna.Framework.Input;
# c9 O2 N. B' O" n! K7 c4 T: t9 N) Wusing Microsoft.Xna.Framework.Net;
0 a# i0 d: M9 ?% F. F  ~using Microsoft.Xna.Framework.Storage;
1 R* |$ C  i; ^* a, `( `* m# h. \) P- C! f6 h1 Q% O

1 G: n5 T1 q5 p7 |# b% l' snamespace HelloWorld {9 @( C; ^* ~/ l$ n  P' l
    public class GameMain : Microsoft.Xna.Framework.Game {
4 H8 F% D. w+ ~2 O0 i        private GraphicsDeviceManager graphics;   $ T1 v( Z3 _7 Q5 O3 a
        private SpriteBatch           spriteBatch;3 N, V+ b4 d6 b5 Z4 p9 h
        private SpriteFont            font;      
0 |3 U5 K" t# R5 x2 g* I! a
3 I: E1 E: X5 W3 P5 B% z, P: {# U, \6 E/ N; l4 y+ _
        public GameMain() {
6 l) A- B1 @3 [; f7 W            graphics=new GraphicsDeviceManager(this);
& ]" w( F8 ~* O% j( E9 I3 h# k            Content.RootDirectory="Content";/ S3 b2 d! p+ y6 K8 ?
        }
. D7 q  x* ?/ c, L
. [6 c0 Q7 D  I; |  `; @
* N& c$ Y  x# E0 ]        protected override void Initialize() {
5 l# B& F% d' ~% z            base.Initialize();
1 m; V  V! Z( E" d# X9 a        }
5 A& w* A, o1 M! C' @
5 {& ~' H6 c9 Q; N5 c& C2 B3 C$ E- k
        protected override void LoadContent() {; n' I- l! a3 o6 Y
            spriteBatch=new SpriteBatch(GraphicsDevice);$ K: F) q8 I' k- @
            font=Content.Load<SpriteFont>("DefaultFont");
4 ?3 l2 |+ \  x9 u6 @7 [        }; c  l. z2 _6 [( _( B7 f' d
: [# ]$ k# Q6 [. v% g6 }
& \, s6 m& ~; U7 d) q% t
        protected override void UnloadContent() {( U$ Y! q& I5 l8 ^% t: K! }* I
        }
. ]% n; a$ H! J0 }; b
# b' G7 k4 }% J: L! q" b2 h5 |9 B3 h9 U9 w2 O
        protected override void Update(GameTime gameTime) {
2 n. o- B+ _3 h3 E- R7 T4 o            if (GamePad.GetState(PlayerIndex.One).Buttons.Back==0 J+ g/ |- W- G4 G+ o2 h
                ButtonState.Pressed) {
# @- h; g" d- N. a0 C" V                Exit();% M9 I3 A5 U# `# {/ |$ v$ D% b  w+ P9 r( O
            }2 N# W2 `- ?. e6 q* b! X1 T
: B/ v/ K% o! x+ P' d
. Z5 g9 G7 }8 ]5 Z, o+ v
            base.Update(gameTime);7 G/ B& e( d! `! ^6 K! t4 P7 Z
        }+ k( I* N  I- Y4 |- [* C1 G: J9 g8 y
" @1 `7 S/ @( o) q
- n/ E# w0 ^" B0 b$ w" K
        protected override void Draw(GameTime gameTime) {: y" c2 Q3 ~- B9 Q0 l
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);2 e4 g; z& n/ @, F& c
) B% o8 k3 @7 k1 n
; }. g2 F% v+ d( C( v
            spriteBatch.Begin();
( a) ]' m! G* O2 N: E, e            DrawString("中文输入测试,日本語テストを入力する",50,50);$ V, Y) ^# P) }, c( C$ k% ?. A, p
            spriteBatch.End();
& s6 j- L" G: l9 V
5 G' |) N( W7 M: R7 F
0 n1 O  H. [; I7 [" {            base.Draw(gameTime);6 \. C: F1 Y' e9 W: \
        }. H, x( {8 S9 Y/ x
2 ?( ?2 k/ ], g
/ e# A2 t: L3 c6 q# _1 N/ X# h! F
        private void DrawString(String str,int x,int y) {% _' C, N+ w' F: `8 h/ |* d  w! V, j) V3 }
            spriteBatch.DrawString(font,str,new Vector2(x,y),Color.White);
# o' a, A9 {- R+ e! _        }
  M$ \9 z6 X- N    }* G# W5 Z! L) D: y( h  H8 ^
}
1 L! ?9 v3 B* {: U4 z
0 v& c, p) P3 L. P





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