Sunday, February 3, 2008

002 - Creating a simple Hello World project

Tutorial 002

Creating a Simple “Hello World” Project

Hello again! In this tutorial I will demonstrate how to make a simple project which uses Dark Game Development Kit. Prerequisites must be installed on your PC.

I had to remove some formatting and images to make this tutorial compatible to be viewed on this blog. However, you can download the original tutorial in PDF format.

So let’s begin!

  1. Start Visual C++.
  2. Create a new Project. Select General -> Empty Project and give it a name.From the Solution Explorer right-click Source Files and add new C++ file Main.cpp (You can give any name you like. It is not necessary to name it Main)
  3. Now write the following code in Main.cpp and press F5
//DarkGDK.h MUST be included.

#include "DarkGDK.h"

//Instead of main() we have DarkGDK() for game development.

void DarkGDK ( void )

{

//Syncronization must be on. Set default framerate to 60.

dbSyncOn ();

dbSyncRate ( 60 );

//Main loop of the game.

while( LoopGDK () )

{

//Show something on the screen

dbText (0 , 10 , "Welcome to Dark Game Development" );

//Synchronize

dbSync ();

}

}


Explanation:


DarkGDK.h

This header file includes all the lower level functions necessary to interact with DirectX.

void DarkGDK ( void )

This is the main entry point for the Game Project. For our normal C++ application we would call main function, but for games we call DarkGDK function.

while ( LoopGDK () ) { }

In my previous tutorial I have mentioned that the game runs in an infinite loop. Every object gets some time to perform its task in each cycle. This is that loop. LoopGDK () always returns true unless you press the Escape Key. Hence, the while loop will run indefinitely unless you break it or use Escape Key. You have to write the core game code inside this loop. Whatever you write inside will get executed in each cycle. If you want to setup something like loading characters, setting up values etc. do it before this loop starts.

dbSyncOn (), dbSyncRate (60) and dbSync()

At the end of each loop, we have to synchronize the screen. I will explain why it is necessary. Suppose we are making a war strategy game and we are moving a whole army. To facilitate that, we have to write code which will move each character of that army one by one by some distance value. Imagine how it would look if this whole process was actually being seen on the screen? So we must perform the actions necessary in the loop and at the end of the loop we have to show it on the screen at once. The command for that is dbSync().

dbSyncOn() & dbSyncRate (60) tells the computer that we want to turn synchronization on and sync should be done 60 times per second. These two commands should be written before the loop.

dbText (0 , 10 , "Welcome to Dark Game Development" )

This is the command to show 2D text on screen. 0 and 10 are ( X , Y ) co-ordinates. Why was this text written inside while loop? Because we want the text to be seen on the screen continuously. Had we written it before loop, it would be seen for a fraction of second and then disappear on the first dbSync() call.



So that’s it! I hope you’ve learned how to create a simple Hello World project. Now you will be more comfortable when we move towards making a simple game.

Sunday, January 27, 2008

#001 - Beginning Game Development

Tutorial 001

Introduction to Dark Game Development

Hello and welcome to Dark Game Development blog. My name is Orpheus. I am a software engineer and a hobbyist game developer. I have started this series of tutorials to share my knowledge with everybody. I had to do a lot of R&D when the first time I started developing games, because the resources were not available or not free. So I hope this series will help you to make your games easily.

In this tutorial I will explain the life cycle of game development and some fundamental differences between standard application and a game. I will also list out the tools needed to make a complete game, and the links to find them.

I had to remove some formatting and images to make this tutorial compatible to be viewed on this blog. However, you can download the original tutorial in PDF format.

Prerequisites:

  • · You should have at least working knowledge of OOP using C++. You need not be a guru, but I can not teach you C++ the language itself. I will teach how to develop a game using C++ & Dark Game Development Kit

So let’s begin!

The Virtual World

Every game has a world of its own. This world includes various entities like locations, objects, characters etc. Every entity has a life of its own. Every entity has some specific attribute & behavior.

Let’s take an example of GTA Vice City. See the image.

All these Plants, Sky, Buildings, Player, Female Characters etc are entities. The world of GTA Vice City is composed of them. These entities have a life span. They appear during the period when they are live and then they disappear. These entities can be either static or dynamic. Static entities are fixed. They do not move or animate, where as dynamic entities can. Dynamic entities can have artificial intelligence, so they can not only perform actions, but can behave in a certain manner.

Now these entities are nothing but 3D objects. We can make 3D objects using any decent tool like 3DS MAX, Maya etc. But 3D modeling can be very difficult if you are not an expert. There are also some tools available which are targeted towards specific needs. For example gameSpace (very easy for general 3D modeling), 3D World Studio (to make buildings, level, landscape etc.), TextureMaker (To make skin* for the3D objects) etc..

Skin is an image which contains colors for the 3D object. Image is always 2D, so it uses UV projection of 3D objects. If you don’t understand all this mumbo jumbo, just leave it. It’s simply a JPEG or BITMAP or DDS which will be used to color parts of 3D objects.

In this tutorial series I will not go deep into 3D modeling. Instead, I will use 3D objects which are freely available on net. Although 3D modeling is the first part of the game designing, you will soon lose patience if you are a hardcore programmer and not an artist.

To summarize, the physical structure of the entity is designed, not programmed. It’s the attribute and the behavior that is defined using the program. This is where the programming languages come into the picture. Programming languages use DirectX, OpenGL etc technologies to import these entities into a virtual 3D space. To make them behave in a certain manner we use artificial intelligence techniques, which are nothing but pure logic.

Using DirectX directly can be too difficult to handle, so we can use readymade engines which provide us a set of functions to perform basic tasks. Dark Game Development Kit is one of them and we will be using it because it is FREE to develop and distribute our games made using Dark GDK. However, if you want to sell your game, you will have to purchase a $500 license.

Although we can make our objects (entities) to behave in a certain manner without using Object Oriented Programming techniques, using OOP is always good, because as we’ve already seen, every entity is actually an object which has lifespan, attributes, and behavior which is very similar to OOP fundamentals. So we will be using

· gameSpace, 3D World Studio etc. to make landscapes, levels, static dynamic objects
· TextureMaker to make textures for 3D objects, as well as background image for game menu
· C++ to make Classes for each object, to define attributes (member variables) and behavior (functions),
· DarkGDK (which is a function library for C++) to aid in defining the behavior, mainly to perform lower level actions like importing and placing a 3D object from file into a virtual 3D space, moving it to a specific co-ordinate etc.

The Main Loop

There is a major difference between application programming and game programming. In application, we give an interface to user and then wait for the user to fire an event. Or in old style procedure oriented program, we do everything step by step which are predefined. But in the game programming, there is a combination of both. Although the game will wait for user input, it will also start the life of other objects than the player. For example, in GTA Vice City, the enemy character won’t wait for you to pull the trigger. He has his own intelligence and will kill you if you come too close. Another example is that the female character will wander around the pool area, they will not stop for you to press forward button.

To facilitate this kind of behavior, the main program has an infinite loop running, in which you will write the main game code which will get executed in every cycle. It may seem weird right now, but soon you will get comfortable with the idea of infinite loop (which is considered a dangerous flaw in standard application programming).

So, normally your program will be like this:

1. Load the level object
2. Load all the static and dynamic objects and position them into the level
3. Declare all the flags, variables etc. Finish all the remaining settings
4. Start the main loop
5. Go to step 11 if user has pressed Escape Key
6. Animate all dynamic characters
7. If user has pressed movement keys then move him in 3D world.
8. If user has pressed shoot key then shoot the bullet
9. If bullet has been fired then check whether it has hurt or killed somebody, if it has then act accordingly
10. Go to step 4
11. Unload all the objects from memory
12. Exit

Example

Let me show this using an example. Note that the example will not work actually because I am not showing all the small programming details. I just want to give you an overview about how your project will look.

Game.h

class Level

{

int _levelID;

char* _levelPath;

public:

Level ()

{

}

Level ( char* levelPath )

{

//Initiate level object

}

};

class Sky

{

//Define Sky

};

class Gun

{

//Define Gun

};

class ShotGun: public Gun

{

//Derived from Gun

};

class Bullet

{

//Define Bullet

};

class Player

{

int _playerID;

char* _playerPath;

Gun* _playerGun;

public:

Player ()

{

}

Player ( char* playerPath , Gun* playerGun )

{

//Initiate Player object

}

int Move ( float distance )

{

//Call DarkGDK command for moving object (here object is Player)

}

int Shoot ()

{

Bullet b;

//Call DarkGDK command for moving object (here object is Bullet)

}

~Player ()

{

}

};

class FemaleCharacter

{

//Define FemaleCharacter

void MoveAround ( float area )

{

//Call DarkGDK command for moving object (here object is FemaleCharacter)

}

};

Main.cpp

//Include header files.

#include "DarkGDK.h"

#include "Game.h"

//The main function of the project

void DarkGDK ( void )

{

//Load and Setup all the objects

Level* level1 = new Level("C:\\Obj\\level1.x");

Sky* skyDay = new Sky("C:\\Obj\\sky.x");

Player* hero = new Player("C:\\Obj\\hero.x",new ShotGun());

FemaleCharacter* fem = new FemaleCharacter("C:\\Obj\\bikinigirl.x");

//The main loop

while( /* Condition */ )

{

//Move around Female Characters in area of 1000 units

fem->MoveAround(1000);

//Move Player by 2 units if user presses Forward Key

if ( /* Condition */ ) hero->Move(2);

}

}

So you can see that apart from the differences, other aspects of programming is exactly what you already do using your normal C++.

Tools

You will need the following tools to develop game.

3D Modeling & Designing
gameSpaceLight Free Edition (Registration required, but totally FREE)
3DS Max
Maya

3D World Designing
3D World Studio

Texturing Tools
Texture Maker 3 (Although it says Trial, it has a limited functionality mode which is more than necessary)
Adobe Photoshop

Programming
C++ (Visual C++ 2008 Express - FREE)
Dark Game Development Kit for VC++
Direct X SDK (32-bit) for VC++
Direct X SDK (64-bit) for VC++
Dark GDK Collision Library for VC++
Direct X 9.0c Redistributable
VC++ Redistributable (32-bit)
VC++ Redistributable (64-bit)





Dark GDK Installation:

  1. Install Visual C++ 2008
  2. Install DirectX 9.0c Redist Aug2007
  3. Install DirectX SDK 9.0c Aug2007 (x86 for 32-bit processor & x64 for 64-bit processor)
  4. Install Dark GDK
To run your game on another PC you will need to bundle
  1. DirectX 9.0c Redist Aug2007
  2. VC++ Runtimes
along with your game.