topical media & game development

talk show tell print

lib-game-darkgdk-samples-Light-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 )
  {
          // switch  to media directory
          dbSetDir ( "media\\" );
  
          // set sync rate
          dbSyncOn   ( );
          dbSyncRate ( 0 );
  
          // turn backdrop off
          dbBackdropOff ( );
  
          // switch light normalization on
          dbSetNormalizationOn ( );
  
          // load our character / statue model
          dbLoadObject ( "models\\statue.x", 1 );
          dbLoadImage  ( "models\\bumpmap.jpg", 1 );
  
          // position it
          dbXRotateObject     ( 1, 270.0f );
          dbFixObjectPivot    ( 1 );
          dbSetLightMappingOn ( 1, 1 );
          dbSetObjectSpecular ( 1, 0 );
          dbRotateObject      ( 1, 0, 0, 0 );
  
          // load our world
          dbLoadObject        ( "world\\room.x", 2 );
          dbXRotateObject     ( 2, 270 );
          dbFixObjectPivot    ( 2 );
          dbSetObjectSpecular ( 2, 0 );
          dbRotateObject      ( 2, 0, 0, 0 );
  
          // hide defaul light
          dbHideLight ( 0 );
  
          // create lights and spheres
          for ( int i = 1; i < 8; i++ )
          {
                  dbMakeLight     ( i );
                  dbSetPointLight ( i, 0, 0, 0 );
                  dbSetLightRange ( i, 2000 );
  
                  dbMakeObjectSphere ( 2 + i, 50 );
                  dbGhostObjectOn    ( 2 + i );
  
                  if ( i > 1 )
                  {
                          dbHideLight  ( i );
                          dbHideObject ( 2 + i );
                  }
          }
  
          // declare some variables
          int keypress = 0;
          float a=0, b=0, c=0;
  
          // main loop
          while ( LoopGDK ( ) )
          {
                  // quit on escape key
                  if ( dbEscapeKey ( ) )
                          return;
  
                  // use variables to move the lights
                  a = dbWrapValue ( a + 0.5f );
                  b = dbWrapValue ( b + 0.2f );
                  c = dbWrapValue ( c + 0.05f );
                  float x = dbCos ( c ) * 250.0f;
                  float z = dbSin ( c ) * 300.0f;
  
                  // position light, camera and point camera
                  dbPositionLight  ( 0, x, 150, z );
                  dbPositionCamera ( x, 100, z );
                  dbPointCamera    ( 0, 150, 0 );
                  
                  // move lights
                  for ( int i = 1; i < 8; i++ )
                  {
                          float x = dbCos ( a + ( i * 85 ) ) * 300.0f;
                          float z = dbSin ( a + ( i * 85 ) ) * 150.0f;
  
                          dbPositionLight  ( i, x, 100 + ( dbSin ( b ) * 100 ), z );
                          dbPositionObject ( 2 + i, x, 100 + ( dbSin ( b ) * 100 ), z );
                  }
  
                  float leva  = 0.0f;
                  leva  = dbWrapValue ( leva + 0.01f );
                  float level = ( float ) cos ( leva );
                  
                  int i;
  
                  // colour lights
                  for ( i = 1; i < 8; i++ )
                  {
                          float col = 127.0f + ( 127.0f * level );
                          float rev = col *- 1;
  
                          if ( i == 1 ) dbColorLight ( i, ( int ) col, ( int ) rev, ( int ) rev );
                          if ( i == 2 ) dbColorLight ( i, 128, ( int ) col, 128 );
                          if ( i == 3 ) dbColorLight ( i, 255, 255, ( int ) col * 2 );
                          if ( i == 4 ) dbColorLight ( i, ( int ) col, ( int ) rev, ( int ) col );
                          if ( i == 5 ) dbColorLight ( i, ( int ) col, ( int ) col, ( int ) col );
                          if ( i == 6 ) dbColorLight ( i, 500, 500, 500 );
                          if ( i == 7 ) dbColorLight ( i, ( int ) rev * 3,( int ) rev * 3, ( int ) rev * 3 );
                  }
                  
                  // when a key has been pressed show or hide a light
                  char* szKey = dbInKey ( );
  
                  if ( strspn ( szKey, "1234567" ) )
                  {
                          if ( keypress == 0 )
                          {
                                  keypress = 1;
  
                                  int i = 1 + ( dbAsc ( szKey ) - dbAsc ( "1" ) );
  
                                  if ( dbLightVisible ( i ) )
                                  {
                                          dbHideLight  ( i );
                                          dbHideObject ( i + 2 );
                                  }
                                  else
                                  {
                                          dbShowLight  ( i );
                                          dbShowObject ( i + 2 );
                                  }
                          }
                  }
                  else
                          keypress = 0;
  
                  // display information
                  dbText ( 20, dbScreenHeight ( ) - 40, "Press keys 1 - 7 to toggle lights" );
  
                  // show frame rate
                  char szFPS [ 256 ] = "";
                  strcpy ( szFPS, "fps = " );
                  strcat ( szFPS, dbStr ( dbScreenFPS ( ) ) );
                  dbText ( dbScreenWidth ( ) - 20 - dbTextWidth ( szFPS ), dbScreenHeight ( ) - 40, szFPS );
  
                  // 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.