[go: up one dir, main page]

File: instrmnt.C

package info (click to toggle)
sabre 0.2.3-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 4,876 kB
  • ctags: 5,106
  • sloc: cpp: 25,581; ansic: 7,439; sh: 2,063; makefile: 92
file content (392 lines) | stat: -rw-r--r-- 8,629 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
    SABRE Fighter Plane Simulator 
    Copyright (c) 1997 Dan Hammer
    Portions Donated By Antti Barck

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 1, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*************************************************
 *           SABRE Fighter Plane Simulator              *
 * Version: 0.1                                  *
 * File   : instrmnt.C                           *
 * Date   : March, 1997                          *
 * Author : Dan Hammer                           *
 *************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <values.h>
#include <time.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <iostream.h>
#include <fstream.h>
#include "defs.h"
#include "grafix.h"
#include "vga_13.h"
#include "traveler.h"
#include "pen.h"
#include "convpoly.h"
#ifdef USING_FIXED
#include "fixpoint.h"
#include "trigtab.h"
#else
#define ConvertToFix(a) (a)
#define fix2f(a) (a)
#define f2fix(a) (a)
#define fix2s(a) ((int)(a))
#endif
#include "vmath.h"
#include "txtrmap.h"
#include "port_3d.h"
#include "cpoly.h"
#include "font8x8.h"
#include "flight.h"
#include "rotate.h"
#include "colorspc.h"
#include "simfile.h"
#include "bits.h"
#include "instrmnt.h"

extern int frame_switch;

ArtHor arthor;
Aileron_I aileron_i;
Elevator_I elevator_i;
Rudder_I rudder_i;
Landing_Gear_LED landing_gear_led;
Wheel_Brakes_LED wheel_brakes_led;
Air_Brakes_LED air_brakes_led;
Air_Speed_I air_speed_i;
Altitude_I altitude_i;
Compass compass;
Throttle_I throttle_i;


Instrument_Panel instrument_panel(13);

void ArtHor::init()
{
  int h_2;
  height = (int) range;
  h_2 = height / 2;
  s[0] = Point(x,y);
  s[1] = Point(x+width,y);
  s[2] = Point(x+width,y+h_2);
  s[3] = Point(x,y+h_2);
  s[4] = s[0];
  g[0] = Point(x,y);
  g[1] = Point(x+width,y);
  g[2] = Point(x+width,y+height);
  g[3] = Point(x,y+height);
  g[4] = g[0];
}

void ArtHor::underpaint(Flight &flight)
{
  Point s1[5];
  Point g1[5];
  int sc_y;
  REAL_TYPE ang,sin_ang,cos_ang;
  Port_3D &port = flight.state.flight_port;
  Vector v1 = Vector(port.look_at - port.look_from);
  Vector v2 = v1;
  v2.Z = 0;
  REAL_TYPE dot = v1.Dot(v2);
  dot = 1 - dot;
  if (port.look_from.z > port.look_at.z)
    dot = -dot;
  dot *= f2fix(scale);
  sc_y = (int) (y + (height / 2) + dot);
  REAL_TYPE cx = x + (width / 2);
  REAL_TYPE cy = y + (height / 2);
  if (sc_y < y + 3)
    sc_y = y + 3;
  else if (sc_y > y + height - 3)
    sc_y = y + height - 3;
  s[2].y = sc_y;
  s[3].y = sc_y;
  if (port.roll)
    ang = _2PI - port.roll;
  else
    ang = 0;
  if (flight.state.negative_phi)
    ang = limit_angle(ang + _PI);
  sin_ang = sin(ang);
  cos_ang = cos(ang);
  for (int i=0;i<5;i++)
    {
      s1[i] = point_rotate(s[i],cx,cy,sin_ang,cos_ang);
      g1[i] = point_rotate(g[i],cx,cy,sin_ang,cos_ang);
    }
  fill_convpoly((int *)g1,5,color+1);
  fill_convpoly((int *)s1,5,color);
}

void ArtHor::display(Flight &)
{
  /*
    int sc_y, sc_x;

    Port_3D &port = flight.state.flight_port;

    Vector v1 = Vector(port.look_at - port.look_from);
    Vector v2 = v1;
    v2.Z = 0;
    REAL_TYPE dot = v1.Dot(v2);
    dot = 1 - dot;
    if (port.look_from.z > port.look_at.z)
    dot = -dot;
    dot *= f2fix(scale);
    sc_y = y + (height / 2) + fix2s(dot);
    if (sc_y < y)
    sc_y = y;
    else if (sc_y > y + height)
    sc_y = y + height;
    h_line(x,sc_y,width,15);
    */
}
void Aileron_I::init()
{
  cx = x + (((int)range) / 2);
}
void Aileron_I::display(Flight &flt)
{
  REAL_TYPE fr = f2fix(range);
  REAL_TYPE fa = REAL_TYPE(flt.controls.ailerons);
  REAL_TYPE fx = (fr * fa) / 61 ;
  int i = fix2s(fx);
  i++;
  v_line(cx - i,y,width,color);
}

void Elevator_I::init()
{
  cy = y + (((int)range) / 2);
}

void Elevator_I::display(Flight &flt)
{
  REAL_TYPE fr = f2fix(range);
  REAL_TYPE fa = REAL_TYPE(flt.controls.elevators + flt.controls.trim);
  // 	REAL_TYPE fx = (fr * fa) / 31 ;
  REAL_TYPE fx = (fr * fa) / 61;
  int i = fix2s(fx);
  //	i;
  h_line(x,cy + i,width,color);
}

void Rudder_I::init()
{
  cx =  x + (((int) range) / 2);
}

void Rudder_I::display(Flight &flt)
{
  REAL_TYPE fr = f2fix(range);
  REAL_TYPE fa = REAL_TYPE(flt.controls.rudder);
  //	REAL_TYPE fx = (fr * fa) / 31 ;
  REAL_TYPE fx = (fr * fa) / 61;
  int i = fix2s(fx);
  i++;
  v_line(cx - i,y,width,color);
}

void LED::on()
{
  extern unsigned char  led[];
  extern int led_X_size,led_Y_size;
  trans_blit(x,y,x+led_X_size-1,y+led_Y_size-1,led,buffer_ptr);
}

void Landing_Gear_LED::display(Flight &flt)
{
  if (flt.controls.landing_gear == 1)
    on();
}

void Wheel_Brakes_LED::display(Flight &flt)
{
  if (flt.controls.wheel_brakes == 1)
    on();
}

void Air_Brakes_LED::display(Flight &flt)
{
  if (flt.controls.speed_brakes == 1)
    on();
}

void Dial_Indicator::init()
{
  cx = x + width / 2;
  cy = y + width / 2;
}

void Dial_Indicator::show_dial(float value)
{
  float angle,fx,fy;
  int d_x,d_y;
  angle = (6.2831853 * value) / range;
  angle -= 1.5707963;
  fx = ((double)(width)) * cos(angle);
  fy = ((double)(width)) * sin(angle);
  d_x = (int)fx;
  d_y = (int)fy;
  b_linedraw(x,y,x+d_x,y+d_y,color,NULL);
}

void Air_Speed_I::display(Flight &flt)
{
  show_dial((flt.state.z_vel * 3600.0 ) / 6000.0);
}

void Altitude_I::display(Flight &flt)
{
  color++;
  range = 100000;
  show_dial(
	    fix2f(flt.state.flight_port.look_from.z)
	    / world_scale
	    );
  range = 1000;
  color--;
  show_dial(
	    fix2f(flt.state.flight_port.look_from.z)
	    / world_scale
	    );
}

void Compass::display(Flight &flt)
{
  float angle,fx,fy;
  int d_x,d_y;
  angle = flt.state.heading - 1.5707963;
  fx = ((double)width) * cos(angle);
  fy = ((double)width) * sin(angle);
  d_x = (int)fx;
  d_y = (int)fy;
  b_linedraw(x,y,x+d_x,y+d_y,color,NULL);
}

void Throttle_I::init()
{
  tmap = map_man->get_map_ptr("throttle");
  if (tmap == NULL)
    printf("missing throttle txr!\n");
}

void Throttle_I::display(Flight &flt)
{
  if (tmap != NULL)
    {
      REAL_TYPE rf = ConvertToFix(range);
      REAL_TYPE tf = ConvertToFix(flt.controls.throttle);
      REAL_TYPE yf = (tf * rf) / 100;
      int dy = y - fix2s(yf);
      trans_blit(x,dy,x+tmap->map_w-1,
		 dy+tmap->map_h-1,tmap->getBytes(),buffer_ptr);
    }
}

Instrument_Panel::Instrument_Panel(int max_i)
{
  max_indicators = max_i;
  indicators = new Indicator *[max_indicators];
  indicators_idx = 0;
}

void Instrument_Panel::add_indicator(Indicator *ind)
{
  if (indicators_idx < max_indicators)
    indicators[indicators_idx++] = ind;
}

istream &operator >>(istream &is, Instrument_Panel &ip)
{
  char c;
  int i;

  is >> c;
  while (is && c!='{')
    is >> c;
  for (i = 0;i < ip.indicators_idx; i++)
    {
      while (is && c != '(')
	is >> c;
      if (is)
	{
	  ip.indicators[i]->read_from(is);
	  is >> c;
	}
      while (is && c != ')')
	is >> c;
    }
  /*
  is >> n;
  for (i=0;i<n;i++)
    is >> cs;
  is >> c;
  while (is && c != '}')
    is >> c;
    */
  return is;
}

void Instrument_Panel::underpaint(Flight &flt)
{
  int fs = frame_switch;
  frame_switch = 0;
  for (int i = 0; i < indicators_idx; i++)
    indicators[i]->underpaint(flt);
  frame_switch = fs;
}

void Instrument_Panel::display(Flight &flt)
{
  int fs = frame_switch;
  frame_switch = 0;
  for (int i = 0; i < indicators_idx; i++)
    indicators[i]->display(flt);
  frame_switch = fs;
}

void Instrument_Panel::init()
{
  for (int i = 0; i < indicators_idx; i++)
    indicators[i]->init();
}

void setup_instruments(char *path, Instrument_Panel &ip)
{
  ip.indicators_idx = 0;
  ip.add_indicator(&arthor);
  ip.add_indicator(&aileron_i);
  ip.add_indicator(&elevator_i);
  ip.add_indicator(&rudder_i);
  ip.add_indicator(&landing_gear_led);
  ip.add_indicator(&wheel_brakes_led);
  ip.add_indicator(&air_brakes_led);
  ip.add_indicator(&air_speed_i);
  ip.add_indicator(&altitude_i);
  ip.add_indicator(&compass);
  ip.add_indicator(&throttle_i);

  ifstream is;
  if (open_is(is,path))
    is >> ip;
  is.close();
  ip.init();
}