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

📄 oldd3dapp.cs

📁 Introduction to 3d game engine design一书的源代码!
💻 CS
📖 第 1 页 / 共 4 页
字号:
	// the bRequireHAL and bRequireREF constraints.  Returns false if no such
	// mode can be found.
	public bool FindBestFullscreenMode(bool bRequireHAL, bool bRequireREF)
	{
		// For fullscreen, default to first HAL DeviceCombo that supports the current desktop 
		// display mode, or any display mode if HAL is not compatible with the desktop mode, or 
		// non-HAL if no HAL is available
		DisplayMode adapterDesktopDisplayMode = new DisplayMode();
		DisplayMode bestAdapterDesktopDisplayMode = new DisplayMode();
		DisplayMode bestDisplayMode = new DisplayMode();
		bestAdapterDesktopDisplayMode.Width = 0;
		bestAdapterDesktopDisplayMode.Height = 0;
		bestAdapterDesktopDisplayMode.Format = 0;
		bestAdapterDesktopDisplayMode.RefreshRate = 0;

		D3DAdapterInfo bestAdapterInfo = null;
		D3DDeviceInfo bestDeviceInfo = null;
		D3DDeviceCombo bestDeviceCombo = null;

		foreach (D3DAdapterInfo adapterInfo in enumerationSettings.AdapterInfoList)
		{
			adapterDesktopDisplayMode = graphicsObject.Adapters[adapterInfo.AdapterOrdinal].DisplayMode;
			foreach (D3DDeviceInfo deviceInfo in adapterInfo.DeviceInfoList)
			{
				if (bRequireHAL && deviceInfo.DevType != DeviceType.Hardware)
					continue;
				if (bRequireREF && deviceInfo.DevType != DeviceType.Reference)
					continue;
				foreach (D3DDeviceCombo deviceCombo in deviceInfo.DeviceComboList)
				{
					bool bAdapterMatchesBB = (deviceCombo.BackBufferFormat == deviceCombo.AdapterFormat);
					bool bAdapterMatchesDesktop = (deviceCombo.AdapterFormat == adapterDesktopDisplayMode.Format);
					if (deviceCombo.IsWindowed)
						continue;
					// If we haven't found a compatible set yet, or if this set
					// is better (because it's a HAL, and/or because formats match better),
					// save it
					if (bestDeviceCombo == null ||
						bestDeviceCombo.DevType != DeviceType.Hardware && deviceInfo.DevType == DeviceType.Hardware ||
						bestDeviceCombo.DevType == DeviceType.Hardware && bestDeviceCombo.AdapterFormat != adapterDesktopDisplayMode.Format && bAdapterMatchesDesktop ||
						bestDeviceCombo.DevType == DeviceType.Hardware && bAdapterMatchesDesktop && bAdapterMatchesBB )
					{
						bestAdapterDesktopDisplayMode = adapterDesktopDisplayMode;
						bestAdapterInfo = adapterInfo;
						bestDeviceInfo = deviceInfo;
						bestDeviceCombo = deviceCombo;
						if (deviceInfo.DevType == DeviceType.Hardware && bAdapterMatchesDesktop && bAdapterMatchesBB)

						{
							// This fullscreen device combo looks great -- take it
							goto EndFullscreenDeviceComboSearch;
						}
						// Otherwise keep looking for a better fullscreen device combo
					}
				}
			}
		}
	EndFullscreenDeviceComboSearch:
		if (bestDeviceCombo == null)
			return false;

		// Need to find a display mode on the best adapter that uses pBestDeviceCombo->AdapterFormat
		// and is as close to bestAdapterDesktopDisplayMode's res as possible
		bestDisplayMode.Width = 0;
		bestDisplayMode.Height = 0;
		bestDisplayMode.Format = 0;
		bestDisplayMode.RefreshRate = 0;
		foreach( DisplayMode displayMode in bestAdapterInfo.DisplayModeList )
		{
			if( displayMode.Format != bestDeviceCombo.AdapterFormat )
				continue;
			if( displayMode.Width == bestAdapterDesktopDisplayMode.Width &&
				displayMode.Height == bestAdapterDesktopDisplayMode.Height && 
				displayMode.RefreshRate == bestAdapterDesktopDisplayMode.RefreshRate )
			{
				// found a perfect match, so stop
				bestDisplayMode = displayMode;
				break;
			}
			else if( displayMode.Width == bestAdapterDesktopDisplayMode.Width &&
				displayMode.Height == bestAdapterDesktopDisplayMode.Height && 
				displayMode.RefreshRate > bestDisplayMode.RefreshRate )
			{
				// refresh rate doesn't match, but width/height match, so keep this
				// and keep looking
				bestDisplayMode = displayMode;
			}
			else if( bestDisplayMode.Width == bestAdapterDesktopDisplayMode.Width )
			{
				// width matches, so keep this and keep looking
				bestDisplayMode = displayMode;
			}
			else if( bestDisplayMode.Width == 0 )
			{
				// we don't have anything better yet, so keep this and keep looking
				bestDisplayMode = displayMode;
			}
		}
		graphicsSettings.Fullscreen_AdapterInfo = bestAdapterInfo;
		graphicsSettings.Fullscreen_DeviceInfo = bestDeviceInfo;
		graphicsSettings.Fullscreen_DeviceCombo = bestDeviceCombo;
		graphicsSettings.IsWindowed = false;
		graphicsSettings.Fullscreen_DisplayMode = bestDisplayMode;
		if (enumerationSettings.AppUsesDepthBuffer)
			graphicsSettings.Fullscreen_DepthStencilBufferFormat = (Format)bestDeviceCombo.DepthStencilFormatList[0];
		graphicsSettings.Fullscreen_MultisampleType = (MultiSampleType)bestDeviceCombo.MultiSampleTypeList[0];
		graphicsSettings.Fullscreen_MultisampleQuality = 0;
		graphicsSettings.Fullscreen_VertexProcessingType = (VertexProcessingType)bestDeviceCombo.VertexProcessingTypeList[0];
		graphicsSettings.Fullscreen_PresentInterval = (PresentInterval)bestDeviceCombo.PresentIntervalList[0];
		return true;
	}

 


	public bool ChooseInitialD3DSettings()
	{
		bool bFoundFullscreen = FindBestFullscreenMode(false, false);
		bool bFoundWindowed = FindBestWindowedMode(false, false);
		if (startFullscreen && bFoundFullscreen)
			graphicsSettings.IsWindowed = false;
		return (bFoundFullscreen || bFoundWindowed);
	}
	/*
//				case WindowMessage.SETCURSOR:
//					// Turn off Windows cursor in fullscreen mode
//					if( active && ready && !windowed )
//					{
//						SetCursor( null );
//						if( showCursorWhenFullscreen )
//							device.ShowCursor( true );
//						return true; // prevent Windows from setting cursor to window class cursor
//					}
//					break;
//
//				case WindowMessage.MOUSEMOVE:
//					if( active && ready && device != null )
//					{
//						Point ptCursor;
//						GetCursorPos( &ptCursor );
//						if( !windowed )
//							ScreenToClient( m_hWnd, &ptCursor );
//						device.SetCursorPosition( ptCursor.x, ptCursor.y, 0L );
//					}
//					break;
	*/
	public void BuildPresentParamsFromSettings()
	{
		presentParams.Windowed               = graphicsSettings.IsWindowed;
		presentParams.BackBufferCount        = 1;
		presentParams.MultiSample        = graphicsSettings.MultisampleType;
		presentParams.MultiSampleQuality     = graphicsSettings.MultisampleQuality;
		presentParams.SwapEffect             = SwapEffect.Discard;
		presentParams.EnableAutoDepthStencil = enumerationSettings.AppUsesDepthBuffer;
		presentParams.AutoDepthStencilFormat = graphicsSettings.DepthStencilBufferFormat;
		presentParams.DeviceWindowHandle          = this.Handle;
		presentParams.Flags                  = 0;
		if( windowed )
		{
			presentParams.BackBufferWidth  = clientRect.Right - clientRect.Left;
			presentParams.BackBufferHeight = clientRect.Bottom - clientRect.Top;
			presentParams.BackBufferFormat = graphicsSettings.DeviceCombo.BackBufferFormat;
			presentParams.FullScreenRefreshRateInHz = 0;
			presentParams.PresentationInterval = PresentInterval.Immediate;
		}
		else
		{
			presentParams.BackBufferWidth  = graphicsSettings.DisplayMode.Width;
			presentParams.BackBufferHeight = graphicsSettings.DisplayMode.Height;
			presentParams.BackBufferFormat = graphicsSettings.DeviceCombo.BackBufferFormat;
			presentParams.FullScreenRefreshRateInHz = graphicsSettings.DisplayMode.RefreshRate;
			presentParams.PresentationInterval = graphicsSettings.PresentInterval;
		}
	}


	//-----------------------------------------------------------------------------
	// Name: Initialize3DEnvironment()
	// Desc:
	//-----------------------------------------------------------------------------
	public void Initialize3DEnvironment()
	{
		D3DAdapterInfo adapterInfo = graphicsSettings.AdapterInfo;
		D3DDeviceInfo deviceInfo = graphicsSettings.DeviceInfo;

		windowed = graphicsSettings.IsWindowed;

		// Prepare window for possible windowed/fullscreen change
		AdjustWindowForChange();

		// Set up the presentation parameters
		BuildPresentParamsFromSettings();

		if (deviceInfo.Caps.PrimitiveMiscCaps.NullReference )
		{
			// Warn user about null ref device that can't render anything
			DisplayErrorMsg( new GraphicsException(GraphicsException.ErrorCode.NullRefDevice), AppMsgType.None );
		}

		CreateFlags createFlags = new CreateFlags();
		if (graphicsSettings.VertexProcessingType == VertexProcessingType.Software)
			createFlags = CreateFlags.SoftwareVertexProcessing;
		else if (graphicsSettings.VertexProcessingType == VertexProcessingType.Mixed)
			createFlags = CreateFlags.MixedVertexProcessing;
		else if (graphicsSettings.VertexProcessingType == VertexProcessingType.Hardware)
			createFlags = CreateFlags.HardwareVertexProcessing;
		else if (graphicsSettings.VertexProcessingType == VertexProcessingType.PureHardware)
		{
			createFlags = CreateFlags.HardwareVertexProcessing | CreateFlags.PureDevice;
		}
		/*
		else
			// TODO: throw exception
		*/
		createFlags |= CreateFlags.MultiThreaded;

		// Create the device
		device = new Device(graphicsObject, graphicsSettings.AdapterOrdinal, graphicsSettings.DevType, 
			this, createFlags, presentParams);

		if( device != null )
		{
			// Cache our local objects
			renderState = device.RenderState;
			sampleState = device.SamplerState;
			textureStates = device.TextureState;
			// When moving from fullscreen to windowed mode, it is important to
			// adjust the window size after recreating the device rather than
			// beforehand to ensure that you get the window size you want.  For
			// example, when switching from 640x480 fullscreen to windowed with
			// a 1000x600 window on a 1024x768 desktop, it is impossible to set
			// the window size to 1000x600 until after the display mode has
			// changed to 1024x768, because windows cannot be larger than the
			// desktop.
			if( windowed )
			{
				// Make sure main window isn't topmost, so error message is visible
				this.Location = new System.Drawing.Point(windowBoundsRect.Left, windowBoundsRect.Top);
				this.Size = new System.Drawing.Size(( windowBoundsRect.Right - windowBoundsRect.Left ), ( windowBoundsRect.Bottom - windowBoundsRect.Top));
			}

			// Store device Caps
			graphicsCaps = device.DeviceCaps;
			behavior = createFlags;

			// Store device description
			if( deviceInfo.DevType == DeviceType.Reference )
				deviceStats = "REF";
			else if( deviceInfo.DevType == DeviceType.Hardware )
				deviceStats = "HAL";
			else if( deviceInfo.DevType == DeviceType.Software )
				deviceStats = "SW";

			BehaviorFlags behaviorFlags = new BehaviorFlags(createFlags);
			if( (behaviorFlags.HardwareVertexProcessing) && 
				(behaviorFlags.PureDevice) )
			{
				if( deviceInfo.DevType == DeviceType.Hardware )
					deviceStats += " (pure hw vp)";
				else
					deviceStats += " (simulated pure hw vp)";
			}
			else if( (behaviorFlags.HardwareVertexProcessing) )
			{
				if( deviceInfo.DevType == DeviceType.Hardware )
					deviceStats  += " (hw vp)";
				else
					deviceStats += " (simulated hw vp)";
			}
			else if( behaviorFlags.MixedVertexProcessing)
			{
				if( deviceInfo.DevType == DeviceType.Hardware )
					deviceStats += " (mixed vp)";
				else
					deviceStats += " (simulated mixed vp)";
			}
			else if( behaviorFlags.SoftwareVertexProcessing )
			{
				deviceStats += " (sw vp)";
			}

			if( deviceInfo.DevType == DeviceType.Hardware )
			{
				deviceStats += ": ";
				deviceStats += adapterInfo.AdapterIdentifier.Description;
			}

			// Store render target surface desc
			Surface BackBuffer = device.GetBackBuffer(0,0, BackBufferType.Mono);
			backBufferDesc = BackBuffer.Description;
			BackBuffer.Dispose();
			BackBuffer = null;

			// Set up the fullscreen cursor
			if( showCursorWhenFullscreen && !windowed )
			{
				System.Windows.Forms.Cursor ourCursor = this.Cursor;
				device.SetCursor(ourCursor.Handle, true);
				device.ShowCursor(true);
			}

			// Confine cursor to fullscreen window
			if( clipCursorWhenFullscreen )
			{
				if (!windowed )
				{
					System.Drawing.Rectangle rcWindow = this.ClientRectangle;
				}
			}

			// Setup the event handlers for our device
			device.DeviceLost += new System.EventHandler(this.InvalidateDeviceObjects);
			device.DeviceReset += new System.EventHandler(this.RestoreDeviceObjects);
			device.Disposing += new System.EventHandler(this.DeleteDeviceObjects);


			// Initialize the app's device-dependent objects
			try
			{
				InitDeviceObjects();
				RestoreDeviceObjects(null, null);
				active = true;
				return;
			}
			catch
			{
				// Cleanup before we try again
				InvalidateDeviceObjects(null, null);
				DeleteDeviceObjects(null, null);
				device.Dispose();
				device = null;
				if (this.Disposing)
					return;
			}
		}

		// If that failed, fall back to the reference rasterizer
		if( deviceInfo.DevType == DeviceType.Hardware )
		{
			if (FindBestWindowedMode(false, true))
			{
				windowed = true;
				// Make sure main window isn't topmost, so error message is visible
				this.Location = new System.Drawing.Point(windowBoundsRect.Left, windowBoundsRect.Top);
				this.Size = new System.Drawing.Size(( windowBoundsRect.Right - windowBoundsRect.Left ), ( windowBoundsRect.Bottom - windowBoundsRect.Top));
				AdjustWindowForChange();

				// Let the user know we are switching from HAL to the reference rasterizer
				DisplayErrorMsg( null, AppMsgType.WarnSwitchToRef);

				Initialize3DEnvironment();
			}
		}
	}




	//-----------------------------------------------------------------------------
	// Name: DisplayErrorMsg()
	// Desc: Displays error messages in a message box
	//-----------------------------------------------------------------------------
	public void DisplayErrorMsg( GraphicsException e, AppMsgType Type )
	{
		string strMsg = null;
		if (e != null)
			strMsg = e.Message;

		if (windowTitle == null)
			windowTitle = "";

		if( AppMsgType.ErrorAppMustExit == Type )
		{
			strMsg  += "\n\nThis sample will now exit.";
			System.Windows.Forms.MessageBox.Show(strMsg, windowTitle, 
				System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

			// Close the window, which shuts down the app
			if( this.IsHandleCreated )
				this.Dispose( );
		}
		else
		{
			if( AppMsgType.WarnSwitchToRef == Type )
				strMsg = "\n\nSwitching to the reference rasterizer,\n";
				strMsg += "a software device that implements the entire\n";
				strMsg += "Direct3D feature set, but runs very slowly.";

			System.Windows.Forms.MessageBox.Show(strMsg, windowTitle, 
				System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
		}
	}




	//-----------------------------------------------------------------------------
	// Name: Resize3DEnvironment
	// Desc: Resizes the environment
	//-----------------------------------------------------------------------------
	public void Resize3DEnvironment()
	{
		if (presentParams.Windowed)

⌨️ 快捷键说明

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