Formeln

Tuesday, May 15, 2012

Organizing your Code: A Class Library for the Engine

You may want to use your Engine for several projects. You could always copy and paste your code again and again, but this is just braindead. By creating a Class Library you just have to reference that library in your new code. Furthermore a Class Library helps you to structure your code and make you think about whats general and what is specific to your project.

Create a Class Library


Rightclick on your solution and choose Add->New Project:



Select Class Library and type a name for the library at the bottom of the dialogue (I will call it Apparat).


Your Solution Explorer should now look like this, our old Project and our Class Library:


Migrating the Code to the Class Library

Drag and drop RenderManager.cs and the DeviceManager.cs to the library project and delete Class1.cs from your Class Library.

Delete DeviceManager.cs and RenderManager.cs from your MDX11Form Project.

Your Solution Explorer should look like this now:


Add a reference to the SlimDX.dll to your library project by right-clicking References in your Class Library, selecting Add Reference and selecting the SlimDX Component.

Add further References to the System.Windows.Form and System.Drawing Components.

Now you can right-click on your Class Library and select Build and you built a dll.

You still can't use your Class Library in this way for your Main Project, so lets make the final changes to our Class Library.

The namespaces of RenderManager.cs and DeviceManager.cs are still called MDX11Form, I rename them to Apparat:


The second step is to and make the two classes  DeviceManager.cs and  RenderManager.cs public:


Changes to the Main Project

Add a reference to your library project to your application project by right-clicking on References->Add Reference. But instead of using the .NET tab use the Projects tab and select your Class Library:


Now you can delete the reference to SlimDX. Your References should look like this now:





Now you have a lot of red markers in your using directices in the Form1.cs of your Main Project.
These are using directives of the former referenced libraries and you have to delete them.

Finally you have to add a using directive to your Class Library to the Form1.cs. The code of your Form1.cs now looks like this:


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

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

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

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

Congratulations! You created a Class Library and made the first real step to a dedicated Library for your Engine ;)

No comments:

Post a Comment