1 #ifndef _MSC_VER // MSVC really needs a definition for wchar_t
2 #define _WCHAR_T_DEFINED 1 // Glut needs this, or else it tries to
10 #include <simgear/compiler.h>
16 #include "fg_props.hxx"
20 // fg_os callback registration APIs
21 // (These are not glut-specific)
24 static fgIdleHandler IdleHandler = 0;
25 static fgDrawHandler DrawHandler = 0;
26 static fgWindowResizeHandler WindowResizeHandler = 0;
27 static fgKeyHandler KeyHandler = 0;
28 static fgMouseClickHandler MouseClickHandler = 0;
29 static fgMouseMotionHandler MouseMotionHandler = 0;
31 void fgRegisterIdleHandler(fgIdleHandler func)
36 void fgRegisterDrawHandler(fgDrawHandler func)
41 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
43 WindowResizeHandler = func;
46 void fgRegisterKeyHandler(fgKeyHandler func)
51 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
53 MouseClickHandler = func;
56 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
58 MouseMotionHandler = func;
62 // Native glut callbacks.
63 // These translate the glut event model into fg*Handler callbacks
66 static int GlutModifiers = 0;
68 static void callKeyHandler(int k, int mods, int x, int y)
70 int puiup = mods & KEYMOD_RELEASED ? PU_UP : PU_DOWN;
71 if(puKeyboard(k, puiup))
73 if(KeyHandler) (*KeyHandler)(k, mods, x, y);
76 static void GLUTmotion (int x, int y)
78 if(MouseMotionHandler) (*MouseMotionHandler)(x, y);
81 static void GLUTmouse (int button, int updown, int x, int y)
83 GlutModifiers = glutGetModifiers();
84 if(MouseClickHandler) (*MouseClickHandler)(button, updown, x, y);
87 static void GLUTspecialkeyup(int k, int x, int y)
89 GlutModifiers = glutGetModifiers();
90 callKeyHandler(256 + k, fgGetKeyModifiers() | KEYMOD_RELEASED, x, y);
93 static void GLUTspecialkey(int k, int x, int y)
95 GlutModifiers = glutGetModifiers();
96 callKeyHandler(256 + k, fgGetKeyModifiers(), x, y);
99 static void GLUTkeyup(unsigned char k, int x, int y)
101 GlutModifiers = glutGetModifiers();
102 callKeyHandler(k, fgGetKeyModifiers() | KEYMOD_RELEASED, x, y);
105 static void GLUTkey(unsigned char k, int x, int y)
107 GlutModifiers = glutGetModifiers();
108 callKeyHandler(k, fgGetKeyModifiers(), x, y);
111 static void GLUTidle()
113 if(IdleHandler) (*IdleHandler)();
116 static void GLUTdraw()
118 if(DrawHandler) (*DrawHandler)();
122 static void GLUTreshape(int w, int h)
124 if(WindowResizeHandler) (*WindowResizeHandler)(w, h);
128 // fg_os API definition
131 void fgOSInit(int* argc, char** argv)
133 glutInit(argc, argv);
136 void fgOSFullScreen()
146 void fgOSExit(int code)
151 static int CurrentCursor = MOUSE_CURSOR_POINTER;
153 int fgGetMouseCursor()
155 return CurrentCursor;
158 void fgSetMouseCursor(int cursor)
160 CurrentCursor = cursor;
161 if (cursor == MOUSE_CURSOR_NONE) cursor = GLUT_CURSOR_NONE;
162 else if(cursor == MOUSE_CURSOR_POINTER) cursor = GLUT_CURSOR_INHERIT;
163 else if(cursor == MOUSE_CURSOR_WAIT) cursor = GLUT_CURSOR_WAIT;
164 else if(cursor == MOUSE_CURSOR_CROSSHAIR) cursor = GLUT_CURSOR_CROSSHAIR;
165 else if(cursor == MOUSE_CURSOR_LEFTRIGHT) cursor = GLUT_CURSOR_LEFT_RIGHT;
166 // Otherwise, pass it through unchanged...
167 glutSetCursor(cursor);
170 void fgWarpMouse(int x, int y)
172 glutWarpPointer(x, y);
175 int fgGetKeyModifiers()
178 if(GlutModifiers & GLUT_ACTIVE_SHIFT) result |= KEYMOD_SHIFT;
179 if(GlutModifiers & GLUT_ACTIVE_CTRL) result |= KEYMOD_CTRL;
180 if(GlutModifiers & GLUT_ACTIVE_ALT) result |= KEYMOD_ALT;
184 void fgRequestRedraw()
189 void fgOSOpenWindow(int w, int h, int bpp, bool alpha,
190 bool stencil, bool fullscreen)
192 int mode = GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE;
193 if(alpha) mode |= GLUT_ALPHA;
194 if(stencil && bpp > 16) mode |= GLUT_STENCIL;
196 glutInitDisplayMode(mode);
197 glutInitWindowSize(w, h);
198 if(!fgGetBool("/sim/startup/game-mode")) {
199 glutCreateWindow("FlightGear");
201 char game_mode_str[256];
202 sprintf(game_mode_str, "width=%d height=%d bpp=%d", w, h, bpp);
203 glutGameModeString( game_mode_str );
207 // Register these here. Calling them before the window is open
209 glutMotionFunc(GLUTmotion);
210 glutPassiveMotionFunc(GLUTmotion);
211 glutMouseFunc(GLUTmouse);
212 glutSpecialUpFunc(GLUTspecialkeyup);
213 glutSpecialFunc(GLUTspecialkey);
214 glutKeyboardUpFunc(GLUTkeyup);
215 glutKeyboardFunc(GLUTkey);
216 glutIdleFunc(GLUTidle);
217 glutDisplayFunc(GLUTdraw);
218 glutReshapeFunc(GLUTreshape);