Mixing MonoGame with others UIViewControllers

Hi guys,

I’m trying to use a Game object as a normal ViewController. My intention is to use the game in fullscreen mode. I could show the game and close it, but when I try to reopen it, it crashes.

I’m doing this:

button.TouchUpInside += (sender, e) => {
				var game = new Game1 ();
		        game.Run();
				UIApplication.SharedApplication.Windows[1].Dispose(); //unsafe way to access to the monogame's windows			
				UIApplication.SharedApplication.Windows[0].MakeKeyAndVisible();

			};

Is there a better way to do this without changing things on the monogame’s source?

Here is the trace:

Unhandled Exception:
Microsoft.Xna.Framework.Graphics.MonoGameGLException: GL.GetError() returned InvalidValue
  at Microsoft.Xna.Framework.Graphics.GraphicsExtensions.CheckGLError () [0x00022] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/GraphicsExtensions.cs:815 
  at Microsoft.Xna.Framework.Graphics.ShaderProgramCache.Link (Microsoft.Xna.Framework.Graphics.Shader vertexShader, Microsoft.Xna.Framework.Graphics.Shader pixelShader) [0x00018] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/Shader/ShaderProgramCache.cs:108 
  at Microsoft.Xna.Framework.Graphics.ShaderProgramCache.GetProgram (Microsoft.Xna.Framework.Graphics.Shader vertexShader, Microsoft.Xna.Framework.Graphics.Shader pixelShader) [0x00024] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/Shader/ShaderProgramCache.cs:93 
  at Microsoft.Xna.Framework.Graphics.GraphicsDevice.ActivateShaderProgram () [0x00013] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/GraphicsDevice.OpenGL.cs:752 
  at Microsoft.Xna.Framework.Graphics.GraphicsDevice.PlatformApplyState (Boolean applyShaders) [0x00192] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/GraphicsDevice.OpenGL.cs:870 
  at Microsoft.Xna.Framework.Graphics.GraphicsDevice.PlatformDrawUserIndexedPrimitives[VertexPositionColorTexture] (PrimitiveType primitiveType, Microsoft.Xna.Framework.Graphics.VertexPositionColorTexture[] vertexData, Int32 vertexOffset, Int32 numVertices, System.Int16[] indexData, Int32 indexOffset, Int32 primitiveCount, Microsoft.Xna.Framework.Graphics.VertexDeclaration vertexDeclaration) [0x00003] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/GraphicsDevice.OpenGL.cs:945 
  at Microsoft.Xna.Framework.Graphics.GraphicsDevice.DrawUserIndexedPrimitives[VertexPositionColorTexture] (PrimitiveType primitiveType, Microsoft.Xna.Framework.Graphics.VertexPositionColorTexture[] vertexData, Int32 vertexOffset, Int32 numVertices, System.Int16[] indexData, Int32 indexOffset, Int32 primitiveCount, Microsoft.Xna.Framework.Graphics.VertexDeclaration vertexDeclaration) [0x00043] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/GraphicsDevice.cs:660 
  at Microsoft.Xna.Framework.Graphics.SpriteBatcher.FlushVertexArray (Int32 start, Int32 end, Microsoft.Xna.Framework.Graphics.Effect effect) [0x000a9] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/SpriteBatcher.cs:311 
  at Microsoft.Xna.Framework.Graphics.SpriteBatcher.DrawBatch (SpriteSortMode sortMode, Microsoft.Xna.Framework.Graphics.Effect effect) [0x001eb] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/SpriteBatcher.cs:268 
  at Microsoft.Xna.Framework.Graphics.SpriteBatch.End () [0x0002c] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/SpriteBatch.cs:99 
  at MonoGameiOS.Game1.Draw (Microsoft.Xna.Framework.GameTime gameTime) [0x0004c] in /Users/jmgomez/Projects/MonoGameiOS/MonoGameiOS/Game1.cs:114 
  at Microsoft.Xna.Framework.Game.DoDraw (Microsoft.Xna.Framework.GameTime gameTime) [0x00026] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Game.cs:658 
  at Microsoft.Xna.Framework.Game.Tick () [0x00235] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Game.cs:493 
  at Microsoft.Xna.Framework.iOSGamePlatform.Tick () [0x0003c] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/iOS/iOSGamePlatform.cs:225 
  at Microsoft.Xna.Framework.iOSGameView.DoTick () [0x00007] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/iOS/iOSGameView.cs:202 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:39 
  at MonoGameiOS.Program.Main (System.String[] args) [0x00008] in /Users/jmgomez/Projects/MonoGameiOS/MonoGameiOS/Main.cs:40 
2014-08-10 18:43:14.298 MonoGameiOS[3170:70b] Unhandled managed exception: GL.GetError() returned InvalidValue (Microsoft.Xna.Framework.Graphics.MonoGameGLException)
  at Microsoft.Xna.Framework.Graphics.GraphicsExtensions.CheckGLError () [0x00022] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/GraphicsExtensions.cs:815 
  at Microsoft.Xna.Framework.Graphics.ShaderProgramCache.Link (Microsoft.Xna.Framework.Graphics.Shader vertexShader, Microsoft.Xna.Framework.Graphics.Shader pixelShader) [0x00018] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/Shader/ShaderProgramCache.cs:108 
  at Microsoft.Xna.Framework.Graphics.ShaderProgramCache.GetProgram (Microsoft.Xna.Framework.Graphics.Shader vertexShader, Microsoft.Xna.Framework.Graphics.Shader pixelShader) [0x00024] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/Shader/ShaderProgramCache.cs:93 
  at Microsoft.Xna.Framework.Graphics.GraphicsDevice.ActivateShaderProgram () [0x00013] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/GraphicsDevice.OpenGL.cs:752 
  at Microsoft.Xna.Framework.Graphics.GraphicsDevice.PlatformApplyState (Boolean applyShaders) [0x00192] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/GraphicsDevice.OpenGL.cs:870 
  at Microsoft.Xna.Framework.Graphics.GraphicsDevice.PlatformDrawUserIndexedPrimitives[VertexPositionColorTexture] (PrimitiveType primitiveType, Microsoft.Xna.Framework.Graphics.VertexPositionColorTexture[] vertexData, Int32 vertexOffset, Int32 numVertices, System.Int16[] indexData, Int32 indexOffset, Int32 primitiveCount, Microsoft.Xna.Framework.Graphics.VertexDeclaration vertexDeclaration) [0x00003] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/GraphicsDevice.OpenGL.cs:945 
  at Microsoft.Xna.Framework.Graphics.GraphicsDevice.DrawUserIndexedPrimitives[VertexPositionColorTexture] (PrimitiveType primitiveType, Microsoft.Xna.Framework.Graphics.VertexPositionColorTexture[] vertexData, Int32 vertexOffset, Int32 numVertices, System.Int16[] indexData, Int32 indexOffset, Int32 primitiveCount, Microsoft.Xna.Framework.Graphics.VertexDeclaration vertexDeclaration) [0x00043] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/GraphicsDevice.cs:660 
  at Microsoft.Xna.Framework.Graphics.SpriteBatcher.FlushVertexArray (Int32 start, Int32 end, Microsoft.Xna.Framework.Graphics.Effect effect) [0x000a9] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/SpriteBatcher.cs:311 
  at Microsoft.Xna.Framework.Graphics.SpriteBatcher.DrawBatch (SpriteSortMode sortMode, Microsoft.Xna.Framework.Graphics.Effect effect) [0x001eb] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/SpriteBatcher.cs:268 
  at Microsoft.Xna.Framework.Graphics.SpriteBatch.End () [0x0002c] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Graphics/SpriteBatch.cs:99 
  at MonoGameiOS.Game1.Draw (Microsoft.Xna.Framework.GameTime gameTime) [0x0004c] in /Users/jmgomez/Projects/MonoGameiOS/MonoGameiOS/Game1.cs:114 
  at Microsoft.Xna.Framework.Game.DoDraw (Microsoft.Xna.Framework.GameTime gameTime) [0x00026] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Game.cs:658 
  at Microsoft.Xna.Framework.Game.Tick () [0x00235] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/Game.cs:493 
  at Microsoft.Xna.Framework.iOSGamePlatform.Tick () [0x0003c] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/iOS/iOSGamePlatform.cs:225 
  at Microsoft.Xna.Framework.iOSGameView.DoTick () [0x00007] in /Users/jmgomez/Projects/MonoGame/MonoGame.Framework/iOS/iOSGameView.cs:202 
  at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
  at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:39 
  at MonoGameiOS.Program.Main (System.String[] args) [0x00008] in /Users/jmgomez/Projects/MonoGameiOS/MonoGameiOS/Main.cs:40 
mono-rt: Stacktrace

Just found a way to do it. Since I don’t mind to dispose the game (my intention is to use the native UI controls for the main UI e.g. for doing a chat) I can just hide/show the game windows. Something like this:

	if (game == null) {
	game = new Game1 ();
	game.Run ();
} else 
    UIApplication.SharedApplication.Windows [1].MakeKeyAndVisible ();

From the game (e.g. services) just hide the windows when it’s needed:

  UIApplication.SharedApplication.Windows [1].Hidden = true;			
  UIApplication.SharedApplication.Windows [0].MakeKeyAndVisible ();