ScreenHandler.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * ScreenHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ScreenHandler.h"
  12. #include "../../lib/CConfigHandler.h"
  13. #include "../../lib/constants/StringConstants.h"
  14. #include "../gui/CGuiHandler.h"
  15. #include "../eventsSDL/NotificationHandler.h"
  16. #include "../gui/WindowHandler.h"
  17. #include "CMT.h"
  18. #include "SDL_Extensions.h"
  19. #ifdef VCMI_ANDROID
  20. #include "../lib/CAndroidVMHelper.h"
  21. #endif
  22. #ifdef VCMI_IOS
  23. # include "ios/utils.h"
  24. #endif
  25. #include <SDL.h>
  26. // TODO: should be made into a private members of ScreenHandler
  27. static SDL_Window * mainWindow = nullptr;
  28. SDL_Renderer * mainRenderer = nullptr;
  29. SDL_Texture * screenTexture = nullptr;
  30. SDL_Surface * screen = nullptr; //main screen surface
  31. SDL_Surface * screen2 = nullptr; //and hlp surface (used to store not-active interfaces layer)
  32. SDL_Surface * screenBuf = screen; //points to screen (if only advmapint is present) or screen2 (else) - should be used when updating controls which are not regularly redrawed
  33. static const std::string NAME = GameConstants::VCMI_VERSION; //application name
  34. static constexpr Point heroes3Resolution = Point(800, 600);
  35. std::tuple<int, int> ScreenHandler::getSupportedScalingRange() const
  36. {
  37. // H3 resolution, any resolution smaller than that is not correctly supported
  38. static constexpr Point minResolution = heroes3Resolution;
  39. // arbitrary limit on *downscaling*. Allow some downscaling, if requested by user. Should be generally limited to 100+ for all but few devices
  40. static constexpr double minimalScaling = 50;
  41. Point renderResolution = getRenderResolution();
  42. double reservedAreaWidth = settings["video"]["reservedWidth"].Float();
  43. Point availableResolution = Point(renderResolution.x * (1 - reservedAreaWidth), renderResolution.y);
  44. double maximalScalingWidth = 100.0 * availableResolution.x / minResolution.x;
  45. double maximalScalingHeight = 100.0 * availableResolution.y / minResolution.y;
  46. double maximalScaling = std::min(maximalScalingWidth, maximalScalingHeight);
  47. return { minimalScaling, maximalScaling };
  48. }
  49. Rect ScreenHandler::convertLogicalPointsToWindow(const Rect & input) const
  50. {
  51. Rect result;
  52. // FIXME: use SDL_RenderLogicalToWindow instead? Needs to be tested on ios
  53. float scaleX, scaleY;
  54. SDL_Rect viewport;
  55. SDL_RenderGetScale(mainRenderer, &scaleX, &scaleY);
  56. SDL_RenderGetViewport(mainRenderer, &viewport);
  57. #ifdef VCMI_IOS
  58. // TODO ios: looks like SDL bug actually, try fixing there
  59. const auto nativeScale = iOS_utils::screenScale();
  60. scaleX /= nativeScale;
  61. scaleY /= nativeScale;
  62. #endif
  63. result.x = (viewport.x + input.x) * scaleX;
  64. result.y = (viewport.y + input.y) * scaleY;
  65. result.w = input.w * scaleX;
  66. result.h = input.h * scaleY;
  67. return result;
  68. }
  69. Point ScreenHandler::getPreferredLogicalResolution() const
  70. {
  71. Point renderResolution = getRenderResolution();
  72. double reservedAreaWidth = settings["video"]["reservedWidth"].Float();
  73. Point availableResolution = Point(renderResolution.x * (1 - reservedAreaWidth), renderResolution.y);
  74. auto [minimalScaling, maximalScaling] = getSupportedScalingRange();
  75. int userScaling = settings["video"]["resolution"]["scaling"].Integer();
  76. int scaling = std::clamp(userScaling, minimalScaling, maximalScaling);
  77. Point logicalResolution = availableResolution * 100.0 / scaling;
  78. return logicalResolution;
  79. }
  80. int ScreenHandler::getScalingFactor() const
  81. {
  82. switch (upscalingFilter)
  83. {
  84. case EUpscalingFilter::NONE: return 1;
  85. case EUpscalingFilter::XBRZ_2: return 2;
  86. case EUpscalingFilter::XBRZ_3: return 3;
  87. case EUpscalingFilter::XBRZ_4: return 4;
  88. }
  89. throw std::runtime_error("invalid upscaling filter");
  90. }
  91. Point ScreenHandler::getLogicalResolution() const
  92. {
  93. return Point(screen->w, screen->h) / getScalingFactor();
  94. }
  95. Point ScreenHandler::getRenderResolution() const
  96. {
  97. assert(mainRenderer != nullptr);
  98. Point result;
  99. SDL_GetRendererOutputSize(mainRenderer, &result.x, &result.y);
  100. return result;
  101. }
  102. Point ScreenHandler::getPreferredWindowResolution() const
  103. {
  104. if (getPreferredWindowMode() == EWindowMode::FULLSCREEN_BORDERLESS_WINDOWED)
  105. {
  106. SDL_Rect bounds;
  107. if (SDL_GetDisplayBounds(getPreferredDisplayIndex(), &bounds) == 0)
  108. return Point(bounds.w, bounds.h);
  109. }
  110. const JsonNode & video = settings["video"];
  111. int width = video["resolution"]["width"].Integer();
  112. int height = video["resolution"]["height"].Integer();
  113. return Point(width, height);
  114. }
  115. int ScreenHandler::getPreferredDisplayIndex() const
  116. {
  117. #ifdef VCMI_MOBILE
  118. // Assuming no multiple screens on Android / ios?
  119. return 0;
  120. #else
  121. if (mainWindow != nullptr)
  122. {
  123. int result = SDL_GetWindowDisplayIndex(mainWindow);
  124. if (result >= 0)
  125. return result;
  126. }
  127. return settings["video"]["displayIndex"].Integer();
  128. #endif
  129. }
  130. EWindowMode ScreenHandler::getPreferredWindowMode() const
  131. {
  132. #ifdef VCMI_MOBILE
  133. // On Android / ios game will always render to screen size
  134. return EWindowMode::FULLSCREEN_BORDERLESS_WINDOWED;
  135. #else
  136. const JsonNode & video = settings["video"];
  137. bool fullscreen = video["fullscreen"].Bool();
  138. bool realFullscreen = settings["video"]["realFullscreen"].Bool();
  139. if (!fullscreen)
  140. return EWindowMode::WINDOWED;
  141. if (realFullscreen)
  142. return EWindowMode::FULLSCREEN_EXCLUSIVE;
  143. else
  144. return EWindowMode::FULLSCREEN_BORDERLESS_WINDOWED;
  145. #endif
  146. }
  147. ScreenHandler::ScreenHandler()
  148. {
  149. #ifdef VCMI_WINDOWS
  150. // set VCMI as "per-monitor DPI awareness". This completely disables any DPI-scaling by system.
  151. // Might not be the best solution since VCMI can't automatically adjust to DPI changes (including moving to monitors with different DPI scaling)
  152. // However this fixed unintuitive bug where player selects specific resolution for windowed mode, but ends up with completely different one due to scaling
  153. // NOTE: requires SDL 2.24.
  154. SDL_SetHint(SDL_HINT_WINDOWS_DPI_AWARENESS, "permonitor");
  155. #endif
  156. if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER))
  157. {
  158. logGlobal->error("Something was wrong: %s", SDL_GetError());
  159. exit(-1);
  160. }
  161. const auto & logCallback = [](void * userdata, int category, SDL_LogPriority priority, const char * message)
  162. {
  163. logGlobal->debug("SDL(category %d; priority %d) %s", category, priority, message);
  164. };
  165. SDL_LogSetOutputFunction(logCallback, nullptr);
  166. #ifdef VCMI_ANDROID
  167. // manually setting egl pixel format, as a possible solution for sdl2<->android problem
  168. // https://bugzilla.libsdl.org/show_bug.cgi?id=2291
  169. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  170. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
  171. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  172. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
  173. #endif // VCMI_ANDROID
  174. validateSettings();
  175. recreateWindowAndScreenBuffers();
  176. }
  177. void ScreenHandler::recreateWindowAndScreenBuffers()
  178. {
  179. destroyScreenBuffers();
  180. if(mainWindow == nullptr)
  181. initializeWindow();
  182. else
  183. updateWindowState();
  184. initializeScreenBuffers();
  185. if(!settings["session"]["headless"].Bool() && settings["general"]["notifications"].Bool())
  186. {
  187. NotificationHandler::init(mainWindow);
  188. }
  189. }
  190. void ScreenHandler::updateWindowState()
  191. {
  192. #ifndef VCMI_MOBILE
  193. int displayIndex = getPreferredDisplayIndex();
  194. switch(getPreferredWindowMode())
  195. {
  196. case EWindowMode::FULLSCREEN_EXCLUSIVE:
  197. {
  198. // for some reason, VCMI fails to switch from FULLSCREEN_BORDERLESS_WINDOWED to FULLSCREEN_EXCLUSIVE directly
  199. // Switch to windowed mode first to avoid this bug
  200. SDL_SetWindowFullscreen(mainWindow, 0);
  201. SDL_SetWindowFullscreen(mainWindow, SDL_WINDOW_FULLSCREEN);
  202. SDL_DisplayMode mode;
  203. SDL_GetDesktopDisplayMode(displayIndex, &mode);
  204. Point resolution = getPreferredWindowResolution();
  205. mode.w = resolution.x;
  206. mode.h = resolution.y;
  207. SDL_SetWindowDisplayMode(mainWindow, &mode);
  208. SDL_SetWindowPosition(mainWindow, SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex), SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex));
  209. return;
  210. }
  211. case EWindowMode::FULLSCREEN_BORDERLESS_WINDOWED:
  212. {
  213. SDL_SetWindowFullscreen(mainWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
  214. SDL_SetWindowPosition(mainWindow, SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex), SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex));
  215. return;
  216. }
  217. case EWindowMode::WINDOWED:
  218. {
  219. Point resolution = getPreferredWindowResolution();
  220. SDL_SetWindowFullscreen(mainWindow, 0);
  221. SDL_SetWindowSize(mainWindow, resolution.x, resolution.y);
  222. SDL_SetWindowPosition(mainWindow, SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex), SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex));
  223. return;
  224. }
  225. }
  226. #endif
  227. }
  228. void ScreenHandler::initializeWindow()
  229. {
  230. mainWindow = createWindow();
  231. if(mainWindow == nullptr)
  232. {
  233. const char * error = SDL_GetError();
  234. Point dimensions = getPreferredWindowResolution();
  235. std::string messagePattern = "Failed to create SDL Window of size %d x %d. Reason: %s";
  236. std::string message = boost::str(boost::format(messagePattern) % dimensions.x % dimensions.y % error);
  237. handleFatalError(message, true);
  238. }
  239. // create first available renderer if no preferred one is set
  240. // use no SDL_RENDERER_SOFTWARE or SDL_RENDERER_ACCELERATED flag, so HW accelerated will be preferred but SW renderer will also be possible
  241. uint32_t rendererFlags = 0;
  242. if(settings["video"]["vsync"].Bool())
  243. {
  244. rendererFlags |= SDL_RENDERER_PRESENTVSYNC;
  245. }
  246. mainRenderer = SDL_CreateRenderer(mainWindow, getPreferredRenderingDriver(), rendererFlags);
  247. if(mainRenderer == nullptr)
  248. {
  249. const char * error = SDL_GetError();
  250. std::string messagePattern = "Failed to create SDL renderer. Reason: %s";
  251. std::string message = boost::str(boost::format(messagePattern) % error);
  252. handleFatalError(message, true);
  253. }
  254. selectUpscalingFilter();
  255. selectDownscalingFilter();
  256. SDL_RendererInfo info;
  257. SDL_GetRendererInfo(mainRenderer, &info);
  258. logGlobal->info("Created renderer %s", info.name);
  259. }
  260. EUpscalingFilter ScreenHandler::loadUpscalingFilter() const
  261. {
  262. static const std::map<std::string, EUpscalingFilter> upscalingFilterTypes =
  263. {
  264. {"auto", EUpscalingFilter::AUTO },
  265. {"none", EUpscalingFilter::NONE },
  266. {"xbrz2", EUpscalingFilter::XBRZ_2 },
  267. {"xbrz3", EUpscalingFilter::XBRZ_3 },
  268. {"xbrz4", EUpscalingFilter::XBRZ_4 }
  269. };
  270. auto filterName = settings["video"]["upscalingFilter"].String();
  271. auto filter = upscalingFilterTypes.at(filterName);
  272. if (filter != EUpscalingFilter::AUTO)
  273. return filter;
  274. // else - autoselect
  275. Point outputResolution = getRenderResolution();
  276. Point logicalResolution = getPreferredLogicalResolution();
  277. float scaleX = static_cast<float>(outputResolution.x) / logicalResolution.x;
  278. float scaleY = static_cast<float>(outputResolution.x) / logicalResolution.x;
  279. float scaling = std::min(scaleX, scaleY);
  280. if (scaling <= 1.0f)
  281. return EUpscalingFilter::NONE;
  282. if (scaling <= 2.0f)
  283. return EUpscalingFilter::XBRZ_2;
  284. if (scaling <= 3.0f)
  285. return EUpscalingFilter::XBRZ_3;
  286. return EUpscalingFilter::XBRZ_4;
  287. }
  288. void ScreenHandler::selectUpscalingFilter()
  289. {
  290. upscalingFilter = loadUpscalingFilter();
  291. logGlobal->debug("Selected upscaling filter %d", static_cast<int>(upscalingFilter));
  292. }
  293. void ScreenHandler::selectDownscalingFilter()
  294. {
  295. SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, settings["video"]["downscalingFilter"].String().c_str());
  296. logGlobal->debug("Selected downscaling filter %s", settings["video"]["downscalingFilter"].String());
  297. }
  298. void ScreenHandler::initializeScreenBuffers()
  299. {
  300. #ifdef VCMI_ENDIAN_BIG
  301. int bmask = 0xff000000;
  302. int gmask = 0x00ff0000;
  303. int rmask = 0x0000ff00;
  304. int amask = 0x000000ff;
  305. #else
  306. int bmask = 0x000000ff;
  307. int gmask = 0x0000ff00;
  308. int rmask = 0x00ff0000;
  309. int amask = 0xFF000000;
  310. #endif
  311. auto logicalSize = getPreferredLogicalResolution() * getScalingFactor();
  312. SDL_RenderSetLogicalSize(mainRenderer, logicalSize.x, logicalSize.y);
  313. screen = SDL_CreateRGBSurface(0, logicalSize.x, logicalSize.y, 32, rmask, gmask, bmask, amask);
  314. if(nullptr == screen)
  315. {
  316. logGlobal->error("Unable to create surface %dx%d with %d bpp: %s", logicalSize.x, logicalSize.y, 32, SDL_GetError());
  317. throw std::runtime_error("Unable to create surface");
  318. }
  319. //No blending for screen itself. Required for proper cursor rendering.
  320. SDL_SetSurfaceBlendMode(screen, SDL_BLENDMODE_NONE);
  321. screenTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, logicalSize.x, logicalSize.y);
  322. if(nullptr == screenTexture)
  323. {
  324. logGlobal->error("Unable to create screen texture");
  325. logGlobal->error(SDL_GetError());
  326. throw std::runtime_error("Unable to create screen texture");
  327. }
  328. screen2 = CSDL_Ext::copySurface(screen);
  329. if(nullptr == screen2)
  330. {
  331. throw std::runtime_error("Unable to copy surface\n");
  332. }
  333. if (GH.windows().count() > 1)
  334. screenBuf = screen2;
  335. else
  336. screenBuf = screen;
  337. clearScreen();
  338. }
  339. SDL_Window * ScreenHandler::createWindowImpl(Point dimensions, int flags, bool center)
  340. {
  341. int displayIndex = getPreferredDisplayIndex();
  342. int positionFlags = center ? SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex) : SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex);
  343. return SDL_CreateWindow(NAME.c_str(), positionFlags, positionFlags, dimensions.x, dimensions.y, flags);
  344. }
  345. SDL_Window * ScreenHandler::createWindow()
  346. {
  347. #ifndef VCMI_MOBILE
  348. Point dimensions = getPreferredWindowResolution();
  349. switch(getPreferredWindowMode())
  350. {
  351. case EWindowMode::FULLSCREEN_EXCLUSIVE:
  352. return createWindowImpl(dimensions, SDL_WINDOW_FULLSCREEN, false);
  353. case EWindowMode::FULLSCREEN_BORDERLESS_WINDOWED:
  354. return createWindowImpl(Point(), SDL_WINDOW_FULLSCREEN_DESKTOP, false);
  355. case EWindowMode::WINDOWED:
  356. return createWindowImpl(dimensions, SDL_WINDOW_RESIZABLE, true);
  357. default:
  358. return nullptr;
  359. };
  360. #endif
  361. #ifdef VCMI_IOS
  362. SDL_SetHint(SDL_HINT_IOS_HIDE_HOME_INDICATOR, "1");
  363. SDL_SetHint(SDL_HINT_RETURN_KEY_HIDES_IME, "1");
  364. uint32_t windowFlags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI;
  365. SDL_Window * result = createWindowImpl(Point(), windowFlags | SDL_WINDOW_METAL, false);
  366. if(result != nullptr)
  367. return result;
  368. logGlobal->warn("Metal unavailable, using OpenGLES");
  369. return createWindowImpl(Point(), windowFlags, false);
  370. #endif
  371. #ifdef VCMI_ANDROID
  372. return createWindowImpl(Point(), SDL_WINDOW_FULLSCREEN, false);
  373. #endif
  374. }
  375. void ScreenHandler::onScreenResize()
  376. {
  377. recreateWindowAndScreenBuffers();
  378. }
  379. void ScreenHandler::validateSettings()
  380. {
  381. #ifndef VCMI_MOBILE
  382. {
  383. int displayIndex = settings["video"]["displayIndex"].Integer();
  384. int displaysCount = SDL_GetNumVideoDisplays();
  385. if (displayIndex >= displaysCount)
  386. {
  387. Settings writer = settings.write["video"]["displayIndex"];
  388. writer->Float() = 0;
  389. }
  390. }
  391. if (getPreferredWindowMode() == EWindowMode::WINDOWED)
  392. {
  393. //we only check that our desired window size fits on screen
  394. int displayIndex = getPreferredDisplayIndex();
  395. Point resolution = getPreferredWindowResolution();
  396. SDL_DisplayMode mode;
  397. if (SDL_GetDesktopDisplayMode(displayIndex, &mode) == 0)
  398. {
  399. if(resolution.x > mode.w || resolution.y > mode.h)
  400. {
  401. Settings writer = settings.write["video"]["resolution"];
  402. writer["width"].Float() = mode.w;
  403. writer["height"].Float() = mode.h;
  404. }
  405. }
  406. }
  407. if (getPreferredWindowMode() == EWindowMode::FULLSCREEN_EXCLUSIVE)
  408. {
  409. auto legalOptions = getSupportedResolutions();
  410. Point selectedResolution = getPreferredWindowResolution();
  411. if(!vstd::contains(legalOptions, selectedResolution))
  412. {
  413. // resolution selected for fullscreen mode is not supported by display
  414. // try to find current display resolution and use it instead as "reasonable default"
  415. SDL_DisplayMode mode;
  416. if (SDL_GetDesktopDisplayMode(getPreferredDisplayIndex(), &mode) == 0)
  417. {
  418. Settings writer = settings.write["video"]["resolution"];
  419. writer["width"].Float() = mode.w;
  420. writer["height"].Float() = mode.h;
  421. }
  422. }
  423. }
  424. #endif
  425. }
  426. int ScreenHandler::getPreferredRenderingDriver() const
  427. {
  428. int result = -1;
  429. const JsonNode & video = settings["video"];
  430. int driversCount = SDL_GetNumRenderDrivers();
  431. std::string preferredDriverName = video["driver"].String();
  432. logGlobal->info("Found %d render drivers", driversCount);
  433. for(int it = 0; it < driversCount; it++)
  434. {
  435. SDL_RendererInfo info;
  436. if (SDL_GetRenderDriverInfo(it, &info) == 0)
  437. {
  438. std::string driverName(info.name);
  439. if(!preferredDriverName.empty() && driverName == preferredDriverName)
  440. {
  441. result = it;
  442. logGlobal->info("\t%s (active)", driverName);
  443. }
  444. else
  445. logGlobal->info("\t%s", driverName);
  446. }
  447. else
  448. logGlobal->info("\t(error)");
  449. }
  450. return result;
  451. }
  452. void ScreenHandler::destroyScreenBuffers()
  453. {
  454. // screenBuf is not a separate surface, but points to either screen or screen2 - just set to null
  455. screenBuf = nullptr;
  456. if(nullptr != screen2)
  457. {
  458. SDL_FreeSurface(screen2);
  459. screen2 = nullptr;
  460. }
  461. if(nullptr != screen)
  462. {
  463. SDL_FreeSurface(screen);
  464. screen = nullptr;
  465. }
  466. if(nullptr != screenTexture)
  467. {
  468. SDL_DestroyTexture(screenTexture);
  469. screenTexture = nullptr;
  470. }
  471. }
  472. void ScreenHandler::destroyWindow()
  473. {
  474. if(nullptr != mainRenderer)
  475. {
  476. SDL_DestroyRenderer(mainRenderer);
  477. mainRenderer = nullptr;
  478. }
  479. if(nullptr != mainWindow)
  480. {
  481. SDL_DestroyWindow(mainWindow);
  482. mainWindow = nullptr;
  483. }
  484. }
  485. void ScreenHandler::close()
  486. {
  487. if(settings["general"]["notifications"].Bool())
  488. NotificationHandler::destroy();
  489. destroyScreenBuffers();
  490. destroyWindow();
  491. SDL_Quit();
  492. }
  493. void ScreenHandler::clearScreen()
  494. {
  495. SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
  496. SDL_RenderClear(mainRenderer);
  497. SDL_RenderPresent(mainRenderer);
  498. }
  499. std::vector<Point> ScreenHandler::getSupportedResolutions() const
  500. {
  501. int displayID = getPreferredDisplayIndex();
  502. return getSupportedResolutions(displayID);
  503. }
  504. std::vector<Point> ScreenHandler::getSupportedResolutions( int displayIndex) const
  505. {
  506. //NOTE: this method is never called on Android/iOS, only on desktop systems
  507. std::vector<Point> result;
  508. int modesCount = SDL_GetNumDisplayModes(displayIndex);
  509. for (int i =0; i < modesCount; ++i)
  510. {
  511. SDL_DisplayMode mode;
  512. if (SDL_GetDisplayMode(displayIndex, i, &mode) == 0)
  513. {
  514. Point resolution(mode.w, mode.h);
  515. result.push_back(resolution);
  516. }
  517. }
  518. boost::range::sort(result, [](const auto & left, const auto & right)
  519. {
  520. return left.x * left.y < right.x * right.y;
  521. });
  522. // erase potential duplicates, e.g. resolutions with different framerate / bits per pixel
  523. result.erase(boost::unique(result).end(), result.end());
  524. return result;
  525. }
  526. bool ScreenHandler::hasFocus()
  527. {
  528. ui32 flags = SDL_GetWindowFlags(mainWindow);
  529. return flags & SDL_WINDOW_INPUT_FOCUS;
  530. }