]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os.cxx
beaea1bee11a925e86549d44c7be17c5614b557f
[flightgear.git] / src / Main / fg_os.cxx
1 // The mac puts this in a weird location (GLUT/glut.h), so the
2 // configure script detects the location and defines it as a macro.
3 #ifdef HAVE_CONFIG_H
4 #  include <config.h>
5 #  include FG_GLUT_H
6 #else
7 #  include <GL/glut.h>
8 #endif
9
10 #include <plib/pu.h>
11
12 #include "fg_props.hxx"
13 #include "fg_os.hxx"
14
15 //
16 // fg_os callback registration APIs
17 // (These are not glut-specific)
18 //
19
20 static fgIdleHandler IdleHandler = 0;
21 static fgDrawHandler DrawHandler = 0;
22 static fgWindowResizeHandler WindowResizeHandler = 0;
23 static fgKeyHandler KeyHandler = 0;
24 static fgMouseClickHandler MouseClickHandler = 0;
25 static fgMouseMotionHandler MouseMotionHandler = 0;
26
27 void fgRegisterIdleHandler(fgIdleHandler func)
28 {
29     IdleHandler = func;
30 }
31
32 void fgRegisterDrawHandler(fgDrawHandler func)
33 {
34     DrawHandler = func;
35 }
36
37 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
38 {
39     WindowResizeHandler = func;
40 }
41
42 void fgRegisterKeyHandler(fgKeyHandler func)
43 {
44     KeyHandler = func;
45 }
46
47 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
48 {
49     MouseClickHandler = func;
50 }
51
52 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
53 {
54     MouseMotionHandler = func;
55 }
56
57 //
58 // Native glut callbacks.
59 // These translate the glut event model into fg*Handler callbacks
60 //
61
62 static int GlutModifiers = 0;
63
64 static void callKeyHandler(int k, int mods, int x, int y)
65 {
66     int puiup = mods & KEYMOD_RELEASED ? PU_UP : PU_DOWN;
67     if(puKeyboard(k, puiup))
68         return;
69     if(KeyHandler) (*KeyHandler)(k, mods, x, y);
70 }
71
72 static void GLUTmotion (int x, int y)
73 {
74     if(MouseMotionHandler) (*MouseMotionHandler)(x, y);
75 }
76
77 static void GLUTmouse (int button, int updown, int x, int y)
78 {
79     GlutModifiers = glutGetModifiers();
80     if(MouseClickHandler) (*MouseClickHandler)(button, updown, x, y);
81 }
82
83 static void GLUTspecialkeyup(int k, int x, int y)
84 {
85     GlutModifiers = glutGetModifiers();
86     callKeyHandler(256 + k, fgGetKeyModifiers() | KEYMOD_RELEASED, x, y);
87 }
88
89 static void GLUTspecialkey(int k, int x, int y)
90 {
91     GlutModifiers = glutGetModifiers();
92     callKeyHandler(256 + k, fgGetKeyModifiers(), x, y);
93 }
94
95 static void GLUTkeyup(unsigned char k, int x, int y)
96 {
97     GlutModifiers = glutGetModifiers();
98     callKeyHandler(k, fgGetKeyModifiers() | KEYMOD_RELEASED, x, y);
99 }
100
101 static void GLUTkey(unsigned char k, int x, int y)
102 {
103     GlutModifiers = glutGetModifiers();
104     callKeyHandler(k, fgGetKeyModifiers(), x, y);
105 }
106
107 static void GLUTidle()
108 {
109     if(IdleHandler) (*IdleHandler)();
110 }
111
112 static void GLUTdraw()
113 {
114     if(DrawHandler) (*DrawHandler)();
115     glutSwapBuffers();
116 }
117
118 static void GLUTreshape(int w, int h)
119 {
120     if(WindowResizeHandler) (*WindowResizeHandler)(w, h);
121 }
122
123 //
124 // fg_os API definition
125 //
126
127 void fgOSInit(int* argc, char** argv)
128 {
129     glutInit(argc, argv);
130 }
131
132 void fgOSFullScreen()
133 {
134     glutFullScreen();
135 }
136
137 void fgOSMainLoop()
138 {
139     glutMainLoop();
140 }
141
142 void fgOSExit(int code)
143 {
144     exit(code);
145 }
146
147 static int CurrentCursor = MOUSE_CURSOR_POINTER;
148
149 int fgGetMouseCursor()
150 {
151     return CurrentCursor;
152 }
153
154 void fgSetMouseCursor(int cursor)
155 {
156     CurrentCursor = cursor;
157     if     (cursor == MOUSE_CURSOR_NONE)      cursor = GLUT_CURSOR_NONE;
158     else if(cursor == MOUSE_CURSOR_POINTER)   cursor = GLUT_CURSOR_INHERIT;
159     else if(cursor == MOUSE_CURSOR_WAIT)      cursor = GLUT_CURSOR_WAIT;
160     else if(cursor == MOUSE_CURSOR_CROSSHAIR) cursor = GLUT_CURSOR_CROSSHAIR;
161     else if(cursor == MOUSE_CURSOR_LEFTRIGHT) cursor = GLUT_CURSOR_LEFT_RIGHT;
162     // Otherwise, pass it through unchanged...
163     glutSetCursor(cursor);
164 }
165
166 void fgWarpMouse(int x, int y)
167 {
168     glutWarpPointer(x, y);
169 }
170
171 int fgGetKeyModifiers()
172 {
173     int result = 0;
174     if(GlutModifiers & GLUT_ACTIVE_SHIFT) result |= KEYMOD_SHIFT;
175     if(GlutModifiers & GLUT_ACTIVE_CTRL)  result |= KEYMOD_CTRL;
176     if(GlutModifiers & GLUT_ACTIVE_ALT)   result |= KEYMOD_ALT;
177     return result;
178 }
179
180 void fgRequestRedraw()
181 {
182     glutPostRedisplay();
183 }
184
185 void fgOSOpenWindow(int w, int h, int bpp, bool alpha,
186                     bool stencil, bool fullscreen)
187 {
188     int mode = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE;
189     if(alpha) mode |= GLUT_ALPHA;
190     if(stencil) mode |= GLUT_STENCIL;
191
192     glutInitDisplayMode(mode);
193     glutInitWindowSize(w, h);
194     if(!fgGetBool("/sim/startup/game-mode")) {
195         glutCreateWindow("FlightGear");
196     } else {
197         char game_mode_str[256];
198         sprintf(game_mode_str, "width=%d height=%d bpp=%d", w, h, bpp);
199         glutGameModeString( game_mode_str );
200         glutEnterGameMode();
201     }
202
203     // Register these here.  Calling them before the window is open
204     // crashes.
205     glutMotionFunc(GLUTmotion);
206     glutPassiveMotionFunc(GLUTmotion);
207     glutMouseFunc(GLUTmouse);
208     glutSpecialUpFunc(GLUTspecialkeyup);
209     glutSpecialFunc(GLUTspecialkey);
210     glutKeyboardUpFunc(GLUTkeyup);
211     glutKeyboardFunc(GLUTkey);
212     glutIdleFunc(GLUTidle);
213     glutDisplayFunc(GLUTdraw);
214     glutReshapeFunc(GLUTreshape);
215
216 }