]> git.mxchange.org Git - flightgear.git/blob - src/Main/fg_os_sdl.cxx
Early-access version of an SDL implementation of the fg_os API. It works,
[flightgear.git] / src / Main / fg_os_sdl.cxx
1 #include <stdlib.h>
2
3 #include <SDL/SDL.h>
4 #include <plib/pu.h>
5
6 #include "fg_os.hxx"
7
8 //
9 // fg_os callback registration APIs
10 //
11
12 static fgIdleHandler IdleHandler = 0;
13 static fgDrawHandler DrawHandler = 0;
14 static fgKeyHandler KeyHandler = 0;
15 static fgMouseClickHandler MouseClickHandler = 0;
16 static fgMouseMotionHandler MouseMotionHandler = 0;
17
18 static int CurrentModifiers = 0;
19 static int CurrentMouseX = 0;
20 static int CurrentMouseY = 0;
21 static bool NeedRedraw = false;
22
23 void fgRegisterIdleHandler(fgIdleHandler func)
24 {
25     IdleHandler = func;
26 }
27
28 void fgRegisterDrawHandler(fgDrawHandler func)
29 {
30     DrawHandler = func;
31     NeedRedraw = true;
32 }
33
34 void fgRegisterWindowResizeHandler(fgWindowResizeHandler func)
35 {
36     // Noop.  SDL does not support window resize.
37 }
38
39 void fgRegisterKeyHandler(fgKeyHandler func)
40 {
41     KeyHandler = func;
42 }
43
44 void fgRegisterMouseClickHandler(fgMouseClickHandler func)
45 {
46     MouseClickHandler = func;
47 }
48
49 void fgRegisterMouseMotionHandler(fgMouseMotionHandler func)
50 {
51     MouseMotionHandler = func;
52 }
53
54 //
55 // fg_os implementation
56 //
57
58 void fgOSOpenWindow(int w, int h, int bpp, bool alpha, bool fullscreen)
59 {
60     int cbits = (bpp <= 16) ?  5 :  8;
61     int zbits = (bpp <= 16) ? 16 : 32;
62
63     SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE); // FIXME: handle errors
64
65     SDL_GL_SetAttribute(SDL_GL_RED_SIZE, cbits);
66     SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, cbits);
67     SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, cbits);
68     if(alpha)
69         SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
70     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, zbits);
71     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
72
73     int vidmask = SDL_OPENGL;
74     if(fullscreen) vidmask |= SDL_FULLSCREEN;
75     SDL_SetVideoMode(w, h, 16, vidmask); // FIXME: handle errors
76
77     SDL_WM_SetCaption("FlightGear", "FlightGear");
78
79     // This enables keycode translation (e.g. capital letters when
80     // shift is pressed, as well as i18n input methods).  Eventually,
81     // we may want to port the input maps to specify <mod-shift>
82     // explicitly, and will turn this off.
83     SDL_EnableUNICODE(1);
84
85     // FIXME: we may not get what we asked for (especially in full
86     // screen modes), so these need to be propagated back to the
87     // property tree for the rest of the code to inspect...
88     //
89     // SDL_Surface* screen = SDL_GetVideoSurface();
90     // int realw = screen->w;
91     // int realh = screen->h;
92 }
93
94 static void handleKey(int key, int keyup)
95 {
96     int modmask = 0;
97     switch(key) {
98     case SDLK_RSHIFT: modmask = KEYMOD_SHIFT; break;
99     case SDLK_LSHIFT: modmask = KEYMOD_SHIFT; break;
100     case SDLK_RCTRL:  modmask = KEYMOD_CTRL;  break;
101     case SDLK_LCTRL:  modmask = KEYMOD_CTRL;  break;
102     case SDLK_RALT:   modmask = KEYMOD_ALT;   break;
103     case SDLK_LALT:   modmask = KEYMOD_ALT;   break;
104
105     case SDLK_LEFT:     key = PU_KEY_LEFT;      break;
106     case SDLK_UP:       key = PU_KEY_UP;        break;
107     case SDLK_RIGHT:    key = PU_KEY_RIGHT;     break;
108     case SDLK_DOWN:     key = PU_KEY_DOWN;      break;
109     case SDLK_PAGEUP:   key = PU_KEY_PAGE_UP;   break;
110     case SDLK_PAGEDOWN: key = PU_KEY_PAGE_DOWN; break;
111     case SDLK_HOME:     key = PU_KEY_HOME;      break;
112     case SDLK_END:      key = PU_KEY_END;       break;
113     case SDLK_INSERT:   key = PU_KEY_INSERT;    break;
114     case SDLK_F1:       key = PU_KEY_F1;        break;
115     case SDLK_F2:       key = PU_KEY_F2;        break;
116     case SDLK_F3:       key = PU_KEY_F3;        break;
117     case SDLK_F4:       key = PU_KEY_F4;        break;
118     case SDLK_F5:       key = PU_KEY_F5;        break;
119     case SDLK_F6:       key = PU_KEY_F6;        break;
120     case SDLK_F7:       key = PU_KEY_F7;        break;
121     case SDLK_F8:       key = PU_KEY_F8;        break;
122     case SDLK_F9:       key = PU_KEY_F9;        break;
123     case SDLK_F10:      key = PU_KEY_F10;       break;
124     case SDLK_F11:      key = PU_KEY_F11;       break;
125     case SDLK_F12:      key = PU_KEY_F12;       break;
126     }
127     if(keyup) CurrentModifiers &= ~modmask;
128     else      CurrentModifiers |= modmask;
129     if(modmask == 0 && KeyHandler)
130         (*KeyHandler)(key, CurrentModifiers, CurrentMouseX, CurrentMouseY);
131 }
132
133 // FIXME: need to export this and get the rest of FlightGear to use
134 // it, the SDL hook is required to do things like reset the video
135 // mode and key repeat.  Integrate with existing fgExit() in util.cxx.
136 void fgOSExit(int code)
137 {
138     SDL_Quit();
139     exit(code);
140 }
141
142 void fgOSMainLoop()
143 {
144     while(1) {
145         SDL_Event e;
146         while(SDL_PollEvent(&e)) {
147             switch(e.type) {
148             case SDL_QUIT:
149                 fgOSExit(0);
150                 break;
151             case SDL_KEYDOWN:
152             case SDL_KEYUP:
153                 handleKey(e.key.keysym.unicode, e.key.state == SDL_RELEASED);
154                 break;
155             case SDL_MOUSEBUTTONDOWN:
156             case SDL_MOUSEBUTTONUP:
157                 // Note offset: SDL uses buttons 1,2,3 not 0,1,2
158                 CurrentMouseX = e.button.x;
159                 CurrentMouseY = e.button.y;
160                 if(MouseClickHandler)
161                     (*MouseClickHandler)(e.button.button - 1,
162                                          e.button.state == SDL_RELEASED,
163                                          e.button.x, e.button.y);
164                 break;
165             case SDL_MOUSEMOTION:
166                 CurrentMouseX = e.motion.x;
167                 CurrentMouseY = e.motion.y;
168                 if(MouseMotionHandler)
169                     (*MouseMotionHandler)(e.motion.x, e.motion.y);
170                 break;
171             }
172         }
173         if(IdleHandler) (*IdleHandler)();
174         if(NeedRedraw && DrawHandler) {
175             (*DrawHandler)();
176             SDL_GL_SwapBuffers();
177             NeedRedraw = false;
178         }
179     }
180 }
181
182 int fgGetKeyModifiers()
183 {
184     return CurrentModifiers;
185 }
186
187 void fgWarpMouse(int x, int y)
188 {
189     SDL_WarpMouse(x, y);
190 }
191
192 void fgRequestRedraw()
193 {
194     NeedRedraw = true;
195 }
196
197 void fgOSInit(int* argc, char** argv)
198 {
199     // Nothing to do here.  SDL has no command line options.
200 }
201
202 void fgOSFullScreen()
203 {
204     // Noop.  SDL must set fullscreen at window open time and cannot
205     // change modes.
206 }
207
208 // Figure out what to do with these
209 void fgSetMouseCursor(int cursor) {}
210 int  fgGetMouseCursor() { return MOUSE_CURSOR_POINTER; }