KallDrexx
KallDrexx

Reputation: 27813

Why is creating a GraphicsDevice causing an "unexpected error" on instantiation?

I'm trying to embed a bare bones XNA application in a Winforms MDI application, and I seem to be having issues. I am following the example of http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_1 and I'm not sure what I'm doing wrong.

In my MDI parent I am instantiating the rendering form via:

    private void MainForm_Load(object sender, EventArgs e)
    {
        var render = new RenderForm();
        render.MdiParent = this;
        render.Show();
    }

The code for my rendering form is:

public class RenderForm : Form
{
    private XnaRenderer _renderer;

    protected override void OnCreateControl()
    {
        if (!DesignMode)
            _renderer = new XnaRenderer(Handle, ClientSize.Width, ClientSize.Height);

        base.OnCreateControl();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        _renderer.RenderScene(null);
    }
}

So when the form is created, it attempts to create an instance of my XnaRenderer class. ClientSize.Width is 284 and ClientSize.Height is 261, and the Handle looks to be valid. The constructor's code is:

    public XnaRenderer(IntPtr windowHandle, int width, int height)
    {
        _graphicsService = new GraphicsDeviceService(windowHandle, width, height);

        SetViewport(width, height);
    }

The GraphicsDeviceService class is essentially the same as the one from the example code, but it's made to not be a singleton. The code for the constructor is:

    public GraphicsDeviceService(IntPtr windowHandle, int width, int height)
    {
        _presentationParams = new PresentationParameters
        {
            BackBufferFormat = SurfaceFormat.Color,
            BackBufferHeight = Math.Max(height, 1),
            BackBufferWidth = Math.Max(width, 1),
            DepthStencilFormat = DepthFormat.Depth24,
            DeviceWindowHandle = windowHandle,
            PresentationInterval = PresentInterval.Immediate
        };

        _graphicsDevice = new GraphicsDevice(GraphicsAdapter.DefaultAdapter, GraphicsProfile.Reach,
                                             _presentationParams);
    }

However, when the GraphicsDevice object is instantiated I get the following InvalidOperationException:

An unexpected error has occurred.

There is no more to the exception's message and no inner exception, making this pretty hard to debug without a lot of XNA knowledge.

Does anyone see something I am doing wrong?

Upvotes: 2

Views: 352

Answers (2)

KallDrexx
KallDrexx

Reputation: 27813

Figured it out!

In my building of the presentation parameters, I needed to add IsFullScreen = false.

Would have been much easier to figure out if it gave a good exception message

Upvotes: 3

Cédric Bignon
Cédric Bignon

Reputation: 13022

Try using OnHandleCreated instead of OnCreateControl:

protected override void OnHandleCreated()
{
    if (!DesignMode)
        _renderer = new XnaRenderer(Handle, ClientSize.Width, ClientSize.Height);

    base.OnHandleCreated ();
}

if it does not work, try using HiDef profile instead of Reach profile.

Otherwise, I don't see anything wrong.

Upvotes: 1

Related Questions