2 fegdk: FE Game Development Kit
3 Copyright (C) 2001-2008 Alexey "waker" Yakovenko
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 waker@users.sourceforge.net
26 #include "f_sdlviewport.h"
27 #include "f_sdlviewportdata.h"
28 #include "f_helpers.h"
31 #include "f_application.h"
39 // table to translate sdl keycodes into native fe codes
41 0, 0, 0, 0, 0, 0, 0, 0, Key_BackSpace
, Key_Tab
, 0, 0, Key_CR
, 0, 0, 0, 0,
42 0, Key_Pause
, 0, 0, 0, 0, 0, 0, 0, Key_Esc
, 0, 0, 0, 0, Key_Space
,
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
44 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
45 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
47 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
48 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52 0, 0, 0, 0, Key_Del
, 0, 0, 0, // del is 127
53 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65 0, 0, 0, 0, 0, 0, // 255
69 Key_Kp_Ins
, Key_Kp_DownArrow
, Key_Kp_End
, Key_Kp_PgDn
,
70 Key_Kp_LeftArrow
, Key_Kp_5
, Key_Kp_RightArrow
,
71 Key_Kp_Home
, Key_Kp_UpArrow
, Key_Kp_PgUp
,
72 Key_Kp_Del
, Key_Kp_Slash
, Key_Kp_Star
,
73 Key_Kp_Minus
, Key_Kp_Plus
, Key_Kp_Enter
, Key_Kp_Equals
,
75 Key_UpArrow
, Key_DownArrow
, Key_RightArrow
, Key_LeftArrow
,
76 Key_Ins
, Key_Home
, Key_End
, Key_PgUp
, Key_PgDn
,
79 Key_F1
, Key_F2
, Key_F3
, Key_F4
, Key_F5
, Key_F6
, Key_F7
, Key_F8
, Key_F9
,
80 Key_F10
, Key_F11
, Key_F12
, Key_F13
, Key_F14
, Key_F15
,
85 Key_Kp_NumLock
, Key_CapsLock
, Key_ScrollLock
, Key_RShift
, Key_LShift
,
86 Key_RCtrl
, Key_LCtrl
, Key_RAlt
, Key_LAlt
, Key_RMeta
, Key_LMeta
,
87 Key_LSuper
, Key_RSuper
91 sdlViewportData::sdlViewportData (void)
95 sdlViewportData::~sdlViewportData (void)
101 sdlViewportData::init (const char *appName
)
103 // create and show window
104 SDL_Init (SDL_INIT_VIDEO
);
105 SDL_WM_SetCaption (appName
, NULL
);
106 SDL_EnableUNICODE (1);
107 SDL_EnableKeyRepeat (200, 50);
111 sdlViewportData::handleEvent (SDL_Event
*event
)
115 case SDL_MOUSEMOTION
:
116 onMouseMove (event
->motion
.x
, event
->motion
.y
);
118 case SDL_MOUSEBUTTONDOWN
:
119 onMouseBtnDown (event
->button
.button
);
121 case SDL_MOUSEBUTTONUP
:
122 onMouseBtnUp (event
->button
.button
);
125 onKeyDown (event
->key
.keysym
.sym
, event
->key
.keysym
.unicode
);
128 onKeyUp (event
->key
.keysym
.sym
);
134 sdlViewportData::onMouseMove (int x
, int y
)
136 smartPtr
<inputDevice
> mouse
= g_engine
->getInputDevice ();
137 mouse
->mouseMove (x
, y
);
138 g_engine
->getApplication ()->mouseMove (x
, y
);
142 sdlViewportData::onMouseBtnDown (int sdl_btn
)
144 smartPtr
<inputDevice
> mouse
= g_engine
->getInputDevice ();
148 case SDL_BUTTON_WHEELUP
:
149 g_engine
->getApplication ()->mouseWheel (1);
151 case SDL_BUTTON_WHEELDOWN
:
152 g_engine
->getApplication ()->mouseWheel (-1);
154 case SDL_BUTTON_LEFT
:
155 mouse
->lButtonDown ();
156 g_engine
->getApplication ()->mouseDown (Key_Mouse1
);
158 case SDL_BUTTON_RIGHT
:
159 mouse
->rButtonDown ();
160 g_engine
->getApplication ()->mouseDown (Key_Mouse2
);
162 case SDL_BUTTON_MIDDLE
:
163 mouse
->mButtonDown ();
164 g_engine
->getApplication ()->mouseDown (Key_Mouse3
);
170 sdlViewportData::onMouseBtnUp (int sdl_btn
)
172 smartPtr
<inputDevice
> mouse
= g_engine
->getInputDevice ();
176 case SDL_BUTTON_LEFT
:
178 g_engine
->getApplication ()->mouseUp (Key_Mouse1
);
180 case SDL_BUTTON_RIGHT
:
182 g_engine
->getApplication ()->mouseUp (Key_Mouse2
);
184 case SDL_BUTTON_MIDDLE
:
186 g_engine
->getApplication ()->mouseUp (Key_Mouse3
);
192 sdlViewportData::onKeyDown (int sdl_key
, int unicode
)
194 if (in_debugkeys
->ivalue
)
195 fprintf (stderr
, "SDL keydown: sdl_key=%d, unicode=%d\n", sdl_key
, unicode
);
196 // this routine could be more complex then win32 one, cause we don't have
197 // anything like WM_CHAR here
199 smartPtr
<inputDevice
> keyb
= g_engine
->getInputDevice ();
200 if (unicode
!= 0x7f // del is exception
203 keyb
->printableChar (unicode
);
206 // remap sdl codes to fe codes
207 if (sdl_key
>= (sizeof (sdl_to_key
) / sizeof (int)) || sdl_to_key
[sdl_key
] == 0)
209 if (in_debugkeys
->ivalue
)
210 fprintf (stderr
, "translated to %d\n", sdl_key
);
211 keyb
->keyDown (sdl_key
);
215 if (in_debugkeys
->ivalue
)
216 fprintf (stderr
, "translated to %d\n", sdl_to_key
[sdl_key
]);
217 keyb
->keyDown (sdl_to_key
[sdl_key
]);
222 sdlViewportData::onKeyUp (int sdl_key
)
224 smartPtr
<inputDevice
> keyb
= g_engine
->getInputDevice ();
225 // remap sdl codes to fe codes
226 if (sdl_key
>= (sizeof (sdl_to_key
) / sizeof (int)) || sdl_to_key
[sdl_key
] == 0)
227 keyb
->keyUp (sdl_key
);
229 keyb
->keyUp (sdl_to_key
[sdl_key
]);
234 sdlViewport::sdlViewport (void)
236 mpViewportData
= new sdlViewportData ();
237 init (g_engine
->getApplication ()->getTitle ());
240 sdlViewport::~sdlViewport (void)
244 delete mpViewportData
;
245 mpViewportData
= NULL
;
250 sdlViewport::init (const char *appName
)
252 mpViewportData
->init (appName
);
255 // common window access
257 sdlViewport::setPosition (int x
, int y
)
262 sdlViewport::setSize (int w
, int h
)
267 sdlViewport::getLeft (void) const
273 sdlViewport::getTop (void) const
279 sdlViewport::getWidth (void) const
281 return SDL_GetVideoSurface ()->w
;
285 sdlViewport::getHeight (void) const
287 return SDL_GetVideoSurface ()->h
;
291 sdlViewport::close (ulong flags
)
295 memset (&event
, 0, sizeof (SDL_Event
));
296 event
.type
= SDL_QUIT
;
297 SDL_PushEvent (&event
);
301 sdlViewport::getViewportData (void)
303 return mpViewportData
;
306 void sdlViewport::mainLoop (void)
309 bool mbRunning
= true;
314 while (SDL_PollEvent(&event
))
316 if (event
.type
== SDL_QUIT
)
321 mpViewportData
->handleEvent (&event
);