Formeln

Wednesday, May 9, 2012

Organizing your Code: Classes

In this tutorial we will add two classes: the DeviceManager and the RenderManager. In theses Classes we will organize our variables for the device and our rendering process.

Let's start with the DeviceManager:

The DeviceManager Class


Navigate in Visual C# to your Solution Explorer and right click on our Project. A dialogue will appear and click Add and in the next dialogue Class.

The following dialogue should appear:


Select Class and choose a name for this Class at the bottom of the dialogue. I will name it DeviceManager.

Copy the following in Form1.cs declared variables to the DeviceManager Class and delete them in Form1:


 
public Device device;
public SwapChain swapChain;
public Viewport viewport;
public RenderTargetView renderTarget;
public DeviceContext context;

Same with the Method createDeviceAndSwapChain.


Now add the Method shutDown to our Class DeviceManager and add the objects you want to dispose to it:

public void shutDown()
{
  renderTarget.Dispose();
  swapChain.Dispose();
  device.Dispose();
}

The RenderManager Class


Now let's add another Class to our Project called RenderManager just like you added your DeviceManager Class to the Project.

Copy the renderThread variable over to the RenderManager Class and the init Method.
Now add a shutDown Method where we tell our renderThread to abort:

public void shutDown()
{
  renderThread.Abort();
}

Final Steps

We have organized our Methods and variables in dedicated Classes and need to declare them in our Form1.cs and create Objects of them. Finally we call the according Methods of our Objects and
Form1cs now looks like this:

public partial class Form1 : Form
{
  DeviceManager dm;
  RenderManager rm;

  public Form1()
  {
    InitializeComponent();
    dm = new DeviceManager();
    rm = new RenderManager();

    dm.createDeviceAndSwapChain(this);
    rm.init();
  }

  public void shutDown()
  {
    rm.shutDown();
    dm.shutDown();
  }

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

Much cleaner this way!
But there is still one problem: it won't compile, because our RenderManager Class knows nothing about 
our variables context, renderTarget and swapChain, because they were declared in our DeviceManager Class.

We will help ourselves by adding a Constructor to our RenderManager Class that takes a reference to our
DeviceManager Class. Second we add a Member variable to our RenderManager Class that holds a reference to our DeviceManager Object:

public RenderManager(DeviceManager dm)
{
  this.dm = dm;
}

DeviceManager dm;

Finally we have to change our Form1 Constructor and pass the DeviceManager Object to our RenderManager as a parameter:

public Form1()
{
  InitializeComponent();
  dm = new DeviceManager();
  rm = new RenderManager(dm);

  dm.createDeviceAndSwapChain(this);
  rm.init();
}

Now everything compiles again. In the next Tutuial I will tell you why it is a bad idea to pass our DeviceManager as a Reference to other Objects that depend on it and how this can be solved in an elegant way. 

Download

You can download the code to this project here: download
















2 comments:

  1. What is wrong with your download? I really like your tutorial but it takes you to a page that has three download buttons. And, you have to wait 10 seconds on each page and when it finally lets you download it is some sort of lividplayer.exe. I'm not installing that.

    ReplyDelete
  2. This is a great beginning tutorial to SlimDX! I did have a little trouble following this tutorial. The init function in the RenderManager seems to be checking for key input for fullscreen and not. But, this is called only once in startup. I'm thinking there should be a message peek in the RenderManager renderscene function. Or better yet, a peekmessage from the form that tells the RenderManager to stop rendering if it gets the message to die or key stroke. If this is in a later tutorial I apologize. :)

    ReplyDelete