0
\$\begingroup\$

I'm trying to put in a new feature into my level editor. Part of the feature is to bring up a form when a user places a new tile onto the map, but once the form is brought up the XNA embedded screen throws up a big red X. I think this is due to the embedded screen losing focus to the form and then crashing.

This is where the form is initialized

 public Form1()
   {
    InitializeComponent();

    //Node Editor

    tileDisplay1.OnInitialize += new EventHandler(tileDisplay1_OnInitialize);
    tileDisplay1.OnDraw += new EventHandler(tileDisplay1_OnDraw);

    Microsoft.Xna.Framework.Input.Keys[] allKeys = (Microsoft.Xna.Framework.Input.Keys[])
     Enum.GetValues(typeof(Microsoft.Xna.Framework.Input.Keys));

    foreach (var key in allKeys)
    KeyboardInput.AddKey(key);

     KeyboardInput.KeyRelease += new KeyHandler(KeyboardInput_KeyRelease);
     MouseInput.MouseMove += new MouseMoveHandler(MouseInput_MouseMove);
     MouseInput.MouseDown += new MouseClickHandler(MouseInput_MouseDown);
     MouseInput.MouseUp += new MouseClickHandler(MouseInput_MouseUp);

     Application.Idle += delegate { tileDisplay1.Invalidate(); };

     saveFileDialog1.Filter = "Map File|*.map";

     Mouse.WindowHandle = tileDisplay1.Handle;

   }

This is where the draw event is fired off

void tileDisplay1_OnDraw(object sender, EventArgs e)
        {
            Logic();
            Render();

            KeyboardInput.Update();
            MouseInput.Update();

            foreach (var actor in Actor.Actors)
                actor.Update();
        }

This is where the form is being called

     private void Logic()
    {
               if (colIndex == 8 && AssociateBox.SelectedIndex == 0)
                {
                    currentCollisionLayer.SetCellIndex(collideCellX, collideCellY,form.spawnNumber.ToString());

                    if(form.spawnNumber == "12")
                    {
                       camdetails.ShowDialog();
                    }
                }
    }
\$\endgroup\$
1
  • \$\begingroup\$ Have you considered simply having a separate form that sits beside the main XNA game window and contains a reference to the game? That's what I did on my map editor. \$\endgroup\$ Commented Feb 29, 2012 at 15:19

2 Answers 2

2
\$\begingroup\$

The "Big Red X" from the WinForms sample comes up when your draw/update code throws an exception. Rather than simply ignoring it, the WinForms sample catches the exception, switches to displaying the Big Red X, and stops calling your draw/update code.

This is the equivalent of a regular XNA programming exiting with an "Unhandled Exception" error.

Simply resetting the graphics device and attempting to continue is the wrong solution.

What you need to do is figure out where the exception is being thrown, and stop it percolating up past your draw/update code. Try setting "break on exception" in Visual Studio. Or wrap your draw/update code in a try-catch block and adding a breakpoint.

\$\endgroup\$
0
1
\$\begingroup\$

I recommend reviewing the XNA AppHub sample on WinForms that is located here: http://create.msdn.com/en-US/education/catalog/sample/winforms_series_1

They create a GraphicsDeviceService derived from IGraphicsDeviceService. The GraphicsDeviceService updates PresentationParameters and calls graphicsDevices.Reset(parameters); any time the GraphicsDevice needs it, which is anytime the window or form resizes.

From the code you've posted I'm guessing that camDetails.ShowDialog(); or something inside of SetCellIndex could be the cause.

As a quick test you might try this right before you call Logic();:

graphicsDeviceService.ResetDevice(ClientSize.Width, ClientSize.Height);

Try calling that from your Systems.Windows.Forms.Control

\$\endgroup\$
5
  • \$\begingroup\$ The camDetails.ShowDialog(); is causing the issue, but I thought the graphics device could be reset when the call to it is made so that the screen doesn't crash or isn't that possible ? \$\endgroup\$
    – dbomb101
    Commented Feb 29, 2012 at 14:28
  • \$\begingroup\$ I believe you should be able to create the dialog, reset the device, and then draw. \$\endgroup\$
    – Nic Foster
    Commented Feb 29, 2012 at 14:59
  • \$\begingroup\$ That should be doable, but the moment dialog is called it doesn't execute the next line and instead throws up the big red X, so I'm guessing it's throwing up an exception the debugger isn't catching \$\endgroup\$
    – dbomb101
    Commented Feb 29, 2012 at 15:13
  • \$\begingroup\$ You might try wrapping the statement in a try/catch block to see if you can catch the exception. \$\endgroup\$
    – Nic Foster
    Commented Feb 29, 2012 at 15:39
  • \$\begingroup\$ My editor is based on this sample and it behaves well in the situations you described. I've never had problems opening new dialogs or switching windows, and I can even have multiple GraphicsDeviceControl objects created and rendering at the same time. I'd recommend starting from there too. \$\endgroup\$ Commented Mar 1, 2012 at 9:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .