Thursday, March 26, 2009

Problems

I am having some technical problems and can't make posts for a few days. When I am able to I will make a couple posts.

Monday, March 23, 2009

Tutorial seven coming soon

I have finished writing the code for the next tutorial but the actual tutorial is taking a little longer. It should be up soon though. I have gone back to writing a long and short version for this one. Mostly so those who are new to these tutorials can learn from the start. There is a lot to cover in this tutorial, that is why it is taking so long. To let you know what this one is about, it is about drawing a square in 3D and rotate it around the Z axis. It will demonstrate setting up a simple camera and other concepts necessary for rendering a 3D scene.

So stay tuned and the tutorials should be up shortly.

Sunday, March 22, 2009

Managed DirectX Tutorial 6

I have finished today's tutorial. In this tutorial I show how to draw a triangle using Managed DirectX. It isn't exactly in 3D yet but I believe you have to start somewhere. There are two ways that you can do this tutorail. You can download the template and type in the additions to the template or you can download the project and just read the tutorial.

There is only one version of the tutorial because everything that I have added is new and deserves explaination. This is where you can find the tutorial.

Managed DirectX Tutorial 6

Saturday, March 21, 2009

Direct3D Template

I have finished making the first draft of the template I mentioned yesterday. So instead of starting from scratch each time you will be able to use the template. After you have downloaded the template there is one thing that you have to do. You need to copy it to the following directory:

My Documents\Visual Studio 2005\Templates\Project Templates

This is where Visual Studio looks for templates by default. If you have changed the location where Visual Studio looks for template then you will need to put the .zip file in that directory. I will try and have the next tutorial up soon so come back later.

You can find the template here:

Direct3D Template

Sorry about the icon. I will never claim to be an artist. :)

Friday, March 20, 2009

Good news (for me any way)

I was unable to write my tutorial today because I got my laptop back from the shop. The hard drive had to be replaced so I had to spend the evening trying to restore everything. What is going to happen now is I will be starting another blog, one about using Visual C# Express 2008 and XNA Game Studio 3.0. I said in my first post that I was going to do XNA 2.0 but I think that it might be a good idea to use the most current software available. When I manage to get things up and running I will post a link here to the new blog. I will try to post a Starter Kit for the Managed DirectX tutorials that you will be able to download to create the projects that I will be writing so you can just add the new code. I will keep updating the Starter Kit to include the new features that will be added to the projects. I will still be posting the projects if you perfer to just download those and look at the code instead of typing it in.

My next tutorial

I'm sitting here trying to decide what to do next. I know so far these tutorials have not done a lot of drawing. So far all they have done is create blank screens. The question in my mind is what type of drawing should I do. There is 3D and 2D. Both have their merits. There are a lot of 3D games out on the market and I believe they are the most popular. Still 2D games also have their audience as well. I want to write a tutorial that will appeal to the broadest audience possible. What I am thinking is doing an alternating format. 3D one day and then 2D another. 2D is an important aspect that should be considered when making games. A lot of games require text to written to the screen, especially my favorite, role-playing games.

Both systems also have their difficulties. That is why I started out just setting up Direct3D in the first 5 tutorials. Now I will start writing tutorials that render scenes and try to build incrementally on them. Like I mentioned yesterday I will try and write two different tutorials. One for those who haven't done the previous tutorials and one that will start off where the last one had left off.

So stay tuned for the first 3D tutorial. It should be posted soon, maybe even today.

Thursday, March 19, 2009

Managed DirectX Tutorial 5

Good news, I found the time to write the tutorial today. Today's tutorial shows how to make a pop-up window in your program using Managed DirectX. I have also written two versions of the tutorial. The first I call the Full Version. In it I describe everything that I am doing for those of you who have not gone through the other tutorials. In the short version I go over things more quickly and try to only explain the things that are different in this tutorial.

This is the full version: Tutorial 5 Full Version

And this is the short version: Tutorial 5 Short Version

And this is the link to the project: DirectX Tutorial 5

Busy, busy, busy...

As can happen, "the best made plans of mice and men often go astray" It is spring break here for students and we have two visiting young girls so I am not sure if I will be able to get to a tutorial today. I will try to get to one but there is a lot going on here and I might not have the time. Check back later (9:00pm EST) and see if I managed to get to a tutorial. I have two in mind and will try and write them.

Wednesday, March 18, 2009

Uploaded the fourth tutorial

I have finished and have uploaded the fourth tutorial in my Managed DirectX tutorials. You can find the tutorial here:

Managed DirectX Tutorial 4

And you can find the project here:

Managed DirectX Tutorial 4 Project

New format

Okay, I have been considering the format of these tutorials. I am finding that it is hard to read them. What I am going to do is post general information about the tutorials here so if you are interested you can click a link to go to the full tutorial. They will be posted on my website, hopefully in a format that is easier to read. I will also post a link to the project that you can download if all you want is the code. The projects that I will be posting will contain comments from now on to explain what is going on.

Today I am writing a tutorial about going full screen in DirectX. There are a few differences between windowed and full screen mode. You can't just set the Windowed property to false and expect your program to work in full screen mode. There is a little work that needs to be done before you can run in full screen. One important thing that you need to do is have a way to exit the program. This will be accomplished by using the KeyDown event handler. It will check to see if the Escape key has been pressed and then exit the program.

The code is finished and has been tested. I just have to write the tutorial. Check back a little later and I will post the link to the project when it has been uploaded to my website.

Tuesday, March 17, 2009

Managed DirectX Tutorial 4

I have been working on a new tutorial but I don't know when I will be able to post it. I'm hoping to post it later on today but it might not be ready. I might also start posting these tutorials on www.dreamincode.net. My member name there is SixOfEleven.

The tutorial that I'm working on now is drawing sprites using Direct3D. Direct3D itself does not have sprites. You need to also use Direct3DX.

Monday, March 16, 2009

DirectX Tutorial 3 project

I have uploaded the project for the third tutorial to my website. You can download it here:

DirectX Tutorial 3

Managed DirectX Template for C#

I've been working on the template that I talked about yesterday. It is coming along very well. I'm trying to make it so that the size of the window or screen can be set when the Direct3D device is created by just changing a few variables in the Program.cs file. Also, I tried to make it so that there are seperate methods for the logic and the rendering methods. So far the template is coming along quite nicely.

I'm going to write a tutorial about how to seperate the logic and rendering methods.

So, to start Visual C# and create a new project. Like you did in the previous tutorials you will have to add the references to Microsoft.DirectX and Microsoft.DirectX.Direct3D. If you missed the first two tutorials you do this by clicking the Project menu item then selecting Add reference item. When the window pops up select the .NET tab scroll down and click the Microsoft.DirectX entry and holding down the Crtl key click the Microsoft.DirectX.Direct3D entry.

Open the code view for the form and add these two using statements.

using Microsoft.DirectX;
using D3D = Microsoft.DirectX.Direct3D;

You might be wondering why I put the D3D = in front of the second using statement. The reason is simple. In managed DirectX Direct3D, DirectSound and DirectInput (which we are not using right now) every thing is a Device. By adding the D3D = you can easily qualify everything so there is an easy way to know what Device you want to use. You will see how this works in a moment.

Right now we are going to modify the Program.cs file. So open the code for that file. You want to change the Main method.

First delete the line:

Application.Run(new Form1());

You will be replacing this with different code. So go ahead and add the following code:

using (Form1 frm = new Form1())
{
    if (!frm.InitializeDirectX())
    {
        MessageBox.Show("Error creating DirectX.");
        return;
    }
    frm.Show();
    frm.Run();
}

What this code does is create a form and disposes of everything when it leaves the code block. First it tries to initialize DirectX. If that fails it reports an error and exits the program. Then we make sure that the form is visible and then call the Run method of Form1 that we will write shortly. Go back to the code view of Form1 and add the following a variable as follows:

private D3D.Device device = null;

Now we will write two methods. The InitializeDirectX and the Run method. They are both fairly simple.

public bool InitializeDirectX()
{
    D3D.PresentParameters pParam =
        new D3D.PresentParameters();


    pParam.Windowed = true;
    pParam.SwapEffect = D3D.SwapEffect.Discard;
    try
    {
        device = new D3D.Device(0, D3D.DeviceType.Hardware,
        D3D.CreateFlags.SoftwareVertexProcessing,
        pParam);

    }
    catch
    {
        return false;
    }
    return true;
}

public void Run()
{
    while (this.Created)
    {
        GameLogic();
        Render();
        Application.DoEvents();
    }
}

The first method should be familiar to you if you have followed the tutorials. The Run method might require a little explaining. First it is a simple loop that runs while the form is open. The this.Created flag is valid while the form is open. Next there are three method calls. GameLogic and Render are methods that we will write. The last tells the program to run the events for the form.

Right now the GameLogic method is just a stub that can be writen later. The Render method is where we will do the rendering. So create two methods as follows:

private void GameLogic()
{
    // TODO: add game logic here
}

private void Render()
{
    device.Clear(D3D.ClearFlags.Target, Color.Blue, 1f, 0);
    device.Present();
}

The Render method should be familiar to you, it is the same code that was used in the previous tutorials. The GameLogic will be written later when you actually start to write games.

So that is all for today. Later I will make the project available for download so check back a little later.

Sunday, March 15, 2009

Newest Tutorial...

What I'm working on today is creating a template for using Managed DirectX in C#. So I probably won't be posting a tutorial today but who knows, I might get the time later on today. When I have the template finished I will make it available for download and will only post the changes that have been made for the new tutorials. I'm trying to make the template very robust so it can be changed easily to suit your needs. So check back soon and I will try to have it available for download.

Saturday, March 14, 2009

Add projects for download

I have added the projects for download. You should be able to click these links to get them.

Managed DirectX Tutorial 1
Managed DirectX Tutorial 2

I have changed my mind about the next tutorial. If you are interested in going fullscreen and being able to press the Escape key to exit I will just put a link to the project on the blog. What I'm going to write about is the starts to making a game engine using Managed DirectX. So stay tuned and look for the next tutorial shortly.

Second managed DirectX tutorial

Today I'm going to show you how to create two Direct3D devices on the same form that work indendantly of each other. This is an easy way to develop a split screen game where you could play multiplayer games where each player has their own view.

To get started, create a new project in Visual C# and add the following references to your project Microsoft.DirectX and Microsoft.DirectX.Direct3D as you did in the last tutorial. Now with Form1 open in design view edit the properties of the size of the form to be: 610, 429. Next drag to panels from the toolbox onto the form. Technically you should give them meaningful names but this is a simple tutorial so you don't have to.

Change the location of panel1 to: 0, 0. Then change the size of panel1 to: 300, 400. Now change the location of panel2 to : 301, 0 and the size to 300, 400. That is all that needs to be done to set up the form for two Direct3D devices.

Like in the last tutorial two using statements have to be added to the code of Form1. They are:

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

Now you need to add two device variables:

pulic Device device1 = null;
pulic Device device2 = null;

Switch back to the design view of Form1 and either double click the title bar of the form or open the properties window and click the Events button, scroll down to the Load event handler and double click that to add the Form1_Load event to the code. If you have the properties window open go ahead on scroll down to the Paint event and double click that now as well to add the Form1_Paint event handler.

Add the following code to the Form1_Load event handler:

InitializeDirectX();

if (device1 == null || device2 == null)
{
    MessageBox.Show("Error creating DirectX device.");
    Application.Exit();
}

After you have added the code to the Form1_Load event handler add this code to the Form1_Paint event handler.

device1.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
device1.Present();
device2.Clear(ClearFlags.Target, Color.Red, 1.0f, 0);
device2.Present();

Now all you have to do is add the InitializeDirectX method. It is as follows:

private void InitializeDirectX()
{
    PresentParameters presentParams = new PresentParameters();

    presentParams.Windowed = true;
    presentParams.SwapEffect = SwapEffect.Discard;

    device1 = new Device(0, DeviceType.Hardware,
        panel1,
        CreateFlags.SoftwareVertexProcessing,
        presentParams);
    device2 = new Device(0, DeviceType.Hardware,
        panel2,
        CreateFlags.SoftwareVertexProcessing,
        presentParams);
}

If you build and run your program you should see two panels, one of them blue and the other red. One thing worth mentioning is that you do not have to use the same presentation paramaters when you create your devices so you can have different effects for both panels.

That is all the time that I have today. I will try and post another tutorial tomorrow but it is spring break for the kids here and we have company coming so I might not get around to it. If I can find the time I will try and put the projects for this tutorial on my website for download. Check back soon, hopefully I will have another tutorial ready. What I am planning is showing how to go full screen and be able to press the Escape key to exit the program.

Plans for this blog...

Here it is late at night again and once again I can't seem to sleep so I thought I would just post a little note. I think that I'm going to concentrate on 2D topics for now. I know when you think of Direct3D you probably think of 3D games but 2D games have their place. That and it is a good idea, IMHO, that you have a good understanding of programming and the way that graphics work in 2D before making the move to 3D. There is more math involved with 3D than there is in 2D and it can be confusing, even for experienced programmers.

So, while I'm working on my current project, which is an RPG that I hope to make into an MMO eventually, I will post tutorials about the methods that I have used making the game. Later on today (Sunday) I will be posting an update to my last tutorial. This one will show you how to create two devices used on the same form using panels. If I have time I will add in how to go fullscreen and be able to end the program by pressing the Escape key.

I also want to make these tutorials so those who can't get the latest and greatest equipment can use them. So all of these tutorials will be tested on my second system, an IBM PC 300GL, with 256MB of RAM, a 667MHz processor and a 16MB graphics card running Microsoft Windows XP Pro. SP3.

So stay tuned and look for the next tutorial.

Friday, March 13, 2009

Creating a DirectX Skeleton

Welcome back!

In this post I will show you how to set up a simple DirectX program using Visual C# Express 2005 using Managed DirectX. This post might be of interest of those of you who want to write games using C# but do not have a graphics card that supports Pixel Shader 1.1, which is required to write games with XNA.

You will need to install the DirectX SDK from microsoft. It can be found here:

http://msdn.microsoft.com/en-us/directx/aa937788.aspx

After you have it installed you are ready to begin.

The first thing you will need to do is start Visual C# and create a Windows Application.

Now you have to setup your program to use DirectX. To do this you have to enter two references. Click the Program menu item, then select the Add Reference entry.

You will choose the Microsoft.DirectX and Microsoft.DirectX.Direct3D enteries in the .NET tab.

Open the code view tab for Form1. You will want to add two using statements at the top of the program:

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;


After adding the using statements you will need to add a variable for the Direct3D device.

public Device device = null;

Go back to the Design view of Form1 then double click on the form (or you could click the events button on the properties window and double click the Load entry.) This will bring up the event handler for the Form Load event. Add the following code:

    InitializeDirectX();

    if (device == null)
    {
        MessageBox.Show("Failed to initiralize DirectX");
        Application.Exit();
    }


Now we will write the InitializeDirectX method.

private void InitializeDirectX()
{
    PresentParameters presParams = new PresentParameters();
    presParams.Windowed = true;
    presParams.SwapEffect = SwapEffect.Discard;

    device = new Device(0, DeviceType.Hardware, this,
            CreateFlags.SoftwareVertexProcessing,
            presParams);
}


The first thing this method does is create an instance of the presentation parameters for our device. In this tutorial I will not explain them too much as this is just to get your feet wet. The two fields that we are changing are Windows, which tells us if we want to use windowed or fullscreen for this device. The second, SwapEffect, tells the device how we want to deal with swapping from the backbuffer to the device. In this case we simply want to discard it. There is one more thing that we have to do to get the program ready to display. We need to add code to the Paint event for the form.

So, switch to the design view of your form and in the properties windon click the events button. Then scroll down to the Paint event and double click it. This will bring up the event handler for the Paint event. For this sample all we are going to do is clear the window then draw the window.
Add the following code to the Paint event:

    device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
    device.Present();

All that we are doing here is clearing the buffer then presenting the scene to our devie. The parameters for the Clear method are: ClearFlags - describes what we want to clear, Color - a System.Drawing.Color, the color we want to clear the buffer to, zBuffer - I will go into this later when we start doing 3D work, stencil - this deals with stencil buffers, for now just set this to 0.

That is all you need to setup a device using Managed DirectX
I will try and make another post in the next day or so. Come back an look form more!

Introduction

Welcome to my Blog!

What you will find here are posts on programming with C#. All of the samples will be written using Visual C# Express Edition 2005 as I want to try and write these tutorials so that people with less capable hardware should be able to follow them. I use two computers for my programming. I have a Comap Persaio V2000 laptop (which is in the shop right now) and I also use an OLD IBM PC 300GL.

For the tutorials that I will write about XNA (which will not be until I get my laptop back) will be written using XNA Game Studio 2.0. For the turorials about Managed DirectX, they will be written using the November 2008 DirectX SDK.

I will try to post frequently so keep coming back. If you have any questions or suggestions, leave a comment or email me. I will try and answer quickly.

Hope that you find something useful.