File: ScreenHandler.cpp

package info (click to toggle)
vcmi 1.7.2%2Bdfsg-1
  • links: PTS, VCS
  • area: contrib
  • in suites: sid
  • size: 43,732 kB
  • sloc: cpp: 275,851; ansic: 734; sh: 235; xml: 163; objc: 61; makefile: 50; python: 41
file content (735 lines) | stat: -rw-r--r-- 21,794 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
/*
 * ScreenHandler.cpp, part of VCMI engine
 *
 * Authors: listed in file AUTHORS in main folder
 *
 * License: GNU General Public License v2.0 or later
 * Full text of license available in license.txt file, in main folder
 *
 */

#include "StdInc.h"
#include "ScreenHandler.h"

#include "SDL_Extensions.h"

#include "../CMT.h"
#include "../eventsSDL/NotificationHandler.h"
#include "../GameEngine.h"
#include "../GameInstance.h"
#include "../CServerHandler.h"
#include "../GameChatHandler.h"
#include "../gui/CursorHandler.h"
#include "../gui/WindowHandler.h"
#include "../render/Canvas.h"
#include "../renderSDL/SDLImage.h"

#include "../../lib/CConfigHandler.h"
#include "../../lib/constants/StringConstants.h"
#include "../../lib/VCMIDirs.h"
#include "../../lib/texts/MetaString.h"

#include <vstd/DateUtils.h>

#ifdef VCMI_ANDROID
#include "../lib/CAndroidVMHelper.h"
#endif

#ifdef VCMI_IOS
#	include "ios/utils.h"
#endif

#include <SDL.h>

// TODO: should be made into a private members of ScreenHandler
SDL_Renderer * mainRenderer = nullptr;

static constexpr Point heroes3Resolution = Point(800, 600);

std::tuple<int, int> ScreenHandler::getSupportedScalingRange() const
{
	// H3 resolution, any resolution smaller than that is not correctly supported
	static constexpr Point minResolution = heroes3Resolution;
	// arbitrary limit on *downscaling*. Allow some downscaling, if requested by user. Should be generally limited to 100+ for all but few devices
	static constexpr double minimalScaling = 50;

	Point renderResolution = getRenderResolution();
	double reservedAreaWidth = settings["video"]["reservedWidth"].Float();
	Point availableResolution = Point(renderResolution.x * (1 - reservedAreaWidth), renderResolution.y);
	if(renderResolution.x < renderResolution.y) // reserved in portrait mode
		availableResolution = Point(renderResolution.x, renderResolution.y * (1 - reservedAreaWidth));

	double maximalScalingWidth = 100.0 * availableResolution.x / minResolution.x;
	double maximalScalingHeight = 100.0 * availableResolution.y / minResolution.y;
	double maximalScaling = std::min(maximalScalingWidth, maximalScalingHeight);

	return { minimalScaling, maximalScaling };
}

Rect ScreenHandler::convertLogicalPointsToWindow(const Rect & input) const
{
	Rect result;

	// FIXME: use SDL_RenderLogicalToWindow instead? Needs to be tested on ios

	float scaleX, scaleY;
	SDL_Rect viewport;
	SDL_RenderGetScale(mainRenderer, &scaleX, &scaleY);
	SDL_RenderGetViewport(mainRenderer, &viewport);

#ifdef VCMI_IOS
	// TODO ios: looks like SDL bug actually, try fixing there
	const auto nativeScale = iOS_utils::screenScale();
	scaleX /= nativeScale;
	scaleY /= nativeScale;
#endif

	result.x = (viewport.x + input.x) * scaleX;
	result.y = (viewport.y + input.y) * scaleY;
	result.w = input.w * scaleX;
	result.h = input.h * scaleY;

	return result;
}

int ScreenHandler::getInterfaceScalingPercentage() const
{
	auto [minimalScaling, maximalScaling] = getSupportedScalingRange();

	int userScaling = settings["video"]["resolution"]["scaling"].Integer();

	if (userScaling == 0) // autodetection
	{
#ifdef VCMI_MOBILE
		// for mobiles - stay at maximum scaling unless we have large screen
		// might be better to check screen DPI / physical dimensions, but way more complex, and may result in different edge cases, e.g. chromebooks / tv's
		int preferredMinimalScaling = 200;
#else
		// for PC - avoid downscaling if possible
		int preferredMinimalScaling = 100;
#endif
		// prefer a little below maximum - to give space for extended UI
		int preferredMaximalScaling = maximalScaling * 10 / 12;
		userScaling = std::max(std::min(maximalScaling, preferredMinimalScaling), preferredMaximalScaling);
	}

	int scaling = std::clamp(userScaling, minimalScaling, maximalScaling);
	return scaling;
}

Point ScreenHandler::getPreferredLogicalResolution() const
{
	Point renderResolution = getRenderResolution();
	double reservedAreaWidth = settings["video"]["reservedWidth"].Float();

	int scaling = getInterfaceScalingPercentage();
	Point availableResolution = Point(renderResolution.x * (1 - reservedAreaWidth), renderResolution.y);
	if(renderResolution.x < renderResolution.y) // reserved in portrait mode
		availableResolution = Point(renderResolution.x, renderResolution.y * (1 - reservedAreaWidth));
	Point logicalResolution = availableResolution * 100.0 / scaling;
	return logicalResolution;
}

int ScreenHandler::getScalingFactor() const
{
	switch (upscalingFilter)
	{
		case EUpscalingFilter::NONE: return 1;
		case EUpscalingFilter::XBRZ_2: return 2;
		case EUpscalingFilter::XBRZ_3: return 3;
		case EUpscalingFilter::XBRZ_4: return 4;
	}

	throw std::runtime_error("invalid upscaling filter");
}

Point ScreenHandler::getLogicalResolution() const
{
	return Point(screen->w, screen->h) / getScalingFactor();
}

Point ScreenHandler::getRenderResolution() const
{
	assert(mainRenderer != nullptr);

	Point result;
	SDL_GetRendererOutputSize(mainRenderer, &result.x, &result.y);

	return result;
}

Point ScreenHandler::getPreferredWindowResolution() const
{
	if (getPreferredWindowMode() == EWindowMode::FULLSCREEN_BORDERLESS_WINDOWED)
	{
		SDL_Rect bounds;
		if (SDL_GetDisplayBounds(getPreferredDisplayIndex(), &bounds) == 0)
			return Point(bounds.w, bounds.h);
	}

	const JsonNode & video = settings["video"];
	int width = video["resolution"]["width"].Integer();
	int height = video["resolution"]["height"].Integer();

	return Point(width, height);
}

int ScreenHandler::getPreferredDisplayIndex() const
{
#ifdef VCMI_MOBILE
	// Assuming no multiple screens on Android / ios?
	return 0;
#else
	if (mainWindow != nullptr)
	{
		int result = SDL_GetWindowDisplayIndex(mainWindow);
		if (result >= 0)
			return result;
	}

	return settings["video"]["displayIndex"].Integer();
#endif
}

EWindowMode ScreenHandler::getPreferredWindowMode() const
{
#ifdef VCMI_MOBILE
	// On Android / ios game will always render to screen size
	return EWindowMode::FULLSCREEN_BORDERLESS_WINDOWED;
#else
	const JsonNode & video = settings["video"];
	bool fullscreen = video["fullscreen"].Bool();
	bool realFullscreen = settings["video"]["realFullscreen"].Bool();

	if (!fullscreen)
		return EWindowMode::WINDOWED;

	if (realFullscreen)
		return EWindowMode::FULLSCREEN_EXCLUSIVE;
	else
		return EWindowMode::FULLSCREEN_BORDERLESS_WINDOWED;
#endif
}

ScreenHandler::ScreenHandler()
{
#ifdef VCMI_WINDOWS
	// set VCMI as "per-monitor DPI awareness". This completely disables any DPI-scaling by system.
	// Might not be the best solution since VCMI can't automatically adjust to DPI changes (including moving to monitors with different DPI scaling)
	// However this fixed unintuitive bug where player selects specific resolution for windowed mode, but ends up with completely different one due to scaling
	// NOTE: requires SDL 2.24.
	SDL_SetHint(SDL_HINT_WINDOWS_DPI_AWARENESS, "permonitor");
#endif
	if(settings["video"]["allowPortrait"].Bool())
		SDL_SetHint(SDL_HINT_ORIENTATIONS, "Portrait PortraitUpsideDown LandscapeLeft LandscapeRight");
	else
		SDL_SetHint(SDL_HINT_ORIENTATIONS, "LandscapeLeft LandscapeRight");

#ifdef VCMI_IOS
	if(!settings["general"]["ignoreMuteSwitch"].Bool())
		SDL_SetHint(SDL_HINT_AUDIO_CATEGORY, "AVAudioSessionCategoryAmbient");
#endif

	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER))
	{
		logGlobal->error("Something was wrong: %s", SDL_GetError());
		exit(-1);
	}

	const auto & logCallback = [](void * userdata, int category, SDL_LogPriority priority, const char * message)
	{
		logGlobal->debug("SDL(category %d; priority %d) %s", category, priority, message);
	};

	SDL_LogSetOutputFunction(logCallback, nullptr);

#ifdef VCMI_ANDROID
	// manually setting egl pixel format, as a possible solution for sdl2<->android problem
	// https://bugzilla.libsdl.org/show_bug.cgi?id=2291
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
#endif // VCMI_ANDROID

	validateSettings();
	recreateWindowAndScreenBuffers();
}

void ScreenHandler::recreateWindowAndScreenBuffers()
{
	destroyScreenBuffers();

	if(mainWindow == nullptr)
		initializeWindow();
	else
		updateWindowState();

	initializeScreenBuffers();

	if(!settings["session"]["headless"].Bool() && settings["general"]["notifications"].Bool())
	{
		NotificationHandler::init(mainWindow);
	}
}

void ScreenHandler::updateWindowState()
{
#ifndef VCMI_MOBILE
	int displayIndex = getPreferredDisplayIndex();

	switch(getPreferredWindowMode())
	{
		case EWindowMode::FULLSCREEN_EXCLUSIVE:
		{
			// for some reason, VCMI fails to switch from FULLSCREEN_BORDERLESS_WINDOWED to FULLSCREEN_EXCLUSIVE directly
			// Switch to windowed mode first to avoid this bug
			SDL_SetWindowFullscreen(mainWindow, 0);
			SDL_SetWindowFullscreen(mainWindow, SDL_WINDOW_FULLSCREEN);

			SDL_DisplayMode mode;
			SDL_GetDesktopDisplayMode(displayIndex, &mode);
			Point resolution = getPreferredWindowResolution();

			mode.w = resolution.x;
			mode.h = resolution.y;

			SDL_SetWindowDisplayMode(mainWindow, &mode);
			SDL_SetWindowPosition(mainWindow, SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex), SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex));

			return;
		}
		case EWindowMode::FULLSCREEN_BORDERLESS_WINDOWED:
		{
			SDL_SetWindowFullscreen(mainWindow, SDL_WINDOW_FULLSCREEN_DESKTOP);
			SDL_SetWindowPosition(mainWindow, SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex), SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex));
			return;
		}
		case EWindowMode::WINDOWED:
		{
			Point resolution = getPreferredWindowResolution();
			SDL_SetWindowFullscreen(mainWindow, 0);
			SDL_SetWindowSize(mainWindow, resolution.x, resolution.y);
			return;
		}
	}
#endif
}

void ScreenHandler::initializeWindow()
{
	mainWindow = createWindow();

	if(mainWindow == nullptr)
	{
		const char * error = SDL_GetError();
		Point dimensions = getPreferredWindowResolution();

		std::string messagePattern = "Failed to create SDL Window of size %d x %d. Reason: %s";
		std::string message = boost::str(boost::format(messagePattern) % dimensions.x % dimensions.y % error);

		handleFatalError(message, true);
	}

	// create first available renderer if no preferred one is set
	// use no SDL_RENDERER_SOFTWARE or SDL_RENDERER_ACCELERATED flag, so HW accelerated will be preferred but SW renderer will also be possible
	uint32_t rendererFlags = 0;
	if(settings["video"]["vsync"].Bool())
	{
		rendererFlags |= SDL_RENDERER_PRESENTVSYNC;
	}
	mainRenderer = SDL_CreateRenderer(mainWindow, getPreferredRenderingDriver(), rendererFlags);

	if(mainRenderer == nullptr)
	{
		const char * error = SDL_GetError();
		std::string messagePattern = "Failed to create SDL renderer. Reason: %s";
		std::string message = boost::str(boost::format(messagePattern) % error);
		handleFatalError(message, true);
	}

	selectUpscalingFilter();
	selectDownscalingFilter();

	SDL_RendererInfo info;
	SDL_GetRendererInfo(mainRenderer, &info);
	logGlobal->info("Created renderer %s", info.name);
}

EUpscalingFilter ScreenHandler::loadUpscalingFilter() const
{
	static const std::map<std::string, EUpscalingFilter> upscalingFilterTypes =
	{
		{"auto", EUpscalingFilter::AUTO },
		{"none", EUpscalingFilter::NONE },
		{"xbrz2", EUpscalingFilter::XBRZ_2 },
		{"xbrz3", EUpscalingFilter::XBRZ_3 },
		{"xbrz4", EUpscalingFilter::XBRZ_4 }
	};

	auto filterName = settings["video"]["upscalingFilter"].String();
	auto filter = upscalingFilterTypes.count(filterName) ? upscalingFilterTypes.at(filterName) : EUpscalingFilter::AUTO;

	if (filter != EUpscalingFilter::AUTO)
		return filter;

	// else - autoselect
	Point outputResolution = getRenderResolution();
	Point logicalResolution = getPreferredLogicalResolution();

	float scaleX = static_cast<float>(outputResolution.x) / logicalResolution.x;
	float scaleY = static_cast<float>(outputResolution.x) / logicalResolution.x;
	float scaling = std::min(scaleX, scaleY);
	int systemMemoryMb = SDL_GetSystemRAM();

	if (scaling <= 1.001f)
		return EUpscalingFilter::NONE; // running at original resolution or even lower than that - no need for xbrz

	if (systemMemoryMb <= 4096)
		return EUpscalingFilter::NONE; // xbrz2 may use ~1.0 - 1.5 Gb of RAM and has notable CPU cost - avoid on low-spec hardware

	// Only using xbrz2 for autoselection.
	// Higher options may have high system requirements and should be only selected explicitly by player
	return EUpscalingFilter::XBRZ_2;
}

void ScreenHandler::selectUpscalingFilter()
{
	upscalingFilter	= loadUpscalingFilter();
	logGlobal->debug("Selected upscaling filter %d", static_cast<int>(upscalingFilter));
}

void ScreenHandler::selectDownscalingFilter()
{
	SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, settings["video"]["downscalingFilter"].String().c_str());
	logGlobal->debug("Selected downscaling filter %s", settings["video"]["downscalingFilter"].String());
}

void ScreenHandler::initializeScreenBuffers()
{
#ifdef VCMI_ENDIAN_BIG
	int bmask = 0xff000000;
	int gmask = 0x00ff0000;
	int rmask = 0x0000ff00;
	int amask = 0x000000ff;
#else
	int bmask = 0x000000ff;
	int gmask = 0x0000ff00;
	int rmask = 0x00ff0000;
	int amask = 0xFF000000;
#endif

	auto logicalSize = getPreferredLogicalResolution() * getScalingFactor();
	SDL_RenderSetLogicalSize(mainRenderer, logicalSize.x, logicalSize.y);

	screen = SDL_CreateRGBSurface(0, logicalSize.x, logicalSize.y, 32, rmask, gmask, bmask, amask);
	if(nullptr == screen)
	{
		logGlobal->error("Unable to create surface %dx%d with %d bpp: %s", logicalSize.x, logicalSize.y, 32, SDL_GetError());
		throw std::runtime_error("Unable to create surface");
	}
	//No blending for screen itself. Required for proper cursor rendering.
	SDL_SetSurfaceBlendMode(screen, SDL_BLENDMODE_NONE);

	screenTexture = SDL_CreateTexture(mainRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, logicalSize.x, logicalSize.y);

	if(nullptr == screenTexture)
	{
		logGlobal->error("Unable to create screen texture");
		logGlobal->error(SDL_GetError());
		throw std::runtime_error("Unable to create screen texture");
	}

	clearScreen();
}

SDL_Window * ScreenHandler::createWindowImpl(Point dimensions, int flags, bool center)
{
	int displayIndex = getPreferredDisplayIndex();
	int positionFlags = center ? SDL_WINDOWPOS_CENTERED_DISPLAY(displayIndex) : SDL_WINDOWPOS_UNDEFINED_DISPLAY(displayIndex);

	return SDL_CreateWindow(GameConstants::VCMI_VERSION.c_str(), positionFlags, positionFlags, dimensions.x, dimensions.y, flags);
}

SDL_Window * ScreenHandler::createWindow()
{
#ifndef VCMI_MOBILE
	Point dimensions = getPreferredWindowResolution();

	switch(getPreferredWindowMode())
	{
		case EWindowMode::FULLSCREEN_EXCLUSIVE:
			return createWindowImpl(dimensions, SDL_WINDOW_FULLSCREEN, false);

		case EWindowMode::FULLSCREEN_BORDERLESS_WINDOWED:
			return createWindowImpl(Point(), SDL_WINDOW_FULLSCREEN_DESKTOP, false);

		case EWindowMode::WINDOWED:
			return createWindowImpl(dimensions, SDL_WINDOW_RESIZABLE, true);

		default:
			return nullptr;
	};
#endif

#ifdef VCMI_IOS
	SDL_SetHint(SDL_HINT_IOS_HIDE_HOME_INDICATOR, "1");
	SDL_SetHint(SDL_HINT_RETURN_KEY_HIDES_IME, "1");

	uint32_t windowFlags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_ALLOW_HIGHDPI;
	SDL_Window * result = createWindowImpl(Point(), windowFlags | SDL_WINDOW_METAL, false);

	if(result != nullptr)
		return result;

	logGlobal->warn("Metal unavailable, using OpenGLES");
	return createWindowImpl(Point(), windowFlags, false);
#endif

#ifdef VCMI_ANDROID
	return createWindowImpl(Point(), SDL_WINDOW_RESIZABLE, false);
#endif
}

bool ScreenHandler::onScreenResize(bool keepWindowResolution)
{
	if(getPreferredWindowMode() == EWindowMode::WINDOWED && keepWindowResolution)
	{
		auto res = getRenderResolution();

		if(res.x < heroes3Resolution.x || res.y < heroes3Resolution.y)
			return false;

		Settings video = settings.write["video"];
		video["resolution"]["width"].Integer() = res.x;
		video["resolution"]["height"].Integer() = res.y;
	}
	else if(keepWindowResolution)
		return false;

	recreateWindowAndScreenBuffers();
	return true;
}

void ScreenHandler::validateSettings()
{
#ifndef VCMI_MOBILE
	{
		int displayIndex = settings["video"]["displayIndex"].Integer();
		int displaysCount = SDL_GetNumVideoDisplays();

		if (displayIndex >= displaysCount)
		{
			Settings writer = settings.write["video"]["displayIndex"];
			writer->Float() = 0;
		}
	}

	if (getPreferredWindowMode() == EWindowMode::WINDOWED)
	{
		//we only check that our desired window size fits on screen
		int displayIndex = getPreferredDisplayIndex();
		Point resolution = getPreferredWindowResolution();

		SDL_DisplayMode mode;

		if (SDL_GetDesktopDisplayMode(displayIndex, &mode) == 0)
		{
			if(resolution.x > mode.w || resolution.y > mode.h)
			{
				Settings writer = settings.write["video"]["resolution"];
				writer["width"].Float() = mode.w;
				writer["height"].Float() = mode.h;
			}
		}
	}

	if (getPreferredWindowMode() == EWindowMode::FULLSCREEN_EXCLUSIVE)
	{
		auto legalOptions = getSupportedResolutions();
		Point selectedResolution = getPreferredWindowResolution();

		if(!vstd::contains(legalOptions, selectedResolution))
		{
			// resolution selected for fullscreen mode is not supported by display
			// try to find current display resolution and use it instead as "reasonable default"
			SDL_DisplayMode mode;

			if (SDL_GetDesktopDisplayMode(getPreferredDisplayIndex(), &mode) == 0)
			{
				Settings writer = settings.write["video"]["resolution"];
				writer["width"].Float() = mode.w;
				writer["height"].Float() = mode.h;
			}
		}
	}
#endif
}

int ScreenHandler::getPreferredRenderingDriver() const
{
	int result = -1;
	const JsonNode & video = settings["video"];

	int driversCount = SDL_GetNumRenderDrivers();
	std::string preferredDriverName = video["driver"].String();

	logGlobal->info("Found %d render drivers", driversCount);

	for(int it = 0; it < driversCount; it++)
	{
		SDL_RendererInfo info;
		if (SDL_GetRenderDriverInfo(it, &info) == 0)
		{
			std::string driverName(info.name);

			if(!preferredDriverName.empty() && driverName == preferredDriverName)
			{
				result = it;
				logGlobal->info("\t%s (active)", driverName);
			}
			else
				logGlobal->info("\t%s", driverName);
		}
		else
			logGlobal->info("\t(error)");
	}
	return result;
}

void ScreenHandler::destroyScreenBuffers()
{
	if(nullptr != screen)
	{
		SDL_FreeSurface(screen);
		screen = nullptr;
	}

	if(nullptr != screenTexture)
	{
		SDL_DestroyTexture(screenTexture);
		screenTexture = nullptr;
	}
}

void ScreenHandler::destroyWindow()
{
	if(nullptr != mainRenderer)
	{
		SDL_DestroyRenderer(mainRenderer);
		mainRenderer = nullptr;
	}

	if(nullptr != mainWindow)
	{
		SDL_DestroyWindow(mainWindow);
		mainWindow = nullptr;
	}
}

ScreenHandler::~ScreenHandler()
{
	if(settings["general"]["notifications"].Bool())
		NotificationHandler::destroy();

	destroyScreenBuffers();
	destroyWindow();
	SDL_Quit();
}

void ScreenHandler::clearScreen()
{
	SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
	SDL_RenderClear(mainRenderer);
	SDL_RenderPresent(mainRenderer);
}

Canvas ScreenHandler::getScreenCanvas() const
{
	return Canvas::createFromSurface(screen, CanvasScalingPolicy::AUTO);
}

void ScreenHandler::updateScreenTexture()
{
	if(colorScheme == ColorScheme::NONE)
	{
		SDL_UpdateTexture(screenTexture, nullptr, screen->pixels, screen->pitch);
		return;
	}

	SDL_Surface * screenScheme = SDL_ConvertSurface(screen, screen->format, screen->flags);
	if(colorScheme == ColorScheme::GRAYSCALE)
		CSDL_Ext::convertToGrayscale(screenScheme, Rect(0, 0, screen->w, screen->h));
	else if(colorScheme == ColorScheme::H2_SCHEME)
		CSDL_Ext::convertToH2Scheme(screenScheme, Rect(0, 0, screen->w, screen->h));
	SDL_UpdateTexture(screenTexture, nullptr, screenScheme->pixels, screenScheme->pitch);
	SDL_FreeSurface(screenScheme);
}

void ScreenHandler::presentScreenTexture()
{
	SDL_RenderClear(mainRenderer);
	SDL_RenderCopy(mainRenderer, screenTexture, nullptr, nullptr);
	ENGINE->cursor().render();
	SDL_RenderPresent(mainRenderer);
}

std::vector<Point> ScreenHandler::getSupportedResolutions() const
{
	int displayID = getPreferredDisplayIndex();
	return getSupportedResolutions(displayID);
}

std::vector<Point> ScreenHandler::getSupportedResolutions( int displayIndex) const
{
	//NOTE: this method is never called on Android/iOS, only on desktop systems

	std::vector<Point> result;

	int modesCount = SDL_GetNumDisplayModes(displayIndex);

	for (int i =0; i < modesCount; ++i)
	{
		SDL_DisplayMode mode;
		if (SDL_GetDisplayMode(displayIndex, i, &mode) == 0)
		{
			Point resolution(mode.w, mode.h);
			result.push_back(resolution);
		}
	}

	boost::range::sort(result, [](const auto & left, const auto & right)
	{
		return left.x * left.y < right.x * right.y;
	});

	// erase potential duplicates, e.g. resolutions with different framerate / bits per pixel
	result.erase(boost::unique(result).end(), result.end());

	return result;
}

bool ScreenHandler::hasFocus()
{
	ui32 flags = SDL_GetWindowFlags(mainWindow);
	return flags & SDL_WINDOW_INPUT_FOCUS;
}

void ScreenHandler::setColorScheme(ColorScheme scheme)
{
	colorScheme = scheme;
}

void ScreenHandler::screenShot() const
{
	const boost::filesystem::path outPath = VCMIDirs::get().userExtractedPath() / "screenshots";
	boost::filesystem::create_directories(outPath);
	const boost::filesystem::path filePath = outPath / ("screenshot-" + vstd::getDateTimeISO8601Basic(std::time(nullptr)) + ".png");
	auto img = std::make_shared<SDLImageShared>(screen);
	img->exportBitmap(filePath, nullptr);
	MetaString txt;
	txt.appendTextID("vcmi.client.screenShot");
	txt.replaceRawString(filePath.string());
	if(GAME->interface())
		GAME->server().getGameChat().sendMessageGameplay(txt.toString());
}