]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalDisplay.cxx
Add an srand() function to nasal (hooked into sg_srandom_time()).
[flightgear.git] / src / Scripting / NasalDisplay.cxx
1 // NasalDisplay.cxx - routines to display Nasal output
2 //
3 // Written by David Luff, started October 2001.
4 //
5 // Copyright (C) 2001  David C Luff - david.luff@nottingham.ac.uk
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #ifdef HAVE_WINDOWS_H
26 #   include <windows.h>
27 #endif
28
29 #include <simgear/compiler.h>
30
31 #include SG_GLU_H
32
33 #include <simgear/props/props.hxx>
34
35 #include <Include/general.hxx>
36 #include <Main/fg_props.hxx>
37 #include <GUI/gui.h>
38
39 #include "NasalDisplay.hxx"
40
41
42 // Constructor
43 FGNasalDisplay::FGNasalDisplay() {
44         rep_msg = false;
45         change_msg_flag = false;
46         dsp_offset1 = 0.0;
47         dsp_offset2 = 0.0;
48 }
49
50
51 // Destructor
52 FGNasalDisplay::~FGNasalDisplay() {
53 }
54
55 void FGNasalDisplay::init() {
56 }
57
58 void FGNasalDisplay::bind() {
59 }
60
61 void FGNasalDisplay::unbind() {
62 }
63
64 // update - this actually draws the visuals and should be called from the main Flightgear rendering loop.
65 void FGNasalDisplay::update(double dt) {
66         
67         // These strings are used for temporary storage of the transmission string in order
68         // that the string we view only changes when the next repetition starts scrolling
69         // even though the master string (rep_msg_str) may change at any time.
70         static string msg1 = "";
71         static string msg2 = "";
72         
73         if( rep_msg || msgList.size() ) {
74                 SGPropertyNode *xsize_node = fgGetNode("/sim/startup/xsize");
75                 SGPropertyNode *ysize_node = fgGetNode("/sim/startup/ysize");
76                 int iwidth   = xsize_node->getIntValue();
77                 int iheight  = ysize_node->getIntValue();
78                 
79                 glMatrixMode( GL_PROJECTION );
80                 glPushMatrix();
81                 glLoadIdentity();
82                 gluOrtho2D( 0, iwidth, 0, iheight );
83                 glMatrixMode( GL_MODELVIEW );
84                 glPushMatrix();
85                 glLoadIdentity();
86                 
87                 glDisable( GL_DEPTH_TEST );
88                 glDisable( GL_LIGHTING );
89                 
90                 glColor3f( 0.9, 0.4, 0.2 );
91                 
92                 float fps = general.get_frame_rate();
93                 
94                 if(rep_msg) {
95                         //cout << "dsp_offset1 = " << dsp_offset1 << " dsp_offset2 = " << dsp_offset2 << endl;
96                         if(dsp_offset1 == 0) {
97                                 msg1 = rep_msg_str;
98                         }
99                         if(dsp_offset2 == 0) {
100                                 msg2 = rep_msg_str;
101                         }
102                         // Check for the situation where one offset is negative and the message is changed
103                         if(change_msg_flag) {
104                                 if(dsp_offset1 < 0) {
105                                         msg1 = rep_msg_str;
106                                 } else if(dsp_offset2 < 0) {
107                                         msg2 = rep_msg_str;
108                                 }
109                                 change_msg_flag = false;
110                         }
111                         
112                         //      guiFnt.drawString( rep_msg_str.c_str(),
113                         //          int(iwidth - guiFnt.getStringWidth(buf) - 10 - (int)dsp_offset),
114                         //          (iheight - 20) );
115                         guiFnt.drawString( msg1.c_str(),
116                         int(iwidth - 10 - dsp_offset1),
117                         (iheight - 20) );
118                         guiFnt.drawString( msg2.c_str(),
119                         int(iwidth - 10 - dsp_offset2),
120                         (iheight - 20) );
121                         
122                         // Try to scroll at a frame rate independent speed
123                         // 40 pixels/second looks about right for now
124                         if(dsp_offset1 >= dsp_offset2) {
125                                 dsp_offset1+=(40.0/fps);
126                                 dsp_offset2 = dsp_offset1 - (rep_msg_str.size() * 10) - 100;
127                                 if(dsp_offset1 > (iwidth + (rep_msg_str.size() * 10)))
128                                         dsp_offset1 = 0;
129                         } else {
130                                 dsp_offset2+=(40.0/fps);
131                                 dsp_offset1 = dsp_offset2 - (rep_msg_str.size() * 10) - 100;
132                                 if(dsp_offset2 > (iwidth + (rep_msg_str.size() * 10)))
133                                         dsp_offset2 = 0;
134                         }
135                         
136                 }
137                 
138                 if(msgList.size()) {
139                         //cout << "Attempting to render single message\n";
140                         // We have at least one non-repeating message to process
141                         if(fgGetBool("/ATC/display/scroll-single-messages")) {  // Scroll single messages across the screen.
142                                 msgList_itr = msgList.begin();
143                                 int i = 0;
144                                 while(msgList_itr != msgList.end()) {
145                                         nasalMessage m = *msgList_itr;
146                                         //cout << "m.counter = " << m.counter << '\n';
147                                         if(m.dsp_offset > (iwidth + (m.msg.size() * 10))) {
148                                                 //cout << "Stopping single message\n";
149                                                 msgList_itr = msgList.erase(msgList_itr);
150                                         } else if(m.counter > m.start_count) {
151                                                 //cout << "Drawing single message\n";
152                                                 guiFnt.drawString( m.msg.c_str(),
153                                                 int(iwidth - 10 - m.dsp_offset),
154                                                 (iheight - 80) );
155                                                 m.counter += dt;
156                                                 m.dsp_offset += (80.0/fps);
157                                                 msgList[i] = m;
158                                                 ++msgList_itr;
159                                                 ++i;
160                                         } else {
161                                                 //cout << "Not yet started single message\n";
162                                                 m.counter += dt;
163                                                 msgList[i] = m;
164                                                 ++msgList_itr;
165                                                 ++i;
166                                         }
167                                 }
168                         } else {        // Display single messages for a short period of time.
169                                 msgList_itr = msgList.begin();
170                                 int i = 0;
171                                 while(msgList_itr != msgList.end()) {
172                                         nasalMessage m = *msgList_itr;
173                                         //cout << "m.counter = " << m.counter << '\n';
174                                         if(m.counter > m.stop_count) {
175                                                 //cout << "Stopping single message\n";
176                                                 msgList_itr = msgList.erase(msgList_itr);
177                                         } else if(m.counter > m.start_count) {
178                                                 int pin = (((int)m.msg.size() * 8) >= iwidth ? 5 : (iwidth - (m.msg.size() * 8))/2);
179                                                 //cout << m.msg << '\n';
180                                                 //cout << "pin = " << pin << ", iwidth = " << iwidth << ", msg.size = " << m.msg.size() << '\n';
181                                                 guiFnt.drawString( m.msg.c_str(), pin, (iheight - 80) );
182                                                 m.counter += dt;
183                                                 msgList[i] = m;
184                                                 ++msgList_itr;
185                                                 ++i;
186                                         } else {
187                                                 m.counter += dt;
188                                                 msgList[i] = m;
189                                                 ++msgList_itr;
190                                                 ++i;
191                                         }
192                                 }
193                         }
194                 }
195                 glEnable( GL_DEPTH_TEST );
196                 glEnable( GL_LIGHTING );
197                 glMatrixMode( GL_PROJECTION );
198                 glPopMatrix();
199                 glMatrixMode( GL_MODELVIEW );
200                 glPopMatrix();
201         }
202 }
203
204 void FGNasalDisplay::RegisterSingleMessage(const string& msg, double delay) {
205         //cout << msg << '\n';
206         nasalMessage m;
207         m.msg = msg;
208         m.repeating = false;
209         m.counter = 0.0;
210         m.start_count = delay;
211         m.stop_count = m.start_count + 5.0;             // Display for 5ish seconds for now - this might have to change eg. be related to length of message in future
212         //cout << "m.stop_count = " << m.stop_count << '\n';
213         m.id = 0;
214         m.dsp_offset = 0.0;
215         
216         msgList.push_back(m);
217         //cout << "Single message registered\n";
218 }
219
220 void FGNasalDisplay::RegisterRepeatingMessage(const string& msg) {
221         rep_msg = true;
222         rep_msg_str = msg;
223         return;
224 }
225
226 void FGNasalDisplay::ChangeRepeatingMessage(const string& newmsg) {
227         rep_msg_str = newmsg;
228         change_msg_flag = true;
229         return;
230 }
231
232 void FGNasalDisplay::CancelRepeatingMessage() {
233         rep_msg = false;
234         rep_msg_str = "";
235         dsp_offset1 = 0;
236         dsp_offset2 = 0;
237         return;
238 }
239