| 1 | /* |
|---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
|---|
| 3 | |
|---|
| 4 | Copyright (C) 2004 orx |
|---|
| 5 | |
|---|
| 6 | This program is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the terms of the GNU General Public License as published by |
|---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
|---|
| 9 | any later version. |
|---|
| 10 | |
|---|
| 11 | ### File Specific: |
|---|
| 12 | main-programmer: Christoph Renner |
|---|
| 13 | co-programmer: ... |
|---|
| 14 | */ |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | #include "synchronizeable_vector.h" |
|---|
| 18 | #include "converter.h" |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | /** |
|---|
| 22 | * standard constructor |
|---|
| 23 | * @todo this constructor is not jet implemented - do it |
|---|
| 24 | */ |
|---|
| 25 | SynchronizeableVector::SynchronizeableVector( Vector * ptrIn, Vector * ptrOut, std::string name, int permission, int priority) : SynchronizeableVar( ptrIn, ptrOut, name, 3*FLOATSIZE, permission, priority ) |
|---|
| 26 | { |
|---|
| 27 | this->vPtrIn = ptrIn; |
|---|
| 28 | this->vPtrOut = ptrOut; |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | /** |
|---|
| 33 | * standard deconstructor |
|---|
| 34 | */ |
|---|
| 35 | SynchronizeableVector::~SynchronizeableVector () |
|---|
| 36 | { |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * write var data to byte buffer |
|---|
| 41 | * @param buf pointer to write to |
|---|
| 42 | * @param maxLength writeToBuf will not write more than maxLength bytes |
|---|
| 43 | * @return number bytes written |
|---|
| 44 | */ |
|---|
| 45 | int SynchronizeableVector::writeToBuf( byte * buf, int maxLength ) |
|---|
| 46 | { |
|---|
| 47 | int n = 0; |
|---|
| 48 | int res; |
|---|
| 49 | |
|---|
| 50 | res = Converter::floatToByteArray( vPtrIn->x, buf + n, maxLength - n ); |
|---|
| 51 | assert( res > 0 ); |
|---|
| 52 | n += res; |
|---|
| 53 | |
|---|
| 54 | res = Converter::floatToByteArray( vPtrIn->y, buf + n, maxLength - n ); |
|---|
| 55 | assert( res > 0 ); |
|---|
| 56 | n += res; |
|---|
| 57 | |
|---|
| 58 | res = Converter::floatToByteArray( vPtrIn->z, buf + n, maxLength - n ); |
|---|
| 59 | assert( res > 0 ); |
|---|
| 60 | n += res; |
|---|
| 61 | |
|---|
| 62 | assert( 3*FLOATSIZE == n ); |
|---|
| 63 | |
|---|
| 64 | return n; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | /** |
|---|
| 68 | * read var data from byte buffer |
|---|
| 69 | * @param buf pointer to read from |
|---|
| 70 | * @param maxLength readFromBuf will not read more than maxLength bytes |
|---|
| 71 | * @return number bytes read |
|---|
| 72 | */ |
|---|
| 73 | int SynchronizeableVector::readFromBuf( byte * buf, int maxLength ) |
|---|
| 74 | { |
|---|
| 75 | assert( maxLength >= 3*FLOATSIZE ); |
|---|
| 76 | |
|---|
| 77 | float x,y,z; |
|---|
| 78 | |
|---|
| 79 | int res; |
|---|
| 80 | int n = 0; |
|---|
| 81 | |
|---|
| 82 | Vector oldVec = *vPtrOut; |
|---|
| 83 | |
|---|
| 84 | res = Converter::byteArrayToFloat( buf + n, &x ); |
|---|
| 85 | assert( res > 0 ); |
|---|
| 86 | n += res; |
|---|
| 87 | |
|---|
| 88 | res = Converter::byteArrayToFloat( buf + n, &y ); |
|---|
| 89 | assert( res > 0 ); |
|---|
| 90 | n += res; |
|---|
| 91 | |
|---|
| 92 | res = Converter::byteArrayToFloat( buf + n, &z ); |
|---|
| 93 | assert( res > 0 ); |
|---|
| 94 | n += res; |
|---|
| 95 | |
|---|
| 96 | *vPtrOut = Vector( x, y, z ); |
|---|
| 97 | |
|---|
| 98 | setHasChanged( !(*vPtrOut == oldVec) ); |
|---|
| 99 | |
|---|
| 100 | assert( n == 3*FLOATSIZE ); |
|---|
| 101 | return n; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | /** |
|---|
| 106 | * print out variable value |
|---|
| 107 | */ |
|---|
| 108 | void SynchronizeableVector::SynchronizeableVector::debug( ) |
|---|
| 109 | { |
|---|
| 110 | printf( "SYNCHRONIZEABLE_VAR: %s IN: (%f, %f, %f) OUT: (%f %f %f)\n", name.c_str(), vPtrIn->x, vPtrIn->y, vPtrIn->z, vPtrOut->x, vPtrOut->y, vPtrOut->z ); |
|---|
| 111 | } |
|---|
| 112 | |
|---|