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

📄 app.cs

📁 Introduction to 3d game engine design一书的源代码!
💻 CS
📖 第 1 页 / 共 2 页
字号:
//-----------------------------------------------------------------------------
// File: App.cs
//
// Desc: Sample code for Introduction to 3D Game Engine Design.
//
//       This sample shows the basic application software that sets up the
//       base application and the process flow.  The application uses a version of the
//       CD3DApplication base class provided with the Microsoft DirectX 9 SDK to
//       perform the standard initialization of DirectX.
//
//       Note: This code uses the D3D Framework helper library.
//
// Copyright (c) 2002 Lynn T. Harrison All rights reserved.
//-----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Collections;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX.DirectInput;
using GameEngine;
using GameAI;

namespace SampleGame
{
	/// <summary>
	/// Summary description for GameEngine.
	/// </summary>
	class CGameApplication : GraphicsSample
	{
		#region	// Game State enumeration
		/// <summary>
		/// Each member of this enumeration is one possible state for the application
		/// </summary>
		/// 
		/// <remarks>
		/// DevSplash         - Display the Developer splash screen
		/// </remarks>
		/// <remarks>
		/// GameSplash        - Display the game splash screen
		/// </remarks>
		/// <remarks>
		/// OptionsMain       - Displays and process the primary options screen
		/// </remarks>
		/// <remarks>
		/// GamePlay          - state to actually play the game
		/// </remarks>
		/// <remarks>
		/// AfterActionReview - Display the results of the game
		/// </remarks>
		public enum GameState
		{
			/// <summary>
			/// Display the Developer splash screen
			/// </summary>
			DevSplash,	
			/// <summary>
			/// Display the game splash screen
			/// </summary>
			GameSplash,		
			/// <summary>
			/// Displays and process the primary options screen
			/// </summary>
			OptionsMain,		
			/// <summary>
			/// state to actually play the game
			/// </summary>
			GamePlay,			 
			/// <summary>
			/// Display the results of the game
			/// </summary>
			AfterActionReview,	 
		}
	#endregion

		#region // Application member variables
		/// <summary>
		/// Current state of the application
		/// </summary>
		private GameState          m_State; 
		private static CGameEngine m_Engine = new CGameEngine();  // connection to the game engine
		private GraphicsFont       m_pFont = null;  // font for screen text rendering
		private GameEngine.Console m_Console;
		private ArrayList          m_opponents = null;
		private OptionScreen       m_OptionScreen = null;
		private bool			   m_bShowStatistics = false;
		private bool               m_bScreenCapture = false;
		private bool               m_bUsingJoystick = true;
		private bool               m_bUsingKeyboard = false;
		private bool               m_bUsingMouse = false;
		private Ownship            m_ownship = null;
		private Cloth			   m_flag = null;
		private Jukebox            music = null;
		#endregion

		public static CGameEngine Engine { get { return m_Engine; } }
		
		/// <summary>
		/// Application constructor. Sets attributes for the app.
		/// </summary>
		public CGameApplication()
		{
			// Initialize the Game state for the Developer Splash Screen
			m_State = GameState.DevSplash;

			// create a copy of the game engine
			m_pFont = new GraphicsFont( "Aerial", System.Drawing.FontStyle.Bold );
			windowed = false;

			m_opponents = new ArrayList();

		}

		/// <summary>
		/// Called during initial app startup, this function performs all the
		/// permanent initialization.
		/// </summary>
		protected override void OneTimeSceneInitialization()
		{
			// Initialize the font's internal textures
			m_pFont.InitializeDeviceObjects( device );

			m_Engine.Initialize( this, device );

			CGameEngine.Inputs.MapKeyboardAction(Key.Escape,new ButtonAction(Terminate), true);  
			CGameEngine.Inputs.MapKeyboardAction(Key.A,new ButtonAction(MoveCameraXM), false);  
			CGameEngine.Inputs.MapKeyboardAction(Key.W,new ButtonAction(MoveCameraZP), false);  
			CGameEngine.Inputs.MapKeyboardAction(Key.S,new ButtonAction(MoveCameraXP), false);  
			CGameEngine.Inputs.MapKeyboardAction(Key.Z,new ButtonAction(MoveCameraZM), false);  
			CGameEngine.Inputs.MapKeyboardAction(Key.P,new ButtonAction(ScreenCapture), true);  
			CGameEngine.Inputs.MapMouseAxisAction(0,new AxisAction(PointCamera));  
			CGameEngine.Inputs.MapMouseAxisAction(1,new AxisAction(PitchCamera));  

			m_Console = new GameEngine.Console( m_pFont, @"..\..\Resources\console.jpg" );

			GameEngine.Console.AddCommand("QUIT", "Terminate the game", new CommandFunction(TerminateCommand));
			GameEngine.Console.AddCommand("STATISTICS", "Toggle statistics display", new CommandFunction(ToggleStatistics));

			m_OptionScreen = new OptionScreen( @"..\..\Resources\Options2.jpg" );
			m_OptionScreen.AddButton( 328, 150, @"..\..\Resources\PlayOff.bmp", @"..\..\Resources\PlayOn.bmp", @"..\..\Resources\PlayHover.bmp", new ButtonFunction(Play) );
			m_OptionScreen.AddButton( 328, 300, @"..\..\Resources\QuitOff.bmp", @"..\..\Resources\QuitOn.bmp", @"..\..\Resources\QuitHover.bmp", new ButtonFunction(Terminate) );
			m_Engine.SetOptionScreen( m_OptionScreen );

			music = new Jukebox();
			music.AddSong("nadine.mp3");
			music.AddSong("ComeOn.mp3");
			music.AddSong("Rock.mp3");
			music.Volume = 0.75f;
			music.Play();

		}


		/// <summary>
		/// Called once per frame, the call is the entry point for all game processing. 
		/// This function calls the appropriate part of the game engine based on the
		/// current state.
		/// </summary>
		protected override void FrameMove()
		{
			try
			{
				SelectControls select_form = null;
				// get any player inputs
				m_Engine.GetPlayerInputs();

				// Clear the viewport
				device.Clear( ClearFlags.Target | ClearFlags.ZBuffer, 0x00000000, 1.0f, 0 );

				device.BeginScene();

				// determine what needs to be rendered based on the current game state
				switch ( m_State ) 
				{
					case GameState.DevSplash:
						if ( m_Engine.ShowSplash(@"..\..\Resources\devsplash.jpg", 8, new BackgroundTask(LoadOptions)) ) 
						{
							m_State = GameState.GameSplash;
						}
						break;
					case GameState.GameSplash:
						if ( m_Engine.ShowSplash(@"..\..\Resources\gamesplash.jpg", 8, null) ) 
						{
							m_State = GameState.OptionsMain;
							select_form = new SelectControls();
							select_form.ShowDialog(this);
							m_bUsingJoystick = select_form.UseJoystick.Checked;
							m_bUsingKeyboard = select_form.UseKeyboard.Checked;
							m_bUsingMouse = select_form.UseMouse.Checked;
							if ( m_bUsingJoystick ) GameEngine.Console.AddLine("Using Joystick");
							if ( m_bUsingKeyboard ) GameEngine.Console.AddLine("Using Keyboard");
							if ( m_bUsingMouse ) GameEngine.Console.AddLine("Using Mouse");
							m_ownship = (Ownship)Engine.GetObject("car1");
							m_ownship.UseJoystick = m_bUsingJoystick;
							m_ownship.UseKeyboard = m_bUsingKeyboard;
							m_ownship.UseMouse = m_bUsingMouse;
						}
						break;
					case GameState.OptionsMain:
						m_Engine.DoOptions();
						break;
					case GameState.GamePlay:
						m_Engine.GetPlayerInputs();
						m_Engine.DoAI( elapsedTime );
						m_Engine.DoDynamics( elapsedTime );
						m_Engine.DoNetworking( elapsedTime );
						m_Engine.Render();
						break;
					case GameState.AfterActionReview:
						m_Engine.DoAfterActionReview();
						break;
				}

				GameEngine.Console.Render();

				if ( false && m_ownship != null && m_State == GameState.GamePlay )
				{
					m_pFont.DrawText( 200, 560, Color.FromArgb(255,0,0,0), m_ownship.MPH.ToString() );
					m_pFont.DrawText( 280, 560, Color.FromArgb(255,0,0,0), m_ownship.RPM.ToString() );
					m_pFont.DrawText( 200, 580, Color.FromArgb(255,0,0,0), m_ownship.ForwardVelocity.ToString() );
					m_pFont.DrawText( 100, 560, Color.FromArgb(255,0,0,0), m_ownship.SidewaysVelocity.ToString() );
					m_pFont.DrawText( 100, 580, Color.FromArgb(255,0,0,0), m_ownship.Steering.ToString() );
				}

				// Output statistics
				if ( m_bShowStatistics )
				{
					m_pFont.DrawText( 2, 560, Color.FromArgb(255,255,255,0), frameStats );
					m_pFont.DrawText( 2, 580, Color.FromArgb(255,255,255,0), deviceStats );
					m_pFont.DrawText( 500, 580, Color.FromArgb(255,255,255,0), 
						m_Engine.Cam.Heading.ToString() + " " + m_Engine.Cam.Pitch.ToString()  + " " +
						m_Engine.Cam.X  + " " +m_Engine.Cam.Y  + " " +m_Engine.Cam.Z);
					m_pFont.DrawText( 2, 600, Color.FromArgb(255,255,255,0), 
						"Steering " + (CGameEngine.Inputs.GetJoystickNormalX()-1.0f).ToString()  + " " +
						"throttle/Brake " + (1.0f-CGameEngine.Inputs.GetJoystickNormalY()).ToString());
				}

				if ( m_bScreenCapture )
				{
					SurfaceLoader.Save("capture.bmp",ImageFileFormat.Bmp,device.GetBackBuffer(0,0,BackBufferType.Mono));
					m_bScreenCapture = false;
					GameEngine.Console.AddLine("snapshot taken");
				}
			}
			catch (DirectXException d3de)
			{
				System.Diagnostics.Debug.WriteLine("Error in Sample Game Application FrameMove" );
				System.Diagnostics.Debug.WriteLine(d3de.ErrorString);
			}
			catch ( Exception e )
			{
				System.Diagnostics.Debug.WriteLine("Error in Sample Game Application FrameMove" );
				System.Diagnostics.Debug.WriteLine(e.Message);
			}
			finally
			{
				device.EndScene();
			}


		}


		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			try
			{
				CGameApplication d3dApp = new CGameApplication();
				if (d3dApp.CreateGraphicsSample())
					d3dApp.Run();
			}
			catch (DirectXException d3de)
			{
				System.Diagnostics.Debug.WriteLine("Error in Sample Game Application" );
				System.Diagnostics.Debug.WriteLine(d3de.ErrorString);
			}
			catch ( Exception e )
			{
				System.Diagnostics.Debug.WriteLine("Error in Sample Game Application" );
				System.Diagnostics.Debug.WriteLine(e.Message);
			}
		}

⌨️ 快捷键说明

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