views:

500

answers:

3

I am making a Map Editor for a 2D tile-based game. I would like to host two XNA controls inside the Windows Form - the first to render the map; the second to render the tileset. I used the code here to make the XNA control host inside the Windows Form. This all works very well - as long as there is only one XNA control inside the Windows Form. But I need two - one for the map; the second for the tileset. How can I run two XNA controls inside the Windows Form? While googling, I came across the terms "swap chain" and "multiple viewports", but I can't understand them and would appreciate support.

Just as a side note, I know the XNA control example was designed so that even if you ran 100 XNA controls, they would all share the same GraphicsDevice - essentially, all 100 XNA controls would share the same screen. I tried modifying the code to instantiate a new GraphicsDevice for each XNA control, but the rest of the code doesn't work. The code is a bit long to post, so I won't post it unless someone needs it to be able to help me.

Thanks in advance.

A: 

I have done something similar to what you are trying to do. All you need to do is tell the graphics device where to present the "stuff" you have rendered. You do this by passing it a pointer to a canvas.

Here is a sample form class:

public class DisplayForm : Form
{

    IntPtr canvas;
    Panel displaypanel;

    public Panel DisplayPanel
    {
        get { return displaypanel; }
        set { displaypanel = value; }
    }

    public IntPtr Canvas
    {
        get { return canvas; }
        set { canvas = value; }
    }

    public DisplayForm()
    {
        displaypanel = new Panel();
        displaypanel.Dock = DockStyle.Fill;

        this.canvas = displaypanel.Handle;
        this.Controls.Add(displaypanel);
    }

}

Then simply add this to your game class draw call:

graphics.GraphicsDevice.Present(displayform.Canvas);

After you are done drawing to that instance of DisplayForm you can clear, render something else, and call Present again pointing to another canvas.

Arriu
Does this method work if you are rendering into multiple Panels on the same Form?
John JJ Curtis
Yes it does. Here is what I created: http://imgur.com/UCKx8.png
Arriu
A: 

You might find these two XNA samples useful:

http://creators.xna.com/en-US/sample/winforms_series1

http://creators.xna.com/en-US/sample/winforms_series2

RodYan
He already posted that he used that above.
Chris Watts
A: 

Just a thought but have you considered making this app of yours an MDI app?

that way you can load a form that contains 1 instance of xna multiple times.

Failing that ... do what RodYan suggests :)

Wardy