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 / Example6 / main.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-30  |  3.8 KB  |  150 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. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string>
  14. #include "RNReplicaNet/Inc/ReplicaNet.h"
  15. #include "AnObject.h"
  16.  
  17. using namespace RNReplicaNet;
  18.  
  19. int main(int argc,char **argv)
  20. {
  21.     ReplicaNet *network;
  22.  
  23.     // Alloocate a ReplicaNet session
  24.     network = new ReplicaNet();
  25.  
  26.     // Start to find a session
  27.     printf("Searching for a game...\n");
  28.     network->SessionFind();
  29.  
  30.     // Sleep for half a second to wait for replies
  31.     CurrentThread::Sleep(500);
  32.  
  33.     // Enumerate the first found session
  34.     std::string found = network->SessionEnumerateFound();
  35.  
  36.     // Check to see if we have found a session for Example6
  37.     if (found.find("Example6") == std::string::npos)
  38.     {
  39.         found = "";
  40.     }
  41.  
  42.     // If no session name is found then...
  43.     if (found == "")
  44.     {
  45.         // Create a new session
  46.         network->SessionCreate("Example6");
  47.         printf("New game started '%s'\n",network->SessionExportURL().c_str());
  48.     }
  49.     else
  50.     {
  51.         // Join the found session
  52.         printf("Joining session '%s'\n",found.c_str());
  53.         network->SessionJoin(found);
  54.  
  55.         // Wait until the session is stable
  56.         while (!network->IsStable())
  57.         {
  58.             printf(".");
  59.             // Sleep for a short while
  60.             CurrentThread::Sleep(100);
  61.         }
  62.  
  63.         printf("\n");
  64.     }
  65.  
  66.     // Seed our random number generator with our session ID
  67.     srand(network->GetSessionID());
  68.  
  69.     // Now allocate our test object
  70.     AnObject *object;
  71.     object = new AnObject();
  72.  
  73.     object->mList.push_back(10);
  74.     object->mList.push_back(20);
  75.     object->mList.push_back(30);
  76.     object->mList.push_back(40);
  77.  
  78.     object->mSTLVector.push_back(110);
  79.     object->mSTLVector.push_back(120);
  80.     object->mSTLVector.push_back(130);
  81.     object->mSTLVector.push_back(140);
  82.  
  83.  
  84.     // Publish the object on to the network
  85.     object->Publish();
  86.  
  87.     // Go around in an endless loop
  88.     while(1)
  89.     {
  90.         // Now randomly change the mWatchedValue value for our object to test that the values get transmitted
  91.         if (rand() & 1)
  92.         {
  93.             object->mWatchedValue.SetValue(float(rand()));
  94.         }
  95.  
  96.         // Now iterate through all objects for this session
  97.         network->ObjectListBeginIterate();
  98.         ReplicaObject *objects;
  99.         while ( (objects = network->ObjectListIterate()) != 0)
  100.         {
  101.             printf("ReplicaObject %d:%d:%d\n",objects->GetSessionID(),objects->GetUniqueID(),(int)objects->IsMaster());
  102.             // Double check that this ReplicaObject is of a type AnObject
  103.             if (objects->GetClassID() == AnObject::StaticGetClassID())
  104.             {
  105.                 AnObject *realObject = (AnObject *) objects;
  106.                 if (realObject->mWatchedValue.GetUpdated())
  107.                 {
  108.                     printf("Object %d:%d has the value updated\n",realObject->GetSessionID(),realObject->GetUniqueID());
  109.                 }
  110.  
  111.                 std::list<float>::iterator st,en;
  112.                 st = realObject->mList.begin();
  113.                 en = realObject->mList.end();
  114.                 int index = 0;
  115.                 while (st != en)
  116.                 {
  117.                     printf("mList[%d] = %f\n",index++,*st);
  118.                     st++;
  119.                 }
  120.  
  121.                 std::vector<float>::iterator st2,en2;
  122.                 st2 = realObject->mSTLVector.begin();
  123.                 en2 = realObject->mSTLVector.end();
  124.                 index = 0;
  125.                 while (st2 != en2)
  126.                 {
  127.                     printf("mSTLVector[%d] = %f\n",index++,*st2);
  128.                     st2++;
  129.                 }
  130.             }
  131.         }
  132.         network->ObjectListFinishIterate();
  133.  
  134.         if (network->GetLocalTime() > 10.0f)
  135.         {
  136.             // After 10 seconds have elapsed on the local time clock delete the first entry in the mSTLVector vector variable each iteration.
  137.             if (!object->mSTLVector.empty())
  138.             {
  139.                 object->mSTLVector.erase(object->mSTLVector.begin());
  140.             }
  141.         }
  142.  
  143.         // Sleep for one second
  144.         CurrentThread::Sleep(1000);
  145.  
  146.     }
  147.  
  148.     return 0;
  149. }
  150.