zhigu 发表于 2006-12-25 10:52:00

XNA帮助文档——2D图形

<p>概述<br/>&nbsp; 图形是直接在屏幕而非在3D空间上绘制,图形通常用来向玩家显示诸如生命槽、命数或者是显示</p><p>分数的文字。一些游戏,尤其是一些老游戏,几乎全部由图形组成。<br/>&nbsp; 要绘制一个图形,你必须要建立一个SpriteBatch对象,通过调用SpriteBatch.Begin()来进行初</p><p>始化,接着为每一个图形调用Draw方法。一个图形的位图信息是从一个Texture对象来取得。这个文</p><p>本可能包含alpha通道信息以此来令部分文本透明或者半透明。你可以通过Draw方法来对图形进行着</p><p>色、旋转和缩放等操作。这个方法也给了你只在屏幕上绘制部分文本的选项。<br/>&nbsp; 绘制图形时最重要的概念是图形原点。这个原点是图形上的一个特殊的点--默认的是图形左上方</p><p>的点(0,0)。Draw方法在屏幕上你所指定的位置绘制图形的原点。当你旋转一个图形,Draw方法</p><p>利用圆点作为旋转的中心。鉴于此,当你要计算在屏幕何处绘制图形的时候,通常利用图形的中心</p><p>点作为原点。<br/>&nbsp; 在绘制任何一个图形之前,你必须调用SpriteBatch.Begin()作为渲染循环的一部分。在绘制完图</p><p>形后,在调用GraphicsDevice.Present()之前先调用End().</p><p>绘制图形<br/>&nbsp; 这篇文章重点讲述如何通过使用SpriteBatch类来绘制图形。<br/>&nbsp; 要绘制图形:<br/>&nbsp; 如How to:Create a Simple Game Loop中所述,从Game类中继承一个类。<br/>&nbsp; 定义一个SpriteBatch对象作为你的game类的一个域。<br/>&nbsp; 如How to:Load Resources中所述,初始化来加载资源。<br/>&nbsp; 在你的LoadResources方法中,构造SpriteBatch对象,passing当前的图形设备。<br/>&nbsp; 在LoadRecoures中加载那些将要在绘制图形中使用的文本。这种情况下,示例利用FromFile来从</p><p>磁盘加载文本。文本位置必须和游戏执行的目录相同。在C# Express中,向项目中添加文本时设置" </p><p>copy if newer"来保证这个texture在运行时可用。</p><p>private Texture2D SpriteTexture;<br/>private SpriteBatch ForegroundBatch;</p><p>protected virtual void LoadResources()<br/>{<br/>&nbsp;&nbsp;&nbsp; //在这里加载资源(文本、网格、声音)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ForegroundBatch = new SpriteBatch( graphics.GraphicsDevice );<br/>&nbsp;&nbsp;&nbsp; SpriteTexture = Texture2D.FromFile( graphics.GraphicsDevice, "sprite.dds" );<br/>}<br/>&nbsp; 调用绘制方法之前,首先调用Clear和BeginScene方法。<br/>&nbsp; BeginScene之后,调用SpriteBatch对象的SpriteBatch.Begin()方法。<br/>&nbsp; 创建一个Vector2来代表图形在屏幕上的位置。<br/>&nbsp; 调用SpriteBatch对象的Draw方法,传递要绘制的文字、屏幕位置以及应用的颜色。用白色绘制文</p><p>字时没有任何色彩效果。<br/>&nbsp; 当所有的图形都绘制完毕,调用SpriteBatch对象的End方法。 <br/>&nbsp; 当所有的图形对象都已经绘制完毕,调用EndScene和GraphicsDevice.Present()。</p><p>protected override void Draw()<br/>{<br/>&nbsp;&nbsp;&nbsp; if (!graphics.EnsureDevice()) return;</p><p>&nbsp;&nbsp;&nbsp; graphics.GraphicsDevice.Clear( Color.CornflowerBlue );<br/>&nbsp;&nbsp;&nbsp; graphics.GraphicsDevice.BeginScene();</p><p>&nbsp;&nbsp;&nbsp; // 在这里添加你的绘图代码<br/>&nbsp;&nbsp;&nbsp; ForegroundBatch.Begin();<br/>&nbsp;&nbsp;&nbsp; Vector2 pos = new Vector2( 0,0 );<br/>&nbsp;&nbsp;&nbsp; ForegroundBatch.Draw( SpriteTexture, pos, Color.White );<br/>&nbsp;&nbsp;&nbsp; ForegroundBatch.End();</p><p>&nbsp;&nbsp;&nbsp; //让GameComponents来进行绘制<br/>&nbsp;&nbsp;&nbsp; DrawComponents();</p><p>&nbsp;&nbsp;&nbsp; graphics.GraphicsDevice.EndScene();<br/>&nbsp;&nbsp;&nbsp; graphics.GraphicsDevice.Present();<br/>}<br/>C#&nbsp; <br/>using System;<br/>using System.Collections.Generic;<br/>using Microsoft.Xna.Framework;<br/>using Microsoft.Xna.Framework.Components;<br/>using Microsoft.Xna.Framework.Graphics;<br/>private Texture2D SpriteTexture;<br/>private SpriteBatch ForegroundBatch;</p><p>protected virtual void LoadResources()<br/>{<br/>&nbsp;&nbsp;&nbsp; //在这里加载资源(文本、网格、声音)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ForegroundBatch = new SpriteBatch( graphics.GraphicsDevice );<br/>&nbsp;&nbsp;&nbsp; SpriteTexture = Texture2D.FromFile( graphics.GraphicsDevice, "sprite.dds" );<br/>}<br/>protected override void Draw()<br/>{<br/>&nbsp;&nbsp;&nbsp; if (!graphics.EnsureDevice()) return;</p><p>&nbsp;&nbsp;&nbsp; graphics.GraphicsDevice.Clear( Color.CornflowerBlue );<br/>&nbsp;&nbsp;&nbsp; graphics.GraphicsDevice.BeginScene();</p><p>&nbsp;&nbsp;&nbsp; // 在这里添加你的绘图代码<br/>&nbsp;&nbsp;&nbsp; ForegroundBatch.Begin();<br/>&nbsp;&nbsp;&nbsp; Vector2 pos = new Vector2( 0,0 );<br/>&nbsp;&nbsp;&nbsp; ForegroundBatch.Draw( SpriteTexture, pos, Color.White );<br/>&nbsp;&nbsp;&nbsp; ForegroundBatch.End();</p><p>&nbsp;&nbsp;&nbsp; // 让GameComponents来进行绘制<br/>&nbsp;&nbsp;&nbsp; DrawComponents();</p><p>&nbsp;&nbsp;&nbsp; graphics.GraphicsDevice.EndScene();<br/>&nbsp;&nbsp;&nbsp; graphics.GraphicsDevice.Present();<br/>}</p><p></p><p>制作动画<br/>&nbsp; 这篇文章重点介绍如何通过一个<br/>&nbsp; 示例中的源代码假定被加载的文字是一系列等大小的图片。在这个例子里,提供的文字都是有4帧</p><p>结构的256x64的文字。<br/>&nbsp; 要实现一个动画:<br/>&nbsp; 遵循How to:Draw a Sprite中的1-4步。<br/>&nbsp; 加载文字或是向动画提供图片数据的文字。在示例中,AnimatedTexture类加载了一个单独的文字</p><p>,并把它分解为动画的帧。</p><p>private AnimatedTexture SpriteTexture;<br/>private SpriteBatch ForegroundBatch;<br/>protected virtual void LoadResources()<br/>{<br/>&nbsp;&nbsp;&nbsp; //TODO: Load Resources (textures, meshes, sounds) here.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; ForegroundBatch = new SpriteBatch( graphics.GraphicsDevice );<br/>&nbsp;&nbsp;&nbsp; SpriteTexture.Load( graphics.GraphicsDevice, "viperanimated.dds", 4 );<br/>}<br/>&nbsp; 为了保证动画的流畅,设定你的游戏循环在固定的间隔内调用更新。在你的OnStarting方法中,</p><p>设定IsFixedTimeStep为真,并为TargetElapsedTime赋值。在示例中,我们设定游戏循环每秒钟调</p><p>用Update 4次。</p><p>protected override void OnStarting()<br/>{<br/>&nbsp;&nbsp;&nbsp; base.OnStarting();<br/>&nbsp;&nbsp;&nbsp; LoadResources();<br/>&nbsp;&nbsp;&nbsp; graphics.DeviceReset += new EventHandler( graphics_DeviceReset );<br/>&nbsp;&nbsp;&nbsp; // Call Update() 4 times per second<br/>&nbsp;&nbsp;&nbsp; IsFixedTimeStep = true;<br/>&nbsp;&nbsp;&nbsp; TargetElapsedTime = new TimeSpan( 2500000 );<br/>}<br/>在你游戏的Update方法中,决定动画的那一帧要进行显示。</p><p>//The time since Update was called last<br/>float elapsed = (float)ElapsedTime.TotalSeconds;</p><p>//在这里添加你的游戏逻辑<br/>CurrentFrame++;<br/>//令当前帧保持在0至3之间<br/>CurrentFrame = CurrentFrame % 3;<br/>利用包含所要求的动画的文字subrectangle来绘制图形。</p><p>protected override void Draw()<br/>{<br/>&nbsp;&nbsp;&nbsp; ...<br/>&nbsp;&nbsp;&nbsp; ForegroundBatch.Begin();<br/>&nbsp;&nbsp;&nbsp; SpriteTexture.DrawFrame( ForegroundBatch, CurrentFrame, Vector2.Zero );<br/>&nbsp;&nbsp;&nbsp; ForegroundBatch.End();<br/>&nbsp;&nbsp;&nbsp; ...<br/>}<br/>public void DrawFrame( SpriteBatch Batch, int Frame, Vector2 screenpos )<br/>{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp; int FrameWidth = TextureInfo.Width / FrameCount;<br/>&nbsp;&nbsp;&nbsp; Rectangle sourcerect = new Rectangle( FrameWidth * Frame, 0,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FrameWidth, TextureInfo.Height );<br/>&nbsp;&nbsp;&nbsp; Batch.Draw( myTexture, screenpos, sourcerect, Color.White );<br/>}<br/>当所有的图形绘制完毕,调用SpriteBatch对象的End方法。<br/>当所有的图形对象绘制完毕,调用EndScene和GraphicsDevice.Present()方法。<br/>C#&nbsp; <br/>public class AnimatedTexture<br/>{<br/>&nbsp;&nbsp;&nbsp; private int FrameCount;<br/>&nbsp;&nbsp;&nbsp; private Texture2D myTexture;<br/>&nbsp;&nbsp;&nbsp; private TextureInformation TextureInfo;</p><p>&nbsp;&nbsp;&nbsp; public void Load( GraphicsDevice device, string filename, int Frames )<br/>&nbsp;&nbsp;&nbsp; {<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FrameCount = Frames;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myTexture = Texture2D.FromFile( device, filename );<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TextureInfo = Texture2D.GetTextureInformation( filename );<br/>&nbsp;&nbsp;&nbsp; }<br/>&nbsp;&nbsp;&nbsp; public void DrawFrame( SpriteBatch Batch, int Frame, Vector2 screenpos )<br/>&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int FrameWidth = TextureInfo.Width / FrameCount;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Rectangle sourcerect = new Rectangle( FrameWidth * Frame, 0,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; FrameWidth, TextureInfo.Height );<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Batch.Draw( myTexture, screenpos, sourcerect, Color.White );<br/>&nbsp;&nbsp;&nbsp; }<br/>}<br/>&nbsp;<br/>制作有图形遮蔽的文件<br/>&nbsp; 这篇文章将要解释如何使用.png或.bmp格式的文件以及DirectX Texture工具来制作一个带遮盖层</p><p>(透明度)的文本。<br/>&nbsp; 利用.png文件来制作透明文本:<br/>&nbsp; 在.png文件编辑器里,创建或者拷贝你所希望用到文本上的图片,并为文件设置透明区域。<br/>&nbsp; 在DirectX Texture 工具中,创建一个和.png文件尺寸相同的新的文本。<br/>&nbsp; 选择一个拥有alpha通道的文本格式,例如A8R8G8B8。<br/>&nbsp; 在DirectX Texture工具中,点击Open Onto This Surface并选择该.png文件。<br/>&nbsp; 最后在DirectX Texture工具中生成的文本现在应含有和.png文件相同的图形以及透明区域。<br/>&nbsp; <br/>&nbsp; 利用.bmp文件来制作透明文本:<br/>&nbsp; 在.bmp文件编辑器(例如Windows画图程序),创建或者拷贝你所希望用到文本上的.bmp图片。<br/>&nbsp; 再创建一个"遮蔽"bmp文件,文本不透明的部分用白色像素,而透明部分用黑色像素。<br/>&nbsp; 在DirectX Texture工具中,创建一个和.bmp文件尺寸相同的新的文本。<br/>&nbsp; 选择一个拥有alpha通道的文本格式,例如A8R8G8B8。<br/>&nbsp; 在DirectX Texture工具中,点击Open Onto This Surface并选择第一个.bmp文件。<br/>&nbsp; 在DirectX Texture工具中,点击Open Onto Alpha Channel Of This Surface并选择第二个.bmp</p><p>文件。<br/>&nbsp; 最后在DirectX Texture工具中生成的文本现在应含有和第一个.bmp文件相同的图形以及和第二个</p><p>.bmp文件相同的透明区域。</p><p></p><p></p><p></p><p></p><p>p.s:不是图形专业的,好多名词可能都不对,见谅。供大家参考修改。。。</p>
页: [1]
查看完整版本: XNA帮助文档——2D图形