SpriteExample.cs
上传用户:lslight
上传日期:2022-01-10
资源大小:14248k
文件大小:5k
- ////////////////////////////////////////////////////////////////////////
- // ■■■■ ■■■■■ ■■■■ ■ ■ //
- // ■ ■ ■ ■ ■ //
- // ■ ■ ■ ■■■ ■ ■ //
- // ■ ■ ■ ■ ■ ■ //
- // ■■■■ ■ ■■■■ ■■■■ //
- // Copyright (c) 三峡大学水利与环境学院 肖泽云. All rights reserved. //
- ////////////////////////////////////////////////////////////////////////
- using System;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Windows.Forms;
- using Microsoft.DirectX;
- using Microsoft.DirectX.Direct3D;
- namespace 二维显示
- {
- public partial class SpriteExample : Form
- {
- Device device = null;//定义绘图设备
-
- private Sprite sprite;//定义Sprite对象
- private Texture showPicture;//定义图片对象
- private const int frameCount = 8;//定义动画帧数目
- private int currentFrame=0;//定义当前动画帧
- private System.Windows.Forms.Timer aniTimer;//定义计时器
- private System.Drawing.Font currentFont;//定义字体
- private Microsoft.DirectX.Direct3D.Font myFont;//定义文字对象
- public SpriteExample()
- {
- this.ClientSize = new Size(800, 600);//指定窗体尺寸
- this.Text = "SpriteExample";//指定窗体标题
- }
- public bool InitializeDirect3D()
- {
- try
- {
- PresentParameters presentParams = new PresentParameters();
- presentParams.Windowed = true; //指定以Windows窗体形式显示
- presentParams.SwapEffect = SwapEffect.Discard; //当前屏幕绘制后它将自动从内存中删除
- device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams); //实例化device对象
- sprite = new Sprite(device);//实例化Sprite对象
- string imagePath = @"E:DirectXDirectX_C#Media火焰fire.png";
- showPicture = TextureLoader.FromFile(device, imagePath);
- currentFont = new System.Drawing.Font("Times New Roman", 28, FontStyle.Regular);//设置字体
- myFont = new Microsoft.DirectX.Direct3D.Font(device, currentFont);//创建文字对象
- aniTimer = new Timer();
- aniTimer.Enabled = true;
- aniTimer.Interval = 100;
- aniTimer.Tick += new System.EventHandler(this.aniTimer_Tick);
- return true;
- }
- catch (DirectXException e)
- {
- MessageBox.Show(e.ToString(), "Error"); //处理异常
- return false;
- }
- }
- public void Render()
- {
- if (device == null) //如果device为空则不渲染
- {
- return;
- }
- device.Clear(ClearFlags.Target, Color.DarkSlateBlue, 1.0f, 0); //清除windows界面为深蓝色
- device.BeginScene();
-
- //在此添加渲染图形代码
-
- sprite.Begin(SpriteFlags.AlphaBlend);
- Rectangle originPicRect = new Rectangle(64 * currentFrame, 0, 64, 128);
- sprite.Draw(showPicture, originPicRect, Vector3.Empty, new Vector3(50, 50, 0), Color.White.ToArgb());
- /*
- int spriteX = 10;
- sprite.Begin(SpriteFlags.AlphaBlend);
- sprite.Draw(showPicture, Vector3.Empty, new Vector3(spriteX, 50, 0), Color.White.ToArgb());
- spriteX += showPicture.GetLevelDescription(0).Width + 10;
- sprite.Draw(showPicture, new Rectangle(4, 4, 100, 100), Vector3.Empty, new Vector3(spriteX, 50, 0), Color.Green);
- spriteX += showPicture.GetLevelDescription(1).Width;
- sprite.Draw(showPicture, Rectangle.Empty, Vector3.Empty, new Vector3(spriteX, 50, 0), Color.Yellow);
- */
- //myFont.DrawText(sprite, "Hello,DirectX!", 200, 100, Color.Yellow);
- sprite.End();
- device.EndScene();
- device.Present();
- }
- static void Main()
- {
- SpriteExample SpriteExample = new SpriteExample(); //创建窗体对象
- if (SpriteExample.InitializeDirect3D() == false) //检查Direct3D是否启动
- {
- MessageBox.Show("无法启动Direct3D!", "错误!");
- return;
- }
- SpriteExample.Show(); //如果一切都初始化成功,则显示窗体
- SpriteExample.aniTimer.Enabled = true;
- while (SpriteExample.Created) //设置一个循环用于实时更新渲染状态
- {
- SpriteExample.Render(); //保持device渲染,直到程序结束
- Application.DoEvents(); //处理键盘鼠标等输入事件
- }
- }
- private void aniTimer_Tick(object sender, EventArgs e)
- {
- if (currentFrame >= frameCount-1)
- {
- currentFrame = 0;
- }
- else
- {
- currentFrame++;
- }
- }
- }
- }