CIrrDeviceSDL.cpp 28 KB

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