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

📄 frmdirectx.cs

📁 Beginning C# Game Programming 的源代码
💻 CS
📖 第 1 页 / 共 2 页
字号:
		public void ListAdapters() {
			// Add each adapter to the LstAdapters listbox 
			foreach(AdapterInformation info in Manager.Adapters) {
				AdaptersListBox.Items.Add(info.Information.Description);
			}
			// Select the first availiable index, in order to fire the change event 
			AdaptersListBox.SelectedIndex = 0;
		}

		public void ListDevices(int adapter) {
			Caps deviceCaps = Manager.GetDeviceCaps(adapter, DeviceType.Reference);

			// Add each supported device to the DevicesListBox listbox 
			DevicesListBox.Items.Clear();
			// The Reference Rasterizer will always be supported
			DevicesListBox.Items.Add("Reference Rasterizer (REF)");

			// If there's no error when getting the HAL capabilities, 
			// then we have a hardware acceleration board installed
			try {
				deviceCaps = Manager.GetDeviceCaps(adapter, DeviceType.Hardware);
				DevicesListBox.Items.Add("Hardware Acceleration (HAL)");
			}
			catch {
			}
			// Select the first available index, in order to fire the change event 
			DevicesListBox.SelectedIndex = 0;
		}

		private void FrmDirectX_Load(object sender, System.EventArgs e) {
			// Fill the Adapters list
			ListAdapters();
		}

		private void FrmDirectX_Closing(object sender, CancelEventArgs e) {

			if(device != null) {
				device.Dispose();
			}
			device = null;
		}

		private void CmdWindow_Click(object sender, System.EventArgs e) {
			using (WindowTest windowTest = new WindowTest()) {
			
				windowTest.Show();

				// Initialize Direct3D and the device object
				if(!windowTest.InitD3D(windowTest.Handle)) {
					MessageBox.Show("Could not initialize Direct3D.");
					windowTest.Dispose();
					return;
				}			
				else {
					// Load the textures and create the square to show them
					if(!windowTest.CreateTextures()) {
						MessageBox.Show("Could not initialize vertices and textures.");
						return;
					}
				}

				// Uncomment the lines  below to see a smooth walking man
				//int desiredFrameRate = 10;
				//int lastTick = 0;
				while(!windowTest.EndTest) {
					// Force a Frame rate of 10 frames to second on maximum
					//if(System.Environment.TickCount - lastTick >= 1000 / desiredFrameRate) {
					windowTest.Render();
					// Frame rate calculation
					windowTest.Text = "Window Test.  Frame rate: " + DirectXLists.CalcFrameRate().ToString();
					//lastTick = System.Environment.TickCount;
					//}
					Application.DoEvents();
				}
			}
		}

		private void CmdFullScreen_Click(object sender, System.EventArgs e) {
			using (FullScreenTest fullScreenTest = new FullScreenTest()) {
				fullScreenTest.Show();

				// Initialize Direct3D and the device object
				if(!fullScreenTest.InitD3D(fullScreenTest.Handle)) {
					MessageBox.Show("Could not initialize Direct3D.");
					fullScreenTest.Dispose();
					return;
				}
				else {
					// Load the textures and create the square to show them
					if(!fullScreenTest.CreateTextures()) {
						MessageBox.Show("Could not initialize vertices and textures.");
						fullScreenTest.DisposeD3D();
						fullScreenTest.Dispose();
						return;
					}
				}

				// If we have no errors, then enter the rendering loop
				while(!fullScreenTest.EndTest) {
					try {
						fullScreenTest.Render();
						Application.DoEvents();
					}
					catch {
						// Ignore any errors that may arise when the window close
					}
				}
			}
		}

		private void CmdTransparency_Click(object sender, System.EventArgs e) {	
			using (TransparentTest transparentTest = new TransparentTest()) {
				transparentTest.Show();

				// Initialize Direct3D and the device object
				if(!transparentTest.InitD3D(transparentTest.Handle)) {
					MessageBox.Show("Could not initialize Direct3D.");
					transparentTest.Dispose();
					return;
				}
				else {
					// Load the textures and create the square to show them
					if(!(transparentTest.CreateTextures() && transparentTest.CreateTransparentVertices(0, 0))) {
						MessageBox.Show("Could not initialize vertices and textures.");
						transparentTest.DisposeD3D();
						transparentTest.Dispose();
						return;
					}
				}

				// If we have no errors, then enter the rendering loop
				while(!transparentTest.EndTest) {
					transparentTest.Render();
					// Frame rate calculation
					transparentTest.Text = 
						"Transparency Test.  Frame rate: " + DirectXLists.CalcFrameRate().ToString();
					Application.DoEvents();
				}
			}		
		}

		private void CmdMatrix_Click(object sender, System.EventArgs e) {
			using (MatrixControl matrixControl = new MatrixControl()) {
				MatrixTest matrixTest = new MatrixTest();
				matrixControl.Show();
				matrixTest.Show();

				// Initialize Direct3D and the device object
				if(!matrixControl.InitD3D(matrixTest.Handle)) {
					MessageBox.Show("Could not initialize Direct3D.");
					matrixControl.Dispose();
					return;
				}
				else {
					// Load the textures and create the cube to show them
					if(!matrixControl.CreateCube()) {
						MessageBox.Show("Could not initialize geometry.");
						matrixControl.DisposeD3D();
						matrixControl.Dispose();
						return;
					}
				}

				// Start with a simple rotation, to position the cube more nicely;
				//  and with no scale (100% of the original size)
			
				matrixControl.RotationX.Value = 45;
				matrixControl.RotationY.Value = 45;
				matrixControl.RotationZ.Value = 45;
				matrixControl.ScaleX.Value = 100;
				matrixControl.ScaleY.Value = 100;
				matrixControl.ScaleZ.Value = 100;

				// Ends the test if ESC is pressed in any of the 2 windows
				while(!matrixControl.EndTest && !matrixTest.EndTest) {
					matrixControl.Render();
					// Frame rate calculation
					matrixTest.Text = "Matrix Tests.  Frame Rate: " + DirectXLists.CalcFrameRate().ToString();
					Application.DoEvents();
				}
				matrixTest.Close();
			}		
		}

		private void CmdLight_Click(object sender, System.EventArgs e) {
			LightControl winLightControl = new LightControl();
			using (LightTest lightTest = new LightTest()) {

				winLightControl.Show();
				lightTest.Show();

				// Initialize Direct3D and the device object
				if(!winLightControl.InitD3D(lightTest.Handle)) {
					MessageBox.Show("Could not initialize Direct3D.");
					winLightControl.Dispose();
				}			
				else {
					// Load the textures and create the vertices
					if(!winLightControl.CreateTextures()) {
						MessageBox.Show("Could not initialize the textures and vertices.");
						winLightControl.DisposeD3D();
						winLightControl.Dispose();
					}
				}

				// Start with full white light in all vertices
			
				winLightControl.Red1.Value = 255;
				winLightControl.Green1.Value = 255;
				winLightControl.Blue1.Value = 255;
				winLightControl.Red2.Value = 255;
				winLightControl.Green2.Value = 255;
				winLightControl.Blue2.Value = 255;
				winLightControl.Red3.Value = 255;
				winLightControl.Green3.Value = 255;
				winLightControl.Blue3.Value = 255;
				winLightControl.Red4.Value = 255;
				winLightControl.Green4.Value = 255;
				winLightControl.Blue4.Value = 255;
			

				// Ends the test if ESC is pressed in any of the 2 windows
				while(!winLightControl.EndTest && !lightTest.EndTest) {
					winLightControl.Render();
					// Frame rate calculation
					lightTest.Text = "Light Test.  Frame Rate: " + DirectXLists.CalcFrameRate().ToString();
					Application.DoEvents();
				}
			}
		}

		private void CmdClose_Click(object sender, System.EventArgs e) {
			this.Close();
		}

		private void AdaptersListBox_SelectedIndexChanged(object sender, System.EventArgs e) {
			// Update the devices list every time a new adapter is chosen 
			ListDevices(AdaptersListBox.SelectedIndex);
		}

		private void DevicesListBox_SelectedIndexChanged(object sender, System.EventArgs e) {
			// The first entry in DevicesListBox is the Reference Rasterizer
			DeviceType deviceType = (DevicesListBox.SelectedIndex == 0) ? DeviceType.Reference : DeviceType.Hardware;
			ListDisplayModes(AdaptersListBox.SelectedIndex, deviceType, Format.X8R8G8B8);
			ListDisplayModes(AdaptersListBox.SelectedIndex, deviceType, Format.X1R5G5B5);
			ListDisplayModes(AdaptersListBox.SelectedIndex, deviceType, Format.R5G6B5);
			ListDeviceCaps(AdaptersListBox.SelectedIndex, deviceType);
		}

		private void ListDisplayModes(int adapter, DeviceType renderer, Format adapterFormat) {
			DisplayModesListBox.Items.Clear();
			foreach(DisplayMode dispMode in Manager.Adapters[adapter].SupportedDisplayModes) {
				// Check to see if the display mode is supported by the device
				if(Manager.CheckDeviceType(adapter, renderer, dispMode.Format, dispMode.Format, false)) {
					// Fill the display modes list with the width, height, the mode name and the refresh rate              
					DisplayModesListBox.Items.Add(dispMode.Width + "x" + dispMode.Height
						+ "   ( " + dispMode.Format.ToString() + " - " 
						+ dispMode.RefreshRate + "Khz)");
				}
			}
		}

		private void ListDeviceCaps(int adapter, DeviceType deviceType) {
			DeviceCapsListBox.Items.Clear();
			Caps caps = Manager.GetDeviceCaps(adapter, deviceType);
			DirectXLists.ListGeneralCaps(caps, DeviceCapsListBox);
			DirectXLists.ListDevCaps(caps.DeviceCaps, DeviceCapsListBox);
			DirectXLists.ListDriverCaps(caps.DriverCaps, DeviceCapsListBox);
			DirectXLists.ListRasterCaps(caps.RasterCaps, DeviceCapsListBox);
			DirectXLists.ListTextureCaps(caps.TextureCaps, DeviceCapsListBox);
		}
	}
}

⌨️ 快捷键说明

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