| 165 | | unsigned int _numEvents; |
| 166 | | PackedEvent _events[MAX_NUM_EVENTS]; |
| 167 | | |
| | 66 | osgProducer::KeyboardMouseCallback::EventQueue _events; |
| | 67 | |
| | 68 | }; |
| | 69 | |
| | 70 | class DataConverter |
| | 71 | { |
| | 72 | public: |
| | 73 | |
| | 74 | DataConverter(unsigned int numBytes): |
| | 75 | _startPtr(0), |
| | 76 | _endPtr(0), |
| | 77 | _swapBytes(false), |
| | 78 | _currentPtr(0) |
| | 79 | { |
| | 80 | _currentPtr = _startPtr = new char[numBytes]; |
| | 81 | _endPtr = _startPtr+numBytes; |
| | 82 | _numBytes = numBytes; |
| | 83 | } |
| | 84 | |
| | 85 | char* _startPtr; |
| | 86 | char* _endPtr; |
| | 87 | unsigned int _numBytes; |
| | 88 | bool _swapBytes; |
| | 89 | |
| | 90 | char* _currentPtr; |
| | 91 | |
| | 92 | inline void write1(char* ptr) |
| | 93 | { |
| | 94 | if (_currentPtr+1>=_endPtr) return; |
| | 95 | |
| | 96 | *(_currentPtr++) = *(ptr); |
| | 97 | } |
| | 98 | |
| | 99 | inline void read1(char* ptr) |
| | 100 | { |
| | 101 | if (_currentPtr+1>=_endPtr) return; |
| | 102 | |
| | 103 | *(ptr) = *(_currentPtr++); |
| | 104 | } |
| | 105 | |
| | 106 | inline void write2(char* ptr) |
| | 107 | { |
| | 108 | if (_currentPtr+2>=_endPtr) return; |
| | 109 | |
| | 110 | *(_currentPtr++) = *(ptr++); |
| | 111 | *(_currentPtr++) = *(ptr); |
| | 112 | } |
| | 113 | |
| | 114 | inline void read2(char* ptr) |
| | 115 | { |
| | 116 | if (_currentPtr+2>=_endPtr) return; |
| | 117 | |
| | 118 | if (_swapBytes) |
| | 119 | { |
| | 120 | *(ptr+1) = *(_currentPtr++); |
| | 121 | *(ptr) = *(_currentPtr++); |
| | 122 | } |
| | 123 | else |
| | 124 | { |
| | 125 | *(ptr++) = *(_currentPtr++); |
| | 126 | *(ptr) = *(_currentPtr++); |
| | 127 | } |
| | 128 | } |
| | 129 | |
| | 130 | inline void write4(char* ptr) |
| | 131 | { |
| | 132 | if (_currentPtr+4>=_endPtr) return; |
| | 133 | |
| | 134 | *(_currentPtr++) = *(ptr++); |
| | 135 | *(_currentPtr++) = *(ptr++); |
| | 136 | *(_currentPtr++) = *(ptr++); |
| | 137 | *(_currentPtr++) = *(ptr); |
| | 138 | } |
| | 139 | |
| | 140 | inline void read4(char* ptr) |
| | 141 | { |
| | 142 | if (_currentPtr+4>=_endPtr) return; |
| | 143 | |
| | 144 | if (_swapBytes) |
| | 145 | { |
| | 146 | *(ptr+3) = *(_currentPtr++); |
| | 147 | *(ptr+2) = *(_currentPtr++); |
| | 148 | *(ptr+1) = *(_currentPtr++); |
| | 149 | *(ptr) = *(_currentPtr++); |
| | 150 | } |
| | 151 | else |
| | 152 | { |
| | 153 | *(ptr++) = *(_currentPtr++); |
| | 154 | *(ptr++) = *(_currentPtr++); |
| | 155 | *(ptr++) = *(_currentPtr++); |
| | 156 | *(ptr) = *(_currentPtr++); |
| | 157 | } |
| | 158 | } |
| | 159 | |
| | 160 | inline void write8(char* ptr) |
| | 161 | { |
| | 162 | if (_currentPtr+8>=_endPtr) return; |
| | 163 | |
| | 164 | *(_currentPtr++) = *(ptr++); |
| | 165 | *(_currentPtr++) = *(ptr++); |
| | 166 | *(_currentPtr++) = *(ptr++); |
| | 167 | *(_currentPtr++) = *(ptr++); |
| | 168 | |
| | 169 | *(_currentPtr++) = *(ptr++); |
| | 170 | *(_currentPtr++) = *(ptr++); |
| | 171 | *(_currentPtr++) = *(ptr++); |
| | 172 | *(_currentPtr++) = *(ptr); |
| | 173 | } |
| | 174 | |
| | 175 | inline void read8(char* ptr) |
| | 176 | { |
| | 177 | char* endPtr = _currentPtr+8; |
| | 178 | if (endPtr>=_endPtr) return; |
| | 179 | |
| | 180 | if (_swapBytes) |
| | 181 | { |
| | 182 | *(ptr+7) = *(_currentPtr++); |
| | 183 | *(ptr+6) = *(_currentPtr++); |
| | 184 | *(ptr+5) = *(_currentPtr++); |
| | 185 | *(ptr+4) = *(_currentPtr++); |
| | 186 | |
| | 187 | *(ptr+3) = *(_currentPtr++); |
| | 188 | *(ptr+2) = *(_currentPtr++); |
| | 189 | *(ptr+1) = *(_currentPtr++); |
| | 190 | *(ptr) = *(_currentPtr++); |
| | 191 | } |
| | 192 | else |
| | 193 | { |
| | 194 | *(ptr++) = *(_currentPtr++); |
| | 195 | *(ptr++) = *(_currentPtr++); |
| | 196 | *(ptr++) = *(_currentPtr++); |
| | 197 | *(ptr++) = *(_currentPtr++); |
| | 198 | |
| | 199 | *(ptr++) = *(_currentPtr++); |
| | 200 | *(ptr++) = *(_currentPtr++); |
| | 201 | *(ptr++) = *(_currentPtr++); |
| | 202 | *(ptr) = *(_currentPtr++); |
| | 203 | } |
| | 204 | } |
| | 205 | |
| | 206 | inline void writeChar(char c) { write1(&c); } |
| | 207 | inline void writeUChar(unsigned char c) { write1((char*)&c); } |
| | 208 | inline void writeShort(short c) { write2((char*)&c); } |
| | 209 | inline void writeUShort(unsigned short c) { write2((char*)&c); } |
| | 210 | inline void writeInt(int c) { write4((char*)&c); } |
| | 211 | inline void writeUInt(unsigned int c) { write4((char*)&c); } |
| | 212 | inline void writeFloat(float c) { write4((char*)&c); } |
| | 213 | inline void writeDouble(double c) { write8((char*)&c); } |
| | 214 | |
| | 215 | inline char readChar() { char c; read1(&c); return c; } |
| | 216 | inline unsigned char readUChar() { unsigned char c; read1((char*)&c); return c; } |
| | 217 | inline short readShort() { short c; read2((char*)&c); return c; } |
| | 218 | inline unsigned short readUShort() { unsigned short c; read2((char*)&c); return c; } |
| | 219 | inline int readInt() { int c; read4((char*)&c); return c; } |
| | 220 | inline unsigned int readUInt() { unsigned int c; read4((char*)&c); return c; } |
| | 221 | inline float readFloat() { float c; read4((char*)&c); return c; } |
| | 222 | inline double readDouble() { double c; read8((char*)&c); return c; } |
| | 223 | |
| | 224 | void write(const osg::FrameStamp& fs) |
| | 225 | { |
| | 226 | writeUInt(fs.getFrameNumber()); |
| | 227 | return writeDouble(fs.getReferenceTime()); |
| | 228 | } |
| | 229 | |
| | 230 | void read(osg::FrameStamp& fs) |
| | 231 | { |
| | 232 | fs.setFrameNumber(readUInt()); |
| | 233 | fs.setReferenceTime(readDouble()); |
| | 234 | } |
| | 235 | |
| | 236 | void write(const osg::Matrix& matrix) |
| | 237 | { |
| | 238 | writeDouble(matrix(0,0)); |
| | 239 | writeDouble(matrix(0,1)); |
| | 240 | writeDouble(matrix(0,2)); |
| | 241 | writeDouble(matrix(0,3)); |
| | 242 | |
| | 243 | writeDouble(matrix(1,0)); |
| | 244 | writeDouble(matrix(1,1)); |
| | 245 | writeDouble(matrix(1,2)); |
| | 246 | writeDouble(matrix(1,3)); |
| | 247 | |
| | 248 | writeDouble(matrix(2,0)); |
| | 249 | writeDouble(matrix(2,1)); |
| | 250 | writeDouble(matrix(2,2)); |
| | 251 | writeDouble(matrix(2,3)); |
| | 252 | |
| | 253 | writeDouble(matrix(3,0)); |
| | 254 | writeDouble(matrix(3,1)); |
| | 255 | writeDouble(matrix(3,2)); |
| | 256 | writeDouble(matrix(3,3)); |
| | 257 | } |
| | 258 | |
| | 259 | void read(osg::Matrix& matrix) |
| | 260 | { |
| | 261 | matrix(0,0) = readDouble(); |
| | 262 | matrix(0,1) = readDouble(); |
| | 263 | matrix(0,2) = readDouble(); |
| | 264 | matrix(0,3) = readDouble(); |
| | 265 | |
| | 266 | matrix(1,0) = readDouble(); |
| | 267 | matrix(1,1) = readDouble(); |
| | 268 | matrix(1,2) = readDouble(); |
| | 269 | matrix(1,3) = readDouble(); |
| | 270 | |
| | 271 | matrix(2,0) = readDouble(); |
| | 272 | matrix(2,1) = readDouble(); |
| | 273 | matrix(2,2) = readDouble(); |
| | 274 | matrix(2,3) = readDouble(); |
| | 275 | |
| | 276 | matrix(3,0) = readDouble(); |
| | 277 | matrix(3,1) = readDouble(); |
| | 278 | matrix(3,2) = readDouble(); |
| | 279 | matrix(3,3) = readDouble(); |
| | 280 | } |
| | 281 | |
| | 282 | void write(const osgProducer::EventAdapter& event) |
| | 283 | { |
| | 284 | writeUInt(event._eventType); |
| | 285 | writeUInt(event._key); |
| | 286 | writeUInt(event._button); |
| | 287 | writeFloat(event._Xmin); |
| | 288 | writeFloat(event._Xmax); |
| | 289 | writeFloat(event._Ymin); |
| | 290 | writeFloat(event._Ymax); |
| | 291 | writeFloat(event._mx); |
| | 292 | writeFloat(event._my); |
| | 293 | writeUInt(event._buttonMask); |
| | 294 | writeUInt(event._modKeyMask); |
| | 295 | writeDouble(event._time); |
| | 296 | } |
| | 297 | |
| | 298 | void read(osgProducer::EventAdapter& event) |
| | 299 | { |
| | 300 | event._eventType = (osgGA::GUIEventAdapter::EventType)readUInt(); |
| | 301 | event._key = readUInt(); |
| | 302 | event._button = readUInt(); |
| | 303 | event._Xmin = readFloat(); |
| | 304 | event._Xmax = readFloat(); |
| | 305 | event._Ymin = readFloat(); |
| | 306 | event._Ymax = readFloat(); |
| | 307 | event._mx = readFloat(); |
| | 308 | event._my = readFloat(); |
| | 309 | event._buttonMask = readUInt(); |
| | 310 | event._modKeyMask = readUInt(); |
| | 311 | event._time = readDouble(); |
| | 312 | } |
| | 313 | |
| | 314 | void write(CameraPacket& cameraPacket) |
| | 315 | { |
| | 316 | writeUInt(cameraPacket._byte_order); |
| | 317 | |
| | 318 | writeUInt(cameraPacket._masterKilled); |
| | 319 | |
| | 320 | write(cameraPacket._matrix); |
| | 321 | write(cameraPacket._frameStamp); |
| | 322 | |
| | 323 | writeUInt(cameraPacket._events.size()); |
| | 324 | for(unsigned int i=0;i<cameraPacket._events.size();++i) |
| | 325 | { |
| | 326 | write(*(cameraPacket._events[i])); |
| | 327 | } |
| | 328 | } |
| | 329 | |
| | 330 | void read(CameraPacket& cameraPacket) |
| | 331 | { |
| | 332 | cameraPacket._byte_order = readUInt(); |
| | 333 | if (cameraPacket._byte_order != SWAP_BYTES_COMPARE) _swapBytes = !_swapBytes; |
| | 334 | |
| | 335 | cameraPacket._masterKilled = readUInt(); |
| | 336 | |
| | 337 | read(cameraPacket._matrix); |
| | 338 | read(cameraPacket._frameStamp); |
| | 339 | |
| | 340 | cameraPacket._events.clear(); |
| | 341 | unsigned int numEvents = readUInt(); |
| | 342 | for(unsigned int i=0;i<numEvents;++i) |
| | 343 | { |
| | 344 | osgProducer::EventAdapter* event = new osgProducer::EventAdapter; |
| | 345 | read(*(event)); |
| | 346 | cameraPacket._events.push_back(event); |
| | 347 | } |
| | 348 | } |