]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os.cxx
81ddf0b55b81ed62c317dac01f8ad23701c74656
[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 static int CurrentCursor = MOUSE_CURSOR_POINTER;
143
144 int fgGetMouseCursor()
145 {
146     return CurrentCursor;
147 }
148
149 void fgSetMouseCursor(int cursor)
150 {
151     CurrentCursor = cursor;
152     if     (cursor == MOUSE_CURSOR_NONE)      cursor = GLUT_CURSOR_NONE;
153     else if(cursor == MOUSE_CURSOR_POINTER)   cursor = GLUT_CURSOR_INHERIT;
154     else if(cursor == MOUSE_CURSOR_WAIT)      cursor = GLUT_CURSOR_WAIT;
155     else if(cursor == MOUSE_CURSOR_CROSSHAIR) cursor = GLUT_CURSOR_CROSSHAIR;
156     else if(cursor == MOUSE_CURSOR_LEFTRIGHT) cursor = GLUT_CURSOR_LEFT_RIGHT;
157     // Otherwise, pass it through unchanged...
158     glutSetCursor(cursor);
159 }
160
161 void fgWarpMouse(int x, int y)
162 {
163     glutWarpPointer(x, y);
164 }
165
166 int fgGetKeyModifiers()
167 {
168     int result = 0;
169     if(GlutModifiers & GLUT_ACTIVE_SHIFT) result |= KEYMOD_SHIFT;
170     if(GlutModifiers & GLUT_ACTIVE_CTRL)  result |= KEYMOD_CTRL;
171     if(GlutModifiers & GLUT_ACTIVE_ALT)   result |= KEYMOD_ALT;
172     return result;
173 }
174
175 void fgRequestRedraw()
176 {
177     glutPostRedisplay();
178 }
179
180 void fgOSOpenWindow(int w, int h, int bpp, bool alpha, bool stencil)
181 {
182     int mode = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE;
183     if(alpha) mode |= GLUT_ALPHA;
184     if(stencil) mode |= GLUT_STENCIL;
185
186     glutInitDisplayMode(mode);
187     glutInitWindowSize(w, h);
188     if(!fgGetBool("/sim/startup/game-mode")) {
189         glutCreateWindow("FlightGear");
190     } else {
191         char game_mode_str[256];
192         sprintf(game_mode_str, "width=%d height=%d bpp=%d", w, h, bpp);
193         glutGameModeString( game_mode_str );
194         glutEnterGameMode();
195     }
196
197     // Register these here.  Calling them before the window is open
198     // crashes.
199     glutMotionFunc(GLUTmotion);
200     glutPassiveMotionFunc(GLUTmotion);
201     glutMouseFunc(GLUTmouse);
202     glutSpecialUpFunc(GLUTspecialkeyup);
203     glutSpecialFunc(GLUTspecialkey);
204     glutKeyboardUpFunc(GLUTkeyup);
205     glutKeyboardFunc(GLUTkey);
206     glutIdleFunc(GLUTidle);
207     glutDisplayFunc(GLUTdraw);
208     glutReshapeFunc(GLUTreshape);
209
210 }