/// <summary> /// Releases the unmanaged resources used by the AccelerometerInput and optionally /// releases the managed resources. /// </summary> /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param> protected override void Dispose(bool disposing) { if (!isDisposed) { if (disposing) { // Properly dispose of the accelerometer try { if (accelerometer != null) { lock (accelerometerDataLock) { // Always unregister event handlers to avoid a zombie objects, even where you're sure it'll die anyway. accelerometer.ReadingChanged -= accelerometer_ReadingChanged; accelerometer.Dispose(); accelerometer = null; } } } catch { // Goonies never say die and Dispose never throws exceptions! } } _instance = null; isDisposed = true; } try { base.Dispose(disposing); } catch { } }
/// <summary> /// The constructor for this game component. Do not create more than one instance of this. /// </summary> /// <param name="game">The game this component is being created for.</param> /// <exception cref="InvalidOperationException">Thrown when you try to create more than one instance of this game component.</exception> public AccelerometerInput(Game game) : base(game) { // Create the accelerometer data queue. We create it with numberOfReadingsToAverage + 1 so that // we can just add readings and then if it's over the count, dequeue the first one without creating // any allocations. accelerometerDataQueue = new Queue<Vector3>(numberOfReadingsToAverage + 1); adjustedAccelerometerDataQueue = new Queue<Vector2>(numberOfReadingsToAverage + 1); // Create our accelerometer instance accelerometer = new Accelerometer(); // Hook up the ReadingChanged event handler. accelerometer.ReadingChanged += new EventHandler<AccelerometerReadingEventArgs>(accelerometer_ReadingChanged); if (_instance != null) { throw new InvalidOperationException("You must only have one instance of this game component. You have tried to create a second one."); } _instance = this; }