topical media & game development
lib-game-darkgdk-samples-Camera-Main.cpp / cpp
// Dark GDK - The Game Creators - www.thegamecreators.com
// include Dark GDK header file
include <DarkGDK.h>
// main entry point for program
void DarkGDK ( void )
{
// this program will show how to use multiple camera
// switch to media directory
dbSetDir ( "media\\" );
// set sync rate and backdrop properties
dbSyncOn ( );
dbSyncRate ( 60 );
dbColorBackdrop ( dbRGB ( 0, 128, 0 ) );
// show a loading message
dbPrint ( "Please wait loading...." );
dbSync ( );
dbSync ( );
// load a wall image
dbLoadImage ( "wall.jpg", 1 );
// load in some character models
for ( int t = 1; t < 3; t++ )
{
// load model and set properties
dbLoadObject ( "miko\\miko.x", t );
dbSetObjectSpecular ( t, 0 );
dbXRotateObject ( t, 270 );
dbFixObjectPivot ( t );
// position based on which one we're creating
if ( t == 1 )
{
dbPositionObject ( t, 0, 0, 200 );
dbRotateObject ( t, 0, 180, 0 );
}
else
{
dbPositionObject ( t, 0, 0, -200 );
dbRotateObject ( t, 0, 180, 0 );
}
// start animation
dbLoopObject ( t, 0, 9000 );
dbSetObjectSpeed ( t, 500 );
}
// load a sphere
dbLoadObject ( <sphere2.x>, 3 );
dbScaleObject ( 3, 1000, 1000, 1000 );
dbScaleObjectTexture ( 3, 10, 5 );
dbTextureObject ( 3, 1 );
// make a reflective sphere
dbMakeObjectSphere ( 5, 1000 );
dbColorObject ( 5, 0 );
dbGhostObjectOn ( 5 );
dbSetReflectionShadingOn ( 5 );
dbScaleObject ( 5, 100, 100, 1 );
dbXRotateObject ( 5, 90 + 180 );
// make a new camera
dbMakeCamera ( 1 );
dbSetCameraView ( 1, 320 + 48, 16, 640 - 16, 240 - 48 );
// set up a light
dbMakeLight ( 1 );
dbColorLight ( 1, 256, 512, 2024 );
dbSetLightRange ( 1, 1000 );
// another sphere for the scene
dbMakeObjectSphere ( 4, 125 );
dbScaleObjectTexture ( 4, 300, 100 );
dbGhostObjectOn ( 4 );
// and another
dbMakeObjectSphere ( 999,10 );
dbColorObject ( 999, dbRGB ( 255, 0, 0 ) );
dbMakeLight ( 7 );
dbColorLight ( 7, 1024, 100, 100 );
dbSetLightRange ( 7, 200 );
// and now some camera properties
dbSetCurrentCamera ( 0 );
dbSetCameraRange ( 1, 7000 );
dbSetPointLight ( 0, 0, 0, 0 );
dbSetLightRange ( 0, 1500 );
dbColorLight ( 0, 256, 512, 256 );
// place some cubes in the scene
for ( int t = 7; t < 29; t++ )
{
float s = ( float ) t;
s = s / 3.0f;
dbMakeObjectCube ( t, s );
dbTextureObject ( t, 4 );
dbSetObject ( t, 1, 1, 1, 0, 0, 0, 0 );
dbGhostObjectOn ( t );
}
// declare some variables for our main loop
float camdist = 0.0;
float v = 0.0f;
float h = 0.0f;
float a = 0.0f;
float x = 0.0f;
float y = 0.0f;
float z = 0.0f;
float d = 0.0f;
float r = 0.0f;
// enter our main loop
while ( LoopGDK ( ) )
{
// quit on escape key
if ( dbEscapeKey ( ) )
return;
// get information from object 1
a = dbObjectAngleY ( 1 );
x = dbObjectPositionX ( 1 );
y = dbObjectPositionY ( 1 );
z = dbObjectPositionZ ( 1 );
// adjust camera 1
dbSetCurrentCamera ( 1 );
dbSetCameraToFollow ( x, y, z, a, -115, 135, 10.0, 0 );
dbSetCurrentCamera ( 0 );
a = dbAtanFull ( x, z );
d = dbSqrt ( ( x * x ) + ( z * z ) );
if ( d > 500 )
{
x = dbSin ( a ) * 500;
z = dbCos ( a ) * 500;
dbPositionObject ( 1, x, y, z );
}
if ( camdist < 100 )
camdist = camdist + 0.1f;
// adjust position of one of our spheres
dbXRotateObject ( 3, dbWrapValue ( dbObjectAngleX ( 3 ) + 0.1f ) );
dbYRotateObject ( 3, dbWrapValue ( dbObjectAngleY ( 3 ) + 0.2f ) );
dbZRotateObject ( 3, dbWrapValue ( dbObjectAngleZ ( 3 ) + 0.3f ) );
v = dbWrapValue ( v + 0.5f );
h = dbWrapValue ( h + 2.0f );
// adjust another sphere
dbPositionObject ( 4, dbSin ( v ) * -750, 100 + ( dbCos ( h ) * 100 ), dbCos ( v ) * -750 );
dbYRotateObject ( 4, v + 60 );
// position light and camera
dbPositionLight ( 1, dbSin ( v ) * -800, 100 + ( dbCos ( h ) * 100 ), dbCos ( v ) * -800 );
dbPositionLight ( 0, dbSin ( v ) * -500, 100 + ( dbCos ( h ) * 100 ), dbCos ( v ) * -500 );
dbPositionCamera ( 0, dbSin ( v ) * ( camdist * 7.0f ), 200 + ( dbCos ( h ) * 100 ), dbCos ( v ) * ( camdist * 7.0f ) );
dbPointCamera ( 0, x, y + 100, z );
// position our cubes
for ( int t = 7; t < 29; t++ )
{
r = dbWrapValue ( r + 0.5f );
dbRotateObject ( t, r, r * 2, r * 3 );
dbPositionObject ( t, dbCos ( r + t ) * 500, 0, dbSin ( r + t ) * 500 );
}
// update screen
dbSync ( );
}
}
(C) Æliens
04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.