home *** CD-ROM | disk | FTP | other *** search
/ Game Programming in C++ - Start to Finish / GameProgrammingS.iso / developer_install / ReplicaNetFreewareV5_4.exe / data1.cab / Program_Executable_Files / Example1 / MainGame.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  7.9 KB  |  229 lines

  1. /* START_LICENSE_HEADER
  2.  
  3. Copyright (C) 2000 Martin Piper, original design and program code
  4. Copyright (C) 2001-2005 Replica Software
  5.  
  6. This program file is copyright (C) Replica Software and can only be used under license.
  7. For more information visit: http://www.replicanet.com/
  8. Or email: info@replicanet.com
  9.  
  10. END_LICENSE_HEADER */
  11. /*
  12. This file contains the main parts of the game that deal with ReplicaNet objects.
  13. There are three mains parts. The init routine (DoGameStuff), the frame tick (FrameMove) and the render (Render)
  14. */
  15. #define STRICT
  16. #include <math.h>
  17. #include <stdio.h>
  18.  
  19. #include "Network.h"
  20.  
  21. #include "Plane.h"
  22. #include "Camera.h"
  23.  
  24. #include "GameClass.h"
  25.  
  26. using namespace RNReplicaNet;
  27.  
  28. /**
  29.  * This initialises the game state.
  30.  */
  31. void CMyD3DApplication::DoGameStuff(void)
  32. {
  33.     /* This starts the ReplicaNet session and allows the suer to create or join a game */
  34.     Network_Init(gCommandLine);
  35.  
  36.     /* Allocate our plane */
  37.     mPlayerConrolledPlane = new Plane();
  38.     /* Publish our plane */
  39.     mPlayerConrolledPlane->Publish();
  40.  
  41.     /* Allocate a camera */
  42.     mPlayerConrolledCamera = new Camera();
  43.     /* Publish our camera */
  44.     mPlayerConrolledCamera->Publish();
  45.     /* Set the observer for this game session to be this camera object */
  46.     /* This is used by the prediction algorithm so calculate the distance to other objects in the scene */
  47.     /* The greater the distance then the less bandwidth the object can use. Each object's parameters for prediction */
  48.     /* are defined in the ROL file for the object */
  49.     gNetwork->SetObserver(mPlayerConrolledCamera);
  50. }
  51.  
  52. float repnettime;
  53. float framemovetime;
  54. float rendertime;
  55.  
  56. /**
  57.  * Called once per frame, this call is the entry point for animating
  58.  */
  59. HRESULT CMyD3DApplication::FrameMove()
  60. {
  61.     // Poll ReplicaNet because we have enabled the manual Poll() calling structure
  62.     repnettime = gNetwork->GetLocalTime();
  63.     gNetwork->Poll();
  64.     repnettime = gNetwork->GetLocalTime() - repnettime;
  65.  
  66.     framemovetime = gNetwork->GetLocalTime();
  67.     // Tell ReplicaNet that we don't want to have objects allocated or free'd while scanning the object list
  68.     gNetwork->LockObjects();
  69.     for (int i=0;i<(int)gGameObjects.size();i++)
  70.     {
  71.         // Call each object's PollIt() function
  72.         gGameObjects[i]->PollIt();
  73.  
  74.         // If the Delete() function got called then delete the object properly
  75.         if (gGameObjects[i]->mDeleteMe)
  76.         {
  77.             delete gGameObjects[i];
  78.             i--;
  79.         }
  80.     }
  81.     // Tell ReplicaNet that the object list is no longer being used
  82.     gNetwork->UnLockObjects();
  83.  
  84.     // Now setup the camera position and lights for this frame
  85.  
  86.     // Setup the world spin matrix
  87.     D3DXMatrixTranslation( &m_matTerrainMatrix, 0.0f, 0.0f, 0.0f );
  88.  
  89.     D3DXVECTOR4 campt = D3DXVECTOR4(0,5,15,0);
  90.     D3DXVec4Transform(&campt,&campt,&mPlayerConrolledPlane->m_matObjectMatrix);
  91.     campt += mPlayerConrolledPlane->GetPosition();
  92.  
  93.     // Move the light
  94.     FLOAT Lx =   5;
  95.     FLOAT Ly =   5;
  96.     FLOAT Lz =  -5;
  97.     D3DLIGHT8 light;
  98.     D3DUtil_InitLight( light, D3DLIGHT_POINT, Lx, Ly, Lz );
  99.     light.Attenuation0 = 0.9f;
  100.     light.Attenuation1 = 0.0f;
  101.     m_pd3dDevice->SetLight( 0, &light );
  102.  
  103.     // Set the transform matrices
  104.     D3DXVECTOR3 vEyePt    = D3DXVECTOR3( campt.x, campt.y, campt.z );
  105.     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( mPlayerConrolledPlane->GetPosition().x,  mPlayerConrolledPlane->GetPosition().y,   mPlayerConrolledPlane->GetPosition().z  );
  106.  
  107. //    vEyePt    = D3DXVECTOR3( 0, 20.0f, 30.0f );
  108. //    vLookatPt = D3DXVECTOR3( 0,  0,   0  );
  109.  
  110.     vEyePt    = D3DXVECTOR3( 0, 30.0f, 40.0f );
  111.     vLookatPt = D3DXVECTOR3( -30.0f,  0,   0  );
  112.  
  113.     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f,  1.0f,   0.0f  );
  114.     D3DXMATRIX matWorld, matView, matProj;
  115.  
  116.     D3DXMatrixIdentity( &matWorld );
  117.     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  118.     FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
  119.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
  120.  
  121.     m_pd3dDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  122.     m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  123.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  124.  
  125.     mPlayerConrolledCamera->mPosition.x = vEyePt.x;
  126.     mPlayerConrolledCamera->mPosition.y = vEyePt.y;
  127.     mPlayerConrolledCamera->mPosition.z = vEyePt.z;
  128.  
  129.     framemovetime = gNetwork->GetLocalTime() - framemovetime;
  130.  
  131.     return S_OK;
  132. }
  133.  
  134. /**
  135.  * Called once per frame, the call is the entry point for 3d
  136.  *       rendering. This function sets up render states, clears the
  137.  *       viewport, and renders the scene.
  138.  */
  139. HRESULT CMyD3DApplication::Render()
  140. {
  141.     rendertime = gNetwork->GetLocalTime();
  142.  
  143.     HRESULT ret = m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0xff0000ff, 1.0f, 0L );
  144.  
  145.     if (ret == D3DERR_INVALIDCALL)
  146.     {
  147.         OutputDebugString("Invalid call\n");
  148.     }
  149.  
  150.     // Begin the scene
  151.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  152.     {
  153.         m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matTerrainMatrix );
  154.         m_pTerrainObject->Render( m_pd3dDevice );
  155.  
  156.         /* Again lock our game objects list while we render */
  157.         gNetwork->LockObjects();
  158.  
  159.         for (int i=0;i<(int)gGameObjects.size();i++)
  160.         {
  161.             ReplicaObject *robject;
  162.             // Get the derived class object pointer that contains a base class type of ReplicaObject from the GameObject class
  163.             robject = gGameObjects[i]->mReplica;
  164.             // If there is no object pointer then ignore this object for rendering
  165.             if (!robject)
  166.             {
  167.                 continue;
  168.             }
  169.             // Compare the class id of the ReplicaObject with the class ID of the ReplicaObject Plane object
  170.             // If they are the same then we can assume the type cast to a Plane class is valid
  171.             if (robject->GetClassID() == _MAKE_RO(Plane)::StaticGetClassID())
  172.             {
  173.                 // Perform the type cast
  174.                 Plane *theplane = (Plane *) gGameObjects[i];
  175.  
  176.                 // Calculate the real matrix for the object
  177.                 D3DXMatrixRotationYawPitchRoll(&theplane->m_matObjectMatrix,theplane->GetRotation().y,theplane->GetRotation().x,theplane->GetRotation().z);
  178.                 if (theplane->IsMaster())
  179.                 {
  180.                     D3DXMATRIX scale;
  181.                     D3DXMatrixScaling(&scale,2.0f,2.0f,2.0f);
  182.                     D3DXMatrixMultiply( &theplane->m_matObjectMatrix, &theplane->m_matObjectMatrix, &scale);
  183.                 }
  184.                 
  185.                 D3DXMATRIX transmat;
  186.                 D3DXMatrixTranslation(&transmat,theplane->GetPosition().x,theplane->GetPosition().y,theplane->GetPosition().z);
  187.                 D3DXMatrixMultiply( &theplane->m_matObjectMatrix, &theplane->m_matObjectMatrix, &transmat);
  188.                 m_matObjectMatrix = theplane->m_matObjectMatrix;
  189.                 m_pd3dDevice->SetTransform( D3DTS_WORLD, &theplane->m_matObjectMatrix );
  190.                 theplane->mMesh->Render( m_pd3dDevice );
  191.  
  192.                 m_pShadowVolume = 0;
  193.             }
  194.         }
  195.  
  196.         /* Now unlock our game objects list since we don't access it from now on */
  197.         gNetwork->UnLockObjects();
  198.  
  199.         rendertime = gNetwork->GetLocalTime() - rendertime;
  200.  
  201.         // Output statistics
  202.         m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  203.         m_pFont->DrawText( 2, 10, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  204.  
  205.         char buffer[256];
  206.  
  207.         if (gNetwork->IsMaster())
  208.         {
  209.             sprintf(buffer,"MasterSession ID %d Send bytes/sec %d Recv %d bytes/sec",gNetwork->GetSessionID(),(int) gNetwork->GetNetworkSendRate(),(int) gNetwork->GetNetworkReceiveRate());
  210.         }
  211.         else
  212.         {
  213.             sprintf(buffer,"Session ID %d Send bytes/sec %d Recv %d bytes/sec",gNetwork->GetSessionID(),(int) gNetwork->GetNetworkSendRate(),(int) gNetwork->GetNetworkReceiveRate());
  214.         }
  215.         m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), buffer );
  216.         sprintf(buffer,"Packets resent %d",gNetwork->GetNetworkPacketsLost());
  217.         m_pFont->DrawText( 2, 30, D3DCOLOR_ARGB(255,255,255,0), buffer );
  218.         sprintf(buffer,"URL: %s",gNetwork->SessionExportURL().c_str());
  219.         m_pFont->DrawText( 2, 40, D3DCOLOR_ARGB(255,255,255,0), buffer );
  220.         sprintf(buffer,"ReplicaNet %.2fms Frame move %.2fms Render %.2fms",repnettime*1000.0f,framemovetime*1000.0f,rendertime*1000.0f);
  221.         m_pFont->DrawText( 2, 50, D3DCOLOR_ARGB(255,255,255,0), buffer );
  222.  
  223.         // End the scene.
  224.         m_pd3dDevice->EndScene();
  225.      }
  226.  
  227.     return S_OK;
  228. }
  229.