home *** CD-ROM | disk | FTP | other *** search
/ NEXT Generation 27 / NEXT27.iso / pc / demos / emperor / dx3.exe / SDK / SAMPLES / IKLOWNS / CGREMQUE.CPP < prev    next >
C/C++ Source or Header  |  1996-08-28  |  7KB  |  228 lines

  1. /*===========================================================================*\
  2. |
  3. |  File:        cgremque.cpp
  4. |
  5. |  Description: 
  6. |   Routines to create and manage received remote actions
  7. |       
  8. |-----------------------------------------------------------------------------
  9. |
  10. |  Copyright (C) 1995-1996 Microsoft Corporation.  All Rights Reserved.
  11. |
  12. |  Written by Moss Bay Engineering, Inc. under contract to Microsoft Corporation
  13. |
  14. \*===========================================================================*/
  15.  
  16. /**************************************************************************
  17.  
  18.     (C) Copyright 1995-1996 Microsoft Corp.  All rights reserved.
  19.  
  20.     You have a royalty-free right to use, modify, reproduce and 
  21.     distribute the Sample Files (and/or any modified version) in 
  22.     any way you find useful, provided that you agree that 
  23.     Microsoft has no warranty obligations or liability for any 
  24.     Sample Application Files which are modified. 
  25.  
  26.     we do not recomend you base your game on IKlowns, start with one of
  27.     the other simpler sample apps in the GDK
  28.  
  29.  **************************************************************************/
  30.  
  31. //** include files **
  32. #ifndef __WATCOMC__
  33. #define WIN32_LEAN_AND_MEAN
  34. #endif
  35. #include <windows.h>
  36. #include <windowsx.h>
  37. #include "linklist.h"
  38. #include "dplay.h"
  39. #include "cgremote.h"
  40.  
  41. //** local definitions **
  42.  
  43. // Entry for object action in action queue
  44. typedef struct 
  45. {
  46.     ACTION  Action;
  47.     DWORD   nBytes;
  48.     void    *Data;
  49. } ACTION_ENTRY;
  50.  
  51. //** external functions **
  52.  
  53. //** external data **
  54.  
  55. //** public data **
  56. //** private data **
  57.  
  58. // Master list of objects controlled by remote peers
  59. static CLinkedList  RemoteObjectList;
  60.  
  61. //** public functions **
  62. //** private functions **
  63.  
  64. // ----------------------------------------------------------
  65. // CreateRemoteObjectQueue - register a new object and create an
  66. //  action queue for it!
  67. // ----------------------------------------------------------
  68. CLinkedList *CreateRemoteObjectQueue(
  69.     REMOTE_OBJECT   *pObj,  // unique object id 
  70.     void    *Data       // game character this pointer
  71. )
  72. {
  73.     if (pObj == NULL)
  74.         return(NULL);
  75.  
  76.     // Create an object entry for the master list
  77.     REMOTE_DATA *pRemoteEntry    = new REMOTE_DATA;
  78.  
  79.     // Initialize the object entry and add it to the list!
  80.     memcpy(&pRemoteEntry->RemObj, pObj, sizeof(REMOTE_OBJECT));
  81.     pRemoteEntry->Data = Data;
  82.     pRemoteEntry->ActionList = new CLinkedList;
  83.     RemoteObjectList.Add((void *)pRemoteEntry);
  84.  
  85.     return(pRemoteEntry->ActionList);
  86. }   
  87.  
  88. // ----------------------------------------------------------
  89. // FindRemoteObjectEntry - search list of registered objects 
  90. //  and return its data
  91. // ----------------------------------------------------------
  92. REMOTE_DATA *FindRemoteObjectEntry(
  93.     REMOTE_OBJECT   *pObj       // unique object id
  94. )
  95. {
  96.     REMOTE_DATA *pRemoteEntry=NULL;
  97.  
  98.     // Beware of wise guys!
  99.     if (pObj == NULL)
  100.         return(NULL);
  101.  
  102.     // Search master object list for desired object entry
  103.     pRemoteEntry = (REMOTE_DATA *)RemoteObjectList.GetFirst();
  104.     while (pRemoteEntry != NULL)
  105.     {
  106.         if (memcmp(&pRemoteEntry->RemObj, pObj, sizeof(REMOTE_OBJECT)) == 0) 
  107.         {
  108.             break;          
  109.         }
  110.         pRemoteEntry = (REMOTE_DATA *) RemoteObjectList.GetNext();
  111.     }
  112.     return(pRemoteEntry);
  113. }   
  114.  
  115. // ----------------------------------------------------------
  116. // DestroyRemoteObjectQueue - destroy an objects action queue
  117. //  and remove it from the list of known objects.
  118. // ----------------------------------------------------------
  119. void DestroyRemoteObjectQueue(
  120.     REMOTE_OBJECT   *pObj       // unique object id
  121. )
  122. {
  123.     REMOTE_DATA *pRemoteEntry;
  124.  
  125.     // If there is an entry in the master list, remove it!
  126.     pRemoteEntry = FindRemoteObjectEntry(pObj);
  127.     if (pRemoteEntry != NULL)
  128.     {
  129.         RemoteObjectList.Remove((void *)pRemoteEntry);
  130.         delete pRemoteEntry;
  131.     }
  132. }
  133.     
  134. // ----------------------------------------------------------
  135. // GetRemoteObjectQueue - get a pointer to the action queue
  136. // ----------------------------------------------------------
  137. CLinkedList *GetRemoteObjectQueue(
  138.     REMOTE_OBJECT *pObj     // unique object id
  139. )
  140. {
  141.     REMOTE_DATA *pRemoteEntry;
  142.  
  143.     // Look up object in master list and return a ptr to 
  144.     // its action queue
  145.     pRemoteEntry = FindRemoteObjectEntry(pObj);
  146.     if (pRemoteEntry != NULL)
  147.         return(pRemoteEntry->ActionList);
  148.     else
  149.         return(NULL);
  150.     
  151. }   
  152.  
  153. // ----------------------------------------------------------
  154. // GetRemoteObjectData - retrieve an objects data value
  155. // ----------------------------------------------------------
  156. void *GetRemoteObjectData(
  157.     REMOTE_OBJECT *pObj     // unique object id
  158. )
  159. {
  160.     REMOTE_DATA *pRemoteEntry;
  161.  
  162.     // Look up object in master list and return its data value
  163.     pRemoteEntry = FindRemoteObjectEntry(pObj);
  164.     if (pRemoteEntry != NULL)
  165.         return(pRemoteEntry->Data);
  166.     else
  167.         return(NULL);
  168.     
  169. }   
  170.  
  171. // ----------------------------------------------------------
  172. // QueueRemoteAction - place an action onto the objects queue
  173. // ----------------------------------------------------------
  174. BOOL QueueRemoteAction(
  175.     CLinkedList *pQueue,    // object's action queue 
  176.     ACTION      Action,     // action code
  177.     void        *Data,      // action data 
  178.     DWORD       nBytes      // size of action data
  179. )
  180. {
  181.     // Be sure action queue is valid!
  182.     if (pQueue == NULL)
  183.         return(FALSE);
  184.  
  185.     // Place a new action entry on the queue
  186.     ACTION_ENTRY    *pAction = new ACTION_ENTRY;
  187.  
  188.     pAction->Action = Action;
  189.     pAction->nBytes = nBytes;
  190.     pAction->Data = Data; 
  191.     pQueue->Append((void *)pAction);
  192.     return(TRUE);
  193. }   
  194.  
  195. // ----------------------------------------------------------
  196. // GetNextRemoteAction - get the next action on the objects queue
  197. // ----------------------------------------------------------
  198. ACTION GetNextRemoteAction(
  199.     CLinkedList *pQueue,    // object's action queue
  200.     void        *&Data,     // ptr to a data ptr
  201.     DWORD       &nBytes     // size of action data
  202. )
  203. {
  204.     ACTION_ENTRY    *pAction;
  205.     ACTION      Action=-1;
  206.  
  207.     // Be sure action queue is valid
  208.     if (pQueue == NULL)
  209.         return(-1);
  210.     
  211.     // Take first action off queue
  212.     pAction = (ACTION_ENTRY *)pQueue->RemoveFirst();
  213.  
  214.     // If there is an action, set information for caller 
  215.     if (pAction != NULL)
  216.     {
  217.         Data = pAction->Data;
  218.         nBytes = pAction->nBytes;
  219.         Action = pAction->Action;
  220.  
  221.         // Dispose of the action entry memory, the caller should
  222.         // delete the actual data itself (using ReleaseRemoteData) 
  223.         delete pAction;
  224.     }
  225.  
  226.     return(Action);
  227. }   
  228.