SpriteExample.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:5k
源码类别:

DirextX编程

开发平台:

C#

  1. ////////////////////////////////////////////////////////////////////////
  2. //      ■■■■     ■■■■■       ■■■■       ■       ■      //
  3. //    ■                 ■         ■               ■       ■      //
  4. //    ■                 ■         ■    ■■■     ■       ■      //
  5. //    ■                 ■         ■       ■      ■       ■      //
  6. //      ■■■■         ■           ■■■■         ■■■■       //
  7. // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved.  //
  8. ////////////////////////////////////////////////////////////////////////
  9. using System;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Windows.Forms;
  14. using Microsoft.DirectX;
  15. using Microsoft.DirectX.Direct3D;
  16. namespace 二维显示
  17. {
  18.     public partial class SpriteExample : Form
  19.     {
  20.         Device device = null;//定义绘图设备
  21.                 
  22.         private Sprite sprite;//定义Sprite对象
  23.         private Texture showPicture;//定义图片对象
  24.         private const int frameCount = 8;//定义动画帧数目
  25.         private int currentFrame=0;//定义当前动画帧
  26.         private System.Windows.Forms.Timer aniTimer;//定义计时器
  27.         private System.Drawing.Font currentFont;//定义字体
  28.         private Microsoft.DirectX.Direct3D.Font myFont;//定义文字对象
  29.         public SpriteExample()
  30.         {
  31.             this.ClientSize = new Size(800, 600);//指定窗体尺寸
  32.             this.Text = "SpriteExample";//指定窗体标题
  33.         }
  34.         public bool InitializeDirect3D()
  35.         {
  36.             try
  37.             {
  38.                 PresentParameters presentParams = new PresentParameters();
  39.                 presentParams.Windowed = true; //指定以Windows窗体形式显示
  40.                 presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
  41.                 device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
  42.                 sprite = new Sprite(device);//实例化Sprite对象
  43.                 string imagePath = @"E:DirectXDirectX_C#Media火焰fire.png";
  44.                 showPicture = TextureLoader.FromFile(device, imagePath);
  45.                 currentFont = new System.Drawing.Font("Times New Roman", 28, FontStyle.Regular);//设置字体
  46.                 myFont = new Microsoft.DirectX.Direct3D.Font(device, currentFont);//创建文字对象
  47.                 aniTimer = new Timer();
  48.                 aniTimer.Enabled = true;
  49.                 aniTimer.Interval = 100;
  50.                 aniTimer.Tick += new System.EventHandler(this.aniTimer_Tick);
  51.                 return true;
  52.             }
  53.             catch (DirectXException e)
  54.             {
  55.                 MessageBox.Show(e.ToString(), "Error"); //处理异常
  56.                 return false;
  57.             }
  58.         }
  59.         public void Render()
  60.         {
  61.             if (device == null)   //如果device为空则不渲染
  62.             {
  63.                 return;
  64.             }
  65.             device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0);  //清除windows界面为深蓝色
  66.             device.BeginScene();
  67.                         
  68.             //在此添加渲染图形代码
  69.             
  70.             sprite.Begin(SpriteFlags.AlphaBlend);
  71.             Rectangle originPicRect = new Rectangle(64 * currentFrame, 0, 64, 128);
  72.             sprite.Draw(showPicture, originPicRect, Vector3.Empty, new Vector3(50, 50, 0), Color.White.ToArgb());
  73.             /*
  74.             int spriteX = 10;
  75.             sprite.Begin(SpriteFlags.AlphaBlend);
  76.             sprite.Draw(showPicture, Vector3.Empty, new Vector3(spriteX, 50, 0), Color.White.ToArgb());
  77.             spriteX += showPicture.GetLevelDescription(0).Width + 10;
  78.             sprite.Draw(showPicture, new Rectangle(4, 4, 100, 100), Vector3.Empty, new Vector3(spriteX, 50, 0), Color.Green);
  79.             spriteX += showPicture.GetLevelDescription(1).Width;
  80.             sprite.Draw(showPicture, Rectangle.Empty, Vector3.Empty, new Vector3(spriteX, 50, 0), Color.Yellow);
  81.             */
  82.             //myFont.DrawText(sprite, "Hello,DirectX!", 200, 100, Color.Yellow);
  83.             sprite.End();
  84.             device.EndScene();            
  85.             device.Present();           
  86.         }
  87.         static void Main()
  88.         {
  89.             SpriteExample SpriteExample = new SpriteExample(); //创建窗体对象
  90.             if (SpriteExample.InitializeDirect3D() == false) //检查Direct3D是否启动
  91.             {
  92.                 MessageBox.Show("无法启动Direct3D!", "错误!");
  93.                 return;
  94.             }
  95.             SpriteExample.Show(); //如果一切都初始化成功,则显示窗体
  96.             SpriteExample.aniTimer.Enabled = true;
  97.             while (SpriteExample.Created) //设置一个循环用于实时更新渲染状态
  98.             {
  99.                 SpriteExample.Render(); //保持device渲染,直到程序结束
  100.                 Application.DoEvents(); //处理键盘鼠标等输入事件
  101.             }
  102.         }
  103.         private void aniTimer_Tick(object sender, EventArgs e)
  104.         {
  105.             if (currentFrame >= frameCount-1)
  106.             {
  107.                 currentFrame = 0;
  108.             }
  109.             else
  110.             {
  111.                 currentFrame++;
  112.             }
  113.         }
  114.     }
  115. }