Formeln

Sunday, November 4, 2012

Creating a UserControl for the Render Engine

In this tutorial I will show you, how you can create a User Control that you can add to new projects just by dragging and dropping it from your toolbox to your form.

Adding a UserControl


Start by Right-Clicking on your Apparat Project and select Add/UserControl:



In the following Dialogue select User Control. I will name this Control RenderControl. To add the Control, click Add on the right bottom of the Dialogue:


Functions in the UserControl

Let's take a look at our class Form1 in the MDX11Form Project:


namespace MDX11Form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            DeviceManager.Instance.createDeviceAndSwapChain(this);
            RenderManager.Instance.init();

            Triangle triangle = new Triangle();
            Scene.Instance.addRenderObject(triangle);
        }

        public void shutDown()
        {
            RenderManager.Instance.shutDown();
            DeviceManager.Instance.shutDown();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            shutDown();
        }
    }
}


Observe, that the function createDeviceAndSwapChain has the following signature:
public void createDeviceAndSwapChain(System.Windows.Forms.Control form)

All User Controls inherit from System.Windows.Controls.Control and we can reuse this code
in our RenderControl. Furthermore I move the shutDown Method into the User Control.
To edit the RenderControl Right-Click on it in the Apparat-Project and select View Code.

Visual Studio created the following Code for us, when we added the User Control:






namespace Apparat
{
    public partial class RenderControl : UserControl
    {
        public RenderControl()
        {
            InitializeComponent();
        }
    }
}

Copying the above mentioned code, and moving the code to create the DeviceManager and RenderManager to an init function, the code in our RenderControl now looks like this:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Apparat.Renderables;

namespace Apparat
{
    public partial class RenderControl : UserControl
    {
        public RenderControl()
        {
            InitializeComponent();
           
        }

        public void init()
        {
            DeviceManager.Instance.createDeviceAndSwapChain(this);
            RenderManager.Instance.init();

            Triangle triangle = new Triangle();
            Scene.Instance.addRenderObject(triangle);
        }

        public void shutDown()
        {
            RenderManager.Instance.shutDown();
            DeviceManager.Instance.shutDown();
        }
    }
}

In order to make the RenderControl visible, when I drag it to a Form, I set the BackColor of the
RenderControl to Orange in the Properties of the RenderControl.
And we are done with the User Control!
At this stage you have to Rebuild the Apparat Library.

Adding the RenderControl to the Form

Now Double-Click the Form1 in the MDX11Form Project. If you select the Toolbox of Visual Studio,
the RenderControl shows up in our Toolbox:



Click the RenderControl in the Toolbox and click somewhere in the Form1 to drop our RenderControl there, or you can use Drag&Drop from the Toolbox. Adjusting the sizes of the Form1 and the RenderControl, the Form1 looks now like this:


If your compile the MDX11Form Project right now, you see nothing but the orange rectangle in the Form1.
Double-Click in the Designer on the Form1 to edit the Handler for the Load event of the Form.
Visual Studio created a renderControl1 object of our RenderControl, when we added it to the Form.
We can access the functions via this renderControl1 object.

All you have to do, to get the RenderControl working, is to call its init function. I do it in the Handler
of the Load event. To shut down, properly, I call the shutDown Method in the Handler of the FormClosing event:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Apparat;
using Apparat.Renderables;

namespace MDX11Form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            renderControl1.shutDown();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            renderControl1.init();
        }
    }
}

If you start the Solution now, you can see that our Render Engine is running in our UserControl:


You can download the Solution here:
http://apparat.codeplex.com/SourceControl/changeset/dc772f719b14


No comments:

Post a Comment