Thursday, May 28, 2009

Creating games in C# with out XNA

Hi everybody,

Well, I'm going to use the blog to create games in C# but not using XNA. For now I'm going to stick to 2D games using GDI+ but eventually I'm hoping to make the move to 3D games using DirectX. Keep on coming back and I should have some tutorials on creating games in C#.

-----------------------------
Proud member of Dream.In.Code

Sunday, May 10, 2009

The next turorial is almost ready..

I've been working on the tutorial and it is almost ready. It should be ready between 9:00pm and 10:00pm EST on May 10, 2009.

-----------------------------
Proud member of Dream.In.Code

Saturday, May 9, 2009

Cold, damp and miserable here...

Hi there.

Well, it is a cold miserable day here. I'm not talking about winter conditions below freezing but just temperatures below room temperature. Any way I've been working on the tutorial I promised. I had a business meeting yesterday that took a lot of time. I also have three other projects on the go so alotting time to each project can be a little difficult. There are also everyday things that need to be done.

Well, please keep coming back I'm going to try and have new content everyday.

-----------------------------
Proud member of Dream.In.Code

Thursday, May 7, 2009

About classes

As I mentioned earlier, object-oriented programming is all about objects. To make objects you create classes. You can think of classes as a factory for building objects. Inside a class you have the attributes and methods of the class. Attributes and methods have a number of access modifiers. This defines encapsulation. That is a big word that means data and methods belong inside the class.

Two examples of access modifiers are private and public. Private means that only methods and attributes inside the class can use it. If you have a variable that is defined private such as:

private int value;

Only items in a class can access that variable. The same is true for methods:

private void method()
{
}


That method can only be called inside the class. Public on the other hand means that the variable can be modified outside of the object. If you remember the Hello, World! program you used Console.WriteLine("Hello, World!"); the reason you were able to is that WriteLine is public.

I just wanted to say that before I post the next tutorial. It will make things a little easier. Just one more thing to say, be default when you make a variable or a method inside a class it is considered private. Hopefully, I will have the tutorial I was talking about ready and on my website. Hope you have a wonderful Thursday!

-----------------------------
Proud member of Dream.In.Code

Tuesday, May 5, 2009

What is going on...

I'm not sure if I've said this here but Tuesdays and Wednesdays are very busy for me so I haven't really had that much time to do the things I wanted to do today and I won't have all that much time tomorrow. I'll make sure that there is something good for Thursday evening though and I will try and have somthing tomorrow night too.

Cheers!

-----------------------------
Proud member of Dream.In.Code

Saturday, May 2, 2009

Tutorial will be posted tomorrow...

Hi again,

Sorry but I didn't finish writing the tutorial. The program is finished but the actual tutorial is taking a little longer.

-----------------------------
Proud member of Dream.In.Code

Your next tutorial

Hi again!

Well, I'm hoping tol have the next tutorial up on my website this evening, say 9:00pm EST. It will be an introduction to classes. I'm going to make a purse and coin class. The purse class will hold a number of coins. It is a little advanced but it will introduce a lot of the basic concepts of a C# program. It will be in console mode but I will eventually update it to a Windows form project.

-----------------------------
Proud member of Dream.In.Code

Friday, May 1, 2009

Welcome to May!

Hi there!

Well, it is a beautiful spring day here, the only thing that would be better if it wasn't so cloudy. I'm working hard on getting things done. I'm hoping with the new month things will move a bit better here.

So have a good day, and if you live in the northern hemisphere be happy, summer is almost here.

-----------------------------
Proud member of Dream.In.Code

Thursday, April 30, 2009

Appologies...

I have been very busy today helping my roommate build her new computer, only had a few minutes here and there to myself so not much got done today. I will try and get back to things tomorrow.

-----------------------------
Proud member of Dream.In.Code
Well, it is the last day in April and I think with the start of a new month I'm going to change things a little. I don't have infinite time and have to divide my attention between many things (my dogs have to eat, go for their walks and I have other things that must be done in a day.) but I'm not going to forget about you.

I have about 4 hours in a day that I can devote to the project. Writing the code takes a while and so does writing the tutorials about the code. One tutorial can take upto 6 hours.

-----------------------------
Proud member of Dream.In.Code

Wednesday, April 29, 2009

What I'm going to try...

Welcome back!

I think I'm going to try a new approach. I'm going to pick a simple program, like finding the factorial of a given number, finding if a number is prime or printing a message. Programs that will introduce different concepts.

-----------------------------
Proud member of Dream.In.Code

Tuesday, April 28, 2009

Help getting you started...

Welcome back!

I'm trying to write some tutorials to help you learn C#. I don't want to go too fast though. If you have a topic that you would like covered feel free to send me an email or leave a comment here on the blog. I will try and help you as best I can.

I will try and have something up very soon. Unfortunately Tuesdays and Wednesdays are very busy for me so I might not get back to this today. Hopefully I will though so please come back after 9:00pm EST and if I managed to get some spare time I will add something to my website.

-----------------------------
Proud member of Dream.In.Code

Monday, April 27, 2009

Hi there..

Welcome back!

I haven't forgotten about you. I'm doing some work on helping you get started with C#. I've been contemplating the best way to do it. I'm thinking that Console projects will be the best starting point. They are simpler to write than Windows projects.

So keep checking back I should have something up soon.

Just a side note, I'm currently redesigning my website. I'm going to divide it into three sections, one for the RPG, one for my business and one for my C# tutorials. Right now I'm working on CSS files. So far I like what I've come up with.

-----------------------------
Proud member of Dream.In.Code

Sunday, April 26, 2009

Expect the unexpected...

Welcome back!

I've got unexpected company so I don't think I will be able to make a post today.

-----------------------------
Proud member of Dream.In.Code

Saturday, April 25, 2009

The next tutorial...

Welcome back.

Hopefully the next full tutorial will be posted on my website soon. In this tutorial I will talk a little more about variables and how to use them. If you will remember, I said that everything in C# is an object and there are some special objects like integers, floating point numbers and booleans. These are called primative data types. All other classes are made up of combinations of these data types. I will go into that more in the tutorial. So please be patient and look for the link soon.

-----------------------------
Proud member of Dream.In.Code

Friday, April 24, 2009

About classes...

Welcome back!

In C# classes are used quite often. They can help reduce the complixity of code by hiding data and methods. This behaviour is call encapsulation. By doing this it is easy to update the class and reuse it elsewhere. This is a key topic in object-oriented programming. Many years ago, and even sometimes today, programmers did not use classes. Updating a program could take hundreds of hours or more going through thousands of lines of code, sometimes hundereds of thousands, even millions. With object-oriented programming the use of objects has made the process a little easier. By using objects, if something needed to be updated, you just found the class and updated the class. Because you encapsulated your class, it didn't matter to the rest of the applicatoin how your class did what it did. All that mattered was it did what it said it did. Can you see the advantage?

I will post a new lesson on 25/04/2009.

Come back and look for it.

-----------------------------
Proud member of Dream.In.Code

So, sorry...

So sorry I missed my post yesterday. I was planning on posting in the evening but as things tend to do, they went astray. I will try and make a post later on today.

-----------------------------
Proud member of Dream.In.Code

Wednesday, April 22, 2009

Your first C# program

I've finished writing the first tutorial, and 48 minutes before my deadline. It is in a PDF format so you will need a PDF reader like Adobe Reader.

This is where you can find the tutorial: Hello, World!

-----------------------------
Proud member of Dream.In.Code

About the tutorial...

Welcome back!

Unfortunately I did not finish the tutorial last night. I'm going to start working on it now and try and have it ready to be posted by this evening (before 9:00pm EST) as I have a pretty busy day today. There never seems to be enough time in one day! I have a lot of projects on the go and time management can be difficult.

-----------------------------
Proud member of Dream.In.Code

Tuesday, April 21, 2009

What is next...

Sorry about today. I've been busy with the tutorial for my RPG in XNA today. It took more time than I thought it would. However I've made a decision about this blog. Instead of making HTML tutorials I will be making PDFs that can be downloaded to your computer. I will try and get a PDF up and going shortly.

-----------------------------
Proud member of Dream.In.Code

Monday, April 20, 2009

What's up for tomorrow

So, I've got my new computer up and running and everything I need installed on it. So, look for a good tutorial tomorrow. What I'm planning on is going over the structure a C# program. It might be a lot to digest but I will try and go over it and explain everything.

Check back tomorrow and look for my newest post.

-----------------------------
Proud member of Dream.In.Code

Saturday, April 18, 2009

Making posts...

I will only be able to make short posts for the next few days. I'm in the process of upgrading my computer from a PIII-667MHz to a P4-1.8GHz and don't have the time that I thought I would. But come back and I will try and get things up and running pretty soon.

-----------------------------
Proud member of Dream.In.Code

Friday, April 17, 2009

C# Basics

Why is object-oriented programming important in C#? It is because C# is a true object-oriented language. In C# everything is an object. Some objects are simple like integers, floats, doubles. Others are more complex like TcpClients and TcpListeners. You can also create your own objects that can be reused in other programs.

Some objects are considered types. These are fundamental objects that are used to build other objects.

Here is a short list of some of them:
  • int represents an integer like 1847 or -12784
  • float represents a floating point number like 1.29847 or 3.14159
  • bool represents a Boolean value that can be true or false
  • char represents a character like A or Z
  • string hold many characters of data like "Hello, my name is Bob!"

Objects are stored in variables. For example, suppose you wanted to store an integer. You would declare it like this:

int myInteger;

I think that is enough for today. Check back tomorrow and I will try and have a tutorial about how you would go about creating a simple C# program.

-----------------------------
Proud member of Dream.In.Code

Thursday, April 16, 2009

Introduction to Object-Oriented Progamming

If you are completely new to programming then there is something you have to understand before you start programming C#. That is the idea of object-oriented programming (OOP). The idea of OOP works something like this. The world is made up of objects. A cat is an object. Your computer is an object. You are an object.

So what does that mean to a programmer? In your applications you break everything that you are trying to model into objects. Say you are trying to write a program that models cars. Think about a car. What are some of things that are common to all cars? Well, cars all have a manufacture like Ford, Buick, Toyota. Cars all have colors. Cars all have a number of gears. Cars can be standards or automatics. These are just a few of the common things about cars. So why is this useful? Well, if you wanted to model a car you need to understand the attributes of cars. Attributes are used to differentiate objects.

Cars also have behaviours. For example you can turn on or off the ignition. You can change gears. You can change speeds. You can steer. Why is that important? When modelling an object you will want the object to perform different actions. These actions are called methods.

So how would you go about implementing this in your programs? You would use what is called a class. A class is used to define the attributes and methods of an object. You can think of a class like a template. When you need a new object you use a class to make the object.

That is a lot to take in so that is all I will be writing about today. Tomorrow I will show you how you would go about creating a class.


-----------------------------
Proud member of Dream.In.Code

Wednesday, April 15, 2009

Getting started with C#

So, you want to learn C#. It is a good programming language to learn for just about anybody. I will try to guide you through the process. If you are not fortunate enough to own a full copy of Visual Studio, there is an alternative. There are Express Editions that Microsoft has developed. They are free to download and use.

In this tutorial series I will be using Visual C# 2008 Express Edition. There was a 2005 edition but it has been discontinued. Not all of the code will work in Visual C# 2005. It is always a good idea to use the latest version of software anyway.

Tomorrow I will help you get started with C#.

Proud member of Dream.In.Code

Tuesday, April 14, 2009

Tutorials on C#

I will try and a new tutorial on C# every other day, starting tomorrow. I will be starting with the basics to help you learn C#. If you already know C# you might not learn much from these tutorials but sometimes reading somebody elses code may give you new insights.

Monday, April 13, 2009

My new blog...

Here is the address of my blog about my Role-Playing Game project in XNA.

Role-Playing Game Project http://xna-rpg.blogspot.com

Change in plans...

This is what I am going to do. I'm going to keep this blog going but only post short tutorials about C#. I'm going to create a new blog about the Role-Playing Game project in XNA. Once I've created the new blog I will post a link here to the blog.

Look for the new blog shortly and the first of my tutorials about C#.

Saturday, April 11, 2009

Introduction to Tiling

Well, I'm not going to post the tutorial until the 13th. What I am going to post today is the basics of creating a map and you would draw the map.

For a simple tile map, the best thing you can do is make an array of integers to the index of like this:

int[,] map = new int[,]
{
    {0, 0, 0, 0, 0, 1, 0, 0, },
    {0, 0, 0, 0, 0, 1, 0, 0, },
    {1, 1, 1, 1, 1, 1, 1, 1, },
    {0, 0, 0, 0, 0, 1, 0, 0, },

    {0, 0, 0, 0, 0, 1, 0, 0, },
    {0, 0, 0, 0, 0, 1, 0, 0, },

};

You can think of the 0's representint the first tile that is say a grass tile, 1's represent the second tile that is say a road tile. , the 2's would represent another tile, and so on.

static int tileWidth = 32;
static int tileHeigh = 32;


It would be a good idea to create two variables to hold the height and width of the tiles you are going to use like this:

Before you can draw something you would have to load the Textures in the LoadContent method. I'm not going to go into that, this is just a quick introduction (because I still haven't found tiles I like). But I will assume that there is a list of tiles that was loaded in the LoadContent method. This is how the list was defined:

List<Texture2D> tileList = new List<Texture2D>();

So, how do you draw the map?

In XNA you will do that in the Draw method. You would do something like this:

int mapWidth = map.GetLength(1);
int mapHeight = map.GetLength(0);
int index;

spriteBatch.Begin();

for (int y = 0; y <>
{
    for (int x = 0; x <>
    {
        index = map[y, x];
        spriteBatch.Draw(tileList[index],
            new Rectangle(x * tileWidth,
                y * tileHeight,
                tileWidth,
                tileHeight),
            Color.White);
    }
}
spriteBatch.End();

Looking at the code you might be thing: He's got the x and y backwards! Truth is typically in math coordinates are writen (x, y) so are screen coordinates. For our tile engine to work we have to reverse them to (y, x).

The drawing code works this way. I get the size of the map that I will be drawing and define a variable to store the index of the texture in our list.

Then I call spriteBatch.Begin(). If you are going to draw Texture2Ds you have to put the
drawing code between a spriteBatch.Begin() and a spriteBatch.End().

I set up two for loops. I start at coordinate (0, 0). Then I go through the rows of the array and draw the tile using the spriteBatch.Draw method. There are many overloads to the Draw method. I just used the first one. The parameters are:


  1. the texture to be drawn

  2. the destination rectanlge on the screen

  3. the tint color

The texture is easy. It is just the texture from the list indexed by the index from the tile map. The destination rectangle is what needs explaining. The destination rectangle is a rectangle whose x and y coordinate will go someting like this: (0, 0), (32, 0), (64, 0), (96, 0), ... for the first row and (0, 0), (0, 32), (0, 64), (0, 96), ... for the first column.

The second row would go like this (32, 0), (32, 32), (32, 64), (32, 96), ...

The second column would go like this (32, 0), (32, 32,), (32, 64), (32, 96)...

You will see better when I manage to get the tutorial for the tile engine working.

The tint color allows you to tint your textures. Typically you don't want any tint so you put in Color.White. That is the basics of how tile works. There are a few more concepts to be tied in. Such as preventing the map from scrolling off the screen, only drawing the portion of that map that is visible and scrolling the map in general.

Friday, April 10, 2009

What's up...

Well, I have been very busy the past few days. I'm trying to find some nice textures to use for the tile demonstration, so far I have not had very much luck. The tile program is done though and I am trying to write it up. I might download the Role-Playing Game starter kit from the XNA Creators Club and use some of those tiles. I don't think they can be distributed with the finished game though. I have found some nice ones for isometric tiling but I don't want to get into that.

Look for the tutorial maybe on the 11th or the 13th at the latest. I am not going to be doing anything on the 12th as it is Easter.

Thursday, April 9, 2009

Introduction to XNA 3.0

XNA Game Studio 3.0 is a Framework offered by Microsoft for creating games with C#. With it you can create Windows games, XBOX 360 games and games for Zune. I don't have an XBOX 360 or a Zune so I really have no idea about those platforms.

For Windows games you must meet the minimum requirements for Visual C# 2008 Express Edition and you must have a graphics card that supports Pixel Shader 1.1 or better. Most modern ATI and nVidia graphics cards support Pixel Shader 1.1 or better. If you do not have a card that supports Pixel Shader 1.1 you really can not program in XNA.

In XNA you have the Content Pipeline. It is used to import a wide variety of content to your project. For the purposes of what I will be doing all you have to worry about is Texture2D, which will be used for tiling and sprites and sound files. Eventually there will be some XML files but I am going to make custom importers, processors and exporters to compile them into .XNB files. Otherwise the XML files could be edited by others playing your game and they can change the content of your game.

XNA has two methods that are called often. They are the Draw and Update methods. In the Update method you process thing like input, collisions, moving objects, etc. In the Draw method you draw your scene. The rate at which they are called can be changed programmatically.

Tomorrow, or the next day, I plan on posting a tutorial on how to do basic tiling. So look for that in the next do or so.

Wednesday, April 8, 2009

Really good news!

I just got the adapter for my notebook! I should be able to start posting some actual game code soon. So look for new tutorials with more coding using XNA!

Next tutorial on RPG

Welcome back.

Today I will be posting a new tutorial on the Role-Playing game. In this tutorial I talk about how you could go about implementing the different modes of play in the game in such a way that you can easily edit and change the modes with out going into your code and manually changing them. Like I said when I first started these tutorials, I want to try and make the game skinable so all you have to do to make a new game is edit the data files. Hopefully this will work out.

Role-Playing Game Tutorial 2

Tuesday, April 7, 2009

What is next...

I am working on writing the second tutorial about making a role-playing game in XNA 3.0. In this one I will be talking about some of the technical details of the game. Some of the features that I want to implement and how I am going to implement them. I also decided to remove the AdSence from the tutorial, after all you are interested in programming not ads and I want to try and keep you in mind when writing these tutorials. You have also noticed that I changed the blog template. I hope it is a little easier to use.

Monday, April 6, 2009

First tutorial on Role-Playing game

Well, I finished the tutorial on creating an editor for making a Role-Playing game using XNA 3.0. It is a little long. If you just want to download the code and look at it, that is fine. If you have downloaded the code and don't understand something or you want to read the tutorial to see why I did something the way I did that is fine too. I will place two links that you can travel to, one for the tutorial and one for the project. Look back tomorrow and I will probably have another post.

Role-Playing Game Tutorial 1

Role-Playing Game Project

The NPC editor

Hi again and welcome back.

I have been working hard on the editor and the tutorial on writing the editor. It is almost done and it should be posted very soon, maybe even tonight. In the editor I have elected to use XML Serialization instead of coding it by hand. I haven't yet added error checking when loading in XML files. I just want to get an editor working so you can start working on creating the different NPCs and monsters for the game. Which in itself can be fun.

It will be a while for the part for my notebook (up to three weeks) so I won't be able to do any XNA stuff. I will try to work on the design aspect until the part comes in. I know a lot of people just want to throw code at a problem but I am a firm believer in planning. I find that if you have a good plan for what you want to accomplish it is easier to work out what you need to do to accomplish your goals. I will also try and talk about some of the XNA specifcs that I am planning on using.

So tomorrow I will try and post some more about how the game will work in more detail. One thing that I am thinking about is having different difficulty levels. An easy mode, normal mode and hard mode. Easy mode characters will level up faster than normal mode characters but they will not get as many points to distribute between their attributes and skills. In hard mode it will take more experience to level up than normal characters but the character will get more points toward their skills and attributes.

Sunday, April 5, 2009

Delays, delays, delays...

It looks like I will have to order the AC adapter from HP directly. That is going to take up to 7 business days to get it. So, what I am going to do is this. I am going to work on an editor for creating NPCs and monsters using Visual C# Express 2005 (only because that is what is on this computer.) You should be able to upgrade it 2008 with no problems though. It will output a XML file that will be added to the game using a content importer, processor and exporter. So today/tomorrow I will decide what is to be included in the NPC/monster classes. Things such as Experience value, gold drop range, spawn rate, etc. The class will be imported into the game later so keep it around. Sorry about the delays.

Role-playing game specifications

Here is a run down on the design elements of the role-playing game engine that I will be developing.

First off it will be a single character game(I might expand it so that others may join your party.) It will be a skill based game instead of a class based game. In that way the player will be able to have more control over their character and they can choose how their character will develop. The character (and all other NPCs and creatures) will have six abilities. They will be Strength, Stamina, Agility, Speed, Intellect and Luck. Strength will help measure how much damage a character can do. Stamina will help determine how much damage a character can take. Agility will help in deteriming attack success and evasion. Speed will help a character strike first in combat. Intellect will determine the power of spells the character can cast. Luck will measure the ability of the character to have critical hits. I'm still working on the skills that a character will be able to learn.

The game will have a simple inventory system. Equipment will require certain levels of ability. For example a character with a low Strength will not be able to wear heavy armor and a character with low Intellect will not be able to learn sophisticated spells.

There will be three types of maps for the player to explore but they will all use the same tile engine to be displayed. The first is the adventure map where the player will be able to travel between towns and dungeons. Towns will have buildings the player can enter to buy equipment, rest, find quests, etc. Dungeons will be where the character will fight monsters. They will all behave relatively the same.

Combat will take place on a seperate screen. It will be a simple turn-based combat system. There will be an intiative roll that will be modified by Speed. The one with the highest intiative will go fisrt.

There will be a quest system where you will be able to find quests to perform. The quest system will be done using XML. It will use a content processor, importer and exporter so that the quests will be compiled into an XNB file.

I'm leaving right now to see about an adapter for my laptop. Hopefully they will have one and I will be able to start the tutorial on the tile engine. So check back soon and hopefully the tutorial will be up shortly.

Saturday, April 4, 2009

When it rains it pours...

Just when I thougt everything had been worked out I have another problem with my computer. I need to replace the AC adapter for my notebook. It will not be until Monday or Tuesday. So tomorrow I will post a sort of design document about what the role-playing game will be like and hopefully I will be able to get the adapter quickly. The store should have them in stock and I can get back to the tutorials.

Friday, April 3, 2009

The next tutorial

I am hoping that the next tutorial will be ready Sunday. But until then I want to make a mini-post.

I find the documentation on how to check if a key has been pressed and released a little confusing. Here is a simple way to do it. You need to save the old state of the keyboard and compare it to the current state of the keyboard. I believe what they are checking is if the current state of the keyboard the key is down and the last state of the keyboard is up. I think that it works better if the current state of the keyboard is up and the last state of the keyboard is down.

So, create an XNA game. In the Game1.cs file add a variable at the top of the class to hold the old state of the keyboard and a variable to hold a color:

KeyboardState oldState;
Color bgColor = Color.White;

Now in the Update method add a call to a new method:

CheckKeyboard();

Now, write the CheckKeyboard method as follows:

private void CheckKeyboard()
{
  KeyboardState currentState = Keyboard.GetState();

  if (currentState.IsKeyUp(Keys.Space))
    {
      if (oldState.IsKeyDown(Keys.Space))
      {
        bgColor = new Color((byte)~bgColor.R,
          (byte)~bgColor.G,
          (byte)~bgColor.B);
      }
    }

    oldState = currentState;
}

Now just replace the Color.CornflowerBlue in the Clear call to bgColor in the Draw method. When you run the program the screen should switch between black and white when you press the Space key and let it go.

Give it a try and see how it works. I am not posting the project for this simple tutorial.

Thursday, April 2, 2009

So, so sorry

Sorry that I haven't posted in a while but life has a way of changing your plans. I don't have the time that I thought I would have and I did have some technical difficulties for the past little while. With that said I have decided to change things a little. I know I said that I wanted to make tutorials that people with less capable hardware could do. Everything that I want to do is incredibly complex and each little step needs tremendous explanation. So I have made a new decision. I am going to make posts to help you write your own skinnable role-playing game using Visual C# Express Edition 2008 and XNA Game Studio 3.0. I know that there is the role-playing game starter kit on creators.xna.com that you can follow and I think it is a good starter kit and well worth exploring. I will be trying a slightly different approach though. You are welcome to use any of the code that I post to create your own game. All that I ask is that if you publish your game you send me an email and tell me that you are going to and that you put my name in the credits.

This will be a 2D game with a scrolling map that you can have as many layers as you want and as many maps as you want. The quests will be fully customisable as well as the characters in the game. I will also be putting in utilities that will help you create quests, maps, items, etc. I want to try and do two or three posts each week to try and help you get started. Just remember, I am not an artist so the graphics will not be the best but there are many places on the Internet where you can find good quality graphics.

So look for the first post in a few days. It will be about creating a simple tile engine that will be at the heart of the game.

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.