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

📄 oldd3dapp.cs

📁 Introduction to 3d game engine design一书的源代码!
💻 CS
📖 第 1 页 / 共 4 页
字号:
		}

		if( 0 == appPausedCount )
		{
			// Restart the timers
			if( frameMoving )
				DXUtil.Timer( TIMER.START );
		}
	}




	//-----------------------------------------------------------------------------
	// Name: Cleanup3DEnvironment()
	// Desc: Cleanup scene objects
	//-----------------------------------------------------------------------------
	public void Cleanup3DEnvironment()
	{
		active = false;
		ready  = false;

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

		FinalCleanup();
	}
	#region Menu EventHandlers



	//-----------------------------------------------------------------------------
	// Name: UserSelectNewDevice()
	// Desc: Displays a dialog so the user can select a new adapter, device, or
	//       display mode, and then recreates the 3D environment if needed
	//-----------------------------------------------------------------------------
	public void UserSelectNewDevice(object sender, EventArgs e)
	{
		// Prompt the user to select a new device or mode
		if( active && ready )
		{
			Pause(true);

			DoSelectNewDevice();

			Pause(false);
		}
	}
	private void DoSelectNewDevice()
	{
		// Can't display dialogs in fullscreen mode
		if( windowed == false )
		{
			try
			{
				ToggleFullscreen();
			}
			catch
			{
				DisplayErrorMsg( new GraphicsException(GraphicsException.ErrorCode.ResizeFailed), AppMsgType.ErrorAppMustExit);
				return;
			}
		}

		// Make sure the main form is in the background
		this.SendToBack();
		D3DSettingsForm settingsForm = new D3DSettingsForm(enumerationSettings, graphicsSettings);
		System.Windows.Forms.DialogResult result = settingsForm.ShowDialog(null);
		if( result != System.Windows.Forms.DialogResult.OK)
		{
			return;
		}
		graphicsSettings = settingsForm.settings;

		windowed = graphicsSettings.IsWindowed;

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

		// Inform the display class of the change. It will internally
		// re-create valid surfaces, a d3ddevice, etc.
		try
		{
			Initialize3DEnvironment();
		}
		catch(GraphicsException d3de)
		{
			DisplayErrorMsg( d3de, AppMsgType.ErrorAppMustExit );
		}
		catch (DirectXException de)
		{
			DisplayErrorMsg( new GraphicsException((GraphicsException.ErrorCode)de.ErrorCode), AppMsgType.ErrorAppMustExit );
		}

		// 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: ToggleStart()
	// Desc: Will start (or stop) simulation
	//-----------------------------------------------------------------------------
	private void ToggleStart(object sender, EventArgs e)
	{
		//Toggle frame movement
		frameMoving = !frameMoving;
		DXUtil.Timer( frameMoving ? TIMER.START : TIMER.STOP);
	}



	//-----------------------------------------------------------------------------
	// Name: SingleStep()
	// Desc: Will single step the simulation
	//-----------------------------------------------------------------------------
	private void SingleStep(object sender, EventArgs e)
	{
		// Single-step frame movement
		if( false == frameMoving )
			DXUtil.Timer( TIMER.ADVANCE );
		else
			DXUtil.Timer( TIMER.STOP );
		frameMoving = false;
		singleStep  = true;
	}



	//-----------------------------------------------------------------------------
	// Name: Exit()
	// Desc: Will end the simulation
	//-----------------------------------------------------------------------------
	private void Exit(object sender, EventArgs e)
	{
		this.Dispose();
	}
	#endregion
	#region WinForms Overrides
	protected override void OnLoad(System.EventArgs e)
	{
		// Show the window and set focus to it
		this.Show();
		this.Select();
		while(Created)
		{
			IdleTime(null, null);
			System.Windows.Forms.Application.DoEvents();
		}
	}

	protected override void Dispose(bool disposing)
	{
		isDisposed = true;
		Cleanup3DEnvironment();
		base.Dispose(disposing);
		mnuMain.Dispose();
	}

	protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
	{
/*
		// Check for our shortcut keys (Space)
		if (e.KeyChar == ' ')
		{
			mnuSingle.PerformClick();
			e.Handled = true;
		}

		// Check for our shortcut keys (Return to start or stop)
		if (e.KeyChar == '\r')
		{
			mnuGo.PerformClick();
			e.Handled = true;

		}
*/
		// Check for our shortcut keys (Escape to quit)
		if ((byte)e.KeyChar == (byte)(int)System.Windows.Forms.Keys.Escape)
		{
			mnuExit.PerformClick();
			e.Handled = true;
		}

	}

	protected override void OnLostFocus(System.EventArgs e)
	{
		hasFocus = false;
		base.OnLostFocus(e);
	}

	protected override void OnGotFocus(System.EventArgs e)
	{
		hasFocus = true;
		base.OnGotFocus(e);
	}
	protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
	{
		char tstr = (char)(e.KeyValue);
		if ( GameEngine.Console.IsVisible && e.KeyData == System.Windows.Forms.Keys.Return )
		{
			GameEngine.Console.ProcessEntry();
		}
		if ( e.KeyData == System.Windows.Forms.Keys.F12 )
		{
			GameEngine.Console.ToggleState();
		}
		else if ( GameEngine.Console.IsVisible && 
			(e.KeyData == System.Windows.Forms.Keys.Space ||
			( e.KeyData >= System.Windows.Forms.Keys.A &&
			e.KeyData <= System.Windows.Forms.Keys.Z ) ||
			( e.KeyData >= System.Windows.Forms.Keys.D0 &&
			e.KeyData <= System.Windows.Forms.Keys.D9 ) 
			) )
		{
			GameEngine.Console.AddCharacterToEntryLine( tstr );
		}
		else if ( GameEngine.Console.IsVisible && e.KeyData == System.Windows.Forms.Keys.OemPeriod )
		{
			GameEngine.Console.AddCharacterToEntryLine( '.' );
		}
		else if ( GameEngine.Console.IsVisible && e.KeyData == System.Windows.Forms.Keys.OemMinus )
		{
			GameEngine.Console.AddCharacterToEntryLine( '-' );
		}
		else if ( GameEngine.Console.IsVisible && e.KeyData == System.Windows.Forms.Keys.Back )
		{
			GameEngine.Console.Backspace();
		}
		if ( (e.Alt) && (e.KeyCode == System.Windows.Forms.Keys.Return))
		{
			// Toggle the fullscreen/window mode
			if( active && ready )
			{
				Pause( true );

				try
				{
					ToggleFullscreen();
					Pause( false );                        
					return;
				}
				catch
				{
					DisplayErrorMsg( new GraphicsException(GraphicsException.ErrorCode.ResizeFailed), AppMsgType.ErrorAppMustExit);
				}
				finally
				{
					e.Handled = true;
				}
			}
		}
		// Allow the control to handle the keystroke now
//		base.OnKeyDown(e);
	}

	private void InitializeComponent()
	{
		// 
		// GraphicsSample
		// 
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.MinimumSize = new System.Drawing.Size(100, 100);
		#region MenuCreation/etc
		this.mnuMain = new System.Windows.Forms.MainMenu();
		this.mnuFile = new System.Windows.Forms.MenuItem();
		this.mnuGo = new System.Windows.Forms.MenuItem();
		this.mnuSingle = new System.Windows.Forms.MenuItem();
		this.mnuBreak1 = new System.Windows.Forms.MenuItem();
		this.mnuChange = new System.Windows.Forms.MenuItem();
		this.mnuBreak2 = new System.Windows.Forms.MenuItem();
		this.mnuExit = new System.Windows.Forms.MenuItem();
		// 
		// mainMenu1
		// 
		this.mnuMain.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																				this.mnuFile});
		// 
		// mnuFile
		// 
		this.mnuFile.Index = 0;
		this.mnuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																				this.mnuGo,
																				this.mnuSingle,
																				this.mnuBreak1,
																				this.mnuChange,
																				this.mnuBreak2,
																				this.mnuExit});
		this.mnuFile.Text = "&File";
		// 
		// mnuGo
		// 
		this.mnuGo.Index = 0;
		this.mnuGo.Text = "&Go/stop\tEnter";
		this.mnuGo.Click += new System.EventHandler(this.ToggleStart);
		// 
		// mnuSingle
		// 
		this.mnuSingle.Index = 1;
		this.mnuSingle.Text = "&Single step\tSpace";
		this.mnuSingle.Click += new System.EventHandler(this.SingleStep);
		// 
		// mnuBreak1
		// 
		this.mnuBreak1.Index = 2;
		this.mnuBreak1.Text = "-";
		// 
		// mnuChange
		// 
		this.mnuChange.Index = 3;
		this.mnuChange.Shortcut = System.Windows.Forms.Shortcut.F2;
		this.mnuChange.ShowShortcut = true;
		this.mnuChange.Text = "&Change Device...";
		this.mnuChange.Click += new System.EventHandler(this.UserSelectNewDevice);
		// 
		// mnuBreak2
		// 
		this.mnuBreak2.Index = 4;
		this.mnuBreak2.Text = "-";
		// 
		// mnuExit
		// 
		this.mnuExit.Index = 5;
		this.mnuExit.Text = "E&xit\tESC";
		this.mnuExit.Click += new System.EventHandler(this.Exit);
		#endregion
	}

	protected override void OnMenuStart(System.EventArgs e)
	{
		Pause(true); // Pause the simulation while the menu starts
	}

	protected override void OnMenuComplete(System.EventArgs e)
	{
		Pause(false); // Unpause the simulation 
	}

	protected override void OnSizeChanged(System.EventArgs e)
	{
		this.OnResize(null);
	}
	protected override void OnResize(System.EventArgs e)
	{
		if (!ready)
			return;

		active = !( this.WindowState == System.Windows.Forms.FormWindowState.Minimized || this.Visible == false );

		if( active && windowed )
		{
			System.Drawing.Rectangle rcClientOld;
			rcClientOld = clientRect;

			// Update window properties
			windowBoundsRect = new System.Drawing.Rectangle(this.Location, this.Size);
			clientRect = this.ClientRectangle;

			if( rcClientOld.Right - rcClientOld.Right !=
				clientRect.Right - clientRect.Left ||
				rcClientOld.Bottom - rcClientOld.Top !=
				clientRect.Bottom - clientRect.Top)
			{
				// A new window size will require a new backbuffer
				// size, so the 3D structures must be changed accordingly.
				ready = false;

				presentParams.BackBufferWidth  = clientRect.Right - clientRect.Left;
				presentParams.BackBufferHeight = clientRect.Bottom - clientRect.Top;

				// Resize the 3D environment
				try
				{
					Resize3DEnvironment();
				}
				catch 
				{
					DisplayErrorMsg( new GraphicsException(GraphicsException.ErrorCode.ResizeFailed), AppMsgType.ErrorAppMustExit);
				}

				ready = true;
			}
		}
	}
	#endregion

}

⌨️ 快捷键说明

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