飞行的天气数据一般包括地面温度、地面气压、降雨量、日期、时间、风速、风向、能见度。通过xPlane11的SDK来设置飞行的天气数据,一般需要使对应的sdk接口;
地面温度:
XPLMDataRef earth_temp = XPLMFindDataRef("sim/weather/temperature_sealevel_c");
地面气压:
XPLMDataRef earth_pressure = XPLMFindDataRef("sim/weather/barometer_sealevel_inhg");
降雨量:
XPLMDataRef rain_percent = XPLMFindDataRef("sim/weather/rain_percent");
风速:
XPLMDataRef wind_speed = XPLMFindDataRef("sim/weather/wind_speed_kt[X]");
风向:
XPLMDataRef wind_dir = XPLMFindDataRef("sim/weather/wind_direction_degt[X]");
能见度:
XPLMDataRef visibility = XPLMFindDataRef("sim/weather/visibility_reported_m");
使用sdk进行设置xPlane11飞行的天气数据:
/*地面温度*/
XPLMSetDataf(earth_temp, m_weatherData.Temp);
/*地面气压*/
XPLMSetDataf(earth_pressure, m_weatherData.QNH);
/*风速*/
XPLMSetDataf(wind_speed, m_weatherData.WinSpeed);
/*风向*/
XPLMSetDataf(wind_dir, m_weatherData.WindDir);
/*能见度*/
XPLMSetDataf(visibility, m_weatherData.Visibility);
/*日期*/
m_weatherset.setDate(_year, _month, _day);
/*时间*/
m_weatherset.setTime(_hour, _miniter, _second);
/*晴*/
m_weatherset.setWeather(0, 0);
/*雨*/
m_weatherset.setWeather(1, 1);
/*雪*/
m_weatherset.setWeather(1, 1);
XPLMSetDataf(earth_temp, -10.0);//温度
/*雾*/
m_weatherset.setWeather(0, 0);
XPLMSetDataf(visibility, 1000);//能见度
/*闪电*/
m_weatherset.setWeather(2, 1);
XPLMSetDataf(visibility, 2000);//能见度
使用sdk设置xplane11飞行的日期时间:
struct XP_DATE_SETTING
{
XPLMDataRef dr_datesetting;
XP_DATE_SETTING()
{
dr_datesetting = XPLMFindDataRef("sim/time/local_date_days");
}
void setData(int year, int month, int day);
int caldays(int year1, int month1, int day1, int year2, int month2, int day2)
{
int monthdays[2][12] = { { 31,28,31,30,31,30,31,31,30,31,30,31 },{ 31,29,31,30,31,30,31,31,30,31,30,31 } };
int yeardays[2] = { 365,366 };
auto isLearyear = [&](int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
return 1;
else
return 0;
};
int sumdays = 0;
if (year1 == year2 && month1 == month2)
{
sumdays = day2 - day1;
}
else
{
if (year1 == year2)
{
sumdays += monthdays[isLearyear(year1)][month1 - 1] - day1;
for (int i = month1; i < month2 - 1; i++)
sumdays += monthdays[isLearyear(year1)][i];
sumdays += day2;
}
else
{
sumdays += monthdays[isLearyear(year1)][month1 - 1] - day1;
for (int i = month1; i < 12; i++)
sumdays += monthdays[isLearyear(year1)][i];
for (int i = year1 + 1; i < year2; i++)
sumdays += yeardays[isLearyear(i)];
for (int i = 0; i < month2 - 1; i++)
sumdays += monthdays[isLearyear(year2)][i];
sumdays += day2;
}
}
return sumdays;
}
};
struct XP_TIME_SETTING
{
XPLMDataRef dr_timesetting;
XP_TIME_SETTING()
{
dr_timesetting = XPLMFindDataRef("sim/time/zulu_time_sec");
}
void setData(int hour, int miniter, int second);
int calSeconds(int hours, int minitors, int seconds)
{
int h = hours;
int secs = h * 60*60 + minitors * 60 + seconds;
return secs;
}
};
通过sdk设置晴、雨、雪、雾、闪电等天气:
//云
struct XP_CLOUD_SETTING
{
int index;
XPLMDataRef dr_cloudtype;//云类型
XPLMDataRef dr_cloudCovege;//云覆盖范围
XPLMDataRef dr_cloudBaseAlt;//云底部高度
XPLMDataRef dr_cloudTopAlt;//云顶部高度
XP_CLOUD_SETTING(int tmpindex)
{
index = tmpindex > 2 ? 0 : tmpindex;
char cloudtype_str[40];
char cloudcovege_str[40];
char cloudbasealt_str[40];
char cloudtopsalt_str[40];
strcpy_s(cloudtype_str, "sim/weather/cloud_type[X]");
strcpy_s(cloudcovege_str, "sim/weather/cloud_coverage[X]");
strcpy_s(cloudbasealt_str, "sim/weather/cloud_base_msl_m[X]");
strcpy_s(cloudtopsalt_str, "sim/weather/cloud_tops_msl_m[X]");
char cTemp = index + 0x30;
cloudtype_str[24] = cTemp;
cloudcovege_str[28] = cTemp;
cloudbasealt_str[30] = cTemp;
cloudtopsalt_str[30] = cTemp;
dr_cloudtype = XPLMFindDataRef(cloudtype_str);
dr_cloudCovege = XPLMFindDataRef(cloudcovege_str);
dr_cloudBaseAlt = XPLMFindDataRef(cloudbasealt_str);
dr_cloudTopAlt = XPLMFindDataRef(cloudtopsalt_str);
}
void setData(int type, float covege, float basealt, float topsalt);
};
//能见度
struct XP_VISIBILITY_SETTING
{
XPLMDataRef dr_visibility;
XP_VISIBILITY_SETTING()
{
dr_visibility = XPLMFindDataRef("sim/weather/visibility_reported_m");
}
void setData(float value);
};
//雷暴
struct XP_THUNDERSTORMS_SETTING
{
XPLMDataRef dr_thunderstorms;
XP_THUNDERSTORMS_SETTING()
{
dr_thunderstorms = XPLMFindDataRef("sim/weather/thunderstorm_percent");
}
void setData(float thunderstorms_per);
};
//常风
struct XP_WIND_SETTING
{
int index;
XPLMDataRef dr_dir;
XPLMDataRef dr_speed;
XPLMDataRef dr_windalt;
XP_WIND_SETTING(int tmpindex)
{
index = tmpindex > 2 ? 0 : tmpindex;
char windspeed_str[40];
char windalt_str[40];
char winddir_str[40];
strcpy_s(windalt_str, "sim/weather/wind_altitude_msl_m[X]");
strcpy_s(winddir_str, "sim/weather/wind_direction_degt[X]");
strcpy_s(windspeed_str, "sim/weather/wind_speed_kt[X]");
char cTemp = index + 0x30;
windalt_str[33] = cTemp;
winddir_str[33] = cTemp;
windspeed_str[28] = cTemp;
dr_dir = XPLMFindDataRef(winddir_str);
dr_speed = XPLMFindDataRef(windspeed_str);
dr_windalt = XPLMFindDataRef(windalt_str);
}
void setData(float windalt, float windspeed, float winddir);
};
/*设置天气函数,type-天气类型,factor-值*/
void WeatherSet::setWeather(int type,float factor)
{
switch (type)
{
case 0:
{
XP_RAIN_SETTING rainsetting;
rainsetting.setData(0);
XP_THUNDERSTORMS_SETTING thundsetting;
thundsetting.setData(0);
}
break;
case 1:
{
XP_RAIN_SETTING rainsetting;
rainsetting.setData(factor);
setCloud(0, 4, 6, 3000, 4000);
setCloud(1, 0, 0, 0, 0);
setCloud(2, 0, 0, 0, 0);
}
break;
case 2:
{
XP_RAIN_SETTING rainsetting;
rainsetting.setData(factor);
XP_THUNDERSTORMS_SETTING thundsetting;
thundsetting.setData(factor);
}
break;
default:
{
XP_RAIN_SETTING rainsetting;
rainsetting.setData(0);
XP_THUNDERSTORMS_SETTING thundsetting;
thundsetting.setData(0);
}
break;
}
}
/*设置雨量大小函数,rainper-雨量百分比*/
void XP_RAIN_SETTING::setData(float rain_per)
{
XPLMDataRef dr_rate_change = XPLMFindDataRef("sim/weather/rate_change_percent");
XPLMSetDataf(dr_rate_change, -1.0);
XPLMSetDataf(dr_precipitation, 1.0);
XPLMSetDataf(dr_rain, rain_per);
}
/*设置雷暴函数,thunderstorms_per-雷暴值*/
void XP_THUNDERSTORMS_SETTING::setData(float thunderstorms_per)
{
XPLMSetDataf(dr_thunderstorms, thunderstorms_per);
}
/*设置云层函数*/
void WeatherSet::setCloud(int index, int type, float coverage, float baseAlt, float topAlt)
{
int tmpindex = -1;
for (auto iter = m_index_type_cloudMap.begin(); iter != m_index_type_cloudMap.end(); ++iter)
{
if (iter->second == type)
{
tmpindex = iter->first;
break;
}
}
if (tmpindex == -1)//如果存在则更新,不存在则覆盖
{
tmpindex = index;
}
XP_CLOUD_SETTING cloudsetting(tmpindex);
cloudsetting.setData(type, coverage, baseAlt, topAlt);
m_index_type_cloudMap.insert(std::make_pair(index, type));
}
/*设置云函数*/
void XP_CLOUD_SETTING::setData(int type, float covege, float basealt, float topsalt)
{
XPLMSetDataf(dr_cloudtype, type);
XPLMSetDataf(dr_cloudCovege, covege);
XPLMSetDataf(dr_cloudBaseAlt, basealt);
XPLMSetDataf(dr_cloudTopAlt, topsalt);
}
版权声明:本文为博主原创文章,转载请附上博文链接!