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

📄 d3dapp.cs

📁 Game Engine Desing Direct X and C#.rar
💻 CS
📖 第 1 页 / 共 4 页
字号:
			// Stop the scene from animating
			if( frameMoving )
				DXUtil.Timer( TIMER.STOP );
		}

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




    /// <summary>
    /// Set our variables to not active and not ready
    /// </summary>
	public void CleanupEnvironment()
	{
		active = false;
		ready  = false;
		if (device != null)
			device.Dispose();

		device = null;
	}
	#region Menu EventHandlers



    /// <summary>
    /// Prepares the simulation for a new device being selected
    /// </summary>
	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);
		}
	}



    /// <summary>
    /// Displays a dialog so the user can select a new adapter, device, or
    /// display mode, and then recreates the 3D environment if needed
    /// </summary>
	private void DoSelectNewDevice()
	{
		// Can't display dialogs in fullscreen mode
		if( windowed == false )
		{
			try
			{
				ToggleFullscreen();
			}
			catch
			{
				HandleSampleException( new ResetFailedException(), ApplicationMessage.ApplicationMustExit);
				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
		{
			InitializeEnvironment();
		}
		catch(SampleException d3de)
		{
			HandleSampleException( d3de, ApplicationMessage.ApplicationMustExit );
		}
		catch
		{
			HandleSampleException( new SampleException(), ApplicationMessage.ApplicationMustExit );
		}

		// 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 );
		}
	}



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



    /// <summary>
    /// Will single step the simulation
    /// </summary>
	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;
	}



    /// <summary>
    /// Will end the simulation
    /// </summary>
	private void ExitSample(object sender, EventArgs e)
	{
		this.Close();
	}
	#endregion

	#region WinForms Overrides

    /// <summary>
    /// Clean up any resources
    /// </summary>
	protected override void Dispose(bool disposing)
	{
		CleanupEnvironment();
		mnuMain.Dispose();
		base.Dispose(disposing);
	}

    /// <summary>
    /// Handle any key presses
    /// </summary>
	protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
	{

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

		// Allow the control to handle the keystroke now
		base.OnKeyPress(e);
	}


    /// <summary>
    /// Handle system keystrokes (ie, alt-enter)
    /// </summary>
	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
				{
					HandleSampleException( new ResetFailedException(), ApplicationMessage.ApplicationMustExit);
				}
				finally
				{
					e.Handled = true;
				}
			}
		}
		// Allow the control to handle the keystroke now
		//		base.OnKeyDown(e);
	}



    /// <summary>
    /// Winforms generated code for initializing the form
    /// </summary>
	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.ExitSample);
		#endregion
	}


    /// <summary>
    /// When the menu is starting pause our simulation
    /// </summary>
	protected override void OnMenuStart(System.EventArgs e)
	{
		Pause(true); // Pause the simulation while the menu starts
	}


    /// <summary>
    /// When the menu is complete our simulation can continue
    /// </summary>
    protected override void OnMenuComplete(System.EventArgs e)
	{
		Pause(false); // Unpause the simulation 
	}


    /// <summary>
    /// Make sure our graphics cursor (if available) moves with the cursor
    /// </summary>
	protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
	{
		// Move the D3D cursor
		device.SetCursorPosition(e.X, e.Y, false);
		// Let the control handle the mouse now
		base.OnMouseMove(e);
	}


    /// <summary>
    /// Handle size changed events
    /// </summary>
	protected override void OnSizeChanged(System.EventArgs e)
	{
		this.OnResize(e);
		base.OnSizeChanged(e);
	}



    /// <summary>
    /// Handle resize events
    /// </summary>
	protected override void OnResize(System.EventArgs e)
	{
		if (isHandlingSizeChanges)
		{
			// Are we maximized?
			isMaximized = (this.WindowState == System.Windows.Forms.FormWindowState.Maximized);
            if (!isMaximized)
                storedSize = this.ClientSize;
		}
		active = !( this.WindowState == System.Windows.Forms.FormWindowState.Minimized || this.Visible == false );
		base.OnResize(e);
	}
	#endregion

}

⌨️ 快捷键说明

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