aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/CIrrDeviceSDL.cpp
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--libraries/irrlicht-1.8/source/Irrlicht/CIrrDeviceSDL.cpp973
1 files changed, 973 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CIrrDeviceSDL.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CIrrDeviceSDL.cpp
new file mode 100644
index 0000000..68e07e2
--- /dev/null
+++ b/libraries/irrlicht-1.8/source/Irrlicht/CIrrDeviceSDL.cpp
@@ -0,0 +1,973 @@
1// Copyright (C) 2002-2012 Nikolaus Gebhardt
2// This file is part of the "Irrlicht Engine".
3// For conditions of distribution and use, see copyright notice in irrlicht.h
4
5#include "IrrCompileConfig.h"
6
7#ifdef _IRR_COMPILE_WITH_SDL_DEVICE_
8
9#include "CIrrDeviceSDL.h"
10#include "IEventReceiver.h"
11#include "irrList.h"
12#include "os.h"
13#include "CTimer.h"
14#include "irrString.h"
15#include "Keycodes.h"
16#include "COSOperator.h"
17#include <stdio.h>
18#include <stdlib.h>
19#include "SIrrCreationParameters.h"
20#include <SDL/SDL_syswm.h>
21#include <SDL/SDL_video.h>
22
23#ifdef _MSC_VER
24#pragma comment(lib, "SDL.lib")
25#endif // _MSC_VER
26
27namespace irr
28{
29 namespace video
30 {
31
32 #ifdef _IRR_COMPILE_WITH_DIRECT3D_8_
33 IVideoDriver* createDirectX8Driver(const irr::SIrrlichtCreationParameters& params,
34 io::IFileSystem* io, HWND window);
35 #endif
36
37 #ifdef _IRR_COMPILE_WITH_DIRECT3D_9_
38 IVideoDriver* createDirectX9Driver(const irr::SIrrlichtCreationParameters& params,
39 io::IFileSystem* io, HWND window);
40 #endif
41
42 #ifdef _IRR_COMPILE_WITH_OPENGL_
43 IVideoDriver* createOpenGLDriver(const SIrrlichtCreationParameters& params,
44 io::IFileSystem* io, CIrrDeviceSDL* device);
45 #endif
46 } // end namespace video
47
48} // end namespace irr
49
50
51namespace irr
52{
53
54//! constructor
55CIrrDeviceSDL::CIrrDeviceSDL(const SIrrlichtCreationParameters& param)
56 : CIrrDeviceStub(param),
57 Screen((SDL_Surface*)param.WindowId), SDL_Flags(SDL_ANYFORMAT),
58 MouseX(0), MouseY(0), MouseButtonStates(0),
59 Width(param.WindowSize.Width), Height(param.WindowSize.Height),
60 Resizable(false), WindowHasFocus(false), WindowMinimized(false)
61{
62 #ifdef _DEBUG
63 setDebugName("CIrrDeviceSDL");
64 #endif
65
66 // Initialize SDL... Timer for sleep, video for the obvious, and
67 // noparachute prevents SDL from catching fatal errors.
68 if (SDL_Init( SDL_INIT_TIMER|SDL_INIT_VIDEO|
69#if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
70 SDL_INIT_JOYSTICK|
71#endif
72 SDL_INIT_NOPARACHUTE ) < 0)
73 {
74 os::Printer::log( "Unable to initialize SDL!", SDL_GetError());
75 Close = true;
76 }
77
78#if defined(_IRR_WINDOWS_)
79 SDL_putenv("SDL_VIDEODRIVER=directx");
80#elif defined(_IRR_OSX_PLATFORM_)
81 SDL_putenv("SDL_VIDEODRIVER=Quartz");
82#else
83 SDL_putenv("SDL_VIDEODRIVER=x11");
84#endif
85// SDL_putenv("SDL_WINDOWID=");
86
87 SDL_VERSION(&Info.version);
88
89 SDL_GetWMInfo(&Info);
90 core::stringc sdlversion = "SDL Version ";
91 sdlversion += Info.version.major;
92 sdlversion += ".";
93 sdlversion += Info.version.minor;
94 sdlversion += ".";
95 sdlversion += Info.version.patch;
96
97 Operator = new COSOperator(sdlversion);
98 os::Printer::log(sdlversion.c_str(), ELL_INFORMATION);
99
100 // create keymap
101 createKeyMap();
102 // enable key to character translation
103 SDL_EnableUNICODE(1);
104
105 (void)SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
106
107 if ( CreationParams.Fullscreen )
108 SDL_Flags |= SDL_FULLSCREEN;
109 if (CreationParams.DriverType == video::EDT_OPENGL)
110 SDL_Flags |= SDL_OPENGL;
111 else if (CreationParams.Doublebuffer)
112 SDL_Flags |= SDL_DOUBLEBUF;
113 // create window
114 if (CreationParams.DriverType != video::EDT_NULL)
115 {
116 // create the window, only if we do not use the null device
117 createWindow();
118 }
119
120 // create cursor control
121 CursorControl = new CCursorControl(this);
122
123 // create driver
124 createDriver();
125
126 if (VideoDriver)
127 createGUIAndScene();
128}
129
130
131//! destructor
132CIrrDeviceSDL::~CIrrDeviceSDL()
133{
134#if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
135 const u32 numJoysticks = Joysticks.size();
136 for (u32 i=0; i<numJoysticks; ++i)
137 SDL_JoystickClose(Joysticks[i]);
138#endif
139 SDL_Quit();
140}
141
142
143bool CIrrDeviceSDL::createWindow()
144{
145 if ( Close )
146 return false;
147
148 if (CreationParams.DriverType == video::EDT_OPENGL)
149 {
150 if (CreationParams.Bits==16)
151 {
152 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 4 );
153 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 4 );
154 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 4 );
155 SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, CreationParams.WithAlphaChannel?1:0 );
156 }
157 else
158 {
159 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
160 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
161 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
162 SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, CreationParams.WithAlphaChannel?8:0 );
163 }
164 SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, CreationParams.ZBufferBits);
165 if (CreationParams.Doublebuffer)
166 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
167 if (CreationParams.Stereobuffer)
168 SDL_GL_SetAttribute( SDL_GL_STEREO, 1 );
169 if (CreationParams.AntiAlias>1)
170 {
171 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
172 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias );
173 }
174 if ( !Screen )
175 Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );
176 if ( !Screen && CreationParams.AntiAlias>1)
177 {
178 while (--CreationParams.AntiAlias>1)
179 {
180 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, CreationParams.AntiAlias );
181 Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );
182 if (Screen)
183 break;
184 }
185 if ( !Screen )
186 {
187 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 0 );
188 SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, 0 );
189 Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );
190 if (Screen)
191 os::Printer::log("AntiAliasing disabled due to lack of support!" );
192 }
193 }
194 }
195 else if ( !Screen )
196 Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );
197
198 if ( !Screen && CreationParams.Doublebuffer)
199 {
200 // Try single buffer
201 if (CreationParams.DriverType == video::EDT_OPENGL)
202 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
203 SDL_Flags &= ~SDL_DOUBLEBUF;
204 Screen = SDL_SetVideoMode( Width, Height, CreationParams.Bits, SDL_Flags );
205 }
206 if ( !Screen )
207 {
208 os::Printer::log( "Could not initialize display!" );
209 return false;
210 }
211
212 return true;
213}
214
215
216//! create the driver
217void CIrrDeviceSDL::createDriver()
218{
219 switch(CreationParams.DriverType)
220 {
221 case video::EDT_DIRECT3D8:
222 #ifdef _IRR_COMPILE_WITH_DIRECT3D_8_
223
224 VideoDriver = video::createDirectX8Driver(CreationParams, FileSystem, HWnd);
225 if (!VideoDriver)
226 {
227 os::Printer::log("Could not create DIRECT3D8 Driver.", ELL_ERROR);
228 }
229 #else
230 os::Printer::log("DIRECT3D8 Driver was not compiled into this dll. Try another one.", ELL_ERROR);
231 #endif // _IRR_COMPILE_WITH_DIRECT3D_8_
232
233 break;
234
235 case video::EDT_DIRECT3D9:
236 #ifdef _IRR_COMPILE_WITH_DIRECT3D_9_
237
238 VideoDriver = video::createDirectX9Driver(CreationParams, FileSystem, HWnd);
239 if (!VideoDriver)
240 {
241 os::Printer::log("Could not create DIRECT3D9 Driver.", ELL_ERROR);
242 }
243 #else
244 os::Printer::log("DIRECT3D9 Driver was not compiled into this dll. Try another one.", ELL_ERROR);
245 #endif // _IRR_COMPILE_WITH_DIRECT3D_9_
246
247 break;
248
249 case video::EDT_SOFTWARE:
250 #ifdef _IRR_COMPILE_WITH_SOFTWARE_
251 VideoDriver = video::createSoftwareDriver(CreationParams.WindowSize, CreationParams.Fullscreen, FileSystem, this);
252 #else
253 os::Printer::log("No Software driver support compiled in.", ELL_ERROR);
254 #endif
255 break;
256
257 case video::EDT_BURNINGSVIDEO:
258 #ifdef _IRR_COMPILE_WITH_BURNINGSVIDEO_
259 VideoDriver = video::createBurningVideoDriver(CreationParams, FileSystem, this);
260 #else
261 os::Printer::log("Burning's video driver was not compiled in.", ELL_ERROR);
262 #endif
263 break;
264
265 case video::EDT_OPENGL:
266 #ifdef _IRR_COMPILE_WITH_OPENGL_
267 VideoDriver = video::createOpenGLDriver(CreationParams, FileSystem, this);
268 #else
269 os::Printer::log("No OpenGL support compiled in.", ELL_ERROR);
270 #endif
271 break;
272
273 case video::EDT_NULL:
274 VideoDriver = video::createNullDriver(FileSystem, CreationParams.WindowSize);
275 break;
276
277 default:
278 os::Printer::log("Unable to create video driver of unknown type.", ELL_ERROR);
279 break;
280 }
281}
282
283
284//! runs the device. Returns false if device wants to be deleted
285bool CIrrDeviceSDL::run()
286{
287 os::Timer::tick();
288
289 SEvent irrevent;
290 SDL_Event SDL_event;
291
292 while ( !Close && SDL_PollEvent( &SDL_event ) )
293 {
294 switch ( SDL_event.type )
295 {
296 case SDL_MOUSEMOTION:
297 irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
298 irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
299 MouseX = irrevent.MouseInput.X = SDL_event.motion.x;
300 MouseY = irrevent.MouseInput.Y = SDL_event.motion.y;
301 irrevent.MouseInput.ButtonStates = MouseButtonStates;
302
303 postEventFromUser(irrevent);
304 break;
305
306 case SDL_MOUSEBUTTONDOWN:
307 case SDL_MOUSEBUTTONUP:
308
309 irrevent.EventType = irr::EET_MOUSE_INPUT_EVENT;
310 irrevent.MouseInput.X = SDL_event.button.x;
311 irrevent.MouseInput.Y = SDL_event.button.y;
312
313 irrevent.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
314
315 switch(SDL_event.button.button)
316 {
317 case SDL_BUTTON_LEFT:
318 if (SDL_event.type == SDL_MOUSEBUTTONDOWN)
319 {
320 irrevent.MouseInput.Event = irr::EMIE_LMOUSE_PRESSED_DOWN;
321 MouseButtonStates |= irr::EMBSM_LEFT;
322 }
323 else
324 {
325 irrevent.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;
326 MouseButtonStates &= !irr::EMBSM_LEFT;
327 }
328 break;
329
330 case SDL_BUTTON_RIGHT:
331 if (SDL_event.type == SDL_MOUSEBUTTONDOWN)
332 {
333 irrevent.MouseInput.Event = irr::EMIE_RMOUSE_PRESSED_DOWN;
334 MouseButtonStates |= irr::EMBSM_RIGHT;
335 }
336 else
337 {
338 irrevent.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;
339 MouseButtonStates &= !irr::EMBSM_RIGHT;
340 }
341 break;
342
343 case SDL_BUTTON_MIDDLE:
344 if (SDL_event.type == SDL_MOUSEBUTTONDOWN)
345 {
346 irrevent.MouseInput.Event = irr::EMIE_MMOUSE_PRESSED_DOWN;
347 MouseButtonStates |= irr::EMBSM_MIDDLE;
348 }
349 else
350 {
351 irrevent.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;
352 MouseButtonStates &= !irr::EMBSM_MIDDLE;
353 }
354 break;
355
356 case SDL_BUTTON_WHEELUP:
357 irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
358 irrevent.MouseInput.Wheel = 1.0f;
359 break;
360
361 case SDL_BUTTON_WHEELDOWN:
362 irrevent.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;
363 irrevent.MouseInput.Wheel = -1.0f;
364 break;
365 }
366
367 irrevent.MouseInput.ButtonStates = MouseButtonStates;
368
369 if (irrevent.MouseInput.Event != irr::EMIE_MOUSE_MOVED)
370 {
371 postEventFromUser(irrevent);
372
373 if ( irrevent.MouseInput.Event >= EMIE_LMOUSE_PRESSED_DOWN && irrevent.MouseInput.Event <= EMIE_MMOUSE_PRESSED_DOWN )
374 {
375 u32 clicks = checkSuccessiveClicks(irrevent.MouseInput.X, irrevent.MouseInput.Y, irrevent.MouseInput.Event);
376 if ( clicks == 2 )
377 {
378 irrevent.MouseInput.Event = (EMOUSE_INPUT_EVENT)(EMIE_LMOUSE_DOUBLE_CLICK + irrevent.MouseInput.Event-EMIE_LMOUSE_PRESSED_DOWN);
379 postEventFromUser(irrevent);
380 }
381 else if ( clicks == 3 )
382 {
383 irrevent.MouseInput.Event = (EMOUSE_INPUT_EVENT)(EMIE_LMOUSE_TRIPLE_CLICK + irrevent.MouseInput.Event-EMIE_LMOUSE_PRESSED_DOWN);
384 postEventFromUser(irrevent);
385 }
386 }
387 }
388 break;
389
390 case SDL_KEYDOWN:
391 case SDL_KEYUP:
392 {
393 SKeyMap mp;
394 mp.SDLKey = SDL_event.key.keysym.sym;
395 s32 idx = KeyMap.binary_search(mp);
396
397 EKEY_CODE key;
398 if (idx == -1)
399 key = (EKEY_CODE)0;
400 else
401 key = (EKEY_CODE)KeyMap[idx].Win32Key;
402
403#ifdef _IRR_WINDOWS_API_
404 // handle alt+f4 in Windows, because SDL seems not to
405 if ( (SDL_event.key.keysym.mod & KMOD_LALT) && key == KEY_F4)
406 {
407 Close = true;
408 break;
409 }
410#endif
411 irrevent.EventType = irr::EET_KEY_INPUT_EVENT;
412 irrevent.KeyInput.Char = SDL_event.key.keysym.unicode;
413 irrevent.KeyInput.Key = key;
414 irrevent.KeyInput.PressedDown = (SDL_event.type == SDL_KEYDOWN);
415 irrevent.KeyInput.Shift = (SDL_event.key.keysym.mod & KMOD_SHIFT) != 0;
416 irrevent.KeyInput.Control = (SDL_event.key.keysym.mod & KMOD_CTRL ) != 0;
417 postEventFromUser(irrevent);
418 }
419 break;
420
421 case SDL_QUIT:
422 Close = true;
423 break;
424
425 case SDL_ACTIVEEVENT:
426 if ((SDL_event.active.state == SDL_APPMOUSEFOCUS) ||
427 (SDL_event.active.state == SDL_APPINPUTFOCUS))
428 WindowHasFocus = (SDL_event.active.gain==1);
429 else
430 if (SDL_event.active.state == SDL_APPACTIVE)
431 WindowMinimized = (SDL_event.active.gain!=1);
432 break;
433
434 case SDL_VIDEORESIZE:
435 if ((SDL_event.resize.w != (int)Width) || (SDL_event.resize.h != (int)Height))
436 {
437 Width = SDL_event.resize.w;
438 Height = SDL_event.resize.h;
439 Screen = SDL_SetVideoMode( Width, Height, 0, SDL_Flags );
440 if (VideoDriver)
441 VideoDriver->OnResize(core::dimension2d<u32>(Width, Height));
442 }
443 break;
444
445 case SDL_USEREVENT:
446 irrevent.EventType = irr::EET_USER_EVENT;
447 irrevent.UserEvent.UserData1 = *(reinterpret_cast<s32*>(&SDL_event.user.data1));
448 irrevent.UserEvent.UserData2 = *(reinterpret_cast<s32*>(&SDL_event.user.data2));
449
450 postEventFromUser(irrevent);
451 break;
452
453 default:
454 break;
455 } // end switch
456
457 } // end while
458
459#if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
460 // TODO: Check if the multiple open/close calls are too expensive, then
461 // open/close in the constructor/destructor instead
462
463 // update joystick states manually
464 SDL_JoystickUpdate();
465 // we'll always send joystick input events...
466 SEvent joyevent;
467 joyevent.EventType = EET_JOYSTICK_INPUT_EVENT;
468 for (u32 i=0; i<Joysticks.size(); ++i)
469 {
470 SDL_Joystick* joystick = Joysticks[i];
471 if (joystick)
472 {
473 int j;
474 // query all buttons
475 const int numButtons = core::min_(SDL_JoystickNumButtons(joystick), 32);
476 joyevent.JoystickEvent.ButtonStates=0;
477 for (j=0; j<numButtons; ++j)
478 joyevent.JoystickEvent.ButtonStates |= (SDL_JoystickGetButton(joystick, j)<<j);
479
480 // query all axes, already in correct range
481 const int numAxes = core::min_(SDL_JoystickNumAxes(joystick), SEvent::SJoystickEvent::NUMBER_OF_AXES);
482 joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_X]=0;
483 joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Y]=0;
484 joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_Z]=0;
485 joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_R]=0;
486 joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_U]=0;
487 joyevent.JoystickEvent.Axis[SEvent::SJoystickEvent::AXIS_V]=0;
488 for (j=0; j<numAxes; ++j)
489 joyevent.JoystickEvent.Axis[j] = SDL_JoystickGetAxis(joystick, j);
490
491 // we can only query one hat, SDL only supports 8 directions
492 if (SDL_JoystickNumHats(joystick)>0)
493 {
494 switch (SDL_JoystickGetHat(joystick, 0))
495 {
496 case SDL_HAT_UP:
497 joyevent.JoystickEvent.POV=0;
498 break;
499 case SDL_HAT_RIGHTUP:
500 joyevent.JoystickEvent.POV=4500;
501 break;
502 case SDL_HAT_RIGHT:
503 joyevent.JoystickEvent.POV=9000;
504 break;
505 case SDL_HAT_RIGHTDOWN:
506 joyevent.JoystickEvent.POV=13500;
507 break;
508 case SDL_HAT_DOWN:
509 joyevent.JoystickEvent.POV=18000;
510 break;
511 case SDL_HAT_LEFTDOWN:
512 joyevent.JoystickEvent.POV=22500;
513 break;
514 case SDL_HAT_LEFT:
515 joyevent.JoystickEvent.POV=27000;
516 break;
517 case SDL_HAT_LEFTUP:
518 joyevent.JoystickEvent.POV=31500;
519 break;
520 case SDL_HAT_CENTERED:
521 default:
522 joyevent.JoystickEvent.POV=65535;
523 break;
524 }
525 }
526 else
527 {
528 joyevent.JoystickEvent.POV=65535;
529 }
530
531 // we map the number directly
532 joyevent.JoystickEvent.Joystick=static_cast<u8>(i);
533 // now post the event
534 postEventFromUser(joyevent);
535 // and close the joystick
536 }
537 }
538#endif
539 return !Close;
540}
541
542//! Activate any joysticks, and generate events for them.
543bool CIrrDeviceSDL::activateJoysticks(core::array<SJoystickInfo> & joystickInfo)
544{
545#if defined(_IRR_COMPILE_WITH_JOYSTICK_EVENTS_)
546 joystickInfo.clear();
547
548 // we can name up to 256 different joysticks
549 const int numJoysticks = core::min_(SDL_NumJoysticks(), 256);
550 Joysticks.reallocate(numJoysticks);
551 joystickInfo.reallocate(numJoysticks);
552
553 int joystick = 0;
554 for (; joystick<numJoysticks; ++joystick)
555 {
556 Joysticks.push_back(SDL_JoystickOpen(joystick));
557 SJoystickInfo info;
558
559 info.Joystick = joystick;
560 info.Axes = SDL_JoystickNumAxes(Joysticks[joystick]);
561 info.Buttons = SDL_JoystickNumButtons(Joysticks[joystick]);
562 info.Name = SDL_JoystickName(joystick);
563 info.PovHat = (SDL_JoystickNumHats(Joysticks[joystick]) > 0)
564 ? SJoystickInfo::POV_HAT_PRESENT : SJoystickInfo::POV_HAT_ABSENT;
565
566 joystickInfo.push_back(info);
567 }
568
569 for(joystick = 0; joystick < (int)joystickInfo.size(); ++joystick)
570 {
571 char logString[256];
572 (void)sprintf(logString, "Found joystick %d, %d axes, %d buttons '%s'",
573 joystick, joystickInfo[joystick].Axes,
574 joystickInfo[joystick].Buttons, joystickInfo[joystick].Name.c_str());
575 os::Printer::log(logString, ELL_INFORMATION);
576 }
577
578 return true;
579
580#endif // _IRR_COMPILE_WITH_JOYSTICK_EVENTS_
581
582 return false;
583}
584
585
586
587//! pause execution temporarily
588void CIrrDeviceSDL::yield()
589{
590 SDL_Delay(0);
591}
592
593
594//! pause execution for a specified time
595void CIrrDeviceSDL::sleep(u32 timeMs, bool pauseTimer)
596{
597 const bool wasStopped = Timer ? Timer->isStopped() : true;
598 if (pauseTimer && !wasStopped)
599 Timer->stop();
600
601 SDL_Delay(timeMs);
602
603 if (pauseTimer && !wasStopped)
604 Timer->start();
605}
606
607
608//! sets the caption of the window
609void CIrrDeviceSDL::setWindowCaption(const wchar_t* text)
610{
611 core::stringc textc = text;
612 SDL_WM_SetCaption( textc.c_str( ), textc.c_str( ) );
613}
614
615
616//! presents a surface in the client area
617bool CIrrDeviceSDL::present(video::IImage* surface, void* windowId, core::rect<s32>* srcClip)
618{
619 SDL_Surface *sdlSurface = SDL_CreateRGBSurfaceFrom(
620 surface->lock(), surface->getDimension().Width, surface->getDimension().Height,
621 surface->getBitsPerPixel(), surface->getPitch(),
622 surface->getRedMask(), surface->getGreenMask(), surface->getBlueMask(), surface->getAlphaMask());
623 if (!sdlSurface)
624 return false;
625 SDL_SetAlpha(sdlSurface, 0, 0);
626 SDL_SetColorKey(sdlSurface, 0, 0);
627 sdlSurface->format->BitsPerPixel=surface->getBitsPerPixel();
628 sdlSurface->format->BytesPerPixel=surface->getBytesPerPixel();
629 if ((surface->getColorFormat()==video::ECF_R8G8B8) ||
630 (surface->getColorFormat()==video::ECF_A8R8G8B8))
631 {
632 sdlSurface->format->Rloss=0;
633 sdlSurface->format->Gloss=0;
634 sdlSurface->format->Bloss=0;
635 sdlSurface->format->Rshift=16;
636 sdlSurface->format->Gshift=8;
637 sdlSurface->format->Bshift=0;
638 if (surface->getColorFormat()==video::ECF_R8G8B8)
639 {
640 sdlSurface->format->Aloss=8;
641 sdlSurface->format->Ashift=32;
642 }
643 else
644 {
645 sdlSurface->format->Aloss=0;
646 sdlSurface->format->Ashift=24;
647 }
648 }
649 else if (surface->getColorFormat()==video::ECF_R5G6B5)
650 {
651 sdlSurface->format->Rloss=3;
652 sdlSurface->format->Gloss=2;
653 sdlSurface->format->Bloss=3;
654 sdlSurface->format->Aloss=8;
655 sdlSurface->format->Rshift=11;
656 sdlSurface->format->Gshift=5;
657 sdlSurface->format->Bshift=0;
658 sdlSurface->format->Ashift=16;
659 }
660 else if (surface->getColorFormat()==video::ECF_A1R5G5B5)
661 {
662 sdlSurface->format->Rloss=3;
663 sdlSurface->format->Gloss=3;
664 sdlSurface->format->Bloss=3;
665 sdlSurface->format->Aloss=7;
666 sdlSurface->format->Rshift=10;
667 sdlSurface->format->Gshift=5;
668 sdlSurface->format->Bshift=0;
669 sdlSurface->format->Ashift=15;
670 }
671
672 SDL_Surface* scr = (SDL_Surface* )windowId;
673 if (!scr)
674 scr = Screen;
675 if (scr)
676 {
677 if (srcClip)
678 {
679 SDL_Rect sdlsrcClip;
680 sdlsrcClip.x = srcClip->UpperLeftCorner.X;
681 sdlsrcClip.y = srcClip->UpperLeftCorner.Y;
682 sdlsrcClip.w = srcClip->getWidth();
683 sdlsrcClip.h = srcClip->getHeight();
684 SDL_BlitSurface(sdlSurface, &sdlsrcClip, scr, NULL);
685 }
686 else
687 SDL_BlitSurface(sdlSurface, NULL, scr, NULL);
688 SDL_Flip(scr);
689 }
690
691 SDL_FreeSurface(sdlSurface);
692 surface->unlock();
693 return (scr != 0);
694}
695
696
697//! notifies the device that it should close itself
698void CIrrDeviceSDL::closeDevice()
699{
700 Close = true;
701}
702
703
704//! \return Pointer to a list with all video modes supported
705video::IVideoModeList* CIrrDeviceSDL::getVideoModeList()
706{
707 if (!VideoModeList.getVideoModeCount())
708 {
709 // enumerate video modes.
710 const SDL_VideoInfo *vi = SDL_GetVideoInfo();
711 SDL_Rect **modes = SDL_ListModes(vi->vfmt, SDL_Flags);
712 if (modes != 0)
713 {
714 if (modes == (SDL_Rect **)-1)
715 os::Printer::log("All modes available.\n");
716 else
717 {
718 for (u32 i=0; modes[i]; ++i)
719 VideoModeList.addMode(core::dimension2d<u32>(modes[i]->w, modes[i]->h), vi->vfmt->BitsPerPixel);
720 }
721 }
722 }
723
724 return &VideoModeList;
725}
726
727
728//! Sets if the window should be resizable in windowed mode.
729void CIrrDeviceSDL::setResizable(bool resize)
730{
731 if (resize != Resizable)
732 {
733 if (resize)
734 SDL_Flags |= SDL_RESIZABLE;
735 else
736 SDL_Flags &= ~SDL_RESIZABLE;
737 Screen = SDL_SetVideoMode( 0, 0, 0, SDL_Flags );
738 Resizable = resize;
739 }
740}
741
742
743//! Minimizes window if possible
744void CIrrDeviceSDL::minimizeWindow()
745{
746 SDL_WM_IconifyWindow();
747}
748
749
750//! Maximize window
751void CIrrDeviceSDL::maximizeWindow()
752{
753 // do nothing
754}
755
756
757//! Restore original window size
758void CIrrDeviceSDL::restoreWindow()
759{
760 // do nothing
761}
762
763
764//! returns if window is active. if not, nothing need to be drawn
765bool CIrrDeviceSDL::isWindowActive() const
766{
767 return (WindowHasFocus && !WindowMinimized);
768}
769
770
771//! returns if window has focus.
772bool CIrrDeviceSDL::isWindowFocused() const
773{
774 return WindowHasFocus;
775}
776
777
778//! returns if window is minimized.
779bool CIrrDeviceSDL::isWindowMinimized() const
780{
781 return WindowMinimized;
782}
783
784
785//! Set the current Gamma Value for the Display
786bool CIrrDeviceSDL::setGammaRamp( f32 red, f32 green, f32 blue, f32 brightness, f32 contrast )
787{
788 /*
789 // todo: Gamma in SDL takes ints, what does Irrlicht use?
790 return (SDL_SetGamma(red, green, blue) != -1);
791 */
792 return false;
793}
794
795//! Get the current Gamma Value for the Display
796bool CIrrDeviceSDL::getGammaRamp( f32 &red, f32 &green, f32 &blue, f32 &brightness, f32 &contrast )
797{
798/* brightness = 0.f;
799 contrast = 0.f;
800 return (SDL_GetGamma(&red, &green, &blue) != -1);*/
801 return false;
802}
803
804//! returns color format of the window.
805video::ECOLOR_FORMAT CIrrDeviceSDL::getColorFormat() const
806{
807 if (Screen)
808 {
809 if (Screen->format->BitsPerPixel==16)
810 {
811 if (Screen->format->Amask != 0)
812 return video::ECF_A1R5G5B5;
813 else
814 return video::ECF_R5G6B5;
815 }
816 else
817 {
818 if (Screen->format->Amask != 0)
819 return video::ECF_A8R8G8B8;
820 else
821 return video::ECF_R8G8B8;
822 }
823 }
824 else
825 return CIrrDeviceStub::getColorFormat();
826}
827
828
829void CIrrDeviceSDL::createKeyMap()
830{
831 // I don't know if this is the best method to create
832 // the lookuptable, but I'll leave it like that until
833 // I find a better version.
834
835 KeyMap.reallocate(105);
836
837 // buttons missing
838
839 KeyMap.push_back(SKeyMap(SDLK_BACKSPACE, KEY_BACK));
840 KeyMap.push_back(SKeyMap(SDLK_TAB, KEY_TAB));
841 KeyMap.push_back(SKeyMap(SDLK_CLEAR, KEY_CLEAR));
842 KeyMap.push_back(SKeyMap(SDLK_RETURN, KEY_RETURN));
843
844 // combined modifiers missing
845
846 KeyMap.push_back(SKeyMap(SDLK_PAUSE, KEY_PAUSE));
847 KeyMap.push_back(SKeyMap(SDLK_CAPSLOCK, KEY_CAPITAL));
848
849 // asian letter keys missing
850
851 KeyMap.push_back(SKeyMap(SDLK_ESCAPE, KEY_ESCAPE));
852
853 // asian letter keys missing
854
855 KeyMap.push_back(SKeyMap(SDLK_SPACE, KEY_SPACE));
856 KeyMap.push_back(SKeyMap(SDLK_PAGEUP, KEY_PRIOR));
857 KeyMap.push_back(SKeyMap(SDLK_PAGEDOWN, KEY_NEXT));
858 KeyMap.push_back(SKeyMap(SDLK_END, KEY_END));
859 KeyMap.push_back(SKeyMap(SDLK_HOME, KEY_HOME));
860 KeyMap.push_back(SKeyMap(SDLK_LEFT, KEY_LEFT));
861 KeyMap.push_back(SKeyMap(SDLK_UP, KEY_UP));
862 KeyMap.push_back(SKeyMap(SDLK_RIGHT, KEY_RIGHT));
863 KeyMap.push_back(SKeyMap(SDLK_DOWN, KEY_DOWN));
864
865 // select missing
866 KeyMap.push_back(SKeyMap(SDLK_PRINT, KEY_PRINT));
867 // execute missing
868 KeyMap.push_back(SKeyMap(SDLK_PRINT, KEY_SNAPSHOT));
869
870 KeyMap.push_back(SKeyMap(SDLK_INSERT, KEY_INSERT));
871 KeyMap.push_back(SKeyMap(SDLK_DELETE, KEY_DELETE));
872 KeyMap.push_back(SKeyMap(SDLK_HELP, KEY_HELP));
873
874 KeyMap.push_back(SKeyMap(SDLK_0, KEY_KEY_0));
875 KeyMap.push_back(SKeyMap(SDLK_1, KEY_KEY_1));
876 KeyMap.push_back(SKeyMap(SDLK_2, KEY_KEY_2));
877 KeyMap.push_back(SKeyMap(SDLK_3, KEY_KEY_3));
878 KeyMap.push_back(SKeyMap(SDLK_4, KEY_KEY_4));
879 KeyMap.push_back(SKeyMap(SDLK_5, KEY_KEY_5));
880 KeyMap.push_back(SKeyMap(SDLK_6, KEY_KEY_6));
881 KeyMap.push_back(SKeyMap(SDLK_7, KEY_KEY_7));
882 KeyMap.push_back(SKeyMap(SDLK_8, KEY_KEY_8));
883 KeyMap.push_back(SKeyMap(SDLK_9, KEY_KEY_9));
884
885 KeyMap.push_back(SKeyMap(SDLK_a, KEY_KEY_A));
886 KeyMap.push_back(SKeyMap(SDLK_b, KEY_KEY_B));
887 KeyMap.push_back(SKeyMap(SDLK_c, KEY_KEY_C));
888 KeyMap.push_back(SKeyMap(SDLK_d, KEY_KEY_D));
889 KeyMap.push_back(SKeyMap(SDLK_e, KEY_KEY_E));
890 KeyMap.push_back(SKeyMap(SDLK_f, KEY_KEY_F));
891 KeyMap.push_back(SKeyMap(SDLK_g, KEY_KEY_G));
892 KeyMap.push_back(SKeyMap(SDLK_h, KEY_KEY_H));
893 KeyMap.push_back(SKeyMap(SDLK_i, KEY_KEY_I));
894 KeyMap.push_back(SKeyMap(SDLK_j, KEY_KEY_J));
895 KeyMap.push_back(SKeyMap(SDLK_k, KEY_KEY_K));
896 KeyMap.push_back(SKeyMap(SDLK_l, KEY_KEY_L));
897 KeyMap.push_back(SKeyMap(SDLK_m, KEY_KEY_M));
898 KeyMap.push_back(SKeyMap(SDLK_n, KEY_KEY_N));
899 KeyMap.push_back(SKeyMap(SDLK_o, KEY_KEY_O));
900 KeyMap.push_back(SKeyMap(SDLK_p, KEY_KEY_P));
901 KeyMap.push_back(SKeyMap(SDLK_q, KEY_KEY_Q));
902 KeyMap.push_back(SKeyMap(SDLK_r, KEY_KEY_R));
903 KeyMap.push_back(SKeyMap(SDLK_s, KEY_KEY_S));
904 KeyMap.push_back(SKeyMap(SDLK_t, KEY_KEY_T));
905 KeyMap.push_back(SKeyMap(SDLK_u, KEY_KEY_U));
906 KeyMap.push_back(SKeyMap(SDLK_v, KEY_KEY_V));
907 KeyMap.push_back(SKeyMap(SDLK_w, KEY_KEY_W));
908 KeyMap.push_back(SKeyMap(SDLK_x, KEY_KEY_X));
909 KeyMap.push_back(SKeyMap(SDLK_y, KEY_KEY_Y));
910 KeyMap.push_back(SKeyMap(SDLK_z, KEY_KEY_Z));
911
912 KeyMap.push_back(SKeyMap(SDLK_LSUPER, KEY_LWIN));
913 KeyMap.push_back(SKeyMap(SDLK_RSUPER, KEY_RWIN));
914 // apps missing
915 KeyMap.push_back(SKeyMap(SDLK_POWER, KEY_SLEEP)); //??
916
917 KeyMap.push_back(SKeyMap(SDLK_KP0, KEY_NUMPAD0));
918 KeyMap.push_back(SKeyMap(SDLK_KP1, KEY_NUMPAD1));
919 KeyMap.push_back(SKeyMap(SDLK_KP2, KEY_NUMPAD2));
920 KeyMap.push_back(SKeyMap(SDLK_KP3, KEY_NUMPAD3));
921 KeyMap.push_back(SKeyMap(SDLK_KP4, KEY_NUMPAD4));
922 KeyMap.push_back(SKeyMap(SDLK_KP5, KEY_NUMPAD5));
923 KeyMap.push_back(SKeyMap(SDLK_KP6, KEY_NUMPAD6));
924 KeyMap.push_back(SKeyMap(SDLK_KP7, KEY_NUMPAD7));
925 KeyMap.push_back(SKeyMap(SDLK_KP8, KEY_NUMPAD8));
926 KeyMap.push_back(SKeyMap(SDLK_KP9, KEY_NUMPAD9));
927 KeyMap.push_back(SKeyMap(SDLK_KP_MULTIPLY, KEY_MULTIPLY));
928 KeyMap.push_back(SKeyMap(SDLK_KP_PLUS, KEY_ADD));
929// KeyMap.push_back(SKeyMap(SDLK_KP_, KEY_SEPARATOR));
930 KeyMap.push_back(SKeyMap(SDLK_KP_MINUS, KEY_SUBTRACT));
931 KeyMap.push_back(SKeyMap(SDLK_KP_PERIOD, KEY_DECIMAL));
932 KeyMap.push_back(SKeyMap(SDLK_KP_DIVIDE, KEY_DIVIDE));
933
934 KeyMap.push_back(SKeyMap(SDLK_F1, KEY_F1));
935 KeyMap.push_back(SKeyMap(SDLK_F2, KEY_F2));
936 KeyMap.push_back(SKeyMap(SDLK_F3, KEY_F3));
937 KeyMap.push_back(SKeyMap(SDLK_F4, KEY_F4));
938 KeyMap.push_back(SKeyMap(SDLK_F5, KEY_F5));
939 KeyMap.push_back(SKeyMap(SDLK_F6, KEY_F6));
940 KeyMap.push_back(SKeyMap(SDLK_F7, KEY_F7));
941 KeyMap.push_back(SKeyMap(SDLK_F8, KEY_F8));
942 KeyMap.push_back(SKeyMap(SDLK_F9, KEY_F9));
943 KeyMap.push_back(SKeyMap(SDLK_F10, KEY_F10));
944 KeyMap.push_back(SKeyMap(SDLK_F11, KEY_F11));
945 KeyMap.push_back(SKeyMap(SDLK_F12, KEY_F12));
946 KeyMap.push_back(SKeyMap(SDLK_F13, KEY_F13));
947 KeyMap.push_back(SKeyMap(SDLK_F14, KEY_F14));
948 KeyMap.push_back(SKeyMap(SDLK_F15, KEY_F15));
949 // no higher F-keys
950
951 KeyMap.push_back(SKeyMap(SDLK_NUMLOCK, KEY_NUMLOCK));
952 KeyMap.push_back(SKeyMap(SDLK_SCROLLOCK, KEY_SCROLL));
953 KeyMap.push_back(SKeyMap(SDLK_LSHIFT, KEY_LSHIFT));
954 KeyMap.push_back(SKeyMap(SDLK_RSHIFT, KEY_RSHIFT));
955 KeyMap.push_back(SKeyMap(SDLK_LCTRL, KEY_LCONTROL));
956 KeyMap.push_back(SKeyMap(SDLK_RCTRL, KEY_RCONTROL));
957 KeyMap.push_back(SKeyMap(SDLK_LALT, KEY_LMENU));
958 KeyMap.push_back(SKeyMap(SDLK_RALT, KEY_RMENU));
959
960 KeyMap.push_back(SKeyMap(SDLK_PLUS, KEY_PLUS));
961 KeyMap.push_back(SKeyMap(SDLK_COMMA, KEY_COMMA));
962 KeyMap.push_back(SKeyMap(SDLK_MINUS, KEY_MINUS));
963 KeyMap.push_back(SKeyMap(SDLK_PERIOD, KEY_PERIOD));
964
965 // some special keys missing
966
967 KeyMap.sort();
968}
969
970} // end namespace irr
971
972#endif // _IRR_COMPILE_WITH_SDL_DEVICE_
973