浏览代码

Fall back to first available resolution if config contains invalid value.

Fixes crash on starting game with unavailable resolution
Ivan Savenko 11 年之前
父节点
当前提交
9a4aa8749f
共有 2 个文件被更改,包括 32 次插入7 次删除
  1. 25 5
      client/CMT.cpp
  2. 7 2
      lib/CConfigHandler.h

+ 25 - 5
client/CMT.cpp

@@ -357,17 +357,37 @@ int main(int argc, char** argv)
 			
 			std::string driverName(info.name);
 			
-						
-			logGlobal->infoStream() << "\t" << driverName;
-			
 			if(!preferredDriverName.empty() && driverName == preferredDriverName)
 			{
 				preferredDriverIndex = it;
-				logGlobal->infoStream() << "\t\twill select this";
-			}					
+				logGlobal->infoStream() << "\t" << driverName << " (active)";
+			}
+			else
+				logGlobal->infoStream() << "\t" << driverName;
 		}			
 		#endif // VCMI_SDL1	
 		
+		config::CConfigHandler::GuiOptionsMap::key_type resPair(res["width"].Float(), res["height"].Float());
+		if (conf.guiOptions.count(resPair) == 0)
+		{
+			// selected resolution was not found - complain & fallback to something that we do have.
+			logGlobal->errorStream() << "Selected resolution " << resPair.first << "x" << resPair.second << " was not found!";
+			if (conf.guiOptions.empty())
+			{
+				logGlobal->errorStream() << "Unable to continue - no valid resolutions found! Please reinstall VCMI to fix this";
+				exit(1);
+			}
+			else
+			{
+				Settings newRes = settings.write["video"]["screenRes"];
+				newRes["width"].Float()  = conf.guiOptions.begin()->first.first;
+				newRes["height"].Float() = conf.guiOptions.begin()->first.second;
+				conf.SetResolution(newRes["width"].Float(), newRes["height"].Float());
+
+				logGlobal->errorStream() << "Falling back to " << newRes["width"].Float() << "x" << newRes["height"].Float();
+			}
+		}
+
 		setScreenRes(res["width"].Float(), res["height"].Float(), video["bitsPerPixel"].Float(), video["fullscreen"].Bool());
 		logGlobal->infoStream() <<"\tInitializing screen: "<<pomtime.getDiff();
 	}

+ 7 - 2
lib/CConfigHandler.h

@@ -170,8 +170,13 @@ namespace config
 		~CConfigHandler(void); //d-tor
 
 		GUIOptions *go() { return current; };
-		void SetResolution(int x, int y) {
-			current = &guiOptions[std::pair<int,int>(x, y)];
+		void SetResolution(int x, int y)
+		{
+			std::pair<int,int> index(x, y);
+			if (guiOptions.count(index) == 0)
+				current = nullptr;
+			else
+				current = &guiOptions.at(index);
 		}
 	};
 }