Windows - Visual Studio Project Setup

First, for Windows, we recommend that you have a copy of Visual Studio. The express editions can be downloaded for free from Microsoft. This tutorial is specific to Visual C# 2005 Express, but instructions should be very similar for the 2008 version.

Visual C# 2005 Express
Start a new project using the "Windows Application" template and save it. AgateLib provides all the functionality necessary for creating windows and drawing to the screen. So you do not need references to many of the default libraries visual studio has.

Take a close look at the solution explorer, expanding any collapsed items (the properties folder can be ignored). Under the reference folder Visual Studio will have automatically added references to System, System.Data, System.Deployment, System.Drawing, System.Windows.Forms, and System.Xml. Delete all of these except System. If you are using 2008 you may want to keep the reference to System.Core. Also delete Form1.cs.

Now you need to add a reference to the AgateLib library. Right click on the References folder in the Solution Explorer and click "Add Reference...", make sure the selected tab is ".NET", and then click the Browse tab. Locate the folder you extracted AgateLib to. If you just want to get on with it, add a reference to AgateLib.dll and AgateMDX.dll, and skip the next section. At some point you should read the page on Drivers.

Visual Studio 2005 contains some nifty features which help you debug applications. Unfortunately, one of these is incompatible with Managed DirectX 1.1, so we need to disable it. In the menu bar, go to Debug -> Exceptions. Expand Managed Debugging Assistants. Scroll down to the LoaderLock exception, and uncheck it. Click OK to accept changes.

If you skip this step, you will notice that the application will get an exception when it starts up. It is important to point out, this is only an issue if you are running your code in the Visual Studio debugger. This is not an issue if you double-click the .exe file outside the debugger, or use CTRL-F5 to run without debugging. It is also not an issue when your game is complete and you wish to send it to other people to play it.

The way Visual Studio is designed, this is a per-project setting. So LoaderLock exceptions have to be disabled for every AgateLib project using the MDX driver.

Now we are ready to write some code. We will be working with the program.cs file. You may rename it if you wish, by single clicking on it and pressing F2. Double-click program.cs in the Solution Explorer. Visual Studio generated some code which creates a Form from Form1 and shows it. Delete the text in program.cs, and replace it with the following code:

using System;
using System.Collections.Generic;
using ERY.AgateLib;
using ERY.AgateLib.Geometry;

namespace TestAgateLib
{
    class HelloWorldProgram
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            using (AgateSetup setup = new AgateSetup(args))
            {
                // initialize AgateLib.
                setup.InitializeAll();

                // if something bad happened, bail out.
                if (setup.Cancel)
                    return;

                // Create a window with resolution 640x480 and title "Hello World"
                DisplayWindow wind = new DisplayWindow("Hello World", 640, 480);

                // Run the program while the window is open.
                while (Display.CurrentWindow.IsClosed == false)
                {
                    // Display.BeginFrame must be called at the start of every frame,
                    // before rendering takes place.
                    Display.BeginFrame();

                    // Clears the display to a nice color.
                    Display.Clear(Color.DarkGreen);

                    // End frame must be called after all drawing is finished.
                    Display.EndFrame();
                   
                    // KeepAlive processes events.
                    Core.KeepAlive();
                }
            }          
        }
    }
}