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 / Example9 / MainGame.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  8.8 KB  |  269 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 "Dolphin.h"
  23.  
  24. #include "GameClass.h"
  25.  
  26. using namespace RNReplicaNet;
  27.  
  28.  
  29. /**
  30.  * This initialises the game state.
  31.  */
  32. void CMyD3DApplication::DoGameStuff(void)
  33. {
  34.     /* This starts the ReplicaNet session and allows the suer to create or join a game */
  35.     Network_Init(gCommandLine);
  36.  
  37.     // If this node is the master then allocate our planes
  38.     if (gNetwork->IsMaster())
  39.     {
  40.         Plane *plane;
  41.  
  42.         plane = new Plane();
  43.         plane->SetPosition(D3DXVECTOR4(0,0,0,0));
  44.         plane->SetRotation(D3DXVECTOR4(0,0,0,0));
  45.         plane->Publish();
  46.  
  47.         plane = new Plane();
  48.         plane->SetPosition(D3DXVECTOR4(-25,0,-25,0));
  49.         plane->SetRotation(D3DXVECTOR4(0,D3DXToRadian(90.0f),0,0));
  50.         plane->Publish();
  51.  
  52.         plane = new Plane();
  53.         plane->SetPosition(D3DXVECTOR4(-50,0,0,0));
  54.         plane->SetRotation(D3DXVECTOR4(0,D3DXToRadian(180.0f),0,0));
  55.         plane->Publish();
  56.  
  57.         plane = new Plane();
  58.         plane->SetPosition(D3DXVECTOR4(-25,0,25,0));
  59.         plane->SetRotation(D3DXVECTOR4(0,D3DXToRadian(270.0f),0,0));
  60.         plane->Publish();
  61.     }
  62.     else
  63.     {
  64.         Dolphin *dolphin = new Dolphin();
  65.         dolphin->Publish();
  66.     }
  67. }
  68.  
  69. float repnettime;
  70. float framemovetime;
  71. float rendertime;
  72.  
  73. /**
  74.  * Called once per frame, this call is the entry point for animating
  75.  */
  76. HRESULT CMyD3DApplication::FrameMove()
  77. {
  78.     // Poll ReplicaNet because we have enabled the manual Poll() calling structure
  79.     repnettime = gNetwork->GetLocalTime();
  80.     gNetwork->Poll();
  81.     repnettime = gNetwork->GetLocalTime() - repnettime;
  82.  
  83.     framemovetime = gNetwork->GetLocalTime();
  84.     // Tell ReplicaNet that we don't want to have objects allocated or free'd while scanning the object list
  85.     gNetwork->LockObjects();
  86.     int i;
  87.     for (i=0;i<(int)gGameObjects.size();i++)
  88.     {
  89.         // Call each object's PollIt() function
  90.         gGameObjects[i]->PollIt();
  91.  
  92.         // If the Delete() function got called then delete the object properly
  93.         if (gGameObjects[i]->mDeleteMe)
  94.         {
  95.             delete gGameObjects[i];
  96.             i--;
  97.         }
  98.     }
  99.     // Tell ReplicaNet that the object list is no longer being used
  100.     gNetwork->UnLockObjects();
  101.  
  102.     // Now setup the camera position and lights for this frame
  103.  
  104.     // Setup the world spin matrix
  105.     D3DXMatrixTranslation( &m_matTerrainMatrix, 0.0f, 0.0f, 0.0f );
  106.  
  107.     D3DXVECTOR4 campt = D3DXVECTOR4(0,5,15,0);
  108.  
  109.     // Move the light
  110.     FLOAT Lx =   5;
  111.     FLOAT Ly =   5;
  112.     FLOAT Lz =  -5;
  113.     D3DLIGHT8 light;
  114.     D3DUtil_InitLight( light, D3DLIGHT_POINT, Lx, Ly, Lz );
  115.     light.Attenuation0 = 0.9f;
  116.     light.Attenuation1 = 0.0f;
  117.     m_pd3dDevice->SetLight( 0, &light );
  118.  
  119.     // Set the transform matrices
  120.     D3DXVECTOR3 vEyePt    = D3DXVECTOR3( campt.x, campt.y, campt.z );
  121.     D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0,  0,  0);
  122.  
  123. //    vEyePt    = D3DXVECTOR3( 0, 20.0f, 30.0f );
  124. //    vLookatPt = D3DXVECTOR3( 0,  0,   0  );
  125.  
  126.     vEyePt    = D3DXVECTOR3( 0, 30.0f, 40.0f );
  127.     vLookatPt = D3DXVECTOR3( -30.0f,  0,   0  );
  128.  
  129.     D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f,  1.0f,   0.0f  );
  130.     D3DXMATRIX matWorld, matView, matProj;
  131.  
  132.     D3DXMatrixIdentity( &matWorld );
  133.     D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  134.     FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
  135.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 100.0f );
  136.  
  137.     m_pd3dDevice->SetTransform( D3DTS_WORLD,      &matWorld );
  138.     m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
  139.     m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  140.  
  141.     framemovetime = gNetwork->GetLocalTime() - framemovetime;
  142.  
  143.     return S_OK;
  144. }
  145.  
  146. /**
  147.  * Called once per frame, the call is the entry point for 3d
  148.  *       rendering. This function sets up render states, clears the
  149.  *       viewport, and renders the scene.
  150.  */
  151. HRESULT CMyD3DApplication::Render()
  152. {
  153.     rendertime = gNetwork->GetLocalTime();
  154.  
  155.     HRESULT ret = m_pd3dDevice->Clear( 0L, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,0xff0000ff, 1.0f, 0L );
  156.  
  157.     if (ret == D3DERR_INVALIDCALL)
  158.     {
  159.         OutputDebugString("Invalid call\n");
  160.     }
  161.  
  162.     // Begin the scene
  163.     if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
  164.     {
  165.         m_pd3dDevice->SetTransform( D3DTS_WORLD, &m_matTerrainMatrix );
  166.         m_pTerrainObject->Render( m_pd3dDevice );
  167.  
  168.         /* Again lock our game objects list while we render */
  169.         gNetwork->LockObjects();
  170.  
  171.         for (int i=0;i<(int)gGameObjects.size();i++)
  172.         {
  173.             ReplicaObject *robject;
  174.             // Get the derived class object pointer that contains a base class type of ReplicaObject from the GameObject class
  175.             robject = gGameObjects[i]->mReplica;
  176.             // If there is no object pointer then ignore this object for rendering
  177.             if (!robject)
  178.             {
  179.                 continue;
  180.             }
  181.             // Compare the class id of the ReplicaObject with the class ID of the ReplicaObject Plane object
  182.             // If they are the same then we can assume the type cast to a Plane class is valid
  183.             if (robject->GetClassID() == _MAKE_RO(Plane)::StaticGetClassID())
  184.             {
  185.                 // Perform the type cast
  186.                 Plane *theplane = (Plane *) gGameObjects[i];
  187.  
  188.                 // Calculate the real matrix for the object
  189.                 D3DXMatrixRotationYawPitchRoll(&theplane->m_matObjectMatrix,theplane->GetRotation().y,theplane->GetRotation().x,theplane->GetRotation().z);
  190.                 if (theplane->IsMaster())
  191.                 {
  192.                     D3DXMATRIX scale;
  193.                     D3DXMatrixScaling(&scale,2.0f,2.0f,2.0f);
  194.                     D3DXMatrixMultiply( &theplane->m_matObjectMatrix, &theplane->m_matObjectMatrix, &scale);
  195.                 }
  196.                 
  197.                 D3DXMATRIX transmat;
  198.                 D3DXMatrixTranslation(&transmat,theplane->GetPosition().x,theplane->GetPosition().y,theplane->GetPosition().z);
  199.                 D3DXMatrixMultiply( &theplane->m_matObjectMatrix, &theplane->m_matObjectMatrix, &transmat);
  200.                 m_matObjectMatrix = theplane->m_matObjectMatrix;
  201.                 m_pd3dDevice->SetTransform( D3DTS_WORLD, &theplane->m_matObjectMatrix );
  202.                 theplane->mMesh->Render( m_pd3dDevice );
  203.  
  204.                 m_pShadowVolume = 0;
  205.             } else if (robject->GetClassID() == _MAKE_RO(Dolphin)::StaticGetClassID())
  206.             {
  207.                 // Perform the type cast
  208.                 Dolphin *thedolphin = (Dolphin *) gGameObjects[i];
  209.  
  210.                 // Calculate the real matrix for the object
  211.                 D3DXMatrixRotationYawPitchRoll(&thedolphin->m_matObjectMatrix,thedolphin->GetRotation().y,thedolphin->GetRotation().x,thedolphin->GetRotation().z);
  212.  
  213.  
  214.                 D3DXMATRIX matrix;
  215.                 D3DXMatrixRotationYawPitchRoll(&matrix,thedolphin->GetRotation().y,thedolphin->GetRotation().x,thedolphin->GetRotation().z);
  216.  
  217.                 D3DXMATRIX temp;
  218.                 D3DXMatrixScaling(&temp,0.05f,0.05f,0.05f);
  219.                 D3DXMatrixMultiply(&matrix, &matrix, &temp);
  220.  
  221.                 D3DXMatrixRotationYawPitchRoll(&temp,D3DXToRadian(-90.0f),0,0);
  222.                 D3DXMatrixMultiply(&matrix, &temp,&matrix);
  223.  
  224.                 D3DXMATRIX transmat;
  225.                 D3DXMatrixTranslation(&transmat,thedolphin->GetPosition().x,thedolphin->GetPosition().y,thedolphin->GetPosition().z);
  226.                 D3DXMatrixMultiply( &matrix, &matrix, &transmat);
  227.                 m_matObjectMatrix = matrix;
  228.                 m_pd3dDevice->SetTransform( D3DTS_WORLD, &matrix );
  229.  
  230.                 thedolphin->mMesh->Render( m_pd3dDevice );
  231.  
  232.                 m_pShadowVolume = 0;
  233.             }
  234.         }
  235.  
  236.         /* Now unlock our game objects list since we don't access it from now on */
  237.         gNetwork->UnLockObjects();
  238.  
  239.         rendertime = gNetwork->GetLocalTime() - rendertime;
  240.  
  241.         // Output statistics
  242.         m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
  243.         m_pFont->DrawText( 2, 10, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
  244.  
  245.         char buffer[256];
  246.  
  247.         if (gNetwork->IsMaster())
  248.         {
  249.             sprintf(buffer,"MasterSession ID %d Send bytes/sec %d Recv %d bytes/sec",gNetwork->GetSessionID(),(int) gNetwork->GetNetworkSendRate(),(int) gNetwork->GetNetworkReceiveRate());
  250.         }
  251.         else
  252.         {
  253.             sprintf(buffer,"Session ID %d Send bytes/sec %d Recv %d bytes/sec",gNetwork->GetSessionID(),(int) gNetwork->GetNetworkSendRate(),(int) gNetwork->GetNetworkReceiveRate());
  254.         }
  255.         m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), buffer );
  256.         sprintf(buffer,"Packets resent %d",gNetwork->GetNetworkPacketsLost());
  257.         m_pFont->DrawText( 2, 30, D3DCOLOR_ARGB(255,255,255,0), buffer );
  258.         sprintf(buffer,"URL: %s",gNetwork->SessionExportURL().c_str());
  259.         m_pFont->DrawText( 2, 40, D3DCOLOR_ARGB(255,255,255,0), buffer );
  260.         sprintf(buffer,"ReplicaNet %.2fms Frame move %.2fms Render %.2fms",repnettime*1000.0f,framemovetime*1000.0f,rendertime*1000.0f);
  261.         m_pFont->DrawText( 2, 50, D3DCOLOR_ARGB(255,255,255,0), buffer );
  262.  
  263.         // End the scene.
  264.         m_pd3dDevice->EndScene();
  265.      }
  266.  
  267.     return S_OK;
  268. }
  269.