00001 /* 00002 00003 ************************************************************************* 00004 00005 ArmageTron -- Just another Tron Lightcycle Game in 3D. 00006 Copyright (C) 2000 Manuel Moos (manuel@moosnet.de) 00007 00008 ************************************************************************** 00009 00010 This program is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU General Public License 00012 as published by the Free Software Foundation; either version 2 00013 of the License, or (at your option) any later version. 00014 00015 This program is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 GNU General Public License for more details. 00019 00020 You should have received a copy of the GNU General Public License 00021 along with this program; if not, write to the Free Software 00022 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00023 00024 *************************************************************************** 00025 00026 */ 00027 00028 #ifndef ArmageTron_tFunction_H 00029 #define ArmageTron_tFunction_H 00030 00031 #include "defs.h" 00032 #include "tError.h" 00033 00035 class tFunction 00036 { 00037 public: 00038 tFunction(); 00039 tFunction(REAL offset, REAL slope); 00040 ~tFunction(); 00041 00042 REAL Evaluate( REAL argument ) const; 00043 inline REAL operator()( REAL argument ) const; 00044 00045 // function parameters: currently limited to offset_ + slope_ * argument 00046 REAL offset_; 00047 REAL slope_; 00048 00049 public: 00050 inline tFunction & SetOffset( REAL const & offset ); 00051 inline REAL const & GetOffset( void ) const; 00052 inline tFunction const & GetOffset( REAL & offset ) const; 00053 inline tFunction & SetSlope( REAL const & slope ); 00054 inline REAL const & GetSlope( void ) const; 00055 inline tFunction const & GetSlope( REAL & slope ) const; 00056 protected: 00057 private: 00058 }; 00059 00060 //These templates are probably only useable with nMessage as parameter. 00061 //The reason they're templates is that nMessage isn't available in src/tools 00062 //and that tFunction might be needed to be in src/tools in the future. 00063 //Imagine a constructor for vValue that converts from a tFunction as an 00064 //example (or the other way, as far as possible). --wrtlprnft 00065 00066 template<typename T> T & operator << ( T & m, tFunction const & f ); 00067 template<typename T> T & operator >> ( T & m, tFunction & f ); 00068 00069 // ******************************************************************************* 00070 // * 00071 // * operator ( ) 00072 // * 00073 // ******************************************************************************* 00077 // ******************************************************************************* 00078 00079 REAL tFunction::operator ( )( REAL argument ) const 00080 { 00081 return Evaluate( argument ); 00082 } 00083 00084 // ******************************************************************************* 00085 // * 00086 // * GetOffset 00087 // * 00088 // ******************************************************************************* 00092 // ******************************************************************************* 00093 00094 REAL const & tFunction::GetOffset( void ) const 00095 { 00096 return this->offset_; 00097 } 00098 00099 // ******************************************************************************* 00100 // * 00101 // * GetOffset 00102 // * 00103 // ******************************************************************************* 00108 // ******************************************************************************* 00109 00110 tFunction const & tFunction::GetOffset( REAL & offset ) const 00111 { 00112 offset = this->offset_; 00113 return *this; 00114 } 00115 00116 // ******************************************************************************* 00117 // * 00118 // * SetOffset 00119 // * 00120 // ******************************************************************************* 00125 // ******************************************************************************* 00126 00127 tFunction & tFunction::SetOffset( REAL const & offset ) 00128 { 00129 this->offset_ = offset; 00130 return *this; 00131 } 00132 00133 // ******************************************************************************* 00134 // * 00135 // * GetSlope 00136 // * 00137 // ******************************************************************************* 00141 // ******************************************************************************* 00142 00143 REAL const & tFunction::GetSlope( void ) const 00144 { 00145 return this->slope_; 00146 } 00147 00148 // ******************************************************************************* 00149 // * 00150 // * GetSlope 00151 // * 00152 // ******************************************************************************* 00157 // ******************************************************************************* 00158 00159 tFunction const & tFunction::GetSlope( REAL & slope ) const 00160 { 00161 slope = this->slope_; 00162 return *this; 00163 } 00164 00165 // ******************************************************************************* 00166 // * 00167 // * SetSlope 00168 // * 00169 // ******************************************************************************* 00174 // ******************************************************************************* 00175 00176 tFunction & tFunction::SetSlope( REAL const & slope ) 00177 { 00178 this->slope_ = slope; 00179 return *this; 00180 } 00181 00182 // ******************************************************************************* 00183 // * 00184 // * operator << 00185 // * 00186 // ******************************************************************************* 00192 // ******************************************************************************* 00193 00194 template<typename T> T & operator << ( T & m, tFunction const & f ) 00195 { 00196 // write ID for compatibility with future extensions 00197 unsigned short ID = 1; 00198 m.Write( ID ); 00199 00200 // write values 00201 m << f.GetOffset(); 00202 m << f.GetSlope(); 00203 00204 return m; 00205 } 00206 00207 // ******************************************************************************* 00208 // * 00209 // * operator >> 00210 // * 00211 // ******************************************************************************* 00217 // ******************************************************************************* 00218 00219 template<typename T> T & operator >> ( T & m, tFunction & f ) 00220 { 00221 // write ID for compatibility with future extensions 00222 unsigned short ID; 00223 m.Read(ID); 00224 tASSERT( ID == 1 ); 00225 00226 // read values 00227 REAL slope, offset; 00228 m >> offset >> slope; 00229 00230 // store values 00231 f.SetOffset( offset ).SetSlope( slope ); 00232 00233 return m; 00234 } 00235 00236 #endif