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