removed tchar.h usage
[fegdk.git] / core / code / video / sdl / f_sdlviewport.cpp
blobf9ceeb89a3508efabb6c5164a79134100e7d19f6
1 /*
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
19 Alexey Yakovenko
20 waker@users.sourceforge.net
23 #include "pch.h"
24 #include "config.h"
26 #include "f_sdlviewport.h"
27 #include "f_sdlviewportdata.h"
28 #include "f_helpers.h"
29 #include "f_engine.h"
30 #include "f_input.h"
31 #include "f_application.h"
32 #include <iostream>
33 #include <SDL/SDL.h>
34 #include "cvars.h"
36 namespace fe
39 // table to translate sdl keycodes into native fe codes
40 int sdl_to_key[] = {
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
67 // keypad
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,
78 // functional keys
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,
82 // unknown codes
83 0, 0, 0,
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
90 // viewport data
91 sdlViewportData::sdlViewportData (void)
95 sdlViewportData::~sdlViewportData (void)
97 SDL_Quit ();
100 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);
110 void
111 sdlViewportData::handleEvent (SDL_Event *event)
113 switch (event->type)
115 case SDL_MOUSEMOTION:
116 onMouseMove (event->motion.x, event->motion.y);
117 break;
118 case SDL_MOUSEBUTTONDOWN:
119 onMouseBtnDown (event->button.button);
120 break;
121 case SDL_MOUSEBUTTONUP:
122 onMouseBtnUp (event->button.button);
123 break;
124 case SDL_KEYDOWN:
125 onKeyDown (event->key.keysym.sym, event->key.keysym.unicode);
126 break;
127 case SDL_KEYUP:
128 onKeyUp (event->key.keysym.sym);
129 break;
133 void
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);
141 void
142 sdlViewportData::onMouseBtnDown (int sdl_btn)
144 smartPtr <inputDevice> mouse = g_engine->getInputDevice ();
146 switch (sdl_btn)
148 case SDL_BUTTON_WHEELUP:
149 g_engine->getApplication ()->mouseWheel (1);
150 break;
151 case SDL_BUTTON_WHEELDOWN:
152 g_engine->getApplication ()->mouseWheel (-1);
153 break;
154 case SDL_BUTTON_LEFT:
155 mouse->lButtonDown ();
156 g_engine->getApplication ()->mouseDown (Key_Mouse1);
157 break;
158 case SDL_BUTTON_RIGHT:
159 mouse->rButtonDown ();
160 g_engine->getApplication ()->mouseDown (Key_Mouse2);
161 break;
162 case SDL_BUTTON_MIDDLE:
163 mouse->mButtonDown ();
164 g_engine->getApplication ()->mouseDown (Key_Mouse3);
165 break;
169 void
170 sdlViewportData::onMouseBtnUp (int sdl_btn)
172 smartPtr <inputDevice> mouse = g_engine->getInputDevice ();
174 switch (sdl_btn)
176 case SDL_BUTTON_LEFT:
177 mouse->lButtonUp ();
178 g_engine->getApplication ()->mouseUp (Key_Mouse1);
179 break;
180 case SDL_BUTTON_RIGHT:
181 mouse->rButtonUp ();
182 g_engine->getApplication ()->mouseUp (Key_Mouse2);
183 break;
184 case SDL_BUTTON_MIDDLE:
185 mouse->mButtonUp ();
186 g_engine->getApplication ()->mouseUp (Key_Mouse3);
187 break;
191 void
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
201 && unicode >= 0x20)
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);
213 else
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]);
221 void
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);
228 else
229 keyb->keyUp (sdl_to_key[sdl_key]);
232 // viewport base
234 sdlViewport::sdlViewport (void)
236 mpViewportData = new sdlViewportData ();
237 init (g_engine->getApplication ()->getTitle ());
240 sdlViewport::~sdlViewport (void)
242 if (mpViewportData)
244 delete mpViewportData;
245 mpViewportData = NULL;
249 void
250 sdlViewport::init (const char *appName)
252 mpViewportData->init (appName);
255 // common window access
256 void
257 sdlViewport::setPosition (int x, int y)
261 void
262 sdlViewport::setSize (int w, int h)
267 sdlViewport::getLeft (void) const
269 return 0;
273 sdlViewport::getTop (void) const
275 return 0;
279 sdlViewport::getWidth (void) const
281 return SDL_GetVideoSurface ()->w;
285 sdlViewport::getHeight (void) const
287 return SDL_GetVideoSurface ()->h;
290 void
291 sdlViewport::close (ulong flags)
293 // emulate SDL_QUIT
294 SDL_Event event;
295 memset (&event, 0, sizeof (SDL_Event));
296 event.type = SDL_QUIT;
297 SDL_PushEvent (&event);
300 sdlViewportData*
301 sdlViewport::getViewportData (void)
303 return mpViewportData;
306 void sdlViewport::mainLoop (void)
308 // run system loop
309 bool mbRunning = true;
310 while (mbRunning)
312 SDL_Event event;
314 while (SDL_PollEvent(&event))
316 if (event.type == SDL_QUIT)
318 mbRunning = false;
319 break;
321 mpViewportData->handleEvent (&event);
324 if (mbRunning)
325 g_engine->tick();