Tutorial: Setup A New Project using IrrlichtLime for x86 and x64 in Visual Studio 2015

There are a few advantages of using Irrlicht with the .NET Framework:
Firstly, you don’t need to learn a new language (if you’re using C# already), it’s easier and you don’t have to care about so many things.
Another major advantage of the .NET Framework is that it’s platform-independent, yet, this does not fully apply to IrrlichtLime. It will only work on Windows and you’ve got separate .dlls for x86 and x86-64.

In this tutorial, I’ll show you, how you can setup a project using IrrlichtLime, that produces a single executable file (.exe), which automatically picks the correct IrrlichtLime.dll.

This tutorial will use Visual Studio 2015. You can also install Update 2.

You will also need to donwload IrrlichtLime itself. In this tutorial, I used version 1.5.

Project Setup in Visual Studio 2015

First create a new project. Select C# as a language and choose Console Application.
I also set the framework to .NET Framework 4, but it should work with other versions, too.
You can choose any name for your project.
Then click OK.

SetupIrrlichtLimeVS2015_01
Fig. 1 | Full Size

 

After the project has been created, we need a few sub-folders.
Right click on the project name, choose Add and then New Folder, as shown below:

SetupIrrlichtLimeVS2015_02
Fig. 2 | Full Size

 

We want to have a dll folder with two subfolders: x86 for 32 bit and x64 for 64 bit.
We’ll put the IrrlichtLime.dll and the Irrlicht.dll from the IrrlichtLime download into these.
The files from bin\Debug go to x86 and the files from bin\Debug-x64 go to x64.
As long as your application is not done yet, you should keep these debug dlls. They perform additional checks and will let your application crash, so you can notice the error.
Once you have tested it and want to publish you application, you can replace them with the dlls from the folders Release and Release-x64 from your IrrlichtLime download.

Now, your folders should look like this:

SetupIrrlichtLimeVS2015_03
Fig. 3

 

For the next step, you’ll need the Properies Window. You can find it via View > Other Windows > Properties Window or by pressing Alt+Enter.
Now, select all the Irrlicht.dll and IrrlichtLime.dll files in the Solution Explorer. (You can select multiple files by holding Ctrl wile clicking.)
In the properties window, set their Build Action to None and their Copy to Output Directory to Copy if newer, as seen below:

SetupIrrlichtLimeVS2015_04
Fig. 4

 

Setting the References

Now you will need to set a reference to the IrrlichtLime.dll, so you can use it in your code. It does not really matter whether you reference the x86- or x64-dll, but you should remember which one you chose. I will add a reference to the x64-dll.

First, right click on References in the Solution Explorer and select Add Reference….
A new window will open up, where you first click the Browse tab on the left and then the Browse… button:

SetupIrrlichtLimeVS2015_05
Fig. 5 | Full Size

 

Then, browse to your IrrlichtLime.dll, select it and click Add:

SetupIrrlichtLimeVS2015_06
Fig. 6 | Full Size

 

In the next window, just click OK.
Now you have to prevent Visual Studio from automatically copying the IrrlichtLime.dll to the output folder.
To do this, select the IrrlichtLime reference in the Solution Explorer and then set Copy Local in the Properties Window to False:

SetupIrrlichtLimeVS2015_07
Fig. 7 | Full Size

 

Automatically Selecting the correct dll

The project setup is complete. Now we have to tell our application how to choose the correct .dll at runtime.

You have to use the namespaces System.IO and System.Reflection for this (and IrrlichtLime, of course).
In the main, we attach an event handler, which will be called if a .dll could not be found.
In the event handler, we check whether that .dll is the IrrlichtLime.dll and load it manually.
To determine whether we are running as a 32 bit or 64 bit process, we check the size of IntPtr, which can be 4 or 8 respectively.

Important: Don’t put any code using IrrlichtLime into Main, or it will crash. You’ll have to put it in a separate method. (For a bigger project, you’ll have it in a separate class, anyway.)

To achieve this, we use this piece of code. It doesn’t do anything useful yet, it just opens a window, but you could paste the code from any IrrlichtLime example into it, if you wanted to.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;

using IrrlichtLime;

namespace Tutorial_IrrlichtLime
{
    class Program
    {
        private static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            String dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);   //the directory of the .exe
            String shortName = new AssemblyName(args.Name).Name;    //the short name of the .dll to load
            String assemblyFilepath = null; //the full filepath for the .dll

            if (shortName == "IrrlichtLime")    //IrrlichtLime is to be loaded?
            {
                String dllName = "IrrlichtLime.dll";
                if (IntPtr.Size == 4)    //32 bit -> x86
                    assemblyFilepath = Path.Combine(dir, "dll\\x86", dllName);
                else if (IntPtr.Size == 8)   //64 bit -> x64
                    assemblyFilepath = Path.Combine(dir, "dll\\x64", dllName);
            }

            if (assemblyFilepath != null)
                return Assembly.LoadFrom(assemblyFilepath); //Load the file and return the assembly
            return null;
        }

        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

            Run();
        }

        static private void Run()   //put the code using IrrlichtLime in a separate method / class, or it will fail at runtime
        {
            IrrlichtDevice device = IrrlichtDevice.CreateDevice();
            while (device.Run()) ;
        }
    }
}

What’s left to do?

There are a few things you can do now:

Once you are ready to publish the application, you can replace the dlls with the ones from the Release folders of IrrlichtLime, which are optimized for higher performance, but perform less runtime checks.

You can also set the Output Type in the Application Settings from Console Application to Windows Application which will hide the console.

And you can add the IrrlichtLime.xml from the doc folder in the IrrlichtLime download. You have to put it next to the IrrlichtLime.dll that you referenced. (Also set the Build Action to None for it.)
This will allow you to see IntelliSense documentation for IrrlichtLime. (You might have to restart Visual Studio to see the change.)
 

If you liked this tutorial, please leave a comment 😀

Advertisements

2 thoughts on “Tutorial: Setup A New Project using IrrlichtLime for x86 and x64 in Visual Studio 2015

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s