protected override void OnCreateControl() { if( !DesignMode ) { _deviceService = GraphicsDeviceService.AddRef( Handle, ClientSize.Width, ClientSize.Height ); ServiceLocator.Add<IGraphicsDeviceService>( _deviceService ); ServiceLocator.Add( _deviceService.GraphicsDevice ); ServiceLocator.Add<IEntityService>( new EntityService() ); ServiceLocator.Add<ICollisionService>( new CollisionService() ); _camera = new Camera( _deviceService.GraphicsDevice.Viewport ); if( !ServiceLocator.Has<Camera>() ) ServiceLocator.Add( _camera ); if( ControlInitializing != null ) { ControlInitializing( this, EventArgs.Empty ); } // Start the animation timer. _timer = Stopwatch.StartNew(); Initialize(); if( ControlInitialized != null ) { ControlInitialized( this, EventArgs.Empty ); } Application.Idle += ( o, args ) => Invalidate( true ); } }
/// <summary> /// Gets a reference to the singleton instance. /// </summary> public static GraphicsDeviceService AddRef( IntPtr windowHandle, int width, int height ) { // Increment the "how many controls sharing the device" reference count. if( Interlocked.Increment( ref _referenceCount ) == 1 ) { // If this is the first control to start using the // device, we must create the singleton instance. _singletonInstance = new GraphicsDeviceService( windowHandle, width, height ); } return _singletonInstance; }
protected override void Dispose( bool disposing ) { if( _deviceService != null ) { try { _deviceService.Release(); } catch { } _deviceService = null; } base.Dispose( disposing ); }