WindowHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * WindowHandler.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 "WindowHandler.h"
  12. #include "../../lib/CConfigHandler.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../gui/NotificationHandler.h"
  15. #include "CMT.h"
  16. #include "SDL_Extensions.h"
  17. #ifdef VCMI_ANDROID
  18. #include "../lib/CAndroidVMHelper.h"
  19. #endif
  20. #include <SDL.h>
  21. SDL_Window * mainWindow = nullptr;
  22. SDL_Renderer * mainRenderer = nullptr;
  23. SDL_Texture * screenTexture = nullptr;
  24. SDL_Surface * screen = nullptr; //main screen surface
  25. SDL_Surface * screen2 = nullptr; //and hlp surface (used to store not-active interfaces layer)
  26. 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
  27. static const std::string NAME_AFFIX = "client";
  28. static const std::string NAME = GameConstants::VCMI_VERSION + std::string(" (") + NAME_AFFIX + ')'; //application name
  29. Point WindowHandler::getPreferredLogicalResolution() const
  30. {
  31. // TODO: CONFIGURABLE ADVMAP - IMPLEMENT UI SCALE SETTING
  32. return {1280, 720};
  33. }
  34. Point WindowHandler::getPreferredRenderingResolution() const
  35. {
  36. if (getPreferredWindowMode() == EWindowMode::FULLSCREEN_WINDOWED)
  37. {
  38. SDL_Rect bounds;
  39. SDL_GetDisplayBounds(getPreferredDisplayIndex(), &bounds);
  40. return Point(bounds.w, bounds.h);
  41. }
  42. else
  43. {
  44. const JsonNode & video = settings["video"];
  45. int width = video["screenRes"]["width"].Integer();
  46. int height = video["screenRes"]["height"].Integer();
  47. return Point(width, height);
  48. }
  49. }
  50. int WindowHandler::getPreferredDisplayIndex() const
  51. {
  52. #ifdef VCMI_MOBILE
  53. // Assuming no multiple screens on Android / ios?
  54. return 0;
  55. #else
  56. if (mainWindow != nullptr)
  57. return SDL_GetWindowDisplayIndex(mainWindow);
  58. else
  59. return settings["video"]["displayIndex"].Integer();
  60. #endif
  61. }
  62. EWindowMode WindowHandler::getPreferredWindowMode() const
  63. {
  64. #ifdef VCMI_MOBILE
  65. // On Android / ios game will always render to screen size
  66. return EWindowMode::FULLSCREEN_WINDOWED;
  67. #else
  68. const JsonNode & video = settings["video"];
  69. bool fullscreen = video["fullscreen"].Bool();
  70. bool realFullscreen = settings["video"]["realFullscreen"].Bool();
  71. if (!fullscreen)
  72. return EWindowMode::WINDOWED;
  73. if (realFullscreen)
  74. return EWindowMode::FULLSCREEN_TRUE;
  75. else
  76. return EWindowMode::FULLSCREEN_WINDOWED;
  77. #endif
  78. }
  79. WindowHandler::WindowHandler()
  80. {
  81. if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE))
  82. {
  83. logGlobal->error("Something was wrong: %s", SDL_GetError());
  84. exit(-1);
  85. }
  86. const auto & logCallback = [](void * userdata, int category, SDL_LogPriority priority, const char * message)
  87. {
  88. logGlobal->debug("SDL(category %d; priority %d) %s", category, priority, message);
  89. };
  90. SDL_LogSetOutputFunction(logCallback, nullptr);
  91. #ifdef VCMI_ANDROID
  92. // manually setting egl pixel format, as a possible solution for sdl2<->android problem
  93. // https://bugzilla.libsdl.org/show_bug.cgi?id=2291
  94. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
  95. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
  96. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
  97. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
  98. #endif // VCMI_ANDROID
  99. validateSettings();
  100. recreateWindow();
  101. }
  102. bool WindowHandler::recreateWindow()
  103. {
  104. destroyScreen();
  105. if(mainWindow == nullptr)
  106. initializeRenderer();
  107. else
  108. updateFullscreenState();
  109. initializeScreen();
  110. if(!settings["session"]["headless"].Bool() && settings["general"]["notifications"].Bool())
  111. {
  112. NotificationHandler::init(mainWindow);
  113. }
  114. return true;
  115. }
  116. void WindowHandler::updateFullscreenState()
  117. {
  118. #if !defined(VCMI_MOBILE)
  119. int displayIndex = getPreferredDisplayIndex();
  120. switch(getPreferredWindowMode())
  121. {
  122. case EWindowMode::FULLSCREEN_TRUE:
  123. {
  124. SDL_SetWindowFullscreen(mainWindow, SDL_WINDOW_FULLSCREEN);
  125. SDL_DisplayMode mode;
  126. SDL_GetDesktopDisplayMode(displayIndex, &mode);
  127. Point resolution = getPreferredRenderingResolution();
  128. mode.w = resolution.x;
  129. mode.h = resolution.y;
  130. SDL_SetWindowDisplayMode(mainWindow, &mode);
  131. SDL_SetWindowPosition(mainWindow, SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex), SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex));
  132. return;
  133. }
  134. case EWindowMode::FULLSCREEN_WINDOWED:
  135. {
  136. SDL_SetWindowFullscreen(mainWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
  137. SDL_SetWindowPosition(mainWindow, SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex), SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex));
  138. return;
  139. }
  140. case EWindowMode::WINDOWED:
  141. {
  142. Point resolution = getPreferredLogicalResolution();
  143. SDL_SetWindowFullscreen(mainWindow, 0);
  144. SDL_SetWindowSize(mainWindow, resolution.x, resolution.y);
  145. SDL_SetWindowPosition(mainWindow, SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex), SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex));
  146. return;
  147. }
  148. }
  149. #endif
  150. }
  151. void WindowHandler::initializeRenderer()
  152. {
  153. mainWindow = createWindow();
  154. if(mainWindow == nullptr)
  155. throw std::runtime_error("Unable to create window\n");
  156. //create first available renderer if preferred not set. Use no flags, so HW accelerated will be preferred but SW renderer also will possible
  157. mainRenderer = SDL_CreateRenderer(mainWindow, getPreferredRenderingDriver(), 0);
  158. if(mainRenderer == nullptr)
  159. throw std::runtime_error("Unable to create renderer\n");
  160. SDL_RendererInfo info;
  161. SDL_GetRendererInfo(mainRenderer, &info);
  162. SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
  163. logGlobal->info("Created renderer %s", info.name);
  164. }
  165. void WindowHandler::initializeScreen()
  166. {
  167. #ifdef VCMI_ENDIAN_BIG
  168. int bmask = 0xff000000;
  169. int gmask = 0x00ff0000;
  170. int rmask = 0x0000ff00;
  171. int amask = 0x000000ff;
  172. #else
  173. int bmask = 0x000000ff;
  174. int gmask = 0x0000ff00;
  175. int rmask = 0x00ff0000;
  176. int amask = 0xFF000000;
  177. #endif
  178. auto logicalSize = getPreferredLogicalResolution();
  179. SDL_RenderSetLogicalSize(mainRenderer, logicalSize.x, logicalSize.y);
  180. screen = SDL_CreateRGBSurface(0, logicalSize.x, logicalSize.y, 32, rmask, gmask, bmask, amask);
  181. if(nullptr == screen)
  182. {
  183. logGlobal->error("Unable to create surface %dx%d with %d bpp: %s", logicalSize.x, logicalSize.y, 32, SDL_GetError());
  184. throw std::runtime_error("Unable to create surface");
  185. }
  186. //No blending for screen itself. Required for proper cursor rendering.
  187. SDL_SetSurfaceBlendMode(screen, SDL_BLENDMODE_NONE);
  188. screenTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, logicalSize.x, logicalSize.y);
  189. if(nullptr == screenTexture)
  190. {
  191. logGlobal->error("Unable to create screen texture");
  192. logGlobal->error(SDL_GetError());
  193. throw std::runtime_error("Unable to create screen texture");
  194. }
  195. screen2 = CSDL_Ext::copySurface(screen);
  196. if(nullptr == screen2)
  197. {
  198. throw std::runtime_error("Unable to copy surface\n");
  199. }
  200. screenBuf = screen;
  201. clearScreen();
  202. }
  203. SDL_Window * WindowHandler::createWindowImpl(Point dimensions, int flags, bool center)
  204. {
  205. int displayIndex = getPreferredDisplayIndex();
  206. int positionFlags = center ? SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex) : SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex);
  207. return SDL_CreateWindow(NAME.c_str(), positionFlags, positionFlags, dimensions.x, dimensions.y, flags);
  208. }
  209. SDL_Window * WindowHandler::createWindow()
  210. {
  211. #ifndef VCMI_MOBILE
  212. Point dimensions = getPreferredRenderingResolution();
  213. switch(getPreferredWindowMode())
  214. {
  215. case EWindowMode::FULLSCREEN_TRUE:
  216. return createWindowImpl(dimensions, SDL_WINDOW_FULLSCREEN, false);
  217. case EWindowMode::FULLSCREEN_WINDOWED:
  218. return createWindowImpl(Point(), SDL_WINDOW_FULLSCREEN_DESKTOP, false);
  219. case EWindowMode::WINDOWED:
  220. return createWindowImpl(dimensions, 0, true);
  221. default:
  222. return nullptr;
  223. };
  224. #endif
  225. #ifdef VCMI_IOS
  226. SDL_SetHint(SDL_HINT_IOS_HIDE_HOME_INDICATOR, "1");
  227. SDL_SetHint(SDL_HINT_RETURN_KEY_HIDES_IME, "1");
  228. SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
  229. uint32_t windowFlags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI;
  230. SDL_Window * result = createWindowImpl(Point(), windowFlags | SDL_WINDOW_METAL, false);
  231. if(result != nullptr)
  232. return result;
  233. logGlobal->warn("Metal unavailable, using OpenGLES");
  234. return createWindowImpl(Point(), windowFlags, false);
  235. #endif
  236. #ifdef VCMI_ANDROID
  237. return createWindowImpl(Point(), SDL_WINDOW_FULLSCREEN, false);
  238. #endif
  239. }
  240. void WindowHandler::onFullscreenChanged()
  241. {
  242. if(!recreateWindow())
  243. {
  244. //will return false and report error if video mode is not supported
  245. return;
  246. }
  247. GH.totalRedraw();
  248. }
  249. void WindowHandler::validateSettings()
  250. {
  251. #if !defined(VCMI_MOBILE)
  252. {
  253. int displayIndex = settings["video"]["displayIndex"].Integer();
  254. int displaysCount = SDL_GetNumVideoDisplays();
  255. if (displayIndex >= displaysCount)
  256. {
  257. Settings writer = settings.write["video"]["displayIndex"];
  258. writer->Float() = 0;
  259. }
  260. }
  261. if (getPreferredWindowMode() == EWindowMode::WINDOWED)
  262. {
  263. //we only check that our desired window size fits on screen
  264. int displayIndex = getPreferredDisplayIndex();
  265. Point resolution = getPreferredRenderingResolution();
  266. SDL_DisplayMode mode;
  267. if (SDL_GetDesktopDisplayMode(displayIndex, &mode) == 0)
  268. {
  269. if(resolution.x > mode.w || resolution.y > mode.h)
  270. {
  271. Settings writer = settings.write["video"]["screenRes"];
  272. writer["width"].Float() = mode.w;
  273. writer["height"].Float() = mode.h;
  274. }
  275. }
  276. }
  277. if (getPreferredWindowMode() == EWindowMode::FULLSCREEN_TRUE)
  278. {
  279. // TODO: check that display supports selected resolution
  280. }
  281. #endif
  282. }
  283. int WindowHandler::getPreferredRenderingDriver() const
  284. {
  285. int result = -1;
  286. const JsonNode & video = settings["video"];
  287. int driversCount = SDL_GetNumRenderDrivers();
  288. std::string preferredDriverName = video["driver"].String();
  289. logGlobal->info("Found %d render drivers", driversCount);
  290. for(int it = 0; it < driversCount; it++)
  291. {
  292. SDL_RendererInfo info;
  293. SDL_GetRenderDriverInfo(it, &info);
  294. std::string driverName(info.name);
  295. if(!preferredDriverName.empty() && driverName == preferredDriverName)
  296. {
  297. result = it;
  298. logGlobal->info("\t%s (active)", driverName);
  299. }
  300. else
  301. logGlobal->info("\t%s", driverName);
  302. }
  303. return result;
  304. }
  305. void WindowHandler::destroyScreen()
  306. {
  307. screenBuf = nullptr; //it`s a link - just nullify
  308. if(nullptr != screen2)
  309. {
  310. SDL_FreeSurface(screen2);
  311. screen2 = nullptr;
  312. }
  313. if(nullptr != screen)
  314. {
  315. SDL_FreeSurface(screen);
  316. screen = nullptr;
  317. }
  318. if(nullptr != screenTexture)
  319. {
  320. SDL_DestroyTexture(screenTexture);
  321. screenTexture = nullptr;
  322. }
  323. }
  324. void WindowHandler::destroyWindow()
  325. {
  326. if(nullptr != mainRenderer)
  327. {
  328. SDL_DestroyRenderer(mainRenderer);
  329. mainRenderer = nullptr;
  330. }
  331. if(nullptr != mainWindow)
  332. {
  333. SDL_DestroyWindow(mainWindow);
  334. mainWindow = nullptr;
  335. }
  336. }
  337. void WindowHandler::close()
  338. {
  339. if(settings["general"]["notifications"].Bool())
  340. NotificationHandler::destroy();
  341. destroyScreen();
  342. destroyWindow();
  343. SDL_Quit();
  344. }
  345. void WindowHandler::clearScreen()
  346. {
  347. SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
  348. SDL_RenderClear(mainRenderer);
  349. SDL_RenderPresent(mainRenderer);
  350. }
  351. std::vector<Point> WindowHandler::getSupportedResolutions() const
  352. {
  353. int displayID = SDL_GetWindowDisplayIndex(mainWindow);
  354. return getSupportedResolutions(displayID);
  355. }
  356. std::vector<Point> WindowHandler::getSupportedResolutions( int displayIndex) const
  357. {
  358. //TODO: check this method on iOS / Android
  359. std::vector<Point> result;
  360. int modesCount = SDL_GetNumDisplayModes(displayIndex);
  361. for (int i =0; i < modesCount; ++i)
  362. {
  363. SDL_DisplayMode mode;
  364. if (SDL_GetDisplayMode(displayIndex, i, &mode) != 0)
  365. continue;
  366. Point resolution(mode.w, mode.h);
  367. result.push_back(resolution);
  368. }
  369. return result;
  370. }