ScreenHandler.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  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. // for now - always fallback to no filter
  275. return EUpscalingFilter::NONE;
  276. // else - autoselect
  277. // Point outputResolution = getRenderResolution();
  278. // Point logicalResolution = getPreferredLogicalResolution();
  279. //
  280. // float scaleX = static_cast<float>(outputResolution.x) / logicalResolution.x;
  281. // float scaleY = static_cast<float>(outputResolution.x) / logicalResolution.x;
  282. // float scaling = std::min(scaleX, scaleY);
  283. //
  284. // if (scaling <= 1.0f)
  285. // return EUpscalingFilter::NONE;
  286. // if (scaling <= 2.0f)
  287. // return EUpscalingFilter::XBRZ_2;
  288. // if (scaling <= 3.0f)
  289. // return EUpscalingFilter::XBRZ_3;
  290. //
  291. // return EUpscalingFilter::XBRZ_4;
  292. }
  293. void ScreenHandler::selectUpscalingFilter()
  294. {
  295. upscalingFilter = loadUpscalingFilter();
  296. logGlobal->debug("Selected upscaling filter %d", static_cast<int>(upscalingFilter));
  297. }
  298. void ScreenHandler::selectDownscalingFilter()
  299. {
  300. SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, settings["video"]["downscalingFilter"].String().c_str());
  301. logGlobal->debug("Selected downscaling filter %s", settings["video"]["downscalingFilter"].String());
  302. }
  303. void ScreenHandler::initializeScreenBuffers()
  304. {
  305. #ifdef VCMI_ENDIAN_BIG
  306. int bmask = 0xff000000;
  307. int gmask = 0x00ff0000;
  308. int rmask = 0x0000ff00;
  309. int amask = 0x000000ff;
  310. #else
  311. int bmask = 0x000000ff;
  312. int gmask = 0x0000ff00;
  313. int rmask = 0x00ff0000;
  314. int amask = 0xFF000000;
  315. #endif
  316. auto logicalSize = getPreferredLogicalResolution() * getScalingFactor();
  317. SDL_RenderSetLogicalSize(mainRenderer, logicalSize.x, logicalSize.y);
  318. screen = SDL_CreateRGBSurface(0, logicalSize.x, logicalSize.y, 32, rmask, gmask, bmask, amask);
  319. if(nullptr == screen)
  320. {
  321. logGlobal->error("Unable to create surface %dx%d with %d bpp: %s", logicalSize.x, logicalSize.y, 32, SDL_GetError());
  322. throw std::runtime_error("Unable to create surface");
  323. }
  324. //No blending for screen itself. Required for proper cursor rendering.
  325. SDL_SetSurfaceBlendMode(screen, SDL_BLENDMODE_NONE);
  326. screenTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, logicalSize.x, logicalSize.y);
  327. if(nullptr == screenTexture)
  328. {
  329. logGlobal->error("Unable to create screen texture");
  330. logGlobal->error(SDL_GetError());
  331. throw std::runtime_error("Unable to create screen texture");
  332. }
  333. screen2 = CSDL_Ext::copySurface(screen);
  334. if(nullptr == screen2)
  335. {
  336. throw std::runtime_error("Unable to copy surface\n");
  337. }
  338. if (GH.windows().count() > 1)
  339. screenBuf = screen2;
  340. else
  341. screenBuf = screen;
  342. clearScreen();
  343. }
  344. SDL_Window * ScreenHandler::createWindowImpl(Point dimensions, int flags, bool center)
  345. {
  346. int displayIndex = getPreferredDisplayIndex();
  347. int positionFlags = center ? SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex) : SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex);
  348. return SDL_CreateWindow(NAME.c_str(), positionFlags, positionFlags, dimensions.x, dimensions.y, flags);
  349. }
  350. SDL_Window * ScreenHandler::createWindow()
  351. {
  352. #ifndef VCMI_MOBILE
  353. Point dimensions = getPreferredWindowResolution();
  354. switch(getPreferredWindowMode())
  355. {
  356. case EWindowMode::FULLSCREEN_EXCLUSIVE:
  357. return createWindowImpl(dimensions, SDL_WINDOW_FULLSCREEN, false);
  358. case EWindowMode::FULLSCREEN_BORDERLESS_WINDOWED:
  359. return createWindowImpl(Point(), SDL_WINDOW_FULLSCREEN_DESKTOP, false);
  360. case EWindowMode::WINDOWED:
  361. return createWindowImpl(dimensions, SDL_WINDOW_RESIZABLE, true);
  362. default:
  363. return nullptr;
  364. };
  365. #endif
  366. #ifdef VCMI_IOS
  367. SDL_SetHint(SDL_HINT_IOS_HIDE_HOME_INDICATOR, "1");
  368. SDL_SetHint(SDL_HINT_RETURN_KEY_HIDES_IME, "1");
  369. uint32_t windowFlags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI;
  370. SDL_Window * result = createWindowImpl(Point(), windowFlags | SDL_WINDOW_METAL, false);
  371. if(result != nullptr)
  372. return result;
  373. logGlobal->warn("Metal unavailable, using OpenGLES");
  374. return createWindowImpl(Point(), windowFlags, false);
  375. #endif
  376. #ifdef VCMI_ANDROID
  377. return createWindowImpl(Point(), SDL_WINDOW_FULLSCREEN, false);
  378. #endif
  379. }
  380. void ScreenHandler::onScreenResize()
  381. {
  382. recreateWindowAndScreenBuffers();
  383. }
  384. void ScreenHandler::validateSettings()
  385. {
  386. #ifndef VCMI_MOBILE
  387. {
  388. int displayIndex = settings["video"]["displayIndex"].Integer();
  389. int displaysCount = SDL_GetNumVideoDisplays();
  390. if (displayIndex >= displaysCount)
  391. {
  392. Settings writer = settings.write["video"]["displayIndex"];
  393. writer->Float() = 0;
  394. }
  395. }
  396. if (getPreferredWindowMode() == EWindowMode::WINDOWED)
  397. {
  398. //we only check that our desired window size fits on screen
  399. int displayIndex = getPreferredDisplayIndex();
  400. Point resolution = getPreferredWindowResolution();
  401. SDL_DisplayMode mode;
  402. if (SDL_GetDesktopDisplayMode(displayIndex, &mode) == 0)
  403. {
  404. if(resolution.x > mode.w || resolution.y > mode.h)
  405. {
  406. Settings writer = settings.write["video"]["resolution"];
  407. writer["width"].Float() = mode.w;
  408. writer["height"].Float() = mode.h;
  409. }
  410. }
  411. }
  412. if (getPreferredWindowMode() == EWindowMode::FULLSCREEN_EXCLUSIVE)
  413. {
  414. auto legalOptions = getSupportedResolutions();
  415. Point selectedResolution = getPreferredWindowResolution();
  416. if(!vstd::contains(legalOptions, selectedResolution))
  417. {
  418. // resolution selected for fullscreen mode is not supported by display
  419. // try to find current display resolution and use it instead as "reasonable default"
  420. SDL_DisplayMode mode;
  421. if (SDL_GetDesktopDisplayMode(getPreferredDisplayIndex(), &mode) == 0)
  422. {
  423. Settings writer = settings.write["video"]["resolution"];
  424. writer["width"].Float() = mode.w;
  425. writer["height"].Float() = mode.h;
  426. }
  427. }
  428. }
  429. #endif
  430. }
  431. int ScreenHandler::getPreferredRenderingDriver() const
  432. {
  433. int result = -1;
  434. const JsonNode & video = settings["video"];
  435. int driversCount = SDL_GetNumRenderDrivers();
  436. std::string preferredDriverName = video["driver"].String();
  437. logGlobal->info("Found %d render drivers", driversCount);
  438. for(int it = 0; it < driversCount; it++)
  439. {
  440. SDL_RendererInfo info;
  441. if (SDL_GetRenderDriverInfo(it, &info) == 0)
  442. {
  443. std::string driverName(info.name);
  444. if(!preferredDriverName.empty() && driverName == preferredDriverName)
  445. {
  446. result = it;
  447. logGlobal->info("\t%s (active)", driverName);
  448. }
  449. else
  450. logGlobal->info("\t%s", driverName);
  451. }
  452. else
  453. logGlobal->info("\t(error)");
  454. }
  455. return result;
  456. }
  457. void ScreenHandler::destroyScreenBuffers()
  458. {
  459. // screenBuf is not a separate surface, but points to either screen or screen2 - just set to null
  460. screenBuf = nullptr;
  461. if(nullptr != screen2)
  462. {
  463. SDL_FreeSurface(screen2);
  464. screen2 = nullptr;
  465. }
  466. if(nullptr != screen)
  467. {
  468. SDL_FreeSurface(screen);
  469. screen = nullptr;
  470. }
  471. if(nullptr != screenTexture)
  472. {
  473. SDL_DestroyTexture(screenTexture);
  474. screenTexture = nullptr;
  475. }
  476. }
  477. void ScreenHandler::destroyWindow()
  478. {
  479. if(nullptr != mainRenderer)
  480. {
  481. SDL_DestroyRenderer(mainRenderer);
  482. mainRenderer = nullptr;
  483. }
  484. if(nullptr != mainWindow)
  485. {
  486. SDL_DestroyWindow(mainWindow);
  487. mainWindow = nullptr;
  488. }
  489. }
  490. void ScreenHandler::close()
  491. {
  492. if(settings["general"]["notifications"].Bool())
  493. NotificationHandler::destroy();
  494. destroyScreenBuffers();
  495. destroyWindow();
  496. SDL_Quit();
  497. }
  498. void ScreenHandler::clearScreen()
  499. {
  500. SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
  501. SDL_RenderClear(mainRenderer);
  502. SDL_RenderPresent(mainRenderer);
  503. }
  504. std::vector<Point> ScreenHandler::getSupportedResolutions() const
  505. {
  506. int displayID = getPreferredDisplayIndex();
  507. return getSupportedResolutions(displayID);
  508. }
  509. std::vector<Point> ScreenHandler::getSupportedResolutions( int displayIndex) const
  510. {
  511. //NOTE: this method is never called on Android/iOS, only on desktop systems
  512. std::vector<Point> result;
  513. int modesCount = SDL_GetNumDisplayModes(displayIndex);
  514. for (int i =0; i < modesCount; ++i)
  515. {
  516. SDL_DisplayMode mode;
  517. if (SDL_GetDisplayMode(displayIndex, i, &mode) == 0)
  518. {
  519. Point resolution(mode.w, mode.h);
  520. result.push_back(resolution);
  521. }
  522. }
  523. boost::range::sort(result, [](const auto & left, const auto & right)
  524. {
  525. return left.x * left.y < right.x * right.y;
  526. });
  527. // erase potential duplicates, e.g. resolutions with different framerate / bits per pixel
  528. result.erase(boost::unique(result).end(), result.end());
  529. return result;
  530. }
  531. bool ScreenHandler::hasFocus()
  532. {
  533. ui32 flags = SDL_GetWindowFlags(mainWindow);
  534. return flags & SDL_WINDOW_INPUT_FOCUS;
  535. }