Tutorial: Making a game with DarkGDK part two

Part 2: Media

I’m not going to go into detail about making 3d meshes or textures right now but I will supply you with some to use.

Download

Now lets get back to working on the game and use those art assets. Open up the project again and here’s what you’ve got to do in Main.cpp to load up everything and get your character moving round. I commented the hell out of it so even if you just copy and paste it all at once hopefully it will make sense. It will be easier to read from within the IDE.


//include DarkGDK and Sparky's collission detection which well be needing for later
#include "DarkGDK.h"
#include "SC_Collision.h"

//now declare the variables that we are going to need to use.
//I want to play in fullscreen without changing the resoltution and GetSystemMetrics is a good way to find out what the current desktop resolution is.
int screenx=GetSystemMetrics(SM_CXSCREEN);
int screeny=GetSystemMetrics(SM_CYSCREEN);
//dark gdk identafies 3d objects by a number and you cant assign multiple objects to the same number so I number them by the order there created in so you can check what the next available number is by keeping track of how many objects already exist.
int objcount=1;
//your going to want to remember what the object number is for your charecter since your going to be moving it around.
int youobj;
//isnt really neccessary but I like to record keypresses as variables
int up;int down;int left;int right;
//keep track of your movement speed and what angle your heading in.
//using double rather then int essentially means that it can deal with decimals rather then only whole numbers.
double movespeed;double moveangle=0;
//you will want to have a few multi purpose variables
double x;double y;double z;

void DarkGDK ( void ){
//now to start with the rest of the pre game setup.
//set the game resolution to the current desktop resolution which we found out before.
dbSetDisplayMode(screenx,screeny,32);
//since the games in full screen people probable wotn see this but its good to label things.
dbSetWindowTitle("TimeFrack");
//set it to full screen.
dbSetWindowOff();
//set what color will be shown on screen where there arent any visisble objects.
dbColorBackdrop(dbRGB(0,0,0));
//throutle the frame rate to a predetermend rate (in this case 30)
dbSyncOn();dbSyncRate(30);
//keep DarkGDK for doing anything anoying with the camera.
dbAutoCamOff();
//turn off the mouse cursore.
dbHideMouse();
//start up Sparky's collission detection.
SC_Start();

//now we start loading those objects.
//load the room.
dbLoadObject("media\\map.x",objcount);
//load the texture.
dbLoadImage("media\\tile.png",1);
//apply the texture.
dbTextureObject(objcount,1);
//update the object count so we know not to assign that number to anymore objects.
objcount=objcount+1;

//load your charecter model.
dbLoadObject("media\\person.x",objcount);
//keep track of its oject number.
youobj=objcount;
//update the object count so we know not to assign that number to anymore objects.
objcount=objcount+1;

//now we run the loop that the actual game occures in.
while ( LoopGDK ( ) ){

//update the charecter object and camera rotation based on mouse movements so you can turn and aim.
dbRotateObject(youobj,0,dbObjectAngleY(youobj)+(double)dbMouseMoveX()/5,0);
dbRotateCamera(dbCameraAngleX()+(double)dbMouseMoveY()/10,dbObjectAngleY(youobj),0);

//check what keys are down.
up=dbKeyState(17);down=dbKeyState(31);left=dbKeyState(30);right=dbKeyState(32);
//now if more then one key is down we need to change your movement direction and speed based on what combenation of keys are being used.
if (up+down+left+right==0){
movespeed=(movespeed*4)/5;}else{movespeed=(movespeed*9+2)/10;
if (up==1)if(down==0)if(left==0)if(right==0)moveangle=0;
if (up==0)if(down==1)if(left==0)if(right==0)moveangle=180;
if (up==0)if(down==0)if(left==1)if(right==0)moveangle=270;
if (up==0)if(down==0)if(left==0)if(right==1)moveangle=90;
if (up==1)if(down==0)if(left==1)if(right==0)moveangle=315;
if (up==1)if(down==0)if(left==0)if(right==1)moveangle=45;
if (up==0)if(down==1)if(left==1)if(right==0)moveangle=225;
if (up==0)if(down==1)if(left==0)if(right==1)moveangle=135;
}

//update your position and reposition the camera just behind you.
x=dbNewXValue(dbObjectPositionX(youobj),dbObjectAngleY(youobj)+moveangle,movespeed);
y=dbObjectPositionY(youobj);
z=dbNewZValue(dbObjectPositionZ(youobj),dbObjectAngleY(youobj)+moveangle,movespeed);
dbPositionObject(youobj,x,y,z);
x=dbNewXValue(x,dbObjectAngleY(youobj),-25);
y=y+25;
z=dbNewZValue(z,dbObjectAngleY(youobj),-25);
dbPositionCamera(x,y,z);

//update the screen.
dbSync ( );
//complete the loop.
}
//exit the game (by default DarkGDK automatically does this when the esc key is hit).
return;}

You might have noticed there seemingly random numbers being used when checking what keys where being pressed. It isn’t actually at all arbitrary. Each key on a keyboard is assigned a scan code and the dbKeyState() command returns a one if the specified key is being pressed down and zero if it isn’t. Here’s a nice little diagram to check anytime you want to know what the scan code is for a key.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: