VR-Link API Documentation for HLA 1.3
Listen C Example

This is a wrapped version of the Listen example, that works in the C environment.


First a C language interface is created. No classes or other C++ concepts are exported.

#ifdef __cplusplus
extern "C" {
#endif

   /* routines to interface between listenC.c and VR-Link C++ calls */

   void initVRLink(int argc, char *argv[]);
   void tickVRLink();
   double *firstEntityPosition();

#ifdef __cplusplus
}
#endif

Here is a C language program that uses the C Language interface.

#include <stdio.h>

#include "listenC.h"
#include "interface.h"

int main(int argc, char *argv[])
{
   double *firstPos;

   /* this C++ function is being called from C */
   initVRLink(argc, argv);
   while (1)
   {
      tickVRLink();
      firstPos = firstEntityPosition();
      if (firstPos)
      {
         printf("Position of first entity: %.1f %.1f %.1f\n", 
            firstPos[0], firstPos[1], firstPos[2]);
      }
   }
   return 0;
}

/* this C function will be called from C++ */
void firePrint(const char* attacker)
{
   printf("Fire Pdu from %s\n", attacker);
}


We write the wrapper code in C++ inside the interface.

/*********************************************************************
** Copyright (c) 1997 MaK Technologies, Inc.
** All rights reserved.
*********************************************************************/

#include <vl/exerciseConn.h>
#include <vl/reflEntList.h>
#include <vlutil/vlProcessControl.h>
#include <vl/fireInter.h>
#include <vl/reflectedEnt.h>
#include <vl/exConnInit.h>

#include "listenC.h"
#include "interface.h"

/* routines to interface between listenC.c and VR-Link C++ calls */

static void fireCb(DtFireInteraction *inter, void *);

static DtExerciseConn *exConn;
static DtReflectedEntityList *rva;

void initVRLink(int argc, char *argv[]) 
{

   // Create a VR-Link initializer to parse command lines.
   DtVrlApplicationInitializer appInit(argc, argv, "VR-Link ListenC");

   // Parse the command lines. 
   appInit.parseCmdLine();

   // Connect to the RTI or the Network.
   exConn = new DtExerciseConn(appInit);

   // Add a callback for FIRE interactions/PDUs
   DtFireInteraction::addCallback(exConn, fireCb, NULL);

   // Create a class which will contain all known entities from the network. 
   rva = new DtReflectedEntityList(exConn);

   // Set up our time.
   DtTimeInit();
}

void tickVRLink()
{
   DtSetSimTime(DtGetElapsedRealTime());
   exConn->drainInput();
   DtSleep(0.1);
}

double *firstEntityPosition()
{  
   DtReflectedEntity *first = rva->first();
   if (first)
   {
      return (double *) (const double*) first->entityStateRep()->location();
   }
   return NULL;
}

static void fireCb(DtFireInteraction *inter, void *)
{
   firePrint(inter->attackerId().string());
}


Document ID: Generated on Mon May 14 08:06:18 EDT 2012 from SVN revision 114750
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)