home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / DirectInput / Mouse / Main.cs < prev    next >
Encoding:
Text File  |  2004-09-27  |  4.0 KB  |  139 lines

  1. using System;
  2. using System.Threading;
  3. using Microsoft.DirectX.DirectInput;
  4. using Microsoft.DirectX;
  5.  
  6. namespace Mouse
  7. {
  8.     public class cMain : IDisposable
  9.     {
  10.         // These two fields hold common data that
  11.         // different threads will have to access
  12.         public static MouseState    g_dims            = new MouseState();
  13.         public static bool            g_bRunning        = true;
  14.  
  15.         InputObject    m_di            = null;
  16.         Device        m_dev            = null;
  17.         Thread        m_threadInput    = null;                
  18.         frmUI        m_UI            = null;
  19.  
  20.         public cMain()
  21.         {
  22.             // Create an instance of the UI form.
  23.             m_UI = new frmUI();
  24.             // Create a DirectInputObject object.
  25.             m_di = new InputObject( DXHelp.AppInstance );
  26.             // Create the device.
  27.             m_dev = new Device(SystemGuid.Mouse, m_di);
  28.             // Set the cooperative level for the device.
  29.             m_dev.SetCooperativeLevel(m_UI.Handle, CooperativeLevelFlags.Exclusive | CooperativeLevelFlags.Foreground);
  30.             // Set the data format to the mouse2 pre-defined format.
  31.             m_dev.SetDataFormat(DeviceDataFormat.Mouse);
  32.             // Must allocate memory for arrays inside
  33.             // the C# app, since no fixed-length arrays
  34.             // are permitted inside of structures.            
  35.             g_dims.Buttons = new byte[8];
  36.             // Create the thread that the app will use
  37.             // to get state information from the device.
  38.             m_threadInput = new Thread(new ThreadStart(DIThread) );
  39.             // Name it something so we can see in the output
  40.             // window when it has exited.
  41.             m_threadInput.Name = "DIThread";
  42.         }
  43.         public void Start()
  44.         {
  45.             // Start the thread.
  46.             m_threadInput.Start();
  47.             // Show the UI form.
  48.             m_UI.ShowDialog();
  49.             // Wait until the input thread exits.
  50.             m_threadInput.Join();
  51.             // Call the function that destroys all the objects.
  52.             this.Dispose();
  53.         }
  54.         /// <summary>
  55.         /// The main entry point for the application.
  56.         /// </summary>
  57.         [MTAThread]
  58.         static void Main()
  59.         {
  60.             cMain main = new cMain();
  61.             main.Start();
  62.         }
  63.         public void Dispose()
  64.         {
  65.             // Unacquire and destroy all Dinput objects.
  66.             m_dev.Unacquire();
  67.             m_dev.Dispose();
  68.             m_di.Dispose();
  69.         }
  70.         public void DIThread()
  71.         {
  72.             // The actual input loop runs in this thread.
  73.  
  74.             // Bool flag that is set when it's ok
  75.             // to get device state information.
  76.             bool bOk = false;
  77.  
  78.             // Make sure there is a valid device.
  79.             if (m_dev != null)
  80.             {                
  81.                 // Keep looping.
  82.                 while (g_bRunning)
  83.                 {
  84.                     try
  85.                     {
  86.                         // Don't really need to poll a mouse, but
  87.                         // this is a good way to check if the app
  88.                         // can get the device state.
  89.                         m_dev.Poll();
  90.                     }
  91.                     catch(DirectXException ex)
  92.                     {
  93.                         // Check to see if either the app
  94.                         // needs to acquire the device, or
  95.                         // if the app lost the mouse to another
  96.                         // process.
  97.                         if ( (ex.ErrorCode == (int)ErrorCode.NotAcquired) || (ex.ErrorCode ==(int)ErrorCode.InputLost) )
  98.                         {
  99.                             try
  100.                             {
  101.                                 // Acquire the device.
  102.                                 m_dev.Acquire();
  103.                                 // Set the flag for now.
  104.                                 bOk = true;
  105.                             }
  106.                             catch(DirectXException ex2)
  107.                             {                                
  108.                                 if ( ex2.ErrorCode != (int)ErrorCode.OtherAppHasPrio )
  109.                                 {    // Something very odd happened.
  110.                                     throw new Exception("An unknown error has occcurred. This app won't be able to process device info.");
  111.                                 }
  112.                                 // Failed to aquire the device.
  113.                                 // This could be because the app
  114.                                 // doesn't have focus.
  115.                                 bOk = false;
  116.                             }
  117.                         }
  118.                     }
  119.                     if (bOk == true)
  120.                     {
  121.                         // Lock the UI class so it can't overwrite
  122.                         // the g_dims structure during a race condition.
  123.                         lock(m_UI)
  124.                         {
  125.                             // Get the state of the device
  126.                             try    { g_dims = m_dev.CurrentMouseState; }
  127.                             // Catch any exceptions. None will be handled here, 
  128.                             // any device re-aquisition will be handled above.    
  129.                             catch(DirectXException){}
  130.                         }
  131.                         // Call the function in the other thread that updates the UI.
  132.                         m_UI.UpdateUI();
  133.                     }
  134.                 } //while (g_bRunning)
  135.             } //if (m_dev != null)
  136.         }
  137.     }
  138. }
  139.