From 3eda5c5ac4f87cab943779f1e20ff10a6dc0959b Mon Sep 17 00:00:00 2001
From: Tim Felgentreff <timfelgentreff@gmail.com>
Date: Sun, 11 Jun 2017 21:54:18 +0200
Subject: [PATCH] make sure viewport width and height are always initialized

---
 src/include/video.h  |  4 +---
 src/ui/script_ui.cpp | 15 ++++++++++++---
 src/video/sdl.cpp    |  2 +-
 src/video/video.cpp  |  2 +-
 4 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/src/include/video.h b/src/include/video.h
index 5f81444a0..46df30758 100644
--- a/src/include/video.h
+++ b/src/include/video.h
@@ -267,7 +267,7 @@ struct EventCallback {
 class CVideo
 {
 public:
-	CVideo() : Width(0), Height(0), Depth(0), ShaderIndex(0), FullScreen(false) {}
+	CVideo() : Width(0), Height(0), ViewportWidth(0), ViewportHeight(0), Depth(0), ShaderIndex(0), FullScreen(false) {}
 
 	void LockScreen();
 	void UnlockScreen();
@@ -373,10 +373,8 @@ public:
 
 	int Width;
 	int Height;
-#if defined(USE_OPENGL) || defined(USE_GLES)
 	int ViewportWidth;         /// Actual width of the window
 	int ViewportHeight;        /// Actual height of the window
-#endif
 #if defined(USE_TOUCHSCREEN) && defined(USE_WIN32)
 	SDL_Cursor *blankCursor;
 #endif
diff --git a/src/ui/script_ui.cpp b/src/ui/script_ui.cpp
index d8fc58e58..c98413a08 100644
--- a/src/ui/script_ui.cpp
+++ b/src/ui/script_ui.cpp
@@ -301,9 +301,18 @@ static int CclSetVideoResolution(lua_State *l)
 	LuaCheckArgs(l, 2);
 	if (CclInConfigFile) {
 		// May have been set from the command line
-		if (!Video.Width || !Video.Height) {
-			Video.Width = LuaToNumber(l, 1);
-			Video.Height = LuaToNumber(l, 2);
+		if (!Video.ViewportWidth || !Video.ViewportHeight) {
+			Video.ViewportWidth = LuaToNumber(l, 1);
+			Video.ViewportHeight = LuaToNumber(l, 2);
+#if defined(USE_OPENGL) || defined(USE_GLES)
+			if (!ZoomNoResize) {
+				Video.Height = Video.ViewportHeight;
+				Video.Width = Video.ViewportWidth;
+			}
+#else
+			Video.Height = Video.ViewportHeight;
+			Video.Width = Video.ViewportWidth;
+#endif
 		}
 	}
 	return 0;
diff --git a/src/video/sdl.cpp b/src/video/sdl.cpp
index 01ddfaece..bcc1022de 100644
--- a/src/video/sdl.cpp
+++ b/src/video/sdl.cpp
@@ -612,11 +612,11 @@ void InitVideoSdl()
 		Video.Depth = 32;
 	}
 
-#if defined(USE_OPENGL) || defined(USE_GLES)
 	if (!Video.ViewportWidth || !Video.ViewportHeight) {
 		Video.ViewportWidth = Video.Width;
 		Video.ViewportHeight = Video.Height;
 	}
+#if defined(USE_OPENGL) || defined(USE_GLES)
 	TheScreen = SDL_SetVideoMode(Video.ViewportWidth, Video.ViewportHeight, Video.Depth, flags);
 #else
 	TheScreen = SDL_SetVideoMode(Video.Width, Video.Height, Video.Depth, flags);
diff --git a/src/video/video.cpp b/src/video/video.cpp
index 4eec2b261..50f17f91d 100644
--- a/src/video/video.cpp
+++ b/src/video/video.cpp
@@ -282,9 +282,9 @@ bool CVideo::ResizeScreen(int w, int h)
 		}
 #endif
 		TheScreen = SDL_SetVideoMode(w, h, TheScreen->format->BitsPerPixel, TheScreen->flags);
-#if defined(USE_OPENGL) || defined(USE_GLES)
 		ViewportWidth = w;
 		ViewportHeight = h;
+#if defined(USE_OPENGL) || defined(USE_GLES)
 		if (ZoomNoResize) {
 			ReloadOpenGL();
 		} else {