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.

No comments: