Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

[C#][XNA 3.1] How can I host two different XNA windows inside one Windows Form?

Tags:

c#

winforms

xna

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.

like image 250
user293575 Avatar asked Mar 14 '10 21:03

user293575


1 Answers

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.

like image 95
zfedoran Avatar answered Oct 27 '22 00:10

zfedoran