DirectX Programming with C#
- What is Microsoft.NET and why would I want to use it?
- What does DirectX do to help me make games?
- Setting up a development environment.
- Creating a simple sample.
- Overview of Managed DirectX.
- Creating a DirectX class library.
- Building 3D mesh objects.
- Movement in 3D (Matrix Transformations).
- Collisions
- Sound Effects
- Background Music
- Advanced Features: Animation, HLSL
The .NET framework is Microsoft's answer to Java. You write code, compile it to an intermediate level language which is then compiled to machine code with a 'Just In Time' compiler right before it is run. The advantages are ...
- The executables are fast no matter what language you write in. (VB, C#, C++)
- The code is 'Managed' so it can't (easily) crash your operating system.
- All memory usage is cleaned up as your objects lose scope.
- Microsoft.NET introduces the C# (c sharp or c plus plus plus plus) a truely object oriented language that combines the ease of VB, the code style of java and the strong typing of C++.
- Last but not least *IT'S FREE!
* NOTE: Nothing in life is free, you just pay for it in another way, but if your willing to spend some extra time finding the right tools you do not need Microsoft's Visual Studio to create C# applications.
What are your choices for a development environment?
- Notepad and the .NET Framework: This is the bare minimum. You write your code in a 'blind' environment and then cross your fingers and compile. If you've ever worked Java then you're probably a pro at this. The C# compiler will be install with the .NET Framework which is available from windowsupdate.microsoft.com. You may already have it so check for the folder (WINDOWS)\Microsoft.NET. The compiler will be at ..\Microsoft.NET\Framework\v(x.x.xxxx)\csc.exe. Run csc.exe /? for the command line syntax. I will show a sample of compiling this way when we build the simple sample. Optionally you can download Microsoft's .NET Framework SDK which has the help files for the C# language.
- A third party tool: There are a few choices out there of 'free' development environments. I have only tried WebMatrix which is designed for building .ASPX web pages but can be convoluted into a simple dev. env.
Just look up C# coding in your favorite search engine to find others. Microsoft's .NET framework makes it easy to produce 'intellegent' coding environments with its built in Reflection and free compiler. I will not have examples of compiling with these third party tools.
- Plunk down the cash and go with Visual Studio .NET: If this is the environment your used to then there is nothing better. It takes about 5 minutes to build a windows form with menus etc.
Let's get started by creating the infamous Hello World app! We will do this in Notepad so that you understand what VS7 is adding to your project. Create a file called Main.cs. (cs stands for C Sharp. This extension naming convention is just for your benefit. You can call it anything you like.)
Enter the following into Notepad:
using System;
using System.Windows.Forms;
using System.Drawing;
namespace HolaMundial
{
public class frmHola : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnHola;
public frmHola()
{
this.btnHola = new System.Windows.Forms.Button();
this.btnHola.Location = new System.Drawing.Point(100, 50);
this.btnHola.Name = "btnHola";
this.btnHola.Size = new System.Drawing.Size(64, 32);
this.btnHola.Text = "Hola";
this.btnHola.Click += new System.EventHandler(this.btnHola_Click);
}
[STAThread]
static void Main()
{
Application.Run(new frmHola());
}
private void btnHola_Click(object sender, System.EventArgs e)
{
MessageBox.Show("Hola Mundial!");
}
}
}
What was all that about?
Let's break it into sections.
The terms 'using' and 'namespace'
: A namespace is just a wrapper to keep your function named 'Init' from colliding with the 'Init' function of another object. So if I have