Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TrackIR.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.6
2  Destroyer Studios LLC
3  Copyright © 1997-2006. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: TrackIR.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  TrackIR head tracker interface class
13 */
14 
15 #include "MemDebug.h"
16 #include "TrackIR.h"
17 #include "NPClient.h"
18 #include "NPClientWraps.h"
19 
20 #include "Game.h"
21 #include "Text.h"
22 
23 // +--------------------------------------------------------------------+
24 
25 const double TRACK_TOP = -8000;
26 const double TRACK_BOTTOM = 8000;
27 const double TRACK_LEFT = 16000;
28 const double TRACK_RIGHT = -16000;
29 const double TRACK_XYZ = 24000;
30 
31 // +--------------------------------------------------------------------+
32 
33 static Text GetDllFromRegistry()
34 {
35  Text dllLoc;
36  BYTE dllBuf[1024];
37  DWORD dllLen = 0;
38  HKEY hkey = 0;
39 
40  ZeroMemory(dllBuf, sizeof(dllBuf));
41 
42  RegOpenKeyEx(HKEY_CURRENT_USER,
43  "Software\\NaturalPoint\\NATURALPOINT\\NPClient Location",
44  0,
45  KEY_QUERY_VALUE,
46  &hkey);
47 
48  if (hkey) {
49  dllLen = 1024;
50 
51  LONG result =
52  RegQueryValueEx(hkey,
53  "Path",
54  NULL,
55  NULL,
56  dllBuf,
57  &dllLen);
58 
59  if (result == ERROR_SUCCESS && dllLen > 0)
60  dllLoc = (const char*) dllBuf;
61 
62  RegCloseKey(hkey);
63  }
64 
65  return dllLoc;
66 }
67 
68 static const char* NPErrString(NPRESULT r)
69 {
70  switch (r) {
71  case NP_OK: return "OK";
72  case NP_ERR_DEVICE_NOT_PRESENT: return "Device not present";
73  case NP_ERR_UNSUPPORTED_OS: return "Unsupported O/S";
74  case NP_ERR_INVALID_ARG: return "Invalid argument";
75  case NP_ERR_DLL_NOT_FOUND: return "NaturalPoint DLL not found";
76  case NP_ERR_NO_DATA: return "No data available";
77  case NP_ERR_INTERNAL_DATA: return "Internal error";
78  }
79 
80  return "Unknown error code";
81 }
82 
83 // +--------------------------------------------------------------------+
84 
86 running(false), frame_signature(0),
87 az(0), el(0), x(0), y(0), z(0)
88 {
89  Print("*** NaturalPoint Game Client Initialization ***\n");
90 
91  // Hook up the NaturalPoint game client DLL using the wrapper module
92  NPRESULT result;
93  Text dllPath = GetDllFromRegistry();
94 
95  // Initialize the NPClient interface
96  result = NPClient_Init(dllPath);
97  if (result == NP_OK) {
98  Print("NPClient - Initialize successful.\n");
99  }
100  else {
101  Print("NPClient - Unable to initialize interface: %s\n", NPErrString(result));
102  return;
103  }
104 
105  // Register the app's window handle
107 
108  if (result == NP_OK) {
109  Print("NPClient - Window handle registration successful.\n");
110  }
111  else {
112  Print("NPClient - Error registering window handle: %s\n", NPErrString(result));
113  return;
114  }
115 
116  // Query the NaturalPoint software version
117  unsigned short wNPClientVer;
118  result = NP_QueryVersion( &wNPClientVer );
119 
120  if (result == NP_OK) {
121  Print("NPClient - NaturalPoint software version: %d.%02d\n", (wNPClientVer >> 8), (wNPClientVer & 0x00FF));
122  }
123  else {
124  Print("NPClient - Error querying NaturalPoint software version: %s\n", NPErrString(result));
125  }
126 
127 
128  // It is *required* that your application registers the Developer ID
129  // assigned by NaturalPoint!
130 
131  // Your assigned developer ID needs to be inserted below!
132 #define NP_DEVELOPER_ID 6401
133 
134  // NOTE : The title of your project must show up
135  // in the list of supported titles shown in the Profiles
136  // tab of the TrackIR software, if it does not then the
137  // TrackIR software will *not* transmit data to your
138  // application. If your title is not present in the list,
139  // you may need to have the TrackIR software perform a
140  // game list update (to download support for new Developer IDs)
141  // using the menu item under the "Help" or "Update" menu.
142 
144 
145  unsigned int DataFields = 0;
146  DataFields |= NPPitch;
147  DataFields |= NPYaw;
148 
149  NP_RequestData(DataFields);
150 
151  result = NP_StopCursor();
152  if (result == NP_OK)
153  Print("NPClient - Cursor stopped.\n");
154  else
155  Print("NPClient - Error stopping cursor: %s\n", NPErrString(result));
156 
157 
158  result = NP_StartDataTransmission();
159  if (result == NP_OK) {
160  Print("NPClient - Data transmission started.\n");
161  running = true;
162  }
163  else {
164  Print("NPClient - Error starting data transmission: %s\n", NPErrString(result));
165  }
166 
167 }
168 
170 {
171  if (running) {
172  Print("NaturalPoint Game Client Shutdown\n");
173 
176  }
177 }
178 
179 // +--------------------------------------------------------------------+
180 
181 DWORD
183 {
184  TRACKIRDATA tid;
185 
186  // Go get the latest data
187  NPRESULT result = NP_GetData( &tid );
188 
189  if (result == NP_OK) {
190  // Got data to process ...
191  running = true;
192 
193  // compare the last frame signature to the current one
194  // if they are not the same then new data has arrived since then
195 
196  if (tid.wNPStatus == NPSTATUS_REMOTEACTIVE) {
197  if (frame_signature != tid.wPFrameSignature) {
198  double pitch = tid.fNPPitch;
199  double yaw = tid.fNPYaw;
200 
201  if (pitch < 0) {
202  el = pitch / TRACK_TOP;
203  }
204  else {
205  el = -pitch / TRACK_BOTTOM;
206  }
207 
208  if (yaw < 0) {
209  az = yaw / TRACK_RIGHT;
210  }
211  else {
212  az = -yaw / TRACK_LEFT;
213  }
214 
215  x = tid.fNPX / TRACK_XYZ * -1;
216  y = tid.fNPY / TRACK_XYZ;
217  z = tid.fNPZ / TRACK_XYZ * -1;
218 
219  if (z < -0.25) z = -0.25;
220 
222  }
223  else {
224  // Either there is no tracking data, the user has
225  // paused the trackIR, or the call happened before
226  // the TrackIR was able to update the interface
227  // with new data
228 
229  az *= 0.75;
230  el *= 0.75;
231  x *= 0.75;
232  y *= 0.75;
233  z *= 0.75;
234 
235  result = NP_ERR_NO_DATA;
236  }
237  }
238  else {
239  // The user has set the device out of trackIR Enhanced Mode
240  // and into Mouse Emulation mode with the hotkey
241  result = NP_ERR_NO_DATA;
242  running = false;
243  }
244  }
245 
246  return result;
247 }