]> git.mxchange.org Git - flightgear.git/blob - PUI/complex.cxx
8610bb2238c5ad78b790ff2f704ee2ec557f33ba
[flightgear.git] / PUI / complex.cxx
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5 #ifdef WIN32
6 #include <windows.h>
7 #else
8 #include <unistd.h>
9 #endif
10 #include <math.h>
11 #include <GL/glut.h>
12 #include "pu.h"
13
14 /***********************************\
15 *                                   *
16 * These are the PUI widget pointers *
17 *                                   *
18 \***********************************/
19
20 puMenuBar   *main_menu_bar ;
21 puButton    *hide_menu_button ;
22 puDialogBox *dialog_box ;
23 puText      *dialog_box_message ;
24 puOneShot   *dialog_box_ok_button ;
25 puText      *timer_text ;
26 puSlider    *rspeedSlider;
27
28
29 /***********************************\
30 *                                   *
31 *  This is a generic tumbling cube  *
32 *                                   *
33 \***********************************/
34
35 GLfloat light_diffuse [] = {1.0, 0.0, 0.0, 1.0} ;  /* Red diffuse light. */
36 GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0} ;  /* Infinite light location. */
37
38 GLfloat cube_n[6][3] =  /* Normals */
39 {
40  {-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
41  { 0.0,-1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0,-1.0}
42 } ;
43
44 GLint cube_i[6][4] =  /* Vertex indices */
45 {
46   {0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
47   {4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3}
48 } ;
49
50 GLfloat cube_v[8][3] =  /* Vertices */
51 {
52   {-1.0,-1.0, 1.0}, {-1.0,-1.0,-1.0}, {-1.0, 1.0,-1.0}, {-1.0, 1.0, 1.0},
53   { 1.0,-1.0, 1.0}, { 1.0,-1.0,-1.0}, { 1.0, 1.0,-1.0}, { 1.0, 1.0, 1.0}
54 } ;
55
56
57 static int firsttime;
58
59 void drawCube (void)
60 {
61
62   if ( firsttime )
63   {
64     /*
65       Deliberately do this only once - it's a better test of
66       PUI's attempts to leave the OpenGL state undisturbed
67     */
68
69     firsttime = FALSE ;
70     glLightfv      ( GL_LIGHT0, GL_DIFFUSE , light_diffuse  ) ;
71     glLightfv      ( GL_LIGHT0, GL_POSITION, light_position ) ;
72     glEnable       ( GL_LIGHT0     ) ;
73     glEnable       ( GL_LIGHTING   ) ;
74     glEnable       ( GL_DEPTH_TEST ) ;
75     glMatrixMode   ( GL_PROJECTION ) ;
76     gluPerspective ( 40.0, 1.0, 1.0, 10.0 ) ;
77     glMatrixMode   ( GL_MODELVIEW ) ;
78     gluLookAt      ( 0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 ) ;
79     glTranslatef   ( 0.0, 0.0, -1.0 ) ;
80     glRotatef      ( 60.0, 1.0, 0.0, 0.0 ) ;
81   }
82
83   glCullFace     ( GL_FRONT ) ;
84   glEnable       ( GL_CULL_FACE ) ;
85   //  glRotatef ( 1.0f, 0.0, 0.0, 1.0 ) ;  /* Tumble that cube! */
86
87   glBegin ( GL_QUADS ) ;
88
89   for ( int i = 0 ; i < 6 ; i++ )
90   {
91     glNormal3fv ( &cube_n[i][0] ) ;
92     glVertex3fv ( cube_v[cube_i[i][0]] ) ; glVertex3fv ( cube_v[cube_i[i][1]] ) ;
93     glVertex3fv ( cube_v[cube_i[i][2]] ) ; glVertex3fv ( cube_v[cube_i[i][3]] ) ;
94   }
95
96   glEnd () ;
97 }
98
99 /********************************\
100 *                                *
101 * End of cube renderer in OpenGL * 
102 *                                *
103 \********************************/
104
105
106 /**************************************\
107 *                                      *
108 * These three functions capture mouse  *
109 * and keystrokes (special and mundane) *
110 * from GLUT and pass them on to PUI.   *
111 *                                      *
112 \**************************************/
113
114 static void specialfn ( int key, int, int )
115 {
116   puKeyboard ( key + PU_KEY_GLUT_SPECIAL_OFFSET, PU_DOWN ) ;
117   glutPostRedisplay () ;
118 }
119
120 static void keyfn ( unsigned char key, int, int )
121 {
122   puKeyboard ( key, PU_DOWN ) ;
123   glutPostRedisplay () ;
124 }
125
126 static void motionfn ( int x, int y )
127 {
128   puMouse ( x, y ) ;
129   glutPostRedisplay () ;
130 }
131
132 static void mousefn ( int button, int updown, int x, int y )
133 {
134   puMouse ( button, updown, x, y ) ;
135   glutPostRedisplay () ;
136 }
137
138 /**************************************\
139 *                                      *
140 * This function redisplays the PUI and *
141 * the tumbling cube, flips the double  *
142 * buffer and then asks GLUT to post a  *
143 * redisplay command - so we re-render  *
144 * at maximum rate.                     *
145 *                                      *
146 \**************************************/
147
148 static void displayfn (void)
149 {
150   /* Clear the screen */
151
152   glClearColor ( 0.0, 0.0, 0.0, 1.0 ) ;
153   glClear      ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
154
155   /* Draw the tumbling cube */
156
157   float val ; rspeedSlider->getValue ( &val ) ;
158
159   glRotatef( 4*val, 15.0, 10.0 , 5.0 );
160
161   drawCube  () ;
162
163   /* Update the 'timer' */
164
165   time_t t = time ( NULL ) ;
166   timer_text -> setLabel ( ctime ( & t ) ) ;
167
168   /* Make PUI redraw */
169
170   puDisplay () ;
171   
172   /* Off we go again... */
173
174   glutSwapBuffers   () ;
175   glutPostRedisplay () ;
176 }
177
178
179 /***********************************\
180 *                                   *
181 * Here are the PUI widget callback  *
182 * functions.                        *
183 *                                   *
184 \***********************************/
185
186 void hide_menu_cb ( puObject *cb )
187 {
188   if ( cb -> getValue () )
189   {
190     main_menu_bar -> reveal () ;
191     hide_menu_button->setLegend ( "Hide Menu" ) ;
192   }
193   else
194   {
195     main_menu_bar -> hide   () ;
196     hide_menu_button->setLegend ( "Show Menu" ) ;
197   }
198 }
199
200
201 void go_away_cb ( puObject * )
202 {
203   /*
204     Delete the dialog box when its 'OK' button is pressed.
205
206     This seems to crash on MSVC compilers - probably because
207     I delete dialog_box - whose member function is calling
208     this function. Hence we return to something that is
209     in a distinctly 'iffy' state.
210   */
211
212   delete dialog_box ;
213   dialog_box = NULL ;
214 }
215
216 void mk_dialog ( char *txt )
217 {
218   dialog_box = new puDialogBox ( 150, 50 ) ;
219   {
220     new puFrame ( 0, 0, 400, 100 ) ;
221     dialog_box_message   = new puText         ( 10, 70 ) ;
222     dialog_box_message   -> setLabel          ( txt ) ;
223     dialog_box_ok_button = new puOneShot      ( 180, 10, 240, 50 ) ;
224     dialog_box_ok_button -> setLegend         ( "OK" ) ;
225     dialog_box_ok_button -> makeReturnDefault ( TRUE ) ;
226     dialog_box_ok_button -> setCallback       ( go_away_cb ) ;
227   }
228   dialog_box -> close  () ;
229   dialog_box -> reveal () ;
230 }
231
232 void ni_cb ( puObject * )
233 {
234   mk_dialog ( "Sorry, that function isn't implemented" ) ;
235 }
236
237 void about_cb ( puObject * )
238 {
239   mk_dialog ( "This is the PUI 'complex' program" ) ;
240 }
241
242 void help_cb ( puObject * )
243 {
244   mk_dialog ( "Sorry, no help is available for this demo" ) ;
245 }
246
247 void edit_cb ( puObject * )
248 {
249 }
250
251 void exit_cb ( puObject * )
252 {
253   fprintf ( stderr, "Exiting PUI demo program.\n" ) ;
254   exit ( 1 ) ;
255 }
256
257 /* Menu bar entries: */
258
259 char      *file_submenu    [] = {  "Exit", "Close", "--------", "Print", "--------", "Save", "New", NULL } ;
260 puCallback file_submenu_cb [] = { exit_cb, exit_cb,       NULL, ni_cb  ,       NULL,  ni_cb, ni_cb, NULL } ;
261
262 char      *edit_submenu    [] = { "Edit text", NULL } ;
263 puCallback edit_submenu_cb [] = {     edit_cb, NULL } ;
264
265 char      *help_submenu    [] = { "About...",  "Help", NULL } ;
266 puCallback help_submenu_cb [] = {   about_cb, help_cb, NULL } ;
267
268
269 void sliderCB( puObject *sliderObj)
270 {
271   glutPostRedisplay();
272 }
273
274 int main ( int argc, char **argv )
275 {
276
277   firsttime = TRUE;
278
279   glutInitWindowSize    ( 640, 480 ) ;
280   glutInit              ( &argc, argv ) ;
281   glutInitDisplayMode   ( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH ) ;
282   glutCreateWindow      ( "Complex PUI Application"  ) ;
283   glutDisplayFunc       ( displayfn ) ;
284   glutKeyboardFunc      ( keyfn     ) ;
285   glutSpecialFunc       ( specialfn ) ;
286   glutMouseFunc         ( mousefn   ) ;
287   glutMotionFunc        ( motionfn  ) ;
288   glutPassiveMotionFunc ( motionfn  ) ;
289   glutIdleFunc          ( displayfn ) ;
290
291   puInit () ;
292
293 #ifdef USING_3DFX
294   puShowCursor () ;
295 #endif
296
297   puSetDefaultStyle        ( PUSTYLE_SMALL_SHADED ) ;
298   puSetDefaultColourScheme ( 0.8, 0.2, 0.2 ) ;
299
300   timer_text = new puText ( 300, 10 ) ;
301   timer_text -> setColour ( PUCOL_LABEL, 1.0, 1.0, 1.0 ) ;
302
303   /* Make a button to hide the menu bar */
304
305   hide_menu_button = new puButton     ( 10, 10, 150, 50 ) ;
306   hide_menu_button->setValue          (    TRUE      ) ;
307   hide_menu_button->setLegend         ( "Hide Menu"  ) ;
308   hide_menu_button->setCallback       ( hide_menu_cb ) ;
309   hide_menu_button->makeReturnDefault (    TRUE      ) ;
310
311   /* Make the menu bar */
312
313   main_menu_bar = new puMenuBar () ;
314   {
315     main_menu_bar -> add_submenu ( "File", file_submenu, file_submenu_cb ) ;
316     main_menu_bar -> add_submenu ( "Edit", edit_submenu, edit_submenu_cb ) ;
317     main_menu_bar -> add_submenu ( "Help", help_submenu, help_submenu_cb ) ;
318   }
319   main_menu_bar -> close () ; 
320
321   rspeedSlider = new puSlider (20,80,150,TRUE);
322   rspeedSlider->setDelta(0.1);
323   rspeedSlider->setCBMode( PUSLIDER_DELTA );
324   rspeedSlider->setCallback(sliderCB);
325
326   glutMainLoop () ;
327   return 0 ;
328 }
329
330