WorldWind源码剖析系列:星球球体的加载与渲染
WorldWind源码剖析系列:星球球体的加载与渲染
WorldWind中主函数Main()的分析
在文件WorldWind.cs中主函数Main()依次作以下几个事情:
1. 使用System.Version在内部,读取软件版本信息,并格式化输出。我们在外面配置软件版本,“关于”部分中版本自动更改。
获取格式化版本号
// Establish the version number string used for user display,
// such as the Splash and Help->About screens.
// To change the Application.ProductVersion make the
// changes in \WorldWind\AssemblyInfo.cs
// For alpha/beta versions, include " alphaN" or " betaN"
// at the end of the format string.
Version ver = new System.Version(Application.ProductVersion);
Release = string.Format("{0}.{1}.{2}.{3}", ver.Major, ver.Minor, ver.Build, ver.Revision);
2.判断该软件是否已经有个实例启动,如果有,则不启动。
判断是否已启动实例
// If World Wind is already running, pass any commandline
// arguments from this instance, and quit.
IntPtr handle = GetWWHandle(); //此处通过获取线程指针
if (!System.IntPtr.Zero.Equals(handle))
{
if(args.Length>0)
NativeMethods.SendArgs( handle, string.Join("\n",args) );
return;
}
3.判断计算机中已经存在的协议数目,并给当前主线程命名。主要是针对.Net FrameWork 1.1版的bug(不允许多于50个协议)。因为该软件要启用自己的新协议"worldwind://"。现在2.0以后不存在该问题啦。
//abort if 50 bindings problem present and user opts to go to the download page
if(BindingsCheck.FiftyBindingsWarning())
return;
// Name the main thread
System.Threading.Thread.CurrentThread.Name = "Main Thread";
4.解析Main(string[] args)中参数args。主要是在控制台中启动程序时同时赋予了参数的形式。
// ParseArgs may set values that are used elsewhere,
// such as startFullScreen and CurrentSettingsDirectory.
ParseArgs(args);
args中参数可能是:
"worldwind://":加载定位显示球体某处。
“/f” :全屏启动。
“/s=……”:指定加载“配置”的文件夹路径。
这里要注意的事,Main函数一般是没有参数的,如果我们以后要写可以在控制台下给启动程序传入参数,可以借鉴一下。
5.加载上次使用的配置信息,包括上次使用的WorldWind主窗体使用信息和上次使用的World球体显示信息。
加载配置
if(CurrentSettingsDirectory == null)
{
// load program settings from default directory
LoadSettings();
World.LoadSettings();
}
else
{
LoadSettings(CurrentSettingsDirectory);
World.LoadSettings(CurrentSettingsDirectory);
}
6.启动主程序,,还有对线程异常事件处理和程序空闲处理(防止过度休眠)。
Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
MainApplication app = new MainApplication();//该构造函数中完成星球球体的加载与渲染
Application.Idle += new EventHandler(app.WorldWindow.OnApplicationIdle);
Application.Run(app);
7.程序启动后,将状态配置保存起来。
// Save World settings
World.Settings.Save();
其中用到了,将World的配置对象WorldSettings(注意:不是其父类SettingsBase,只是调用其父类的Save方法)序列化为XML文件,并保存,以备下次启动前读取World(即球体)上次状态配置。
将WorldSettings序列化
public virtual void Save(string fileName)
{
XmlSerializer ser = null;
try
{
ser = new XmlSerializer(this.GetType());
using(TextWriter tw = new StreamWriter(fileName))
{
ser.Serialize(tw, this);
}
}
catch(Exception ex)
{
throw new System.Exception(String.Format("Saving settings class ‘{0}‘ to {1} failed", this.GetType().ToString(), fileName), ex);
}
}
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/71217.html