Window.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421
  1. /**
  2. * Copyright (c) 2006-2022 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. // LOVE
  21. #include "common/config.h"
  22. #include "graphics/Graphics.h"
  23. #include "Window.h"
  24. #ifdef LOVE_ANDROID
  25. #include "common/android.h"
  26. #endif
  27. #ifdef LOVE_IOS
  28. #include "common/ios.h"
  29. #endif
  30. // C++
  31. #include <iostream>
  32. #include <vector>
  33. #include <algorithm>
  34. // C
  35. #include <cstdio>
  36. // SDL
  37. #include <SDL_syswm.h>
  38. #if defined(LOVE_WINDOWS)
  39. #include <windows.h>
  40. #elif defined(LOVE_MACOS)
  41. #include "common/macos.h"
  42. #endif
  43. #ifndef APIENTRY
  44. #define APIENTRY
  45. #endif
  46. namespace love
  47. {
  48. namespace window
  49. {
  50. namespace sdl
  51. {
  52. Window::Window()
  53. : open(false)
  54. , mouseGrabbed(false)
  55. , window(nullptr)
  56. , glcontext(nullptr)
  57. #ifdef LOVE_GRAPHICS_METAL
  58. , metalView(nullptr)
  59. #endif
  60. , displayedWindowError(false)
  61. , hasSDL203orEarlier(false)
  62. , contextAttribs()
  63. {
  64. if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
  65. throw love::Exception("Could not initialize SDL video subsystem (%s)", SDL_GetError());
  66. // Make sure the screensaver doesn't activate by default.
  67. setDisplaySleepEnabled(false);
  68. SDL_version version = {};
  69. SDL_GetVersion(&version);
  70. hasSDL203orEarlier = (version.major == 2 && version.minor == 0 && version.patch <= 3);
  71. }
  72. Window::~Window()
  73. {
  74. close(false);
  75. graphics.set(nullptr);
  76. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  77. }
  78. void Window::setGraphics(graphics::Graphics *graphics)
  79. {
  80. this->graphics.set(graphics);
  81. }
  82. void Window::setGLFramebufferAttributes(bool sRGB)
  83. {
  84. // Set GL window / framebuffer attributes.
  85. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
  86. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
  87. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
  88. SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
  89. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  90. SDL_GL_SetAttribute(SDL_GL_RETAINED_BACKING, 0);
  91. // Always use 24/8 depth/stencil (make sure any Graphics implementations
  92. // that have their own backbuffer match this, too).
  93. // Changing this after initial window creation would need the context to be
  94. // destroyed and recreated, which we really don't want.
  95. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  96. SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
  97. // Backbuffer MSAA is handled by the love.graphics implementation.
  98. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
  99. SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
  100. SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, sRGB ? 1 : 0);
  101. const char *driver = SDL_GetCurrentVideoDriver();
  102. if (driver && strstr(driver, "x11") == driver)
  103. {
  104. // Always disable the sRGB flag when GLX is used with older SDL versions,
  105. // because of this bug: https://bugzilla.libsdl.org/show_bug.cgi?id=2897
  106. // In practice GLX will always give an sRGB-capable framebuffer anyway.
  107. if (hasSDL203orEarlier)
  108. SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0);
  109. }
  110. #if defined(LOVE_WINDOWS)
  111. // Avoid the Microsoft OpenGL 1.1 software renderer on Windows. Apparently
  112. // older Intel drivers like to use it as a fallback when requesting some
  113. // unsupported framebuffer attribute values, rather than properly failing.
  114. SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
  115. #endif
  116. }
  117. void Window::setGLContextAttributes(const ContextAttribs &attribs)
  118. {
  119. int profilemask = 0;
  120. int contextflags = 0;
  121. if (attribs.gles)
  122. profilemask = SDL_GL_CONTEXT_PROFILE_ES;
  123. else if (attribs.versionMajor * 10 + attribs.versionMinor >= 32)
  124. profilemask |= SDL_GL_CONTEXT_PROFILE_CORE;
  125. else if (attribs.debug)
  126. profilemask = SDL_GL_CONTEXT_PROFILE_COMPATIBILITY;
  127. if (attribs.debug)
  128. contextflags |= SDL_GL_CONTEXT_DEBUG_FLAG;
  129. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, attribs.versionMajor);
  130. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, attribs.versionMinor);
  131. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, profilemask);
  132. SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, contextflags);
  133. }
  134. bool Window::checkGLVersion(const ContextAttribs &attribs, std::string &outversion)
  135. {
  136. typedef unsigned char GLubyte;
  137. typedef unsigned int GLenum;
  138. typedef const GLubyte *(APIENTRY *glGetStringPtr)(GLenum name);
  139. const GLenum GL_VENDOR_ENUM = 0x1F00;
  140. const GLenum GL_RENDERER_ENUM = 0x1F01;
  141. const GLenum GL_VERSION_ENUM = 0x1F02;
  142. // We don't have OpenGL headers or an automatic OpenGL function loader in
  143. // this module, so we have to get the glGetString function pointer ourselves.
  144. glGetStringPtr glGetStringFunc = (glGetStringPtr) SDL_GL_GetProcAddress("glGetString");
  145. if (!glGetStringFunc)
  146. return false;
  147. const char *glversion = (const char *) glGetStringFunc(GL_VERSION_ENUM);
  148. if (!glversion)
  149. return false;
  150. outversion = glversion;
  151. const char *glrenderer = (const char *) glGetStringFunc(GL_RENDERER_ENUM);
  152. if (glrenderer)
  153. outversion += " - " + std::string(glrenderer);
  154. const char *glvendor = (const char *) glGetStringFunc(GL_VENDOR_ENUM);
  155. if (glvendor)
  156. outversion += " (" + std::string(glvendor) + ")";
  157. int glmajor = 0;
  158. int glminor = 0;
  159. // glGetString(GL_VERSION) returns a string with the format "major.minor",
  160. // or "OpenGL ES major.minor" in GLES contexts.
  161. const char *format = "%d.%d";
  162. if (attribs.gles)
  163. format = "OpenGL ES %d.%d";
  164. if (sscanf(glversion, format, &glmajor, &glminor) != 2)
  165. return false;
  166. if (glmajor < attribs.versionMajor
  167. || (glmajor == attribs.versionMajor && glminor < attribs.versionMinor))
  168. return false;
  169. return true;
  170. }
  171. std::vector<Window::ContextAttribs> Window::getContextAttribsList() const
  172. {
  173. // If we already have a set of context attributes that we know work, just
  174. // return that. love.graphics doesn't really support switching GL versions
  175. // after the first initialization.
  176. if (contextAttribs.versionMajor > 0)
  177. return std::vector<ContextAttribs>{contextAttribs};
  178. bool preferGLES = false;
  179. #ifdef LOVE_GRAPHICS_USE_OPENGLES
  180. preferGLES = true;
  181. #endif
  182. const char *curdriver = SDL_GetCurrentVideoDriver();
  183. const char *glesdrivers[] = {"RPI", "Android", "uikit", "winrt", "emscripten"};
  184. // We always want to try OpenGL ES first on certain video backends.
  185. for (const char *glesdriver : glesdrivers)
  186. {
  187. if (curdriver && strstr(curdriver, glesdriver) == curdriver)
  188. {
  189. preferGLES = true;
  190. // Prior to SDL 2.0.4, backends that use OpenGL ES didn't properly
  191. // ask for a sRGB framebuffer when requested by SDL_GL_SetAttribute.
  192. // This doesn't account for windowing backends that sometimes use
  193. // EGL, e.g. the X11 and windows SDL backends.
  194. if (hasSDL203orEarlier)
  195. graphics::setGammaCorrect(false);
  196. break;
  197. }
  198. }
  199. if (!preferGLES)
  200. {
  201. const char *gleshint = SDL_GetHint("LOVE_GRAPHICS_USE_OPENGLES");
  202. preferGLES = (gleshint != nullptr && gleshint[0] != '0');
  203. }
  204. // Do we want a debug context?
  205. bool debug = love::graphics::isDebugEnabled();
  206. const char *preferGL2hint = SDL_GetHint("LOVE_GRAPHICS_USE_GL2");
  207. bool preferGL2 = (preferGL2hint != nullptr && preferGL2hint[0] != '0');
  208. const char *preferGL3hint = SDL_GetHint("LOVE_GRAPHICS_USE_GL3");
  209. bool preferGL3 = (preferGL3hint != nullptr && preferGL3hint[0] != '0');
  210. std::vector<ContextAttribs> glcontexts =
  211. {
  212. {4, 3, false, debug},
  213. {3, 3, false, debug},
  214. {2, 1, false, debug},
  215. };
  216. std::vector<ContextAttribs> glescontexts =
  217. {
  218. {3, 2, true, debug},
  219. {3, 0, true, debug},
  220. {2, 0, true, debug}
  221. };
  222. if (preferGL2)
  223. {
  224. std::swap(glcontexts[0], glcontexts[2]);
  225. std::swap(glescontexts[0], glescontexts[2]);
  226. }
  227. else if (preferGL3)
  228. {
  229. std::swap(glcontexts[0], glcontexts[1]);
  230. std::swap(glescontexts[0], glescontexts[1]);
  231. }
  232. std::vector<ContextAttribs> attribslist;
  233. if (preferGLES)
  234. {
  235. attribslist.insert(attribslist.end(), glescontexts.begin(), glescontexts.end());
  236. attribslist.insert(attribslist.end(), glcontexts.begin(), glcontexts.end());
  237. }
  238. else
  239. {
  240. attribslist.insert(attribslist.end(), glcontexts.begin(), glcontexts.end());
  241. attribslist.insert(attribslist.end(), glescontexts.begin(), glescontexts.end());
  242. }
  243. return attribslist;
  244. }
  245. bool Window::createWindowAndContext(int x, int y, int w, int h, Uint32 windowflags, graphics::Renderer renderer)
  246. {
  247. bool needsglcontext = (windowflags & SDL_WINDOW_OPENGL) != 0;
  248. #ifdef LOVE_GRAPHICS_METAL
  249. bool needsmetalview = (windowflags & SDL_WINDOW_METAL) != 0;
  250. #endif
  251. std::string windowerror;
  252. std::string contexterror;
  253. std::string glversion;
  254. // Unfortunately some OpenGL context settings are part of the internal
  255. // window state in the Windows and Linux SDL backends, so we have to
  256. // recreate the window when we want to change those settings...
  257. // Also, apparently some Intel drivers on Windows give back a Microsoft
  258. // OpenGL 1.1 software renderer context when high MSAA values are requested!
  259. const auto create = [&](const ContextAttribs *attribs) -> bool
  260. {
  261. if (glcontext)
  262. {
  263. SDL_GL_DeleteContext(glcontext);
  264. glcontext = nullptr;
  265. }
  266. #ifdef LOVE_GRAPHICS_METAL
  267. if (metalView)
  268. {
  269. SDL_Metal_DestroyView(metalView);
  270. metalView = nullptr;
  271. }
  272. #endif
  273. if (window)
  274. {
  275. SDL_DestroyWindow(window);
  276. SDL_FlushEvent(SDL_WINDOWEVENT);
  277. window = nullptr;
  278. }
  279. window = SDL_CreateWindow(title.c_str(), x, y, w, h, windowflags);
  280. if (!window)
  281. {
  282. windowerror = std::string(SDL_GetError());
  283. return false;
  284. }
  285. if (attribs != nullptr)
  286. {
  287. glcontext = SDL_GL_CreateContext(window);
  288. if (!glcontext)
  289. contexterror = std::string(SDL_GetError());
  290. // Make sure the context's version is at least what we requested.
  291. if (glcontext && !checkGLVersion(*attribs, glversion))
  292. {
  293. SDL_GL_DeleteContext(glcontext);
  294. glcontext = nullptr;
  295. }
  296. if (!glcontext)
  297. {
  298. SDL_DestroyWindow(window);
  299. window = nullptr;
  300. return false;
  301. }
  302. }
  303. return true;
  304. };
  305. if (renderer == graphics::RENDERER_OPENGL)
  306. {
  307. std::vector<ContextAttribs> attribslist = getContextAttribsList();
  308. // Try each context profile in order.
  309. for (ContextAttribs attribs : attribslist)
  310. {
  311. bool curSRGB = love::graphics::isGammaCorrect();
  312. setGLFramebufferAttributes(curSRGB);
  313. setGLContextAttributes(attribs);
  314. windowerror.clear();
  315. contexterror.clear();
  316. create(&attribs);
  317. if (!window && curSRGB)
  318. {
  319. // The sRGB setting could have caused the failure.
  320. setGLFramebufferAttributes(false);
  321. if (create(&attribs))
  322. curSRGB = false;
  323. }
  324. if (window && glcontext)
  325. {
  326. // Store the successful context attributes so we can re-use them in
  327. // subsequent calls to createWindowAndContext.
  328. contextAttribs = attribs;
  329. love::graphics::setGammaCorrect(curSRGB);
  330. break;
  331. }
  332. }
  333. }
  334. #ifdef LOVE_GRAPHICS_METAL
  335. else if (renderer == graphics::RENDERER_METAL)
  336. {
  337. if (create(nullptr) && window != nullptr)
  338. metalView = SDL_Metal_CreateView(window);
  339. if (metalView == nullptr && window != nullptr)
  340. {
  341. contexterror = SDL_GetError();
  342. SDL_DestroyWindow(window);
  343. window = nullptr;
  344. }
  345. }
  346. #endif
  347. else
  348. {
  349. create(nullptr);
  350. }
  351. bool failed = window == nullptr;
  352. failed |= (needsglcontext && !glcontext);
  353. #ifdef LOVE_GRAPHICS_METAL
  354. failed |= (needsmetalview && !metalView);
  355. #endif
  356. if (failed)
  357. {
  358. std::string title = "Unable to create renderer";
  359. std::string message = "This program requires a graphics card and video drivers which support OpenGL 2.1 or OpenGL ES 2.";
  360. if (!glversion.empty())
  361. message += "\n\nDetected OpenGL version:\n" + glversion;
  362. else if (!contexterror.empty())
  363. message += "\n\nRenderer context creation error: " + contexterror;
  364. else if (!windowerror.empty())
  365. message += "\n\nSDL window creation error: " + windowerror;
  366. std::cerr << title << std::endl << message << std::endl;
  367. // Display a message box with the error, but only once.
  368. if (!displayedWindowError)
  369. {
  370. showMessageBox(title, message, MESSAGEBOX_ERROR, false);
  371. displayedWindowError = true;
  372. }
  373. close();
  374. return false;
  375. }
  376. open = true;
  377. return true;
  378. }
  379. bool Window::setWindow(int width, int height, WindowSettings *settings)
  380. {
  381. if (!graphics.get())
  382. graphics.set(Module::getInstance<graphics::Graphics>(Module::M_GRAPHICS));
  383. if (graphics.get() && graphics->isRenderTargetActive())
  384. throw love::Exception("love.window.setMode cannot be called while a render target is active in love.graphics.");
  385. auto renderer = graphics != nullptr ? graphics->getRenderer() : graphics::RENDERER_NONE;
  386. if (isOpen())
  387. updateSettings(this->settings, false);
  388. WindowSettings f;
  389. if (settings)
  390. f = *settings;
  391. f.minwidth = std::max(f.minwidth, 1);
  392. f.minheight = std::max(f.minheight, 1);
  393. f.display = std::min(std::max(f.display, 0), getDisplayCount() - 1);
  394. // Use the desktop resolution if a width or height of 0 is specified.
  395. if (width == 0 || height == 0)
  396. {
  397. SDL_DisplayMode mode = {};
  398. SDL_GetDesktopDisplayMode(f.display, &mode);
  399. width = mode.w;
  400. height = mode.h;
  401. }
  402. // On Android, disable fullscreen first on window creation so it's
  403. // possible to change the orientation by specifying portait width and
  404. // height, otherwise SDL will pick the current orientation dimensions when
  405. // fullscreen flag is set. Don't worry, we'll set it back later when user
  406. // also requested fullscreen after the window is created.
  407. // See https://github.com/love2d/love-android/issues/196
  408. #ifdef LOVE_ANDROID
  409. bool fullscreen = f.fullscreen;
  410. f.fullscreen = false;
  411. f.fstype = FULLSCREEN_DESKTOP;
  412. #endif
  413. int x = f.x;
  414. int y = f.y;
  415. if (f.useposition)
  416. {
  417. // The position needs to be in the global coordinate space.
  418. SDL_Rect displaybounds = {};
  419. SDL_GetDisplayBounds(f.display, &displaybounds);
  420. x += displaybounds.x;
  421. y += displaybounds.y;
  422. }
  423. else
  424. {
  425. if (f.centered)
  426. x = y = SDL_WINDOWPOS_CENTERED_DISPLAY(f.display);
  427. else
  428. x = y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(f.display);
  429. }
  430. SDL_DisplayMode fsmode = {0, width, height, 0, nullptr};
  431. if (f.fullscreen && f.fstype == FULLSCREEN_EXCLUSIVE)
  432. {
  433. // Fullscreen window creation will bug out if no mode can be used.
  434. if (SDL_GetClosestDisplayMode(f.display, &fsmode, &fsmode) == nullptr)
  435. {
  436. // GetClosestDisplayMode will fail if we request a size larger
  437. // than the largest available display mode, so we'll try to use
  438. // the largest (first) mode in that case.
  439. if (SDL_GetDisplayMode(f.display, 0, &fsmode) < 0)
  440. return false;
  441. }
  442. }
  443. bool needsetmode = false;
  444. Uint32 sdlflags = 0;
  445. if (f.fullscreen)
  446. {
  447. if (f.fstype == FULLSCREEN_DESKTOP)
  448. sdlflags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
  449. else
  450. {
  451. sdlflags |= SDL_WINDOW_FULLSCREEN;
  452. width = fsmode.w;
  453. height = fsmode.h;
  454. }
  455. }
  456. if (renderer != windowRenderer && isOpen())
  457. close();
  458. if (isOpen())
  459. {
  460. if (SDL_SetWindowFullscreen(window, sdlflags) == 0 && renderer == graphics::RENDERER_OPENGL)
  461. SDL_GL_MakeCurrent(window, glcontext);
  462. if (!f.fullscreen)
  463. SDL_SetWindowSize(window, width, height);
  464. // On linux systems 2.0.5+ might not be available...
  465. // TODO: require at least 2.0.5?
  466. #if SDL_VERSION_ATLEAST(2, 0, 5)
  467. if (this->settings.resizable != f.resizable)
  468. SDL_SetWindowResizable(window, f.resizable ? SDL_TRUE : SDL_FALSE);
  469. #endif
  470. if (this->settings.borderless != f.borderless)
  471. SDL_SetWindowBordered(window, f.borderless ? SDL_FALSE : SDL_TRUE);
  472. }
  473. else
  474. {
  475. if (renderer == graphics::RENDERER_OPENGL)
  476. sdlflags |= SDL_WINDOW_OPENGL;
  477. #ifdef LOVE_GRAPHICS_METAL
  478. if (renderer == graphics::RENDERER_METAL)
  479. sdlflags |= SDL_WINDOW_METAL;
  480. #endif
  481. if (f.resizable)
  482. sdlflags |= SDL_WINDOW_RESIZABLE;
  483. if (f.borderless)
  484. sdlflags |= SDL_WINDOW_BORDERLESS;
  485. if (isHighDPIAllowed())
  486. sdlflags |= SDL_WINDOW_ALLOW_HIGHDPI;
  487. if (!createWindowAndContext(x, y, width, height, sdlflags, renderer))
  488. return false;
  489. needsetmode = true;
  490. }
  491. // Make sure the window keeps any previously set icon.
  492. setIcon(icon.get());
  493. // Make sure the mouse keeps its previous grab setting.
  494. setMouseGrab(mouseGrabbed);
  495. // Enforce minimum window dimensions.
  496. SDL_SetWindowMinimumSize(window, f.minwidth, f.minheight);
  497. if (this->settings.display != f.display || f.useposition || f.centered)
  498. SDL_SetWindowPosition(window, x, y);
  499. SDL_RaiseWindow(window);
  500. setVSync(f.vsync);
  501. updateSettings(f, false);
  502. windowRenderer = renderer;
  503. if (graphics.get())
  504. {
  505. double scaledw, scaledh;
  506. fromPixels((double) pixelWidth, (double) pixelHeight, scaledw, scaledh);
  507. if (needsetmode)
  508. {
  509. void *context = nullptr;
  510. if (renderer == graphics::RENDERER_OPENGL)
  511. context = (void *) glcontext;
  512. #ifdef LOVE_GRAPHICS_METAL
  513. if (renderer == graphics::RENDERER_METAL && metalView)
  514. context = (void *) SDL_Metal_GetLayer(metalView);
  515. #endif
  516. graphics->setMode(context, (int) scaledw, (int) scaledh, pixelWidth, pixelHeight, f.stencil, f.msaa);
  517. this->settings.msaa = graphics->getBackbufferMSAA();
  518. }
  519. else
  520. {
  521. graphics->setViewportSize((int) scaledw, (int) scaledh, pixelWidth, pixelHeight);
  522. }
  523. }
  524. // Set fullscreen when user requested it before.
  525. // See above for explanation.
  526. #ifdef LOVE_ANDROID
  527. setFullscreen(fullscreen);
  528. love::android::setImmersive(fullscreen);
  529. #endif
  530. return true;
  531. }
  532. bool Window::onSizeChanged(int width, int height)
  533. {
  534. if (!window)
  535. return false;
  536. windowWidth = width;
  537. windowHeight = height;
  538. SDL_GL_GetDrawableSize(window, &pixelWidth, &pixelHeight);
  539. if (graphics.get())
  540. {
  541. double scaledw, scaledh;
  542. fromPixels((double) pixelWidth, (double) pixelHeight, scaledw, scaledh);
  543. graphics->setViewportSize((int) scaledw, (int) scaledh, pixelWidth, pixelHeight);
  544. }
  545. return true;
  546. }
  547. void Window::updateSettings(const WindowSettings &newsettings, bool updateGraphicsViewport)
  548. {
  549. Uint32 wflags = SDL_GetWindowFlags(window);
  550. // Set the new display mode as the current display mode.
  551. SDL_GetWindowSize(window, &windowWidth, &windowHeight);
  552. pixelWidth = windowWidth;
  553. pixelHeight = windowHeight;
  554. if ((wflags & SDL_WINDOW_OPENGL) != 0)
  555. SDL_GL_GetDrawableSize(window, &pixelWidth, &pixelHeight);
  556. #ifdef LOVE_GRAPHICS_METAL
  557. else if ((wflags & SDL_WINDOW_METAL) != 0)
  558. SDL_Metal_GetDrawableSize(window, &pixelWidth, &pixelHeight);
  559. #endif
  560. if ((wflags & SDL_WINDOW_FULLSCREEN_DESKTOP) == SDL_WINDOW_FULLSCREEN_DESKTOP)
  561. {
  562. settings.fullscreen = true;
  563. settings.fstype = FULLSCREEN_DESKTOP;
  564. }
  565. else if ((wflags & SDL_WINDOW_FULLSCREEN) == SDL_WINDOW_FULLSCREEN)
  566. {
  567. settings.fullscreen = true;
  568. settings.fstype = FULLSCREEN_EXCLUSIVE;
  569. }
  570. else
  571. {
  572. settings.fullscreen = false;
  573. settings.fstype = newsettings.fstype;
  574. }
  575. #ifdef LOVE_ANDROID
  576. settings.fullscreen = love::android::getImmersive();
  577. #endif
  578. // SDL_GetWindowMinimumSize gives back 0,0 sometimes...
  579. settings.minwidth = newsettings.minwidth;
  580. settings.minheight = newsettings.minheight;
  581. settings.resizable = (wflags & SDL_WINDOW_RESIZABLE) != 0;
  582. settings.borderless = (wflags & SDL_WINDOW_BORDERLESS) != 0;
  583. settings.centered = newsettings.centered;
  584. getPosition(settings.x, settings.y, settings.display);
  585. setHighDPIAllowed((wflags & SDL_WINDOW_ALLOW_HIGHDPI) != 0);
  586. settings.usedpiscale = newsettings.usedpiscale;
  587. // Only minimize on focus loss if the window is in exclusive-fullscreen mode
  588. if (settings.fullscreen && settings.fstype == FULLSCREEN_EXCLUSIVE)
  589. SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "1");
  590. else
  591. SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
  592. settings.vsync = getVSync();
  593. settings.stencil = newsettings.stencil;
  594. settings.depth = newsettings.depth;
  595. SDL_DisplayMode dmode = {};
  596. SDL_GetCurrentDisplayMode(settings.display, &dmode);
  597. // May be 0 if the refresh rate can't be determined.
  598. settings.refreshrate = (double) dmode.refresh_rate;
  599. // Update the viewport size now instead of waiting for event polling.
  600. if (updateGraphicsViewport && graphics.get())
  601. {
  602. double scaledw, scaledh;
  603. fromPixels((double) pixelWidth, (double) pixelHeight, scaledw, scaledh);
  604. graphics->setViewportSize((int) scaledw, (int) scaledh, pixelWidth, pixelHeight);
  605. }
  606. }
  607. void Window::getWindow(int &width, int &height, WindowSettings &newsettings)
  608. {
  609. // The window might have been modified (moved, resized, etc.) by the user.
  610. if (window)
  611. updateSettings(settings, true);
  612. width = windowWidth;
  613. height = windowHeight;
  614. newsettings = settings;
  615. }
  616. void Window::close()
  617. {
  618. close(true);
  619. }
  620. void Window::close(bool allowExceptions)
  621. {
  622. if (graphics.get())
  623. {
  624. if (allowExceptions && graphics->isRenderTargetActive())
  625. throw love::Exception("love.window.close cannot be called while a render target is active in love.graphics.");
  626. graphics->unSetMode();
  627. }
  628. if (glcontext)
  629. {
  630. SDL_GL_DeleteContext(glcontext);
  631. glcontext = nullptr;
  632. }
  633. #ifdef LOVE_GRAPHICS_METAL
  634. if (metalView)
  635. {
  636. SDL_Metal_DestroyView(metalView);
  637. metalView = nullptr;
  638. }
  639. #endif
  640. if (window)
  641. {
  642. SDL_DestroyWindow(window);
  643. window = nullptr;
  644. // The old window may have generated pending events which are no longer
  645. // relevant. Destroy them all!
  646. SDL_FlushEvent(SDL_WINDOWEVENT);
  647. }
  648. open = false;
  649. }
  650. bool Window::setFullscreen(bool fullscreen, FullscreenType fstype)
  651. {
  652. if (!window)
  653. return false;
  654. if (graphics.get() && graphics->isRenderTargetActive())
  655. throw love::Exception("love.window.setFullscreen cannot be called while a render target is active in love.graphics.");
  656. WindowSettings newsettings = settings;
  657. newsettings.fullscreen = fullscreen;
  658. newsettings.fstype = fstype;
  659. Uint32 sdlflags = 0;
  660. if (fullscreen)
  661. {
  662. if (fstype == FULLSCREEN_DESKTOP)
  663. sdlflags = SDL_WINDOW_FULLSCREEN_DESKTOP;
  664. else
  665. {
  666. sdlflags = SDL_WINDOW_FULLSCREEN;
  667. SDL_DisplayMode mode = {};
  668. mode.w = windowWidth;
  669. mode.h = windowHeight;
  670. SDL_GetClosestDisplayMode(SDL_GetWindowDisplayIndex(window), &mode, &mode);
  671. SDL_SetWindowDisplayMode(window, &mode);
  672. }
  673. }
  674. #ifdef LOVE_ANDROID
  675. love::android::setImmersive(fullscreen);
  676. #endif
  677. if (SDL_SetWindowFullscreen(window, sdlflags) == 0)
  678. {
  679. if (glcontext)
  680. SDL_GL_MakeCurrent(window, glcontext);
  681. updateSettings(newsettings, true);
  682. // This gets un-set when we exit fullscreen (at least in macOS).
  683. if (!fullscreen)
  684. SDL_SetWindowMinimumSize(window, settings.minwidth, settings.minheight);
  685. return true;
  686. }
  687. return false;
  688. }
  689. bool Window::setFullscreen(bool fullscreen)
  690. {
  691. return setFullscreen(fullscreen, settings.fstype);
  692. }
  693. int Window::getDisplayCount() const
  694. {
  695. return SDL_GetNumVideoDisplays();
  696. }
  697. const char *Window::getDisplayName(int displayindex) const
  698. {
  699. const char *name = SDL_GetDisplayName(displayindex);
  700. if (name == nullptr)
  701. throw love::Exception("Invalid display index: %d", displayindex + 1);
  702. return name;
  703. }
  704. Window::DisplayOrientation Window::getDisplayOrientation(int displayindex) const
  705. {
  706. // TODO: We can expose this everywhere, we just need to watch out for the
  707. // SDL binary being older than the headers on Linux.
  708. #if SDL_VERSION_ATLEAST(2, 0, 9) && (defined(LOVE_ANDROID) || !defined(LOVE_LINUX))
  709. switch (SDL_GetDisplayOrientation(displayindex))
  710. {
  711. case SDL_ORIENTATION_UNKNOWN: return ORIENTATION_UNKNOWN;
  712. case SDL_ORIENTATION_LANDSCAPE: return ORIENTATION_LANDSCAPE;
  713. case SDL_ORIENTATION_LANDSCAPE_FLIPPED: return ORIENTATION_LANDSCAPE_FLIPPED;
  714. case SDL_ORIENTATION_PORTRAIT: return ORIENTATION_PORTRAIT;
  715. case SDL_ORIENTATION_PORTRAIT_FLIPPED: return ORIENTATION_PORTRAIT_FLIPPED;
  716. }
  717. #else
  718. LOVE_UNUSED(displayindex);
  719. #endif
  720. return ORIENTATION_UNKNOWN;
  721. }
  722. std::vector<Window::WindowSize> Window::getFullscreenSizes(int displayindex) const
  723. {
  724. std::vector<WindowSize> sizes;
  725. for (int i = 0; i < SDL_GetNumDisplayModes(displayindex); i++)
  726. {
  727. SDL_DisplayMode mode = {};
  728. SDL_GetDisplayMode(displayindex, i, &mode);
  729. WindowSize w = {mode.w, mode.h};
  730. // SDL2's display mode list has multiple entries for modes of the same
  731. // size with different bits per pixel, so we need to filter those out.
  732. if (std::find(sizes.begin(), sizes.end(), w) == sizes.end())
  733. sizes.push_back(w);
  734. }
  735. return sizes;
  736. }
  737. void Window::getDesktopDimensions(int displayindex, int &width, int &height) const
  738. {
  739. if (displayindex >= 0 && displayindex < getDisplayCount())
  740. {
  741. SDL_DisplayMode mode = {};
  742. SDL_GetDesktopDisplayMode(displayindex, &mode);
  743. width = mode.w;
  744. height = mode.h;
  745. }
  746. else
  747. {
  748. width = 0;
  749. height = 0;
  750. }
  751. }
  752. void Window::setPosition(int x, int y, int displayindex)
  753. {
  754. if (!window)
  755. return;
  756. displayindex = std::min(std::max(displayindex, 0), getDisplayCount() - 1);
  757. SDL_Rect displaybounds = {};
  758. SDL_GetDisplayBounds(displayindex, &displaybounds);
  759. // The position needs to be in the global coordinate space.
  760. x += displaybounds.x;
  761. y += displaybounds.y;
  762. SDL_SetWindowPosition(window, x, y);
  763. settings.useposition = true;
  764. }
  765. void Window::getPosition(int &x, int &y, int &displayindex)
  766. {
  767. if (!window)
  768. {
  769. x = y = 0;
  770. displayindex = 0;
  771. return;
  772. }
  773. displayindex = std::max(SDL_GetWindowDisplayIndex(window), 0);
  774. SDL_GetWindowPosition(window, &x, &y);
  775. // In SDL <= 2.0.3, fullscreen windows are always reported as 0,0. In every
  776. // other case we need to convert the position from global coordinates to the
  777. // monitor's coordinate space.
  778. if (x != 0 || y != 0)
  779. {
  780. SDL_Rect displaybounds = {};
  781. SDL_GetDisplayBounds(displayindex, &displaybounds);
  782. x -= displaybounds.x;
  783. y -= displaybounds.y;
  784. }
  785. }
  786. Rect Window::getSafeArea() const
  787. {
  788. #if defined(LOVE_IOS)
  789. if (window != nullptr)
  790. return love::ios::getSafeArea(window);
  791. #elif defined(LOVE_ANDROID)
  792. if (window != nullptr)
  793. {
  794. int top, left, bottom, right;
  795. if (love::android::getSafeArea(top, left, bottom, right))
  796. {
  797. // DisplayCutout API returns safe area in pixels
  798. // and is affected by display orientation.
  799. double safeLeft, safeTop, safeWidth, safeHeight;
  800. fromPixels(left, top, safeLeft, safeTop);
  801. fromPixels(pixelWidth - left - right, pixelHeight - top - bottom, safeWidth, safeHeight);
  802. return {(int) safeLeft, (int) safeTop, (int) safeWidth, (int) safeHeight};
  803. }
  804. }
  805. #endif
  806. double dw, dh;
  807. fromPixels(pixelWidth, pixelHeight, dw, dh);
  808. return {0, 0, (int) dw, (int) dh};
  809. }
  810. bool Window::isOpen() const
  811. {
  812. return open;
  813. }
  814. void Window::setWindowTitle(const std::string &title)
  815. {
  816. this->title = title;
  817. if (window)
  818. SDL_SetWindowTitle(window, title.c_str());
  819. }
  820. const std::string &Window::getWindowTitle() const
  821. {
  822. return title;
  823. }
  824. bool Window::setIcon(love::image::ImageData *imgd)
  825. {
  826. if (!imgd)
  827. return false;
  828. if (imgd->getFormat() != PIXELFORMAT_RGBA8_UNORM)
  829. throw love::Exception("setIcon only accepts 32-bit RGBA images.");
  830. icon.set(imgd);
  831. if (!window)
  832. return false;
  833. Uint32 rmask, gmask, bmask, amask;
  834. #ifdef LOVE_BIG_ENDIAN
  835. rmask = 0xFF000000;
  836. gmask = 0x00FF0000;
  837. bmask = 0x0000FF00;
  838. amask = 0x000000FF;
  839. #else
  840. rmask = 0x000000FF;
  841. gmask = 0x0000FF00;
  842. bmask = 0x00FF0000;
  843. amask = 0xFF000000;
  844. #endif
  845. int w = imgd->getWidth();
  846. int h = imgd->getHeight();
  847. int bytesperpixel = (int) getPixelFormatBlockSize(imgd->getFormat());
  848. int pitch = w * bytesperpixel;
  849. SDL_Surface *sdlicon = nullptr;
  850. {
  851. // We don't want another thread modifying the ImageData mid-copy.
  852. love::thread::Lock lock(imgd->getMutex());
  853. sdlicon = SDL_CreateRGBSurfaceFrom(imgd->getData(), w, h, bytesperpixel * 8, pitch, rmask, gmask, bmask, amask);
  854. }
  855. if (!sdlicon)
  856. return false;
  857. SDL_SetWindowIcon(window, sdlicon);
  858. SDL_FreeSurface(sdlicon);
  859. return true;
  860. }
  861. love::image::ImageData *Window::getIcon()
  862. {
  863. return icon.get();
  864. }
  865. void Window::setVSync(int vsync)
  866. {
  867. if (glcontext != nullptr)
  868. {
  869. SDL_GL_SetSwapInterval(vsync);
  870. // Check if adaptive vsync was requested but not supported, and fall
  871. // back to regular vsync if so.
  872. if (vsync == -1 && SDL_GL_GetSwapInterval() != -1)
  873. SDL_GL_SetSwapInterval(1);
  874. }
  875. #if defined(LOVE_GRAPHICS_METAL) && defined(LOVE_MACOS)
  876. if (metalView != nullptr)
  877. {
  878. void *metallayer = SDL_Metal_GetLayer(metalView);
  879. love::macos::setMetalLayerVSync(metallayer, vsync != 0);
  880. }
  881. #endif
  882. }
  883. int Window::getVSync() const
  884. {
  885. if (glcontext != nullptr)
  886. return SDL_GL_GetSwapInterval();
  887. #if defined(LOVE_GRAPHICS_METAL)
  888. if (metalView != nullptr)
  889. {
  890. #ifdef LOVE_MACOS
  891. void *metallayer = SDL_Metal_GetLayer(metalView);
  892. return love::macos::getMetalLayerVSync(metallayer) ? 1 : 0;
  893. #else
  894. return 1;
  895. #endif
  896. }
  897. #endif
  898. return 0;
  899. }
  900. void Window::setDisplaySleepEnabled(bool enable)
  901. {
  902. if (enable)
  903. SDL_EnableScreenSaver();
  904. else
  905. SDL_DisableScreenSaver();
  906. }
  907. bool Window::isDisplaySleepEnabled() const
  908. {
  909. return SDL_IsScreenSaverEnabled() != SDL_FALSE;
  910. }
  911. void Window::minimize()
  912. {
  913. if (window != nullptr)
  914. SDL_MinimizeWindow(window);
  915. }
  916. void Window::maximize()
  917. {
  918. if (window != nullptr)
  919. {
  920. SDL_MaximizeWindow(window);
  921. updateSettings(settings, true);
  922. }
  923. }
  924. void Window::restore()
  925. {
  926. if (window != nullptr)
  927. {
  928. SDL_RestoreWindow(window);
  929. updateSettings(settings, true);
  930. }
  931. }
  932. bool Window::isMaximized() const
  933. {
  934. return window != nullptr && (SDL_GetWindowFlags(window) & SDL_WINDOW_MAXIMIZED);
  935. }
  936. bool Window::isMinimized() const
  937. {
  938. return window != nullptr && (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED);
  939. }
  940. void Window::swapBuffers()
  941. {
  942. if (glcontext)
  943. SDL_GL_SwapWindow(window);
  944. }
  945. bool Window::hasFocus() const
  946. {
  947. return (window && SDL_GetKeyboardFocus() == window);
  948. }
  949. bool Window::hasMouseFocus() const
  950. {
  951. return (window && SDL_GetMouseFocus() == window);
  952. }
  953. bool Window::isVisible() const
  954. {
  955. return window && (SDL_GetWindowFlags(window) & SDL_WINDOW_SHOWN) != 0;
  956. }
  957. void Window::setMouseGrab(bool grab)
  958. {
  959. mouseGrabbed = grab;
  960. if (window)
  961. SDL_SetWindowGrab(window, (SDL_bool) grab);
  962. }
  963. bool Window::isMouseGrabbed() const
  964. {
  965. if (window)
  966. return SDL_GetWindowGrab(window) != SDL_FALSE;
  967. else
  968. return mouseGrabbed;
  969. }
  970. int Window::getWidth() const
  971. {
  972. return windowWidth;
  973. }
  974. int Window::getHeight() const
  975. {
  976. return windowHeight;
  977. }
  978. int Window::getPixelWidth() const
  979. {
  980. return pixelWidth;
  981. }
  982. int Window::getPixelHeight() const
  983. {
  984. return pixelHeight;
  985. }
  986. void Window::windowToPixelCoords(double *x, double *y) const
  987. {
  988. if (x != nullptr)
  989. *x = (*x) * ((double) pixelWidth / (double) windowWidth);
  990. if (y != nullptr)
  991. *y = (*y) * ((double) pixelHeight / (double) windowHeight);
  992. }
  993. void Window::pixelToWindowCoords(double *x, double *y) const
  994. {
  995. if (x != nullptr)
  996. *x = (*x) * ((double) windowWidth / (double) pixelWidth);
  997. if (y != nullptr)
  998. *y = (*y) * ((double) windowHeight / (double) pixelHeight);
  999. }
  1000. void Window::windowToDPICoords(double *x, double *y) const
  1001. {
  1002. double px = x != nullptr ? *x : 0.0;
  1003. double py = y != nullptr ? *y : 0.0;
  1004. windowToPixelCoords(&px, &py);
  1005. double dpix = 0.0;
  1006. double dpiy = 0.0;
  1007. fromPixels(px, py, dpix, dpiy);
  1008. if (x != nullptr)
  1009. *x = dpix;
  1010. if (y != nullptr)
  1011. *y = dpiy;
  1012. }
  1013. void Window::DPIToWindowCoords(double *x, double *y) const
  1014. {
  1015. double dpix = x != nullptr ? *x : 0.0;
  1016. double dpiy = y != nullptr ? *y : 0.0;
  1017. double px = 0.0;
  1018. double py = 0.0;
  1019. toPixels(dpix, dpiy, px, py);
  1020. pixelToWindowCoords(&px, &py);
  1021. if (x != nullptr)
  1022. *x = px;
  1023. if (y != nullptr)
  1024. *y = py;
  1025. }
  1026. double Window::getDPIScale() const
  1027. {
  1028. return settings.usedpiscale ? getNativeDPIScale() : 1.0;
  1029. }
  1030. double Window::getNativeDPIScale() const
  1031. {
  1032. #ifdef LOVE_ANDROID
  1033. return love::android::getScreenScale();
  1034. #else
  1035. return (double) pixelHeight / (double) windowHeight;
  1036. #endif
  1037. }
  1038. double Window::toPixels(double x) const
  1039. {
  1040. return x * getDPIScale();
  1041. }
  1042. void Window::toPixels(double wx, double wy, double &px, double &py) const
  1043. {
  1044. double scale = getDPIScale();
  1045. px = wx * scale;
  1046. py = wy * scale;
  1047. }
  1048. double Window::fromPixels(double x) const
  1049. {
  1050. return x / getDPIScale();
  1051. }
  1052. void Window::fromPixels(double px, double py, double &wx, double &wy) const
  1053. {
  1054. double scale = getDPIScale();
  1055. wx = px / scale;
  1056. wy = py / scale;
  1057. }
  1058. const void *Window::getHandle() const
  1059. {
  1060. return window;
  1061. }
  1062. SDL_MessageBoxFlags Window::convertMessageBoxType(MessageBoxType type) const
  1063. {
  1064. switch (type)
  1065. {
  1066. case MESSAGEBOX_ERROR:
  1067. return SDL_MESSAGEBOX_ERROR;
  1068. case MESSAGEBOX_WARNING:
  1069. return SDL_MESSAGEBOX_WARNING;
  1070. case MESSAGEBOX_INFO:
  1071. default:
  1072. return SDL_MESSAGEBOX_INFORMATION;
  1073. }
  1074. }
  1075. bool Window::showMessageBox(const std::string &title, const std::string &message, MessageBoxType type, bool attachtowindow)
  1076. {
  1077. SDL_MessageBoxFlags flags = convertMessageBoxType(type);
  1078. SDL_Window *sdlwindow = attachtowindow ? window : nullptr;
  1079. return SDL_ShowSimpleMessageBox(flags, title.c_str(), message.c_str(), sdlwindow) >= 0;
  1080. }
  1081. int Window::showMessageBox(const MessageBoxData &data)
  1082. {
  1083. SDL_MessageBoxData sdldata = {};
  1084. sdldata.flags = convertMessageBoxType(data.type);
  1085. sdldata.title = data.title.c_str();
  1086. sdldata.message = data.message.c_str();
  1087. sdldata.window = data.attachToWindow ? window : nullptr;
  1088. sdldata.numbuttons = (int) data.buttons.size();
  1089. std::vector<SDL_MessageBoxButtonData> sdlbuttons;
  1090. for (int i = 0; i < (int) data.buttons.size(); i++)
  1091. {
  1092. SDL_MessageBoxButtonData sdlbutton = {};
  1093. sdlbutton.buttonid = i;
  1094. sdlbutton.text = data.buttons[i].c_str();
  1095. if (i == data.enterButtonIndex)
  1096. sdlbutton.flags |= SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
  1097. if (i == data.escapeButtonIndex)
  1098. sdlbutton.flags |= SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
  1099. sdlbuttons.push_back(sdlbutton);
  1100. }
  1101. sdldata.buttons = &sdlbuttons[0];
  1102. int pressedbutton = -2;
  1103. SDL_ShowMessageBox(&sdldata, &pressedbutton);
  1104. return pressedbutton;
  1105. }
  1106. void Window::requestAttention(bool continuous)
  1107. {
  1108. #if defined(LOVE_WINDOWS) && !defined(LOVE_WINDOWS_UWP)
  1109. if (hasFocus())
  1110. return;
  1111. SDL_SysWMinfo wminfo = {};
  1112. SDL_VERSION(&wminfo.version);
  1113. if (SDL_GetWindowWMInfo(window, &wminfo))
  1114. {
  1115. FLASHWINFO flashinfo = {};
  1116. flashinfo.cbSize = sizeof(FLASHWINFO);
  1117. flashinfo.hwnd = wminfo.info.win.window;
  1118. flashinfo.uCount = 1;
  1119. flashinfo.dwFlags = FLASHW_ALL;
  1120. if (continuous)
  1121. {
  1122. flashinfo.uCount = 0;
  1123. flashinfo.dwFlags |= FLASHW_TIMERNOFG;
  1124. }
  1125. FlashWindowEx(&flashinfo);
  1126. }
  1127. #elif defined(LOVE_MACOS)
  1128. love::macos::requestAttention(continuous);
  1129. #else
  1130. LOVE_UNUSED(continuous);
  1131. #endif
  1132. // TODO: Linux?
  1133. }
  1134. const char *Window::getName() const
  1135. {
  1136. return "love.window.sdl";
  1137. }
  1138. } // sdl
  1139. } // window
  1140. } // love