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

📄 gameinput.cs

📁 Game Engine Desing Direct X and C#.rar
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections;
using System.Drawing;
using Microsoft.DirectX.DirectInput;
using Microsoft.DirectX;

namespace GameEngine
{
		#region delegates
		public delegate void ButtonAction();
		public delegate void AxisAction(int nCount);
		#endregion

	/// <summary>
	/// Summary description for GameInput.
	/// This class encapsulates the interface to the keyboard and mouse.  It provides
	/// a unified interface for the game engine where the status of any of these input devices
	/// may be polled.  More importantly it provides the capability to invoke a supplied method
	/// whenever a mapped keyboard or mouse button is pressed.
	/// </summary>
	public class GameInput : IDisposable
	{
		#region Attributes
		public  bool           m_bInitialized  = false;

		private KeyboardState  m_keydata       = null;
		private MouseState     m_mousedata     = new MouseState();
		private JoystickState  m_joystick      = new JoystickState();
		private int            m_NumPov        = 0;
		private Device         KeyboardDev     = null;
		private Device         MouseDev        = null;
		private Device         JoystickDev     = null;
		private bool           m_bJoystickSet  = false;
		private ArrayList      m_ActionMap     = new ArrayList();
		private ArrayList      m_AxisActionMap = new ArrayList();
		private int            m_MouseX        = 0;
		private int            m_MouseY        = 0;
		private int            m_MouseZ        = 0;
		private static Point   m_MousePoint;
		private System.Windows.Forms.Form m_form = null;		
			#endregion

		struct Mapping 
		{
			public int key;
			public ButtonAction action;
			public bool bOnTransition;
		}

		struct AxisMapping 
		{
			public int key;
			public AxisAction action;
		}

		/// <summary>
		/// return a reference to the DirectInput device
		/// </summary>
		/// <returns></returns>
		public Point GetMousePoint()
		{
			return m_MousePoint;
		}

		public GameInput(System.Windows.Forms.Form form)
		{
			m_form = form;

			try
			{

				m_MousePoint = new Point();
				m_MousePoint.X = 400;
				m_MousePoint.Y = 300;

				// create the keyboard device
				KeyboardDev = new Device( SystemGuid.Keyboard );
				KeyboardDev.SetCooperativeLevel( m_form, CooperativeLevelFlags.NoWindowsKey | CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Foreground);
				KeyboardDev.SetDataFormat(DeviceDataFormat.Keyboard);

				// create the mouse device
				MouseDev = new Device( SystemGuid.Mouse );
				MouseDev.SetCooperativeLevel( m_form, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Foreground);
				MouseDev.SetDataFormat(DeviceDataFormat.Mouse);

				// Enumerate joysticks in the system.
				foreach(DeviceInstance instance in Manager.GetDevices(DeviceClass.GameControl, EnumDevicesFlags.AttachedOnly))
				{
					// Create the device.  Just pick the first one
					JoystickDev = new Device( instance.InstanceGuid );
					break;
				}
				if ( JoystickDev != null )
				{
					// Create the device.
					// Set the cooperative level for the device.
					JoystickDev.SetCooperativeLevel( m_form, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Foreground);
					// Set the data format to the Joystick pre-defined format.
					JoystickDev.SetDataFormat(DeviceDataFormat.Joystick);

					// Now find out how many POV's the device has
					// for displaying info in the UI thread.
					m_NumPov = JoystickDev.Caps.NumberPointOfViews;
					m_bJoystickSet = true;
					try
					{
						JoystickDev.Acquire();
					}
					catch{}
				}
			}
			catch {}

		}

		// poll method
		public void Poll()
		{
		KeyboardState oldkeydata = null;

		// Bool flag that is set when it's ok
		// to get device state information.
		bool bKeyboardOk = false;
		bool bMouseOk = false;
		bool bJoystickOk = false;
			

			MouseState oldmousedata = new MouseState();
			JoystickState oldjoystickdata = new JoystickState();

			oldkeydata = m_keydata;

			// get keyboard data
			try
			{
				KeyboardDev.Poll();
				bKeyboardOk = true;
			}
			catch(InputException ex)
			{
				// Check to see if either the app
				// needs to acquire the device, or
				// if the app lost the device to another
				// process.
				if ( (ex is NotAcquiredException)  )
				{
					try
					{
						// Reacquire the device.
						KeyboardDev.Acquire();
						// Set the flag for now.
						bKeyboardOk = true;
					}
					catch(InputException ex2)
					{								
						if ( ex2 is OtherApplicationHasPriorityException)
						{	// Something very odd happened.
							Console.AddLine("An unknown error has occcurred. This app won't be able to process device info.");
						}
						// Failed to aquire the device.
						// This could be because the app
						// doesn't have focus.
						bKeyboardOk = false;
					}
				}
				else
				{
					KeyboardDev = new Device( SystemGuid.Keyboard );
					KeyboardDev.SetCooperativeLevel( m_form, CooperativeLevelFlags.NoWindowsKey | CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Foreground);
					KeyboardDev.SetDataFormat(DeviceDataFormat.Keyboard);
				}
			}

			// get mouse data
			try
			{
				MouseDev.Poll();
				bMouseOk = true;
			}
			catch(InputException ex)
			{
				// Check to see if either the app
				// needs to acquire the device, or
				// if the app lost the device to another
				// process.
				if ( (ex is NotAcquiredException) )
				{
					try
					{
						// Reacquire the device.
						MouseDev.Acquire();
						// Set the flag for now.
						bMouseOk = true;
//						Console.AddLine("had to reacquire the mouse");
					}
					catch(InputException ex2)
					{								
						if ( ex2 is OtherApplicationHasPriorityException )
						{	// Something very odd happened.
							System.Diagnostics.Debug.WriteLine("An unknown error has occcurred. This app won't be able to process device info. " + ex2.ErrorString);
						}
						// Failed to aquire the device.
						// This could be because the app
						// doesn't have focus.
						bMouseOk = false;
					}
				}
				else
				{
					System.Diagnostics.Debug.WriteLine(ex.ErrorString);
					MouseDev.Dispose();
					MouseDev = new Device( SystemGuid.Mouse );
					MouseDev.SetCooperativeLevel( m_form, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Foreground);
					MouseDev.SetDataFormat(DeviceDataFormat.Mouse);
				}
			}

			// get joystick data
			try
			{
				if ( m_bJoystickSet ) 
				{
					JoystickDev.Poll();
					bJoystickOk = true;
				}
			}
			catch(InputException ex)
			{
				// Check to see if either the app
				// needs to acquire the device, or
				// if the app lost the device to another
				// process.
				if ( (ex is NotAcquiredException) )
				{
					try
					{
						if ( JoystickDev != null )
						{
							// Create the device.
							// Set the cooperative level for the device.
							JoystickDev.SetCooperativeLevel( m_form, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Foreground);
							// Set the data format to the Joystick pre-defined format.
							JoystickDev.SetDataFormat(DeviceDataFormat.Joystick);

							// Now find out how many POV's the device has
							// for displaying info in the UI thread.
							m_NumPov = JoystickDev.Caps.NumberPointOfViews;
							m_bJoystickSet = true;
						}
						// Reacquire the device.
						JoystickDev.Acquire();
						// Set the flag for now.
						bJoystickOk = true;
					}
					catch(InputException ex2)
					{								
						if (  ex2 is OtherApplicationHasPriorityException )
						{	// Something very odd happened.
							Console.AddLine("An unknown error has occcurred. This app won't be able to process device info.");
						}
						// Failed to aquire the device.
						// This could be because the app
						// doesn't have focus.
						bJoystickOk = false;
					}
				}
				else 
				{
					if ( JoystickDev != null )
					{
						// Create the device.
						// Set the cooperative level for the device.
						JoystickDev.SetCooperativeLevel( m_form, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Foreground);
						// Set the data format to the Joystick pre-defined format.
						JoystickDev.SetDataFormat(DeviceDataFormat.Joystick);

						// Now find out how many POV's the device has
						// for displaying info in the UI thread.
						m_NumPov = JoystickDev.Caps.NumberPointOfViews;
						m_bJoystickSet = true;
						// Reacquire the device.
						try
						{
							JoystickDev.Acquire();
						}
						catch{}

						// Set the flag for now.
						bJoystickOk = true;
					}
				}
			}
			if (bJoystickOk == true)
			{
				// Get the state of the device
				try	{ m_joystick = JoystickDev.CurrentJoystickState; }
					// Catch any exceptions. None will be handled here, 
					// any device re-aquisition will be handled above.	
				catch(DirectXException){}
			}

			try 
			{
			    if ( bKeyboardOk )
				{
				   m_keydata = KeyboardDev.GetCurrentKeyboardState();
				}
			    if ( bMouseOk )
				{
				    m_mousedata = MouseDev.CurrentMouseState;
					m_MouseX += m_mousedata.X;
					m_MouseY += m_mousedata.Y;
					m_MouseZ += m_mousedata.Z;

					m_MousePoint.X += m_mousedata.X;
					m_MousePoint.Y += m_mousedata.Y;

				}

				// call any axis actions
				foreach ( AxisMapping map in m_AxisActionMap )
				{
					switch ( map.key ) 
					{
						case 0:
							map.action(m_mousedata.X);
							break;
						case 1:
							map.action(m_mousedata.Y);
							break;
						case 2:
							map.action(m_mousedata.Z);
							break;
						case 3:
							map.action(m_joystick.X);
							break;
						case 4:
							map.action(m_joystick.X);
							break;
						case 5:
							map.action(m_joystick.X);
							break;

⌨️ 快捷键说明

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