zoukankan      html  css  js  c++  java
  • 梦想成真 XNA (2) 绘制精灵,绘制文字

    [索引页]
    [源码下载] 


    梦想成真 XNA (2) - 绘制精灵,绘制文字



    作者:webabcd


    介绍
    XNA: 绘制一个精灵,绘制一段文字


    示例
    1、绘制精灵的 Demo(按键盘 B 键加载此 Demo)
    Component/Sprite/DrawDemo.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
    
    namespace XNA.Component.Sprite
    {
        public class DrawDemo : Microsoft.Xna.Framework.DrawableGameComponent
        {
            // 精灵绘制器
            SpriteBatch _spriteBatch;
    
            // 2D 纹理对象
            Texture2D _texture2D;
    
            public DrawDemo(Game game)
                : base(game)
            {
                
            }
    
            public override void Initialize()
            {
                base.Initialize();
            }
    
            protected override void LoadContent()
            {
                _spriteBatch = new SpriteBatch(Game.GraphicsDevice);
                _texture2D = Game.Content.Load<Texture2D>("Image/Son");
            }
    
            public override void Update(GameTime gameTime)
            {
                base.Update(gameTime);
            }
    
            public override void Draw(GameTime gameTime)
            {
                Game.GraphicsDevice.Clear(Color.CornflowerBlue);
    
                _spriteBatch.Begin();
    
                /*
                 * Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) - 绘制一个纹理
                 *     Texture2D texture - 需要绘制的纹理对象
                 *     Vector2 position - 需要绘制的对象的坐标,以左上角为原点
                 *     Rectangle? sourceRectangle - 指定纹理上的一个矩形区域,实际绘制时仅绘制这个矩形区域中的纹理
                 *     Color color - 将需要绘制的纹理对象以指定的颜色进行染色渲染,如果不需做任何染色处理则为 Color.White
                 *     float rotation - 旋转纹理对象,单位是“弧度”
                 *     Vector2 origin - 指定纹理对象的原点(默认为 Vector2.Zero)
                 *     float scale - 缩放比例(还有一个重载方法中,此参数的类型是 Vector2,同样用来设置缩放比例)
                 *     SpriteEffects effects - 纹理的翻转类型
                 *         SpriteEffects.None - 不做处理
                 *         SpriteEffects.FlipHorizontally - 水平翻转
                 *         SpriteEffects.FlipVertically - 垂直翻转
                 *     float layerDepth - 层深度(0 - 1 之间),相当于 z-index
                 * 
                 * Draw(Texture2D texture, Rectangle destinationRectangle, Color color) - 绘制一个纹理
                 *     Rectangle destinationRectangle - 将需要绘制的纹理对象缩放到指定的矩形区域
                 */
    
                // 再 0,0 点绘制一个图像
                _spriteBatch.Draw(_texture2D, Vector2.Zero, Color.White);
    
                // 再 10,10 点绘制一个图像,该图像的宽为原始图像的 1/2,高为原始图像的 1/3
                _spriteBatch.Draw(_texture2D, new Rectangle(10, 10, _texture2D.Width / 2, _texture2D.Height / 3), Color.Green);
    
                // 绘制原始图像的上半部分
                _spriteBatch.Draw(_texture2D, new Vector2(_texture2D.Width, 0), new Rectangle(0, 0, _texture2D.Width, _texture2D.Height / 2), Color.Red);
    
                // 将纹理放大 1.5 倍,并垂直翻转
                _spriteBatch.Draw(_texture2D, new Vector2(_texture2D.Width * 2, 0), null, Color.Yellow, 0f, Vector2.Zero, 1.5f, SpriteEffects.FlipVertically, 0f);
    
                // 将纹理的中心点设置为原点(此时 0,0 点表现为纹理的中心点,而不是左上角点),顺时针旋转 45 度
                _spriteBatch.Draw(_texture2D, new Vector2(_texture2D.Width / 2, _texture2D.Height + _texture2D.Height / 2), null, Color.Blue, (float)(Math.PI / 180) * 45, new Vector2(_texture2D.Width / 2, _texture2D.Height / 2), 1f, SpriteEffects.None, 0f);
    
                _spriteBatch.End();
    
                base.Update(gameTime);
            }
        }
    }
    

     

    2、绘制文字的 Demo(按键盘 C 键加载此 Demo)
    XNAContent/SpriteFont/Menu.spritefont

    <?xml version="1.0" encoding="utf-8"?>
    <!-- 
        字体文件实际上就是一个 XML 配置文件,编译后会根据此配置将指定的字体编译成 .xnb 二进制文件
    -->
    <XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
        <Asset Type="Graphics:FontDescription">
            <!--字体名称-->
            <FontName>Arial</FontName>
            <!--字大小-->
            <Size>32</Size>
            <!--字间距-->
            <Spacing>2</Spacing>
            <!--是否使用字距调整(Kerning)。什么是 Kerning 请参考字体基础知识-->
            <UseKerning>true</UseKerning>
            <!--字体样式:Bold, Italic, Regular-->
            <Style>Bold</Style>
            <!--用户输入的字符不在指定的字符区间内的话,则使用此字符代替-->
            <DefaultCharacter>?</DefaultCharacter>
            
            <!--
                将指定区间内的字符交由 Content Pipeline 管理
            -->
            <CharacterRegions>
                <CharacterRegion>
                    <!--
                        unicode 表示的中文编码区间如下,不过我的电脑编译时死机了
                        <Start>一</Start>
                        <End>龥</End>
                    -->
                    
                    <!--
                        将 ASCII(32-126)中的字符都编译进 .xnb 二进制文件,即从“空格”到“~”之间的所有字符,参见 ASCII码表
                    -->
                    <Start> </Start>
                    <End>~</End>
                </CharacterRegion>
            </CharacterRegions>
        </Asset>
    </XnaContent>
    


    Component/Sprite/DrawString.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
    
    namespace XNA.Component.Sprite
    {
        public class DrawString : Microsoft.Xna.Framework.DrawableGameComponent
        {
            // 精灵绘制器
            SpriteBatch _spriteBatch;
    
            // 文字纹理对象
            SpriteFont _spriteFont;
    
            public DrawString(Game game)
                : base(game)
            {
    
            }
    
            public override void Initialize()
            {
                base.Initialize();
            }
    
            protected override void LoadContent()
            {
                _spriteBatch = new SpriteBatch(Game.GraphicsDevice);
    
                // 加载字体配置文件 SpriteFont/Menu 到 SpriteFont 对象中。字体配置文件的详细信息参见 XNAContent/SpriteFont/Menu.spritefont
                _spriteFont = Game.Content.Load<SpriteFont>("SpriteFont/Menu");
            }
    
            public override void Update(GameTime gameTime)
            {
                base.Update(gameTime);
            }
    
            public override void Draw(GameTime gameTime)
            {
                Game.GraphicsDevice.Clear(Color.CornflowerBlue);
    
                _spriteBatch.Begin();
    
                /*
                 * DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color) - 绘制一个文字纹理
                 * DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)  - 绘制一个文字纹理
                 * DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) - 绘制一个文字纹理
                 * 
                 * 各个参数的详细说明参见 Component/Sprite/DrawDemo.cs 中的示例
                 */
    
                // 中文会用问号代替,因为在字体配置文件中指定了 <DefaultCharacter>?</DefaultCharacter>
                _spriteBatch.DrawString(_spriteFont, "Hello DrawString 中文会用问号代替", Vector2.Zero, Color.Red);
                
                _spriteBatch.End();
    
                base.Update(gameTime);
            }
        }
    }
    



    OK 
    [源码下载]

  • 相关阅读:
    iOS崩溃报告获取一
    GCDTimer
    Runtime
    Socket
    冒泡排序笔记
    学习java虚拟机笔记
    ftp发送文件包括中文名
    java email
    批量数据插入高效 转发
    读取本地硬盘文件,快速扫描插入 数据库
  • 原文地址:https://www.cnblogs.com/webabcd/p/2091062.html
Copyright © 2011-2022 走看看