internal void SetApplication(Application app, XNAWinFormsHostAppWrapper formsWrapper, WinFormsHostGraphicsDeviceService device)
		{
			this.device = device;
			this.application = app;
			this.winFormWrapper = formsWrapper;
			this.winFormWrapper.Exiting += new EventHandler(winFormWrapper_Exiting);
		}
		void winFormWrapper_Exiting(object sender, EventArgs e)
		{
			if (this.winFormWrapper != null)
				this.winFormWrapper.Exiting -= new EventHandler(winFormWrapper_Exiting);
			this.device = null;
			this.application = null;
			this.winFormWrapper = null;
		}
		/// <summary>
		/// Disposes the control.
		/// </summary>
		protected override void Dispose(bool disposing)
		{
			if (application != null)
				application.Shutdown();

			application = null;
			device = null;
			winFormWrapper = null;

			base.Dispose(disposing);
		}
		/// <summary>Construct the Xen GameComponent host</summary>
		public GameComponentHost(Game host, Application xenApplication, GraphicsDeviceManager graphicsManager)
			: base(host)
		{
			if (xenApplication == null || host == null || graphicsManager == null)
				throw new ArgumentNullException();

			this.game = host;
			this.graphicsManager = graphicsManager;
			this.application = xenApplication;

			xenApplication.Run(this);
		}
		public XnaGame(Application application)
		{
			//create the graphics device manager
			graphics = new GraphicsDeviceManager(this);
			graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;

			Content.RootDirectory = "Content";

			//create the xen host
			GameComponent component = new Xen.GameComponentHost(this, application, graphics);

			//add it to the xna component list
			this.Components.Add(component);
		}
		internal XNALogic(Application parent, object host, out Microsoft.Xna.Framework.GamerServices.GamerServicesComponent gamerServicesComponent)
		{
			this.state = new AppState(parent);
			this.parent = parent;

			parent.UpdateManager.Add(state);
			parent.UpdateManager.Add(parent);

			gamerServicesComponent = null;

			if (host is GameComponentHost)
			{
				this.xnaGame = new XNAGameComponentHostAppWrapper(this, parent, host as GameComponentHost);
			}
			else
			{
#if !XBOX360
				if (host == null)
				{
					this.xnaGame = new XNAGameAppWrapper(this, parent, out gamerServicesComponent);
				}
				//else
				//{
				//    WinFormsHostControl _host = host as WinFormsHostControl;
				//    if (_host != null)
				//        this.xnaGame = new XNAWinFormsHostAppWrapper(this, parent, _host);
				//    else
				//        this.xnaGame = new XNAGameAppWrapper(this, parent, out gamerServicesComponent);
				//}
#else
				this.xnaGame = new XNAGameAppWrapper(this, parent, out gamerServicesComponent);
#endif
			}

			this.Exiting += delegate
			{
				Xen.Graphics.Resource.ClearResourceTracking();
			};
		}
		public XnaDrawableGameComponentWrapper(Microsoft.Xna.Framework.DrawableGameComponent component, Xen.Graphics.DrawTarget owner, Application application)
			: base(component.Game)
		{
			if (owner == null || application == null) throw new ArgumentNullException();

			this.owner = owner;
			this.component = component;
			this.application = application;

			this.component.Initialize();
			this.DisposeHandler = new EventHandler<EventArgs>(OnDisposed);
			this.component.Disposed += DisposeHandler;

			//add this object to the main XNA update list, unless it's already there
			bool addToUpdateList = true;
			Microsoft.Xna.Framework.GameComponentCollection components = application.XnaComponents;

			foreach (Microsoft.Xna.Framework.GameComponent item in components)
			{
				XnaDrawableGameComponentWrapper wrapper = item as XnaDrawableGameComponentWrapper;
				if (item == component || (wrapper != null && wrapper.component == component))
				{
					//already in the list...
					addToUpdateList = false;
					break;
				}
			}

			if (addToUpdateList)
			{
				components.Add(this);

				this.UpdateOrderChangedHandler = new EventHandler<EventArgs>(OnUpdateOrderChangedHandler);
				this.component.UpdateOrderChanged += UpdateOrderChangedHandler;
			}
		}
			internal ApplicationProviderService(Application application)
			{
				this.app = application;
			}
		internal XNAGameAppWrapper(XNALogic logic, Application parent, out Microsoft.Xna.Framework.GamerServices.GamerServicesComponent gamerServicesComponent)
		{
			this.parent = parent;
			this.logic = logic;

			graphics = new GraphicsDeviceManager(this);
			graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>(PreparingDeviceSettings);
			graphics.PreferredDepthStencilFormat = DepthFormat.Depth24Stencil8;
			graphics.SynchronizeWithVerticalRetrace = true;
			graphics.GraphicsProfile = GraphicsProfile.HiDef;

			gamerServicesComponent = null;
			if (parent.ApplicationRequiresGamerServices)
			{
				if (gamerServicesRegistered)
					throw new InvalidOperationException("This sample requires the GamerServices component.\nUnfortunately, XNA does not let you create this component more than once in a session.\nPlease restart the application");

				gamerServicesRegistered = true;
				gamerServicesComponent = new Microsoft.Xna.Framework.GamerServices.GamerServicesComponent((Game)this);
				this.Components.Add(gamerServicesComponent);
			}
#if DEBUG
#if !XBOX360
			Window.Title += " [XEN DEBUG API - XNA 4 FX]";
#endif
#endif
			presentation = RenderTargetUsage.PlatformContents;

			parent.SetWindowSizeAndFormat(graphics.PreferredBackBufferWidth,graphics.PreferredBackBufferHeight,graphics.PreferredBackBufferFormat, graphics.PreferredDepthStencilFormat);

			parent.SetupGraphicsDeviceManager(graphics, ref presentation);

			//values may have changed in SetupGraphicsDeviceManager
			parent.SetWindowSizeAndFormat(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, graphics.PreferredBackBufferFormat, graphics.PreferredDepthStencilFormat);
		}
		internal XNAGameComponentHostAppWrapper(XNALogic logic, Application parent, GameComponentHost host)
		{
			this.parent = parent;
			this.logic = logic;

			this.control = host;
			this.game = host.Game;

			content = new ContentManager(game.Services);

			UpdateWindowSize();

			host.DrawEvent += new GameEvent(host_Draw);
			host.UpdateEvent += new GameEvent(host_Update);
			host.InitaliseEvent += new GameEvent(host_InitaliseEvent);
		}
		public GameStateManager(Application application)
		{
			this.playerIndex = PlayerIndex.One;
			this.application = application;
			
#if XBOX360
			//to begin with, have a 'StartScreenState' on the xbox
			this.SetState(new StartScreenState());
#else
			//otherwise jump straight to the menu
			this.SetState(new MenuState());
#endif
		}
		//create the state...
		//Note: this is done before the loading state is displayed.
		public PlayingState(Application application)
		{
			//create the threaded content register
			this.contentRegister = new ThreadedContentRegister(application);

			this.rand = new Random();

			this.spritePosition = new Vector2[SpriteCount];
			this.spriteVelocity = new Vector2[SpriteCount];
		}
		public ThreadedContentRegister(Application application)
		{
			//store a reference to the application for later..
			this.application = application;
			this.contentToLoad = new List<IContentOwner>();
		}
		//align the text elements
		private void SetupTextSize(Application application)
		{
			//setup the item sizes
			Vector2 windowSize = new Vector2(application.WindowWidth, application.WindowHeight);

			float height = 0;
			float maxWidth = 0;

			float offset = 4;
			//work out an approximate offset for title safety
			float titleOffsetX = Math.Max(16, application.WindowWidth - application.TitleSafeArea.Width) / 2;
			float titleOffsetY = Math.Max(16, application.WindowHeight - application.TitleSafeArea.Height) / 2;

			if (this.instance != null && this.instance.ShowOptions == false)
			{
				for (int i = 0; i < stateText.Count; i++)
					stateText[i].Visible = false;
			}

			//space the text out vertically
			for (int i = 0; i < stateText.Count; i++)
			{
				Vector2 size = stateText[i].Font.MeasureString((StringBuilder)stateText[i].Text);
				maxWidth = Math.Max(maxWidth, size.X);

				height += stateText[i].Font.LineSpacing + 2;
				stateText[i].Position = new Vector2(offset + titleOffsetX, height + offset + titleOffsetY);

				if (this.instance != null)
				{
					stateText[i].Visible = true;
					if (this.instance.ShowOptions == false)
						break;
				}

				if (visibleElementGap[i])
					height += 8;
			}

			//set the background to fill in
			this.backgroundContainer.Size = new Vector2(maxWidth + offset * 2, height + offset * 2);
			this.backgroundContainer.Position = new Vector2(titleOffsetX, titleOffsetY);

			if (this.helpText != null)
				this.helpText.Position = this.backgroundContainer.Position + new Vector2(this.backgroundContainer.Size.X + 16, this.helpText.Font.LineSpacing);
		}
		internal XNAWinFormsHostAppWrapper(XNALogic logic, Application parent, WinFormsHostControl host)
		{
			this.parent = parent;
			this.logic = logic;

			this.control = host;

			System.Windows.Forms.Control parentControl = this.control;
			while (parentControl != null)
			{
				if (parentControl is System.Windows.Forms.Form)
				{
					this.parentForm = (System.Windows.Forms.Form)parentControl;
					break;
				}
				parentControl = parentControl.Parent;
			}
			if (parentForm == null)
				throw new ArgumentException("Unable to find Parent Form for display handle");

			parentForm.MouseWheel += new System.Windows.Forms.MouseEventHandler(parentControl_MouseWheel);
			parentForm.FormClosed += new System.Windows.Forms.FormClosedEventHandler(parentForm_FormClosed);

			formsDeviceService = new WinFormsHostGraphicsDeviceService(this, this.control.ClientSize.Width, this.control.ClientSize.Height);

			services = new GameServiceContainer();
			services.AddService(typeof(IGraphicsDeviceService), formsDeviceService);
			services.AddService(typeof(IGraphicsDeviceManager), formsDeviceService);

			presentation = RenderTargetUsage.PlatformContents;
			content = new ContentManager(services);

			int width = 0;
			int height = 0;
			SurfaceFormat format = SurfaceFormat.Color;

			width = control.ClientSize.Width;
			height = control.ClientSize.Height;

			parent.SetWindowSizeAndFormat(width, height, format, DepthFormat.Depth24Stencil8);

			parent.SetupGraphicsDeviceManager(null, ref presentation);

			formsDeviceService.CreateDevice(presentation, host);

			host.SetApplication(parent, this, formsDeviceService);

			host.BeginInvoke((EventHandler)delegate
			{
				parent.SetGraphicsDevice(GraphicsDevice);
				logic.Initialise();
				logic.LoadContent();
			});
		}