sdlWindowMgr.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "windowManager/sdl/sdlWindowMgr.h"
  23. #include "gfx/gfxDevice.h"
  24. #include "core/util/journal/process.h"
  25. #include "core/strings/unicode.h"
  26. #include "SDL.h"
  27. // ------------------------------------------------------------------------
  28. void sdl_CloseSplashWindow(void* hinst);
  29. #ifdef TORQUE_SDL
  30. PlatformWindowManager * CreatePlatformWindowManager()
  31. {
  32. return new PlatformWindowManagerSDL();
  33. }
  34. #endif
  35. // ------------------------------------------------------------------------
  36. PlatformWindowManagerSDL::PlatformWindowManagerSDL()
  37. {
  38. // Register in the process list.
  39. mOnProcessSignalSlot.setDelegate( this, &PlatformWindowManagerSDL::_process );
  40. Process::notify( mOnProcessSignalSlot, PROCESS_INPUT_ORDER );
  41. // Init our list of allocated windows.
  42. mWindowListHead = NULL;
  43. // By default, we have no parent window.
  44. mParentWindow = NULL;
  45. mCurtainWindow = NULL;
  46. mDisplayWindow = true;
  47. mOffscreenRender = false;
  48. buildMonitorsList();
  49. }
  50. PlatformWindowManagerSDL::~PlatformWindowManagerSDL()
  51. {
  52. // Kill all our windows first.
  53. while(mWindowListHead)
  54. // The destructors update the list, so this works just fine.
  55. delete mWindowListHead;
  56. }
  57. RectI PlatformWindowManagerSDL::getPrimaryDesktopArea()
  58. {
  59. // TODO SDL
  60. AssertFatal(0, "");
  61. return RectI(0,0,0,0);
  62. }
  63. Point2I PlatformWindowManagerSDL::getDesktopResolution()
  64. {
  65. SDL_DisplayMode mode;
  66. SDL_GetDesktopDisplayMode(0, &mode);
  67. // Return Resolution
  68. return Point2I(mode.w, mode.h);
  69. }
  70. S32 PlatformWindowManagerSDL::getDesktopBitDepth()
  71. {
  72. // Return Bits per Pixel
  73. SDL_DisplayMode mode;
  74. SDL_GetDesktopDisplayMode(0, &mode);
  75. int bbp;
  76. unsigned int r,g,b,a;
  77. SDL_PixelFormatEnumToMasks(mode.format, &bbp, &r, &g, &b, &a);
  78. return bbp;
  79. }
  80. void PlatformWindowManagerSDL::buildMonitorsList()
  81. {
  82. // TODO SDL
  83. }
  84. S32 PlatformWindowManagerSDL::findFirstMatchingMonitor(const char* name)
  85. {
  86. /// TODO SDL
  87. AssertFatal(0, "");
  88. return 0;
  89. }
  90. U32 PlatformWindowManagerSDL::getMonitorCount()
  91. {
  92. // TODO SDL
  93. AssertFatal(0, "");
  94. return 1;
  95. }
  96. const char* PlatformWindowManagerSDL::getMonitorName(U32 index)
  97. {
  98. // TODO SDL
  99. AssertFatal(0, "");
  100. return "Monitor";
  101. }
  102. RectI PlatformWindowManagerSDL::getMonitorRect(U32 index)
  103. {
  104. // TODO SDL
  105. AssertFatal(0, "");
  106. return RectI(0, 0, 0,0 );
  107. }
  108. void PlatformWindowManagerSDL::getMonitorRegions(Vector<RectI> &regions)
  109. {
  110. // TODO SDL
  111. AssertFatal(0, "");
  112. }
  113. void PlatformWindowManagerSDL::getWindows(VectorPtr<PlatformWindow*> &windows)
  114. {
  115. PlatformWindowSDL *win = mWindowListHead;
  116. while(win)
  117. {
  118. windows.push_back(win);
  119. win = win->mNextWindow;
  120. }
  121. }
  122. PlatformWindow *PlatformWindowManagerSDL::createWindow(GFXDevice *device, const GFXVideoMode &mode)
  123. {
  124. // Do the allocation.
  125. PlatformWindowSDL *window = new PlatformWindowSDL();
  126. U32 windowFlags = /*SDL_WINDOW_SHOWN |*/ SDL_WINDOW_RESIZABLE;
  127. if(GFX->getAdapterType() == OpenGL)
  128. windowFlags |= SDL_WINDOW_OPENGL;
  129. window->mWindowHandle = SDL_CreateWindow("", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, mode.resolution.x, mode.resolution.y, windowFlags );
  130. window->mWindowId = SDL_GetWindowID( window->mWindowHandle );
  131. window->mOwningManager = this;
  132. mWindowMap[ window->mWindowId ] = window;
  133. if(device)
  134. {
  135. window->mDevice = device;
  136. window->mTarget = device->allocWindowTarget(window);
  137. AssertISV(window->mTarget, "PlatformWindowManagerSDL::createWindow - failed to get a window target back from the device.");
  138. }
  139. else
  140. {
  141. Con::warnf("PlatformWindowManagerSDL::createWindow - created a window with no device!");
  142. }
  143. linkWindow(window);
  144. return window;
  145. }
  146. void PlatformWindowManagerSDL::setParentWindow(void* newParent)
  147. {
  148. }
  149. void* PlatformWindowManagerSDL::getParentWindow()
  150. {
  151. return NULL;
  152. }
  153. void PlatformWindowManagerSDL::_process()
  154. {
  155. SDL_Event evt;
  156. while( SDL_PollEvent(&evt) )
  157. {
  158. switch(evt.type)
  159. {
  160. case SDL_QUIT:
  161. {
  162. PlatformWindowSDL *window = static_cast<PlatformWindowSDL*>( getFirstWindow() );
  163. if(window)
  164. window->appEvent.trigger( window->getWindowId(), WindowClose );
  165. break;
  166. }
  167. case SDL_KEYDOWN:
  168. case SDL_KEYUP:
  169. {
  170. PlatformWindowSDL *window = mWindowMap[evt.key.windowID];
  171. if(window)
  172. window->_processSDLEvent(evt);
  173. break;
  174. }
  175. case SDL_MOUSEWHEEL:
  176. {
  177. PlatformWindowSDL *window = mWindowMap[evt.wheel.windowID];
  178. if (window)
  179. window->_processSDLEvent(evt);
  180. break;
  181. }
  182. case SDL_MOUSEMOTION:
  183. {
  184. PlatformWindowSDL *window = mWindowMap[evt.motion.windowID];
  185. if(window)
  186. window->_processSDLEvent(evt);
  187. break;
  188. }
  189. case SDL_MOUSEBUTTONDOWN:
  190. case SDL_MOUSEBUTTONUP:
  191. {
  192. PlatformWindowSDL *window = mWindowMap[evt.button.windowID];
  193. if(window)
  194. window->_processSDLEvent(evt);
  195. break;
  196. }
  197. case SDL_TEXTINPUT:
  198. {
  199. PlatformWindowSDL *window = mWindowMap[evt.text.windowID];
  200. if(window)
  201. window->_processSDLEvent(evt);
  202. break;
  203. }
  204. case SDL_WINDOWEVENT:
  205. {
  206. PlatformWindowSDL *window = mWindowMap[evt.window.windowID];
  207. if(window)
  208. window->_processSDLEvent(evt);
  209. break;
  210. }
  211. default:
  212. {
  213. //Con::printf("Event: %d", evt.type);
  214. }
  215. }
  216. }
  217. }
  218. PlatformWindow * PlatformWindowManagerSDL::getWindowById( WindowId id )
  219. {
  220. // Walk the list and find the matching id, if any.
  221. PlatformWindowSDL *win = mWindowListHead;
  222. while(win)
  223. {
  224. if(win->getWindowId() == id)
  225. return win;
  226. win = win->mNextWindow;
  227. }
  228. return NULL;
  229. }
  230. PlatformWindow * PlatformWindowManagerSDL::getFirstWindow()
  231. {
  232. return mWindowListHead != NULL ? mWindowListHead : NULL;
  233. }
  234. PlatformWindow* PlatformWindowManagerSDL::getFocusedWindow()
  235. {
  236. PlatformWindowSDL* window = mWindowListHead;
  237. while( window )
  238. {
  239. if( window->isFocused() )
  240. return window;
  241. window = window->mNextWindow;
  242. }
  243. return NULL;
  244. }
  245. void PlatformWindowManagerSDL::linkWindow( PlatformWindowSDL *w )
  246. {
  247. w->mNextWindow = mWindowListHead;
  248. mWindowListHead = w;
  249. }
  250. void PlatformWindowManagerSDL::unlinkWindow( PlatformWindowSDL *w )
  251. {
  252. PlatformWindowSDL **walk = &mWindowListHead;
  253. while(*walk)
  254. {
  255. if(*walk != w)
  256. {
  257. // Advance to next item in list.
  258. walk = &(*walk)->mNextWindow;
  259. continue;
  260. }
  261. // Got a match - unlink and return.
  262. *walk = (*walk)->mNextWindow;
  263. return;
  264. }
  265. }
  266. void PlatformWindowManagerSDL::_processCmdLineArgs( const S32 argc, const char **argv )
  267. {
  268. // TODO SDL
  269. }
  270. void PlatformWindowManagerSDL::lowerCurtain()
  271. {
  272. if(mCurtainWindow)
  273. return;
  274. // TODO SDL
  275. }
  276. void PlatformWindowManagerSDL::raiseCurtain()
  277. {
  278. if(!mCurtainWindow)
  279. return;
  280. // TODO SDL
  281. }
  282. bool Platform::closeSplashWindow()
  283. {
  284. return true;
  285. }
  286. void Platform::openFolder(const char* path )
  287. {
  288. AssertFatal(0, "Not Implemented");
  289. }
  290. void Platform::openFile(const char* path )
  291. {
  292. AssertFatal(0, "Not Implemented");
  293. }
  294. //------------------------------------------------------------------------------
  295. namespace GL
  296. {
  297. void gglPerformBinds();
  298. }
  299. void InitWindowingSystem()
  300. {
  301. }
  302. AFTER_MODULE_INIT(gfx)
  303. {
  304. int res = SDL_Init( SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER | SDL_INIT_EVENTS | SDL_INIT_NOPARACHUTE );
  305. AssertFatal(res != -1, "SDL init error");
  306. }