SDL时间和天气显示-屏幕时间和天气不见了怎么恢复

 SDL(Simple DirectMediaLayer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成。SDL提供了数种控制图像、声音、输出入的函数,让开发者只要用相同或是相似的代码就可以开发出跨多个平台(Linux、Windows、MacOS X等)的应用软件。现SDL多用于开发游戏、模拟器、媒体播放器等多媒体应用领域。

1.天气获取

   天气获取采用命令行浏览器curl,天气获取接口使用心知天气网;

  心知天气是中国气象局官方授权的商业气象服务公司,基于气象数值预报和人工智能技术,提供高精度气象数据、天气监控机器人、气象数据可视化产品,本次天气数据获取从心知天气网平台获取。

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!=�) { data[i++]=*p++; } data[i]=�; 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]=�; 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:%sttemp:%st天气:%st天气代号:%dn”,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=%sn”,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);//更新显示 }

审核编辑:汤梓红

免责声明:文章内容来自互联网,本站不对其真实性负责,也不承担任何法律责任,如有侵权等情况,请与本站联系删除。
转载请注明出处:SDL时间和天气显示-屏幕时间和天气不见了怎么恢复 https://www.yhzz.com.cn/a/5903.html

上一篇 2023-04-12 16:31:50
下一篇 2023-04-12 16:36:07

相关推荐

联系云恒

在线留言: 我要留言
客服热线:400-600-0310
工作时间:周一至周六,08:30-17:30,节假日休息。