TN Games Vest Get Shot in C#


I attended the Game Developers Conference (GDC)back in 2008 and saw this vest and fell in love with it.   It has the ability to let you “Feel” when you get shot or fragged in a game.  I quickly noticed how you change the way you play when you wear it because you know the “pain” is coming when you get shot.  Ok not really pain, but an air pocket inflates where you are shot.   I was even more lucky when my wife decided to get me one for my birthday that year.  I played it with many different game but of course in the Waterhobo fashion I wanted more.   

I had hoped to have the vest connected and usable with the WiFi Warthogs at the PDC, but it was not meant to be.  There is a SDK, but it is a C++ unmanaged code.  On top of that it did not have any dlls but only the header and library file.  Of course I am not a C++ person, my C skills are dated and I could not really get any help from TN Games or for that matter the internet.  So it meant C++ here I come, so if you know C++ and see something that could be better please comment on a better way.  I can say that what I put together did work. 

First I needed to turn the C++ unmanaged code into a dll that could be imported into a C# managed dll.  I happened to find a code snipped that kind of met my needs, not perfect, but  it did put me on the right path.   Below is the code that allowed me to make the unmanaged dll that I would need in order make the managed one.  I got it to work fine, but I am not the most qualified to explain it.  The parts to note are the linking of the tngaming.lib and the header file that explain the sdk from TN Games.

   1: // vest.cpp : Defines the exported functions for the DLL application.

   2: //


   4: #include "stdafx.h"

   5: #include "vest.h"


   7: #include "tngaming.h"


   9: /* --- link library */

  10: #pragma comment(lib,"tngaming.lib")



  13: // This is an example of an exported variable

  14: VEST_API int nvest=0;



  17: // This is an example of an exported function.

  18: VEST_API int setupvest(void)

  19: {


  21:     return SetUpJacket();

  22: }


  24: VEST_API void shutdownvest(void)

  25: {

  26:      TearDownJacket();


  28: }


  30: VEST_API int vestseteffect(int nEffect)

  31: {


  33:     return SetEffect(nEffect);

  34: }


  36: VEST_API int vestseteffect2(int speed, int actuator)

  37: {


  39:     return SetEffect2(speed,actuator);

  40: }


  42: VEST_API int vestgeterrorcode(void)

  43: {


  45:     return GetErrorCode();

  46: }

  47: VEST_API int vestgeterrortext(void)

  48: {

  49:     if (SetUpJacket()  != GLIB_OK)

  50:     {

  51:         //printf("Error: %s", GetErrorText());

  52:         return 2;

  53:     }

  54:     return 42;

  55: }


  57: VEST_API void vestflushbuffer(int actuator)

  58: {

  59:     FlushBuffer(actuator);


  61: }


  63: // This is the constructor of a class that has been exported.

  64: // see vest.h for the class definition

  65: Cvest::Cvest()

  66: {

  67:     return;

  68: }


Now with the unmanaged code library built it is a simple matter of importing it into a C# wrapper.  Then another simple process of  creating a test harness to call our new dll and test all of the functionality.

A few notes on the dll building process, I put everything together as best as I could based my limited C++ knowledge and the included SDK documentation.  The test harness works fine, but a few things I did notice.  It is possible I did something wrong with the C++; so if you know C++ please let me know, but it seems as if the TN Games sdk does not match the documentation.  The return codes did not come back as stated, enum did not work as stated, you do not get error statuses if you do not set the vest up first as stated in the documentation.  Again I do not know if it is me, the documentation, the TN Games lib file or a combination of all that I did not get everything to match the documentation; so this is my only disclaimer.  So on with the show…

In the code below I am importing the unmanaged code into my C# dll, I am also defining all the functions that will be called.  They are a match to the C++ dll functions, so this is just a pass through from C# to C++.

   1: public class TNGamesVest

   2: {


   4:     //need to import all of the functions from TNGames.lib vest.dll is the C++ wrapper around the TNGames lib

   5:     [DllImport("vest.dll", EntryPoint = "?setupvest@@YAHXZ")] public static extern int setupvest();

   6:     [DllImport("vest.dll", EntryPoint="?shutdownvest@@YAXXZ")] static extern  void shutdownvest();

   7:     [DllImport("vest.dll", EntryPoint="?vestseteffect@@YAHH@Z")] static extern int vestseteffect(int nEffect);

   8:     [DllImport("vest.dll",EntryPoint="?vestseteffect2@@YAHHH@Z")] static extern int vestseteffect2(int speed, int actuator);

   9:     [DllImport("vest.dll",EntryPoint="?vestgeterrorcode@@YAHXZ")] static extern int vestgeterrorcode();

  10:     [DllImport("vest.dll",EntryPoint="?vestgeterrortext@@YAHXZ")] static extern int vestgeterrortext();

  11:     [DllImport("vest.dll",EntryPoint="?vestflushbuffer@@YAXH@Z")] static extern void vestflushbuffer(int actuator);



  14:     public int SetupVest()

  15:     {

  16:         return setupvest();

  17:     }


  19:     public void ShutDownVest()

  20:     {

  21:         shutdownvest();

  22:     }


  24:     public int SetEffect(int myEffect)

  25:     {

  26:         return vestseteffect(myEffect);

  27:     }


  29:     public int SetEffect2(int speed, int actuator)

  30:     {

  31:         return vestseteffect2(speed, actuator);

  32:     }


  34:     public void FlushVestBuffer(int actuator)

  35:     {

  36:         vestflushbuffer(actuator);

  37:     }

  38: }

  39: }


Now that we have the dll made it is a simple process of importing the dll into our project and building a test harness.

This is a screen shot of our test harness.


At this point you can test the functionality of the vest.  you will first plug the vest  into the USB port  and have everything setup like you would if you were playing a game.  You are ready to now just step through each of the steps.  First  initialize the vest, you do this via the setup vest button.  Next if you read the documentation from TN Games that comes with their SDK you will be able to select the different effects to see exactly what each means (machine gun, grenade, etc.).  You will also be able to test each of the bellows to simulate being shot in different areas.

In summary, I really wished I was able to have gotten this ready for the PDC, I think it would have added a cool twist to the Wi-Fi Warthogs.  So what is next?  ARE YOU KIDDING… now that we have a C# dll the sky is the limited.  How about getting shot from a web page?  Or How about  every time you get an email you also get a grenade effect.  Maybe put a service in the cloud that allows me to play a SilverLight MMO game and when someone shoots me or hits me I know it.  Of course XNA with XBox live would be fun, but TN Games appears to have beat me there, oh well there is SilverLight/XNA for Windows Phone 7, Bluetooth to pc and a service to the vest, yes that might work…

This is the source code that I  made.  I do ask if you decide to use this that you at least give me credit for figuring out this much.  TN Games now if you are looking for a tester or someone to maybe take this in another direction drop me a line at i would love to play some more…

This entry was posted in DIY and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>