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

📄 oldd3dapp.cs

📁 Introduction to 3d game engine design一书的源代码!
💻 CS
📖 第 1 页 / 共 4 页
字号:
		{
			// Store the new width/height
			backBufferDesc.Width = this.ClientSize.Width;
			backBufferDesc.Height = this.ClientSize.Height;
		}
		else
		{
			// Store the new width/height
			backBufferDesc.Width = presentParams.BackBufferWidth;
			backBufferDesc.Height = presentParams.BackBufferHeight;
		}

		// Reset the device
		device.Reset(presentParams);

		this.ClientSize = new System.Drawing.Size(presentParams.BackBufferWidth, presentParams.BackBufferHeight);
		// 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;
			}
		}

		// If the app is paused, trigger the rendering of the current frame
		if( false == frameMoving )
		{
			singleStep = true;
			DXUtil.Timer( TIMER.START );
			DXUtil.Timer( TIMER.STOP );
		}
	}




	//-----------------------------------------------------------------------------
	// Name: ToggleFullScreen()
	// Desc: Called when user toggles between fullscreen mode and windowed mode
	//-----------------------------------------------------------------------------
	public void ToggleFullscreen()
	{
		int AdapterOrdinalOld = graphicsSettings.AdapterOrdinal;
		DeviceType DevTypeOld = graphicsSettings.DevType;

		ready = false;

		// Toggle the windowed state
		windowed = !windowed;
		graphicsSettings.IsWindowed = windowed;

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

		// If AdapterOrdinal and DevType are the same, we can just do a Reset().
		// If they've changed, we need to do a complete device teardown/rebuild.
		if (graphicsSettings.AdapterOrdinal == AdapterOrdinalOld &&
			graphicsSettings.DevType == DevTypeOld)
		{
			// Resize the 3D device
			try
			{
				BuildPresentParamsFromSettings();
				Resize3DEnvironment();
			}
			catch 
			{
				if( windowed )
					ForceWindowed();
				else
					 throw new Exception();
			}
		}
		else
		{
			device.Dispose();
			device = null;
			Initialize3DEnvironment();
		}

		// When moving from fullscreen to windowed mode, it is important to
		// adjust the window size after resetting 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 )
		{
			this.Location = new System.Drawing.Point(windowBoundsRect.Left, windowBoundsRect.Top);
			this.Size = new System.Drawing.Size(( windowBoundsRect.Right - windowBoundsRect.Left ), ( windowBoundsRect.Bottom - windowBoundsRect.Top));
		}

		ready = true;
	}




	//-----------------------------------------------------------------------------
	// Name: ForceWindowed()
	// Desc: Switch to a windowed mode, even if that means picking a new device
	//       and/or adapter
	//-----------------------------------------------------------------------------
	public void ForceWindowed()
	{
		if( windowed )
			return;

		if( !FindBestWindowedMode(false, false) )
		{
			return;
		}
		windowed = true;

		// Now destroy the current 3D device objects, then reinitialize

		ready = false;

		// Release display objects, so a new device can be created
		device.Dispose();
		device = null;

		// Create the new device
		try
		{
			Initialize3DEnvironment();
		}
		catch (GraphicsException e)
		{
			DisplayErrorMsg( e,AppMsgType.ErrorAppMustExit );
		}
		catch
		{
			DisplayErrorMsg( new GraphicsException(),AppMsgType.ErrorAppMustExit );
		}
		ready = true;
	}




	//-----------------------------------------------------------------------------
	// Name: AdjustWindowForChange()
	// Desc: Prepare the window for a possible change between windowed mode and
	//       fullscreen mode.  
	//-----------------------------------------------------------------------------
	public void AdjustWindowForChange()
	{
		if( windowed )
		{
			// Set windowed-mode style
		}
		else
		{
			// Set fullscreen-mode style
		}
	}




	//-----------------------------------------------------------------------------
	// Name: IdleTime()
	// Desc: This function is called anytime our thread has nothing else to do
	// (ie, no messages to process, etc)
	//-----------------------------------------------------------------------------
	public void IdleTime(object sender, EventArgs e)
	{
		// Render a frame during idle time (no messages are waiting)
		if( active && ready )
		{
			try 
			{
				if (deviceLost)
				{
					// Yield some CPU time to other processes
					System.Threading.Thread.Sleep(100); // 100 milliseconds
				}
				if ( m_bTerminate ) 					
					this.Exit(null, null);

				// Render a frame during idle time
				if (active)
				{
					Render3DEnvironment();
				}
			}
			catch (DirectXException d3de)
			{
				System.Diagnostics.Debug.WriteLine(d3de.ErrorString);
			}
			catch (GraphicsException e2)
			{
				DisplayErrorMsg( e2,AppMsgType.ErrorAppMustExit );
			}
			catch ( Exception e3 )
			{
				System.Diagnostics.Debug.WriteLine(e3.Message);
			}
			catch
			{
				DisplayErrorMsg( new GraphicsException(),AppMsgType.ErrorAppMustExit );
			}
/*			catch 
			{
				this.Dispose();
			}
			*/
		}
	}

	//-----------------------------------------------------------------------------
	// Name: Run()
	// Desc: Run the D3D Application
	//-----------------------------------------------------------------------------
	public void Run()
	{
		// Now we're ready to recieve and process Windows messages.
		System.Windows.Forms.Application.Run(this);
		if (!isDisposed)
			this.Dispose();
	}




	//-----------------------------------------------------------------------------
	// Name: Render3DEnvironment()
	// Desc: Draws the scene.
	//-----------------------------------------------------------------------------
	public void Render3DEnvironment()
	{
		if (deviceLost)
		{
			// Test the cooperative level to see if it's okay to render
			int result;
			device.TestCooperativeLevel(out result);
			if (result != 0)
			{
				// If the device was lost, do not render until we get it back
				if (result == (int)ErrorCode.DeviceLost)
					return;

				// Check if the device needs to be resized.
				if( (int)ErrorCode.DeviceNotReset == result )
				{
					// If we are windowed, read the desktop mode and use the same format for
					// the back buffer
					if( windowed )
					{
						D3DAdapterInfo adapterInfo = graphicsSettings.AdapterInfo;
						graphicsSettings.Windowed_DisplayMode = graphicsObject.Adapters[adapterInfo.AdapterOrdinal].DisplayMode;
						presentParams.BackBufferFormat = graphicsSettings.Windowed_DisplayMode.Format;
					}

					Resize3DEnvironment();
				}
			}
			deviceLost = false;
		}

		// Get the app's time, in seconds. Skip rendering if no time elapsed
		float fAppTime        = DXUtil.Timer( TIMER.GETAPPTIME );
		float fElapsedAppTime = DXUtil.Timer( TIMER.GETELAPSEDTIME );
		if( ( 0.0f == fElapsedAppTime ) && frameMoving )
			return;

		// FrameMove (animate) the scene
		if( frameMoving || singleStep )
		{
			// Store the time for the app
			appTime        = fAppTime;
			elapsedTime = fElapsedAppTime;

			// Frame move the scene
			FrameMove();

			singleStep = false;
		}

		// Render the scene as normal
		Render();

		UpdateStats();

		try
		{
			// Show the frame on the primary surface.
			device.Present();
		}
		catch (DirectXException de)
		{
			if (de.ErrorCode == (int) ErrorCode.DeviceLost)
				deviceLost = true;
		}
	}



	//-----------------------------------------------------------------------------
	// Name: UpdateStats()
	// Desc: 
	//-----------------------------------------------------------------------------
	public void UpdateStats()
	{
		// Keep track of the frame count
		float time = DXUtil.Timer( TIMER.GETABSOLUTETIME );
		++frames;

		// Update the scene stats once per second
		if( time - lastTime > 1.0f )
		{
			framePerSecond    = frames / (time - lastTime);
			lastTime = time;
			frames  = 0;

			string strFmt;
			DisplayMode mode = graphicsObject.Adapters[graphicsSettings.AdapterOrdinal].DisplayMode;
			if (mode.Format == backBufferDesc.Format)
			{
				strFmt = mode.Format.ToString();
			}
			else
			{
				strFmt = String.Format("backbuf {0}, adapter {1}", 
					backBufferDesc.Format.ToString(), mode.Format.ToString());
			}

			string strDepthFmt;
			if( enumerationSettings.AppUsesDepthBuffer )
			{
				strDepthFmt = String.Format( " ({0})", 
					graphicsSettings.DepthStencilBufferFormat.ToString());
			}
			else
			{
				// No depth buffer
				strDepthFmt = "";
			}

			string strMultiSample;
			switch( graphicsSettings.MultisampleType )
			{
				case Direct3D.MultiSampleType.OneSample: strMultiSample = " (1x Multisample)"; break;
				case Direct3D.MultiSampleType.TwoSamples: strMultiSample = " (2x Multisample)"; break;
				case Direct3D.MultiSampleType.ThreeSamples: strMultiSample = " (3x Multisample)"; break;
				case Direct3D.MultiSampleType.FourSamples: strMultiSample = " (4x Multisample)"; break;
				case Direct3D.MultiSampleType.FiveSamples: strMultiSample = " (5x Multisample)"; break;
				case Direct3D.MultiSampleType.SixSamples: strMultiSample = " (6x Multisample)"; break;
				case Direct3D.MultiSampleType.SevenSamples: strMultiSample = " (7x Multisample)"; break;
				case Direct3D.MultiSampleType.EightSamples: strMultiSample = " (8x Multisample)"; break;
				case Direct3D.MultiSampleType.NineSamples: strMultiSample = " (9x Multisample)"; break;
				case Direct3D.MultiSampleType.TenSamples: strMultiSample = " (10x Multisample)"; break;
				case Direct3D.MultiSampleType.ElevenSamples: strMultiSample = " (11x Multisample)"; break;
				case Direct3D.MultiSampleType.TwelveSamples: strMultiSample = " (12x Multisample)"; break;
				case Direct3D.MultiSampleType.ThirteenSamples: strMultiSample = " (13x Multisample)"; break;
				case Direct3D.MultiSampleType.FourteenSamples: strMultiSample = " (14x Multisample)"; break;
				case Direct3D.MultiSampleType.FifteenSamples: strMultiSample = " (15x Multisample)"; break;
				case Direct3D.MultiSampleType.SixteenSamples: strMultiSample = " (16x Multisample)"; break;
				default: strMultiSample = ""; break;
			}
			frameStats = String.Format("{0} fps ({1}x{2}), {3}{4}{5}", framePerSecond.ToString("f2"),
				backBufferDesc.Width, backBufferDesc.Height, strFmt, strDepthFmt, strMultiSample);
		}
	}



	//-----------------------------------------------------------------------------
	// Name: Pause()
	// Desc: Called in to toggle the pause state of the app.
	//-----------------------------------------------------------------------------
	public void Pause( bool bPause )
	{

		appPausedCount  += (uint)( bPause ? +1 : -1 );
		ready = ( (appPausedCount > 0) ? false : true );

		// Handle the first pause request (of many, nestable pause requests)
		if( bPause && ( 1 == appPausedCount ) )
		{
			// Stop the scene from animating
			if( frameMoving )
				DXUtil.Timer( TIMER.STOP );

⌨️ 快捷键说明

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