SDL时间和天气显示
1.天气获取
天气获取采用命令行浏览器curl,天气获取接口使用心知天气网;
心知天气网相关参考https://blog.csdn.net/weixin_44453694/article/details/111874441
2.天气获取与解析示例
/******************解析 天气数据****************
形参:u8* buff原始数据
u8 *Weather_stat天气数据标志
u8 *data解析获取到的数据
返回值:0---成功,其他值---失败
************************************************/
u8 Weather_analysis(u8* buff,u8 *Weather_stat,u8 *data)
{
char *p=NULL;
u16 i=0;
p=strstr((char *)buff,(char *)Weather_stat);//获取温度
if(p)
{
p+=strlen((char *)Weather_stat)+2;
i=0;
while(*p!='\"' && *p!='\0')
{
data[i++]=*p++;
}
data[i]='\0';
return 0;
}
else return 1;
}
/*获取天气数据*/
int Http_GetWeather(void)
{
FILE *fp=popen("curl api.seniverse.com/v3/weather/now.json?key=SwD4-ybQxhedD1z7U'&'location=nanchang'&'language=zh-Hans'&'unit=c","r");
if(fp==NULL)
{
printf("重定向失败\n");
return -1;
}
char wthread_buff[1025];
int cnt=fread(wthread_buff,1,1024,fp);
wthread_buff[cnt]='\0';
char buff[100];
wchar_t wc_buff[200];
int stat;
/*
{"results":[{"location":{"id":"WT47HJP3HEMP","name":"南昌","country":"CN","path":"南昌,南昌,江西,中国",
"timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"阴","code":"9","temperature":"16"},"last_update":"2021-11-20T16:57:46+08:00"}]}
*/
/*解析天气数据*/
Weather_analysis(wthread_buff,(u8 *)"\"name\"",(u8 *)weather_info.city_name);//城市名称
if(!Weather_analysis(wthread_buff,(u8 *)"\"temperature\"",(u8 *)buff))//获取温度
{
snprintf((char *)weather_info.city_temp,sizeof(weather_info.city_temp),"%s℃",buff);
}
Weather_analysis(wthread_buff,"\"text\"",(u8 *)weather_info.city_weather);
if(!Weather_analysis(wthread_buff,(u8 *)"\"code\"",(u8 *)buff))//天气代码
{
weather_info.city_code=atoi(buff);//字符串转整数
}
//printf("name:%s\ttemp:%s\t天气:%s\t天气代号:%d\n",weather_info.city_name,weather_info.city_temp,weather_info.city_weather,weather_info.city_code);
pclose(fp);
return 0;
}
3.显示时间和天气示例
sec=time(NULL);
if(sec!=sec2)
{
sec2=sec;
count++;
localtime_r(&sec2,&result);//将秒单位时间转换为时间结构体
strftime(buff,sizeof(buff),"%H:%M",&result);//时间格式化打印
//printf("buff=%s\n",buff);
TTF_SetFontSize(ttffont,100);
surface1=TTF_RenderUTF8_Blended(ttffont,buff,color2);
rect.h=surface1->h;
rect.w=surface1->w;
rect.x=800/2-surface1->w/2;/*要显示的x起始位置*/
rect.y=120;/*要显示的y起始位置*/
srcrect.h=surface1->h;
srcrect.w=surface1->w;
sdltext1=SDL_CreateTextureFromSurface(render,surface1);
SDL_RenderCopy(render,sdltext2,&rect,&rect);
SDL_RenderCopy(render,sdltext1,&srcrect,&rect);
SDL_FreeSurface(surface1);/*释放surface*/
SDL_DestroyTexture(sdltext1);/*释放表面*/
TTF_SetFontSize(ttffont,50);
strftime(week_buff,sizeof(week_buff),"%w",&result);
week_cnt=atoi(week_buff);
strftime(buff,sizeof(buff),"%Y/%m/%d 星期",&result);//时间格式化打印
strncat(buff,week[week_cnt],sizeof(buff));
strncat(buff," | ",sizeof(buff));
surface2=TTF_RenderUTF8_Blended(ttffont,buff,color2);
rect.h=surface2->h;
rect.w=surface2->w;
rect.x=800/2-surface2->w/2-40;/*要显示的x起始位置*/
rect.y=240;/*要显示的y起始位置*/
wather_x=rect.x+surface2->w;
wather_y=rect.y;
srcrect.h=surface2->h;
srcrect.w=surface2->w;
sdltext3=SDL_CreateTextureFromSurface(render,surface2);
SDL_RenderCopy(render,sdltext2,&rect,&rect);
SDL_RenderCopy(render,sdltext3,&srcrect,&rect);
SDL_FreeSurface(surface2);/*释放surface*/
SDL_DestroyTexture(sdltext3);/*释放表面*/
if(count>=60)/*60s获取一次天气数据*/
{
count=0;
if(!Http_GetWeather())/*获取天气*/
{
char image[50];
snprintf(image,sizeof(image),"./weather_photo/%d.png",weather_info.city_code);
SDL_Surface *weather_surface=IMG_Load(image);
if(weather_surface)
{
SDL_Rect rect;
rect.x=wather_x;
rect.y=wather_y;
rect.w=50;
rect.h=50;
srcrect.h=weather_surface->h;
srcrect.w=weather_surface->w;
SDL_Texture *sdltext=SDL_CreateTextureFromSurface(render,weather_surface);/*创建*/
SDL_RenderCopy(render,sdltext2,&rect,&rect);
SDL_RenderCopy(render,sdltext,&srcrect,&rect);
SDL_FreeSurface(weather_surface);/*释放surface*/
SDL_DestroyTexture(sdltext);/*释放表面*/
/*显示温度*/
SDL_Surface *temp_surface=TTF_RenderUTF8_Blended(ttffont,weather_info.city_temp,color2);
SDL_Texture *temp_sdltext=SDL_CreateTextureFromSurface(render,temp_surface);
rect.x=wather_x+50;
rect.y=wather_y;
srcrect.h=temp_surface->h;
srcrect.w=temp_surface->w;
SDL_RenderCopy(render,sdltext2,&rect,&rect);
SDL_RenderCopy(render,temp_sdltext,&srcrect,&rect);
SDL_FreeSurface(temp_surface);/*释放surface*/
SDL_DestroyTexture(temp_sdltext);/*释放表面*/
}
}
}
SDL_RenderPresent(render);//更新显示
}