1 // controls.hxx -- defines a standard interface to all flight sim controls
3 // Written by Curtis Olson, started May 1997.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <simgear/misc/props.hxx>
29 #include <Sound/soundmgr.hxx>
30 #include <Main/fgfs.hxx>
31 #include <Main/globals.hxx>
34 # error This library requires C++
38 // Define a structure containing the control parameters
40 class FGControls : public FGSubsystem
64 double throttle[MAX_ENGINES];
65 double mixture[MAX_ENGINES];
66 double prop_advance[MAX_ENGINES];
67 double brake[MAX_WHEELS];
68 int magnetos[MAX_ENGINES];
70 bool starter[MAX_ENGINES];
73 SGPropertyNode * auto_coordination;
75 inline void CLAMP(double *x, double min, double max ) {
76 if ( *x < min ) { *x = min; }
77 if ( *x > max ) { *x = max; }
80 inline void CLAMP(int *i, int min, int max ) {
81 if ( *i < min ) { *i = min; }
82 if ( *i > max ) { *i = max; }
90 // Implementation of FGSubsystem.
100 inline double get_aileron() const { return aileron; }
101 inline double get_elevator() const { return elevator; }
102 inline double get_elevator_trim() const { return elevator_trim; }
103 inline double get_rudder() const { return rudder; }
104 inline double get_flaps() const { return flaps; }
105 inline double get_throttle(int engine) const { return throttle[engine]; }
106 inline double get_mixture(int engine) const { return mixture[engine]; }
107 inline double get_prop_advance(int engine) const {
108 return prop_advance[engine];
110 inline double get_brake(int wheel) const { return brake[wheel]; }
111 inline int get_magnetos(int engine) const { return magnetos[engine]; }
112 inline bool get_starter(int engine) const { return starter[engine]; }
113 inline bool get_gear_down() const { return gear_down; }
116 inline void set_aileron( double pos ) {
118 CLAMP( &aileron, -1.0, 1.0 );
120 // check for autocoordination
121 if ( auto_coordination->getBoolValue() )
123 set_rudder( aileron / 2.0 );
126 inline void move_aileron( double amt ) {
128 CLAMP( &aileron, -1.0, 1.0 );
130 // check for autocoordination
131 if ( auto_coordination->getBoolValue() )
133 set_rudder( aileron / 2.0 );
136 inline void set_elevator( double pos ) {
138 CLAMP( &elevator, -1.0, 1.0 );
140 inline void move_elevator( double amt ) {
142 CLAMP( &elevator, -1.0, 1.0 );
144 inline void set_elevator_trim( double pos ) {
146 CLAMP( &elevator_trim, -1.0, 1.0 );
148 inline void move_elevator_trim( double amt ) {
149 elevator_trim += amt;
150 CLAMP( &elevator_trim, -1.0, 1.0 );
152 inline void set_rudder( double pos ) {
154 CLAMP( &rudder, -1.0, 1.0 );
156 inline void move_rudder( double amt ) {
158 CLAMP( &rudder, -1.0, 1.0 );
160 inline void set_flaps( double pos ) {
161 if ( flaps != pos ) {
162 globals->get_soundmgr()->play_once( "flaps" );
165 CLAMP( &flaps, 0.0, 1.0 );
167 inline void move_flaps( double amt ) {
168 if ( fabs(amt) > 0.0 ) {
169 globals->get_soundmgr()->play_once( "flaps" );
172 CLAMP( &flaps, 0.0, 1.0 );
174 inline void set_throttle( int engine, double pos ) {
175 if ( engine == ALL_ENGINES ) {
176 for ( int i = 0; i < MAX_ENGINES; i++ ) {
178 CLAMP( &throttle[i], 0.0, 1.0 );
181 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
182 throttle[engine] = pos;
183 CLAMP( &throttle[engine], 0.0, 1.0 );
187 inline void move_throttle( int engine, double amt ) {
188 if ( engine == ALL_ENGINES ) {
189 for ( int i = 0; i < MAX_ENGINES; i++ ) {
191 CLAMP( &throttle[i], 0.0, 1.0 );
194 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
195 throttle[engine] += amt;
196 CLAMP( &throttle[engine], 0.0, 1.0 );
200 inline void set_mixture( int engine, double pos ) {
201 if ( engine == ALL_ENGINES ) {
202 for ( int i = 0; i < MAX_ENGINES; i++ ) {
204 CLAMP( &mixture[i], 0.0, 1.0 );
207 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
208 mixture[engine] = pos;
209 CLAMP( &mixture[engine], 0.0, 1.0 );
213 inline void move_mixture( int engine, double amt ) {
214 if ( engine == ALL_ENGINES ) {
215 for ( int i = 0; i < MAX_ENGINES; i++ ) {
217 CLAMP( &mixture[i], 0.0, 1.0 );
220 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
221 mixture[engine] += amt;
222 CLAMP( &mixture[engine], 0.0, 1.0 );
226 inline void set_prop_advance( int engine, double pos ) {
227 if ( engine == ALL_ENGINES ) {
228 for ( int i = 0; i < MAX_ENGINES; i++ ) {
229 prop_advance[i] = pos;
230 CLAMP( &prop_advance[i], 0.0, 1.0 );
233 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
234 prop_advance[engine] = pos;
235 CLAMP( &prop_advance[engine], 0.0, 1.0 );
239 inline void move_prop_advance( int engine, double amt ) {
240 if ( engine == ALL_ENGINES ) {
241 for ( int i = 0; i < MAX_ENGINES; i++ ) {
242 prop_advance[i] += amt;
243 CLAMP( &prop_advance[i], 0.0, 1.0 );
246 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
247 prop_advance[engine] += amt;
248 CLAMP( &prop_advance[engine], 0.0, 1.0 );
252 inline void set_magnetos( int engine, int pos ) {
253 if ( engine == ALL_ENGINES ) {
254 for ( int i = 0; i < MAX_ENGINES; i++ ) {
256 CLAMP( &magnetos[i], 0, 3 );
259 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
260 magnetos[engine] = pos;
261 CLAMP( &magnetos[engine], 0, 3 );
265 inline void move_magnetos( int engine, int amt ) {
266 if ( engine == ALL_ENGINES ) {
267 for ( int i = 0; i < MAX_ENGINES; i++ ) {
269 CLAMP( &magnetos[i], 0, 3 );
272 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
273 magnetos[engine] += amt;
274 CLAMP( &magnetos[engine], 0, 3 );
278 inline void set_starter( int engine, bool flag ) {
279 if ( engine == ALL_ENGINES ) {
280 for ( int i = 0; i < MAX_ENGINES; i++ ) {
284 if ( (engine >= 0) && (engine < MAX_ENGINES) ) {
285 starter[engine] = flag;
289 inline void set_brake( int wheel, double pos ) {
290 if ( wheel == ALL_WHEELS ) {
291 for ( int i = 0; i < MAX_WHEELS; i++ ) {
293 CLAMP( &brake[i], 0.0, 1.0 );
296 if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
298 CLAMP( &brake[wheel], 0.0, 1.0 );
302 inline void move_brake( int wheel, double amt ) {
303 if ( wheel == ALL_WHEELS ) {
304 for ( int i = 0; i < MAX_WHEELS; i++ ) {
306 CLAMP( &brake[i], 0.0, 1.0 );
309 if ( (wheel >= 0) && (wheel < MAX_WHEELS) ) {
311 CLAMP( &brake[wheel], 0.0, 1.0 );
315 inline void set_gear_down( bool gear ) { gear_down = gear; }
319 #endif // _CONTROLS_HXX