Avionics
Dropship Simulator
AttitudeIndicator.cpp
Go to the documentation of this file.
1 #include "../Instrument.h"
2 
3 AttitudeIndicator::AttitudeIndicator(int prmX, int prmY, int prmWidth, int prmHeight, std::string prmTextureFile): Instrument(prmX, prmY, 1, 1)
4 {
5  width = prmWidth;
6  height = prmHeight;
7  textureFile = prmTextureFile;
8 }
9 
10 void AttitudeIndicator::Initialize(Bus* prmBus, Logger* prmLogger, DEVICE_OBJECT* prmDevice, int prmScreenWidth, int prmScreenHeight, std::vector<Font>* prmFonts, bool prmFlip)
11 {
12  Instrument::Initialize(prmBus, prmLogger, prmDevice, prmScreenWidth, prmScreenHeight, prmFonts, prmFlip);
13 
14  const float newwidth = sqrtf(powf(static_cast<float>(width), 2.0f) + powf(static_cast<float>(height), 2.0f)); // ensures that we don't show past the width of the texture when 45 degrees diagonal
15  const float leftmargin = (newwidth - width) / 2.0f;
16  const float newheight = newwidth;
17  const float topmargin = (newheight - height) / 2.0f;
18 
19  sprite.width = static_cast<int>(newwidth);
20  sprite.height = static_cast<int>(newheight);
22  sprite.Initialize(logger, bus, pDevice, screenHeight, x - static_cast<int>(leftmargin), y - static_cast<int>(topmargin), prmFlip);
23 
24  commandBars.elementName = "CommandBars";
25  commandBars.width = 219;
26  commandBars.height = 70;
27  commandBars.pathAndFilename = "Textures/Instruments/command bars.png";
28  commandBars.z = 0.9f;
30 
31  failflag.pathAndFilename = "Textures/Instruments/yellow_x.png";
32  failflag.z = 0.4f; // want it to be closer than the overlay
33  failflag.Initialize(logger, bus, pDevice, screenHeight, 0, 0, prmFlip);
34 
35  glideslope.width = 25;
36  glideslope.height = 160;
37  glideslope.pathAndFilename = "Textures/Instruments/glideslope.png";
38  glideslope.z = 0.5f; // want it to be above the overlay
39  glideslope.Initialize(logger, bus, pDevice, screenHeight, 409, 175, prmFlip);
40 
41  glideslopebug.width = 18;
42  glideslopebug.height = 18;
43  glideslopebug.pathAndFilename = "Textures/Instruments/glideslope-bug.png";
44  glideslopebug.z = 0.4f; // want it to be closer than the glideslope overlay
45  glideslopebug.Initialize(logger, bus, pDevice, screenHeight, 412, 246, prmFlip);
46 
47  localizer.width = 160;
48  localizer.height = 25;
49  localizer.pathAndFilename = "Textures/Instruments/localizer.png";
50  localizer.z = 0.5f; // want it to be above the overlay
51  localizer.Initialize(logger, bus, pDevice, screenHeight, 187, 400, prmFlip);
52 
54  tvpitchscale = 2.0f / static_cast<float>(sprite.desc.Height) / D3DXToRadian(1); // 2 px per degree converted to radian
55  tvvertoffset = 245.0f / static_cast<float>(sprite.desc.Height);
56  cbpitchscale = 6.7f / D3DXToRadian(1); // 67 pixels for 10 degrees converted to radian
57  cbvertoffset = -13.0f;
58 
59  tvrange = static_cast<float>(sprite.desc.Width) / static_cast<float>(sprite.desc.Height) * height / width; // proportionally match above
61 }
62 
63 void AttitudeIndicator::Render(float fElapsed)
64 {
65  if (roll > D3DX_PI*0.5f && bus->RollAttitude < -D3DX_PI*0.5f)
66  roll -= D3DX_PI*2.0f;
67  if (roll < -D3DX_PI*0.5f && bus->RollAttitude > D3DX_PI*0.5f)
68  roll += D3DX_PI*2.0f;
69  roll += (bus->RollAttitude - roll)*0.25f;
70  pitch += (bus->PitchAttitude - pitch)*0.25f;
71 
72  // artificial horizon is at 1.0
74  sprite.rotate = roll;
75  sprite.Update();
76  sprite.Draw();
77 
78  // then command bars at 0.9
80  {
82  if (bus->AFCS.CurrentLateralMode == Bus::Afcs::LateralModes::Localizer)
83  commandBars.rotate *= 2.0f;
84 
85  float pitchDev = bus->AFCS.DesiredPitch - pitch;
86  if (bus->AFCS.CurrentVerticalMode == Bus::Afcs::VerticalModes::GlideSlope)
87  pitchDev *= 2.0f;
88  if (pitchDev > D3DXToRadian(20.0f)) pitchDev = D3DXToRadian(20.0f);
89  if (pitchDev < D3DXToRadian(-20.0f)) pitchDev = D3DXToRadian(-20.0f);
90 
91  float dist = cbvertoffset - pitchDev*cbpitchscale;
92  commandBars.y = static_cast<int>(cosf(roll)*dist);
93  commandBars.x = static_cast<int>(sinf(roll)*dist);
95  commandBars.Draw();
96  }
97 
98  // tests
99  //bus->AFCS.CurrentLateralMode = Bus::Afcs::LateralModes::Localizer;
100  //static float boo = 0.0f;
101 // boo += fElapsed;
102  //bus->LocalizerDeviation = D3DXToRadian(sinf(boo)*2.6f); // want it from 2.5 degrees to -2.5 degrees
103 
104  if (bus->AFCS.CurrentLateralMode == Bus::Afcs::LateralModes::Localizer || bus->AFCS.StandbyLateralMode == Bus::Afcs::LateralModes::Localizer)
105  {
106  localizer.Draw();
107 
108  if (fabsf(D3DXToDegree(bus->LocalizerDeviation)) <= 2.5f)
109  {
110  glideslopebug.y = 157;
111  glideslopebug.x = static_cast<int>(-154.0f+80.0f * D3DXToDegree(bus->LocalizerDeviation) / 2.5f);
114  }
115  else
116  {
117  // draw with the x through it!
118  failflag.y = 400;
119  failflag.x = 187;
122  failflag.Update();
123  failflag.Draw();
124  }
125 
126  //WCHAR msg[99];
127  //swprintf_s(msg, 99, L"%+.1f", D3DXToDegree(bus->LocalizerDeviation));
128  //DrawTextW(msg, x + 154, y + 318, DT_NOCLIP, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f), 0, "Localizer");
129 
130  }
131 
132  // tests
133  //bus->AFCS.CurrentVerticalMode = Bus::Afcs::VerticalModes::GlideSlope;
134  //static float boo = 0.0f;
135  //boo += fElapsed;
136  //bus->GlideslopeDeviation = D3DXToRadian(sinf(boo)*0.8f); // want it from 0.8 degrees to -0.8 degrees
137 
138  if (bus->AFCS.CurrentVerticalMode == Bus::Afcs::VerticalModes::GlideSlope || bus->AFCS.StandbyVerticalMode == Bus::Afcs::VerticalModes::GlideSlope)
139  {
140  glideslope.Draw();
141 
142  if (fabsf(D3DXToDegree(bus->GlideslopeDeviation)) <= 0.7f)
143  {
144  glideslopebug.x = 0;
145  glideslopebug.y = static_cast<int>(80.0f * D3DXToDegree(bus->GlideslopeDeviation) / 0.7f);
148  }
149  else
150  {
151  // draw with the x through it!
152  failflag.x = 409;
153  failflag.y = 175;
156  failflag.Update();
157  failflag.Draw();
158  }
159 
160  // positive means we are above the glideslope
161 // swprintf_s(msg, 99, L"%+.1f", D3DXToDegree(bus->GlideslopeDeviation));
162 // DrawTextW(msg, x + 307, y + 145, DT_NOCLIP, D3DXCOLOR(0.0f, 1.0f, 0.0f, 1.0f), 0, "Glideslope");
163 
164  }
165 
166 };
virtual void Initialize(Bus *prmBus, Logger *prmLogger, DEVICE_OBJECT *prmDevice, int prmScreenWidth, int prmScreenHeight, std::vector< Font > *prmFonts, bool prmFlip)
Definition: Instrument.h:45
void Render(float fElapsed) override
Definition: Logger.h:5
int height
Definition: Instrument.h:30
int y
Definition: Sprite.h:16
D3DXVECTOR2 texSize
Definition: Sprite.h:24
enum Bus::Afcs::LateralModes CurrentLateralMode
Sprite glideslopebug
Definition: Instrument.h:114
void Update()
Definition: Sprite.h:50
float PitchAttitude
(6) Pitch attitude;
Definition: Bus.h:49
D3D10_TEXTURE2D_DESC desc
Definition: Sprite.h:15
float rotate
Definition: Sprite.h:21
int x
Definition: Sprite.h:16
float z
Definition: Sprite.h:17
D3DXVECTOR2 texCoord
Definition: Sprite.h:23
int height
Definition: Sprite.h:16
float DesiredPitch
Definition: Bus.h:123
Definition: Bus.h:12
float RollAttitude
(7) Roll attitude;
Definition: Bus.h:51
Bus * bus
Definition: Instrument.h:21
void Draw()
Definition: Sprite.h:64
float DesiredRoll
Definition: Bus.h:122
void Initialize(Bus *prmBus, Logger *prmLogger, DEVICE_OBJECT *prmDevice, int prmScreenWidth, int prmScreenHeight, std::vector< Font > *prmFonts, bool prmFlip) override
Abstract base class for instrumentation By definition, instruments don&#39;t do any of the work (they don...
Definition: Instrument.h:15
DEVICE_OBJECT * pDevice
Definition: Instrument.h:24
int width
Definition: Sprite.h:16
std::string elementName
Definition: Sprite.h:19
std::string pathAndFilename
Definition: Sprite.h:18
struct Bus::Afcs AFCS
enum Bus::Afcs::VerticalModes StandbyVerticalMode
Logger * logger
Definition: Instrument.h:22
enum Bus::Afcs::VerticalModes CurrentVerticalMode
AttitudeIndicator(int prmX, int prmY, int prmWidth, int prmHeight, std::string prmTextureFile)
enum Bus::Afcs::LateralModes StandbyLateralMode
int screenHeight
Definition: Instrument.h:23
std::string textureFile
Definition: Instrument.h:117
void Initialize(Logger *prmLogger, Bus *prmBus, DEVICE_OBJECT *prmpDevice, int prmScreenHeight, int prmElementX, int prmElementY, bool prmFlip)
Definition: Sprite.h:26
float LocalizerDeviation
Definition: Bus.h:135
float GlideslopeDeviation
Definition: Bus.h:136