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

📄 lightcontrol.cs

📁 Beginning C# Game Programming 的源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
			this.Red2.TabIndex = 0;
			this.Red2.ValueChanged += new System.EventHandler(this.Color_TextChanged);
			// 
			// Vertex3
			// 
			this.Vertex3.Controls.Add(this.GroupBox3);
			this.Vertex3.Location = new System.Drawing.Point(4, 24);
			this.Vertex3.Name = "Vertex3";
			this.Vertex3.Size = new System.Drawing.Size(248, 156);
			this.Vertex3.TabIndex = 2;
			this.Vertex3.Text = "Vertex 3";
			this.Vertex3.Visible = false;
			// 
			// GroupBox3
			// 
			this.GroupBox3.Controls.Add(this.lblBlue3);
			this.GroupBox3.Controls.Add(this.Blue3);
			this.GroupBox3.Controls.Add(this.lblGreen3);
			this.GroupBox3.Controls.Add(this.Green3);
			this.GroupBox3.Controls.Add(this.lblRed3);
			this.GroupBox3.Controls.Add(this.Red3);
			this.GroupBox3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.GroupBox3.Location = new System.Drawing.Point(32, 8);
			this.GroupBox3.Name = "GroupBox3";
			this.GroupBox3.Size = new System.Drawing.Size(184, 144);
			this.GroupBox3.TabIndex = 3;
			this.GroupBox3.TabStop = false;
			this.GroupBox3.Text = "Color";
			// 
			// lblBlue3
			// 
			this.lblBlue3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblBlue3.Location = new System.Drawing.Point(8, 106);
			this.lblBlue3.Name = "lblBlue3";
			this.lblBlue3.Size = new System.Drawing.Size(80, 24);
			this.lblBlue3.TabIndex = 5;
			this.lblBlue3.Text = "Blue";
			// 
			// Blue3
			// 
			this.Blue3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Blue3.Increment = new System.Decimal(new int[] {
																	5,
																	0,
																	0,
																	0});
			this.Blue3.Location = new System.Drawing.Point(96, 104);
			this.Blue3.Maximum = new System.Decimal(new int[] {
																  255,
																  0,
																  0,
																  0});
			this.Blue3.Name = "Blue3";
			this.Blue3.Size = new System.Drawing.Size(56, 29);
			this.Blue3.TabIndex = 4;
			this.Blue3.ValueChanged += new System.EventHandler(this.Color_TextChanged);
			// 
			// lblGreen3
			// 
			this.lblGreen3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblGreen3.Location = new System.Drawing.Point(8, 66);
			this.lblGreen3.Name = "lblGreen3";
			this.lblGreen3.Size = new System.Drawing.Size(80, 24);
			this.lblGreen3.TabIndex = 3;
			this.lblGreen3.Text = "Green";
			// 
			// Green3
			// 
			this.Green3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Green3.Increment = new System.Decimal(new int[] {
																	 5,
																	 0,
																	 0,
																	 0});
			this.Green3.Location = new System.Drawing.Point(96, 64);
			this.Green3.Maximum = new System.Decimal(new int[] {
																   255,
																   0,
																   0,
																   0});
			this.Green3.Name = "Green3";
			this.Green3.Size = new System.Drawing.Size(56, 29);
			this.Green3.TabIndex = 2;
			this.Green3.ValueChanged += new System.EventHandler(this.Color_TextChanged);
			// 
			// lblRed3
			// 
			this.lblRed3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblRed3.Location = new System.Drawing.Point(8, 26);
			this.lblRed3.Name = "lblRed3";
			this.lblRed3.Size = new System.Drawing.Size(80, 24);
			this.lblRed3.TabIndex = 1;
			this.lblRed3.Text = "Red";
			// 
			// Red3
			// 
			this.Red3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Red3.Increment = new System.Decimal(new int[] {
																   5,
																   0,
																   0,
																   0});
			this.Red3.Location = new System.Drawing.Point(96, 24);
			this.Red3.Maximum = new System.Decimal(new int[] {
																 255,
																 0,
																 0,
																 0});
			this.Red3.Name = "Red3";
			this.Red3.Size = new System.Drawing.Size(56, 29);
			this.Red3.TabIndex = 0;
			this.Red3.ValueChanged += new System.EventHandler(this.Color_TextChanged);
			// 
			// Vertex4
			// 
			this.Vertex4.Controls.Add(this.GroupBox4);
			this.Vertex4.Location = new System.Drawing.Point(4, 24);
			this.Vertex4.Name = "Vertex4";
			this.Vertex4.Size = new System.Drawing.Size(248, 156);
			this.Vertex4.TabIndex = 3;
			this.Vertex4.Text = "Vertex 4";
			this.Vertex4.Visible = false;
			// 
			// GroupBox4
			// 
			this.GroupBox4.Controls.Add(this.lblBlue4);
			this.GroupBox4.Controls.Add(this.Blue4);
			this.GroupBox4.Controls.Add(this.lblGreen4);
			this.GroupBox4.Controls.Add(this.Green4);
			this.GroupBox4.Controls.Add(this.lblRed4);
			this.GroupBox4.Controls.Add(this.Red4);
			this.GroupBox4.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.GroupBox4.Location = new System.Drawing.Point(32, 8);
			this.GroupBox4.Name = "GroupBox4";
			this.GroupBox4.Size = new System.Drawing.Size(184, 144);
			this.GroupBox4.TabIndex = 3;
			this.GroupBox4.TabStop = false;
			this.GroupBox4.Text = "Color";
			// 
			// lblBlue4
			// 
			this.lblBlue4.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblBlue4.Location = new System.Drawing.Point(8, 106);
			this.lblBlue4.Name = "lblBlue4";
			this.lblBlue4.Size = new System.Drawing.Size(80, 24);
			this.lblBlue4.TabIndex = 5;
			this.lblBlue4.Text = "Blue";
			// 
			// Blue4
			// 
			this.Blue4.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Blue4.Increment = new System.Decimal(new int[] {
																	5,
																	0,
																	0,
																	0});
			this.Blue4.Location = new System.Drawing.Point(96, 104);
			this.Blue4.Maximum = new System.Decimal(new int[] {
																  255,
																  0,
																  0,
																  0});
			this.Blue4.Name = "Blue4";
			this.Blue4.Size = new System.Drawing.Size(56, 29);
			this.Blue4.TabIndex = 4;
			this.Blue4.ValueChanged += new System.EventHandler(this.Color_TextChanged);
			// 
			// lblGreen4
			// 
			this.lblGreen4.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblGreen4.Location = new System.Drawing.Point(8, 66);
			this.lblGreen4.Name = "lblGreen4";
			this.lblGreen4.Size = new System.Drawing.Size(80, 24);
			this.lblGreen4.TabIndex = 3;
			this.lblGreen4.Text = "Green";
			// 
			// Green4
			// 
			this.Green4.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Green4.Increment = new System.Decimal(new int[] {
																	 5,
																	 0,
																	 0,
																	 0});
			this.Green4.Location = new System.Drawing.Point(96, 64);
			this.Green4.Maximum = new System.Decimal(new int[] {
																   255,
																   0,
																   0,
																   0});
			this.Green4.Name = "Green4";
			this.Green4.Size = new System.Drawing.Size(56, 29);
			this.Green4.TabIndex = 2;
			this.Green4.ValueChanged += new System.EventHandler(this.Color_TextChanged);
			// 
			// lblRed4
			// 
			this.lblRed4.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblRed4.Location = new System.Drawing.Point(8, 26);
			this.lblRed4.Name = "lblRed4";
			this.lblRed4.Size = new System.Drawing.Size(80, 24);
			this.lblRed4.TabIndex = 1;
			this.lblRed4.Text = "Red";
			// 
			// Red4
			// 
			this.Red4.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.Red4.Increment = new System.Decimal(new int[] {
																   5,
																   0,
																   0,
																   0});
			this.Red4.Location = new System.Drawing.Point(96, 24);
			this.Red4.Maximum = new System.Decimal(new int[] {
																 255,
																 0,
																 0,
																 0});
			this.Red4.Name = "Red4";
			this.Red4.Size = new System.Drawing.Size(56, 29);
			this.Red4.TabIndex = 0;
			this.Red4.ValueChanged += new System.EventHandler(this.Color_TextChanged);
			// 
			// LightControl
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(256, 182);
			this.Controls.Add(this.TabControl1);
			this.Name = "LightControl";
			this.Text = "LC";
			this.Closing += new System.ComponentModel.CancelEventHandler(this.LightControl_Closing);
			this.TabControl1.ResumeLayout(false);
			this.Vertex1.ResumeLayout(false);
			this.GroupBox1.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.Blue1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Green1)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Red1)).EndInit();
			this.Vertex2.ResumeLayout(false);
			this.GroupBox2.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.Blue2)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Green2)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Red2)).EndInit();
			this.Vertex3.ResumeLayout(false);
			this.GroupBox3.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.Blue3)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Green3)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Red3)).EndInit();
			this.Vertex4.ResumeLayout(false);
			this.GroupBox4.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.Blue4)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Green4)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.Red4)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		//Dispose of any used resources
		public void DisposeD3D() {
			DisposeVertices();
			for (int i = 0; i < numTextures; i++)
				if (textures[i] != null) { 
					textures[i].Dispose();
					textures[i] = null;
				}
			if (device != null) {
				device.Dispose();
				device = null;
			}
		}

		public void DisposeVertices() {
			if (vertBuffer != null) { 
				vertBuffer.Dispose();
				vertBuffer = null;
			}
		}
			 
		public bool InitD3D(IntPtr winHandle) {
			DisplayMode dispMode = 
				Manager.Adapters[Manager.Adapters.Default.Adapter].CurrentDisplayMode;
			PresentParameters presentParams = new PresentParameters();
			//Define the presentation parameters
			presentParams.Windowed = true;
			presentParams.SwapEffect = SwapEffect.Discard;
			presentParams.BackBufferFormat = dispMode.Format;
			try {
				device = new Device(Manager.Adapters.Default.Adapter,
					DeviceType.Hardware, winHandle, 
					CreateFlags.SoftwareVertexProcessing,presentParams);
				device.VertexFormat = customVertex;
				return true;
			}	
			catch {
				return false;
			}
		}


		public bool CreateTextures() {
			try {
				for (int i = 1; i <= numTextures; i++)
					textures[i-1] = TextureLoader.FromFile(device, Application.StartupPath + @"\Walk" + i.ToString() + ".bmp");
				return CreateVertices();
			}
			catch {
				return false;
			}
		}

		public bool CreateVertices() {
			CustomVertex[] verts;

			try {
				//If the vertex buffer was previously created, dispose them
				DisposeVertices();

				vertBuffer = new VertexBuffer(typeof(CustomVertex), numVerts, device, Usage.WriteOnly, customVertex, Pool.Default);
				verts = vertBuffer.Lock(0, 0) as CustomVertex[];
				SquareVertices(verts);
				vertBuffer.Unlock();
				return true;
			}
			catch {
				return false;
			}			
		}
		
		private void SquareVertices(CustomVertex[] vertices) {
			// Create a square, composed of 2 triangles
			vertices[0] = CreateFlexVertex(60, 60, 0, 1, Color.FromArgb((int)Red1.Value, (int)Green1.Value, (int)Blue1.Value), 0, 0);
			vertices[1] = CreateFlexVertex(240, 60, 0, 1, Color.FromArgb((int)Red2.Value, (int)Green2.Value, (int)Blue2.Value), 1, 0);
			vertices[2] = CreateFlexVertex(60, 240, 0, 1, Color.FromArgb((int)Red3.Value, (int)Green3.Value, (int)Blue3.Value), 0, 1);
			vertices[3] = CreateFlexVertex(240, 240, 0, 1, Color.FromArgb((int)Red4.Value, (int)Green4.Value, (int)Blue4.Value), 1, 1);
		}



		private CustomVertex CreateFlexVertex(float X, float Y, float Z, float rhw, Color color, float tu, float tv) {
			CustomVertex custVertex = new CustomVertex();
			custVertex.X = X;
			custVertex.Y = Y;
			custVertex.Z = Z;
			custVertex.rhw = rhw;
			custVertex.color = color.ToArgb();
			custVertex.tu = tu;
			custVertex.tv = tv;
			return custVertex;
		}

		public void Render() {		
			if(device == null) return;

			device.Clear(ClearFlags.Target, Color.FromArgb(0, 0, 255).ToArgb(), 1.0F, 0);
			device.BeginScene();
			// Show one texture a time, in order to create the illusion of a walking guy
			device.SetTexture(0, textures[x]);
			x = (x == 9) ? 0 : x+1; //If x is 9, set to 0, otherwise increment x
			device.SetStreamSource(0, vertBuffer, 0);
			device.DrawPrimitives(PrimitiveType.TriangleStrip, 0, numVerts - 2);
			device.EndScene();
			try {
				device.Present();
			}
			catch {
			}
		}

		private void LightControl_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
			DisposeD3D();
			endTest = true;

		}

		private void Color_TextChanged(object sender, System.EventArgs e) {
			CreateVertices();
		}

	}
}

⌨️ 快捷键说明

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