⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 C#游戏编程第三章
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Textures
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
        private Device device = null;
        private VertexBuffer vb = null;
        private Texture tex = null;
        private Texture tex1 = null;
        private Texture tex2 = null;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
        private float angle = 0.0f;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);
		}

        /// <summary>
        /// We will initialize our graphics device here
        /// </summary>
        public void InitializeGraphics()
        {
            // Set our presentation parameters
            PresentParameters presentParams = new PresentParameters();

            presentParams.Windowed = true;
            presentParams.SwapEffect = SwapEffect.Discard;

            // Create our device
            device = new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);

            vb = new VertexBuffer(typeof(CustomVertex.PositionTextured), 36, device, Usage.Dynamic | Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default);
            vb.Created += new EventHandler(this.OnVertexBufferCreate);
            OnVertexBufferCreate(vb, null);

            tex = new Texture(device, new Bitmap(this.GetType(), "puck.bmp"), Usage.Dynamic, Pool.Default);
            tex1 = new Texture(device, new Bitmap(this.GetType(), "banana.bmp"), Usage.Dynamic, Pool.Default);
            tex2 = new Texture(device, new Bitmap(this.GetType(), "ground.bmp"), Usage.Dynamic, Pool.Default);
        }

        private void OnVertexBufferCreate(object sender, EventArgs e)
        {
            VertexBuffer buffer = (VertexBuffer)sender;

            CustomVertex.PositionTextured[] verts = new CustomVertex.PositionTextured[36];

            // Front face
            verts[0] = new CustomVertex.PositionTextured(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f);
            verts[1] = new CustomVertex.PositionTextured(-1.0f, -1.0f, 1.0f, 0.0f, 1.0f);
            verts[2] = new CustomVertex.PositionTextured(1.0f, 1.0f, 1.0f, 1.0f, 0.0f);
            verts[3] = new CustomVertex.PositionTextured(-1.0f, -1.0f, 1.0f, 0.0f, 1.0f);
            verts[4] = new CustomVertex.PositionTextured(1.0f, -1.0f, 1.0f, 1.0f, 1.0f);
            verts[5] = new CustomVertex.PositionTextured(1.0f, 1.0f, 1.0f, 1.0f, 0.0f);

            // Back face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            verts[6] = new CustomVertex.PositionTextured(-1.0f, 1.0f, -1.0f, 0.0f, 0.0f);
            verts[7] = new CustomVertex.PositionTextured(1.0f, 1.0f, -1.0f, 1.0f, 0.0f);
            verts[8] = new CustomVertex.PositionTextured(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f);
            verts[9] = new CustomVertex.PositionTextured(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f);
            verts[10] = new CustomVertex.PositionTextured(1.0f, 1.0f, -1.0f, 1.0f, 0.0f);
            verts[11] = new CustomVertex.PositionTextured(1.0f, -1.0f, -1.0f, 1.0f, 1.0f);

            // Top face
            verts[12] = new CustomVertex.PositionTextured(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f);
            verts[13] = new CustomVertex.PositionTextured(1.0f, 1.0f, -1.0f, 1.0f, 1.0f);
            verts[14] = new CustomVertex.PositionTextured(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f);
            verts[15] = new CustomVertex.PositionTextured(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f);
            verts[16] = new CustomVertex.PositionTextured(1.0f, 1.0f, 1.0f, 1.0f, 0.0f);
            verts[17] = new CustomVertex.PositionTextured(1.0f, 1.0f, -1.0f, 1.0f, 1.0f);

            // Bottom face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            verts[18] = new CustomVertex.PositionTextured(-1.0f, -1.0f, 1.0f, 0.0f, 0.0f);
            verts[19] = new CustomVertex.PositionTextured(-1.0f, -1.0f, -1.0f, 0.0f, 1.0f);
            verts[20] = new CustomVertex.PositionTextured(1.0f, -1.0f, -1.0f, 1.0f, 1.0f);
            verts[21] = new CustomVertex.PositionTextured(-1.0f, -1.0f, 1.0f, 0.0f, 0.0f);
            verts[22] = new CustomVertex.PositionTextured(1.0f, -1.0f, -1.0f, 1.0f, 1.0f);
            verts[23] = new CustomVertex.PositionTextured(1.0f, -1.0f, 1.0f, 1.0f, 0.0f);

            // Left face
            verts[24] = new CustomVertex.PositionTextured(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f);
            verts[25] = new CustomVertex.PositionTextured(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f);
            verts[26] = new CustomVertex.PositionTextured(-1.0f, -1.0f, 1.0f, 1.0f, 0.0f);
            verts[27] = new CustomVertex.PositionTextured(-1.0f, 1.0f, -1.0f, 0.0f, 1.0f);
            verts[28] = new CustomVertex.PositionTextured(-1.0f, -1.0f, -1.0f, 1.0f, 1.0f);
            verts[29] = new CustomVertex.PositionTextured(-1.0f, 1.0f, 1.0f, 0.0f, 0.0f);

            // Right face (remember this is facing *away* from the camera, so vertices should be clockwise order)
            verts[30] = new CustomVertex.PositionTextured(1.0f, 1.0f, 1.0f, 0.0f, 0.0f);
            verts[31] = new CustomVertex.PositionTextured(1.0f, -1.0f, 1.0f, 1.0f, 0.0f);
            verts[32] = new CustomVertex.PositionTextured(1.0f, -1.0f, -1.0f, 1.0f, 1.0f);
            verts[33] = new CustomVertex.PositionTextured(1.0f, 1.0f, -1.0f, 0.0f, 1.0f);
            verts[34] = new CustomVertex.PositionTextured(1.0f, 1.0f, 1.0f, 0.0f, 0.0f);
            verts[35] = new CustomVertex.PositionTextured(1.0f, -1.0f, -1.0f, 1.0f, 1.0f);

            buffer.SetData(verts, 0, LockFlags.None);
        }

        private void SetupCamera()
        {
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4, this.Width / this.Height, 1.0f, 100.0f);
            device.Transform.View = Matrix.LookAtLH(new Vector3(0,0, 18.0f), new Vector3(), new Vector3(0,1,0));
            device.RenderState.Lighting = false;
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            device.Clear(ClearFlags.Target, Color.CornflowerBlue, 1.0f, 0);

            SetupCamera();

            device.BeginScene();
            device.VertexFormat = CustomVertex.PositionTextured.Format;
            device.SetStreamSource(0, vb, 0);

            // Draw our boxes
            DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f, 0.0f, 0.0f, 0.0f, tex);
            DrawBox(angle / (float)Math.PI, angle / (float)Math.PI / 2.0f, angle / (float)Math.PI * 4.0f, 5.0f, 0.0f, 0.0f, tex1);
            DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 4.0f, angle / (float)Math.PI / 2.0f, -5.0f, 0.0f, 0.0f, tex2);

            DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f, 0.0f, -5.0f, 0.0f, tex1);
            DrawBox(angle / (float)Math.PI, angle / (float)Math.PI / 2.0f, angle / (float)Math.PI * 4.0f, 5.0f, -5.0f, 0.0f, tex2);
            DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 4.0f, angle / (float)Math.PI / 2.0f, -5.0f, -5.0f, 0.0f, tex);

            DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 2.0f, angle / (float)Math.PI / 4.0f, 0.0f, 5.0f, 0.0f, tex2);
            DrawBox(angle / (float)Math.PI, angle / (float)Math.PI / 2.0f, angle / (float)Math.PI * 4.0f, 5.0f, 5.0f, 0.0f, tex);
            DrawBox(angle / (float)Math.PI, angle / (float)Math.PI * 4.0f, angle / (float)Math.PI / 2.0f, -5.0f, 5.0f, 0.0f, tex1);

            device.EndScene();

            device.Present();

            this.Invalidate();
        }

        private void DrawBox(float yaw, float pitch, float roll, float x, float y, float z, Texture t)
        {
            angle += 0.01f;

            device.Transform.World = Matrix.RotationYawPitchRoll(yaw, pitch, roll) * Matrix.Translation(x, y, z);
            device.SetTexture(0, t);
            device.DrawPrimitives(PrimitiveType.TriangleList, 0, 12);
        }

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			this.Size = new Size(800,600);
			this.Text = "Form1";
		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
        static void Main() 
        {
            using (Form1 frm = new Form1())
            {
                // Show our form and initialize our graphics engine
                frm.Show();
                frm.InitializeGraphics();
                Application.Run(frm);
            }
        }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -