topical media & game development

talk show tell print

lib-game-darkgdk-samples-Terrain-Demo.cpp / cpp



  
  // Dark GDK - The Game Creators - www.thegamecreators.com
  
  // include Dark GDK header file
  include <DarkGDK.h>
  
  // global variables - used for the camera
  float g_fSpeed        = 0.1f;                // speed at which camera moves
  float g_fTurn         = 0.03f;                // turn speed for mouse look
  float g_fOldCamAngleX = 0.0f;                // to store original x angle
  float g_fOldCamAngleY = 0.0f;                // to store original y angle
  float g_fCameraAngleX = 0.0f;                // to sotre new x angle
  float g_fCameraAngleY = 0.0f;                // to store new y angle
  
  // forward declaration for functions
  void userInput ( void );                        // handles user input
  void showFPS   ( void );                        // show frame rate
  
  void DarkGDK ( void )
  {
          // entry point for the application
  
          dbSetDir ( "media\\" );
  
          // initial application set up
          dbSyncOn         ( );                                // turn sync on
          dbSyncRate       ( 60 );                        // set sync rate to 60
          dbBackdropOff    ( );                                // switch backdrop off - no need to clear screen
          dbSetCameraRange ( 0.5f, 30000 );        // set the camera range
  
          // load images for the terrain
          dbLoadImage ( "media\\texture.jpg", 1 );        // diffuse texture
          dbLoadImage ( "media\\detail.jpg",  2 );        // detail texture
          
          // create the terrain
          dbSetupTerrain        ( );                                                                                // set up terrain library
          dbMakeObjectTerrain   ( 1 );                                                                        // make a new terrain
          dbSetTerrainHeightMap ( 1, "media\\map.bmp" );                                        // set the terrain height map
          dbSetTerrainScale     ( 1, 3, 0.6f, 3 );                                                // set the scale
          dbSetTerrainSplit     ( 1, 16 );                                                                // set the split value
          dbSetTerrainTiling    ( 1, 4 );                                                                        // set the detail tiling
          dbSetTerrainLight     ( 1, 1.0f, -0.25f, 0.0f, 1, 1, 0.78f, 0.5f );        // set the light
          dbSetTerrainTexture   ( 1, 1, 2 );                                                                // set the textures
          dbBuildTerrain        ( 1 );                                                                        // build the terrain
  
          // create our skybox
          dbLoadObject       ( "media\\skybox2.x", 200 );                // load the skybox model
          dbSetObjectLight   ( 200, 0 );                                                // turn lighting off
          dbSetObjectTexture ( 200, 3, 1 );                                        // set texture properties
          dbPositionObject   ( 200, 1000, 2000, 4000 );                // position the skybox
          dbScaleObject      ( 200, 30000, 30000, 30000 );        // and finally scale
          
          // set starting camera position
          dbPositionCamera ( 385, 23, 100 );
  
          // loop round until escape key is pressed
          while ( LoopGDK ( ) )
          {
                  if ( dbEscapeKey ( ) )
                          return;
  
                  // show the frame rate and handle user input
                  showFPS   ( );
                  userInput ( );
  
                  // update the terrain
                  dbUpdateTerrain ( );
  
                  // render everything
                  dbSync ( );
          }
  }
  
  void showFPS ( void )
  {
          char szFPS [ 256 ] = "";
          strcpy ( szFPS, "fps = " );
          strcat ( szFPS, dbStr ( dbScreenFPS ( ) ) );
          dbText ( dbScreenWidth ( ) - 20 - dbTextWidth ( szFPS ), dbScreenHeight ( ) - 40, szFPS );
  }
  
  void userInput ( void )
  {
          dbControlCameraUsingArrowKeys ( 0, g_fSpeed, g_fTurn );
  
          g_fOldCamAngleY = g_fCameraAngleY;
          g_fOldCamAngleX = g_fCameraAngleX;
  
          g_fCameraAngleY = dbWrapValue ( g_fCameraAngleY + dbMouseMoveX ( ) * 0.4f );
          g_fCameraAngleX = dbWrapValue ( g_fCameraAngleX + dbMouseMoveY ( ) * 0.4f );
  
          dbYRotateCamera ( dbCurveAngle ( g_fCameraAngleY, g_fOldCamAngleY, 24 ) );
          dbXRotateCamera ( dbCurveAngle ( g_fCameraAngleX, g_fOldCamAngleX, 24 ) );
  
          char* szKey = dbInKey ( );
  
          if ( strcmp ( szKey, "+" ) == 0 )
          {
                  if ( g_fSpeed < 1000 )
                          g_fSpeed = g_fSpeed + 0.01f;
          }
  
          if ( strcmp ( szKey, "-" ) == 0 )
          {
                  if ( g_fSpeed > 0.02f )
                          g_fSpeed = g_fSpeed - 0.01f;
          }
  
          float fHeight = dbGetTerrainGroundHeight ( 1, dbCameraPositionX ( ), dbCameraPositionZ ( ) );
  
          dbPositionCamera ( dbCameraPositionX ( ), fHeight + 3.0f, dbCameraPositionZ ( ) );
  }


(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.