The 'source' button on my first post does not show the code. So, I'll try it here:
#include "DarkSDK.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
// key scan codes of interest
const int ciScanCode_Enter = 28;
const int ciScanCode_PadEnter = 156;
const int ciScanCode_UpArrow = 200;
const int ciScanCode_DownArrow = 208;
const int ciScanCode_Q = 16;
const int ciScanCode_P = 25;
const int ciScanCode_U = 22;
const int ciScanCode_D = 32;
// font sizes
#define myfs0 14
#define myfs1 20
#define myfs2 18
#define myfs3 16
#define myfs4 22
typedef struct
{
float x, y, z;
float angle;
} fyCoords;
typedef fyCoords& fyCoordsRef;
typedef struct fyPlayerStruct
{
int id;
float x, y, z;
float angle;
} fyPlayer;
// some global stuff
fyPlayer player;
int playable=20;
int attlimb;
void MakeThePlayer()
{
player.id = 1;
player.x = 10.0;
player.y = 10.0;
player.z = 10.0;
player.angle = 45.0;
dbMakeObjectSphere(player.id, 1);
dbHideObject(player.id);
dbPositionObject(player.id, player.x, player.y, player.z);
}
void MakeFloors()
{
// create the floor, load image for it
dbLoadImage("pebble01.png", 1);
// create, position and texture the floor
dbMakeMatrix(1, 500.0, 500.0, 20, 20);
dbPositionMatrix(1, 0, 0, 0);
dbPrepareMatrixTexture(1, 1, 1, 1);
}
int FindAttachToLimb(int obj,char* limb)
{
int i,c;
char* a;
// we need to find the correct limb to attach to
dbPerformCheckListForObjectLimbs(obj);
// get count of limbs
c = dbChecklistQuantity();
// parse the list
for (i=1;i<=c;i++)
{
a = (char*)dbChecklistString(0,i);
if (strcmp(a,limb)==0) return (i-1);
}
return -1;
}
void MakeThePlayable()
{
dbLoadObject("spiral-staircase-up.x",playable);
dbPositionObject(playable,100.0f,0.0f,100.0f);
attlimb = FindAttachToLimb(playable,"player");
}
void PlayThePlayable()
{
dbSetObjectSpeed(playable,10);
dbPlayObject(playable);
}
void doDbText(int xloc, int& yloc,int fntsiz,const char* format,...)
{
char szTxt[1024];
va_list marker;
// initialise variable arguments
va_start(marker,format);
// make local string
vsprintf(szTxt,format,marker);
// close it off
va_end(marker);
// write it out
dbText(xloc,yloc,szTxt);
// update the y location
yloc += fntsiz;
}
void showFPS()
{
char szFPS[256] = "";
sprintf(szFPS, "fps = %d",dbScreenFPS());
dbText((dbScreenWidth()-20-dbTextWidth(szFPS)),
(dbScreenHeight()-40), szFPS);
}
void GetLimbPosition(fyCoordsRef lpos,int lobj,int lnum)
{
// calculate where to place the object
lpos.x = dbLimbPositionX(lobj,lnum);
lpos.y = dbLimbPositionY(lobj,lnum);
lpos.z = dbLimbPositionZ(lobj,lnum);
lpos.angle = dbLimbAngleY(lobj,lnum);
}
void DisplayPlayerInformation()
{
int yloc,xloc;
// calculate initial location
xloc = 10;
yloc = dbScreenHeight() - 4 * myfs0;
// output frame information
dbInk(dbRGB(0,0,0),dbRGB(255,255,255));
doDbText(xloc,yloc,myfs0,"Player x=%.1f y=%.1f z=%.1f angle=%.1f",
player.x,player.y,player.z,player.angle);
}
void DisplayInstructions()
{
int xloc=10;
int yloc=10;
dbInk(dbRGB(0,0,0),dbRGB(255,255,255));
doDbText(xloc,yloc,myfs0,"Left/Right key to change player angle");
doDbText(xloc,yloc,myfs0,"Up/down key to move player forward/back");
doDbText(xloc,yloc,myfs0,"'U' key to move player vertically up");
doDbText(xloc,yloc,myfs0,"'D' key to move player vertically down");
doDbText(xloc,yloc,myfs0,"'P' key to animate object & show limb movement");
}
void DisplayObjectLimbsAndFrames()
{
fyCoords lpos;
char* a;
int xloc=(dbScreenWidth()-20-20*myfs0);
int yloc=10;
int c,i;
GetLimbPosition(lpos,playable,attlimb);
// set color
dbInk(dbRGB(0,0,0),dbRGB(255,255,255));
// output frame information
doDbText(xloc,yloc,myfs0,"object#=%d limb=%d",playable,attlimb);
doDbText(xloc,yloc,myfs0,"frames=%d",dbTotalObjectFrames(playable));
doDbText(xloc,yloc,myfs0,"current=%5.2f",dbObjectFrame(playable));
// output limb position
doDbText(xloc,yloc,myfs0,"limb x=%.1f z=%.1f",lpos.x,lpos.z);
doDbText(xloc,yloc,myfs0,"limb y=%.1f angle=%.1f",lpos.y,lpos.angle);
// get list of limbs for the object
dbPerformCheckListForObjectLimbs(playable);
// get count of limbs
c = dbChecklistQuantity();
// parse the list
for (i=1;i<=c;i++)
{
a = (char*)dbChecklistString(0,i);
doDbText(xloc,yloc,myfs0,"limb#=%d name='%s'",(i-1),a);
}
// clean up
dbEmptyChecklist();
}
void movePlayer(void)
{
float angle = player.angle;
float kd = 0.0;
float newx, newz;
// handle rotation
if(dbLeftKey() == 1)
{
if(dbControlKey() == 1)
{
angle = dbWrapValue(angle - 15.0f);
} else {
angle = dbWrapValue(angle - 2.0f);
}
}
if(dbRightKey() == 1)
{
if(dbControlKey() == 1)
{
angle = dbWrapValue(angle + 15.0f);
} else {
angle = dbWrapValue(angle + 2.0f);
}
}
// check for forward/backward motion
if(dbDownKey() == 1) kd = -1.0;
if(dbUpKey() == 1) kd = 1.0;
// apply accelerators
if(dbShiftKey() == 1) kd *= 5.0;
if(dbControlKey() == 1) kd *= 20.0;
// check for rotation
if(angle != player.angle)
{
// store the new value
player.angle = angle;
// rotate the player
dbYRotateObject(player.id, player.angle);
}
// move the player
dbMoveObject(player.id, kd);
// get the new position
newx = dbObjectPositionX(player.id);
newz = dbObjectPositionZ(player.id);
// check for going over edge
if((newx < -0.10) ||(newx > 510.0) ||
(newz < -0.10) ||(newz > 510.0))
{
// restore position
dbPositionObject(player.id, player.x, player.y, player.z);
} else {
// store the information
player.x = newx;
player.z = newz;
}
}
void positionCamera(void)
{
// get new camera position
float newx = dbNewXValue(player.x,player.angle-180.0f,5.0f);
float newz = dbNewZValue(player.z,player.angle-180.0f,5.0f);
// reposition and point camera
dbPositionCamera(newx,(5.0f + player.y), newz);
dbPointCamera(player.x,(5.0f + player.y), player.z);
}
void DarkSDK(void)
{
if (dbCheckDisplayMode(640,480,16)) dbSetDisplayMode(640,480,16);
if (dbCheckDisplayMode(1024,768,16)) dbSetDisplayMode(1024,768,16);
dbSyncOn ();
dbSyncRate(0);
// set the directory for stuff
dbSetDir("media\\");
// make a floor
MakeFloors();
// create models to be tested
MakeThePlayable();
// create the player
MakeThePlayer();
while(LoopSDK())
{
if(dbEscapeKey()) return;
switch(dbScanCode())
{
case ciScanCode_U:
if(player.y<100.0) player.y += 2.0;
break;
case ciScanCode_D:
if(player.y>-100.0) player.y -= 2.0;
break;
case ciScanCode_P:
PlayThePlayable();
break;
}
movePlayer();
DisplayInstructions();
DisplayObjectLimbsAndFrames();
DisplayPlayerInformation();
positionCamera();
showFPS();
dbSync();
}
}
Cheers,
Pterdactyl