?SDL(Simple DirectMediaLayer)是一套開放源代碼的跨平臺多媒體開發(fā)庫,使用C語言寫成。SDL提供了數(shù)種控制圖像、聲音、輸出入的函數(shù),讓開發(fā)者只要用相同或是相似的代碼就可以開發(fā)出跨多個平臺(Linux、Windows、MacOS X等)的應(yīng)用軟件?,F(xiàn)SDL多用于開發(fā)游戲、模擬器、媒體播放器等多媒體應(yīng)用領(lǐng)域。
1.天氣獲取
?? 天氣獲取采用命令行瀏覽器curl,天氣獲取接口使用心知天氣網(wǎng);
??心知天氣是中國氣象局官方授權(quán)的商業(yè)氣象服務(wù)公司,基于氣象數(shù)值預(yù)報和人工智能技術(shù),提供高精度氣象數(shù)據(jù)、天氣監(jiān)控機器人、氣象數(shù)據(jù)可視化產(chǎn)品,本次天氣數(shù)據(jù)獲取從心知天氣網(wǎng)平臺獲取。
2.天氣獲取與解析示例
/******************解析 天氣數(shù)據(jù)**************** 形參:u8* buff原始數(shù)據(jù) u8 *Weather_stat天氣數(shù)據(jù)標(biāo)志 u8 *data解析獲取到的數(shù)據(jù) 返回值: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; } /*獲取天氣數(shù)據(jù)*/ 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"}]} */ /*解析天氣數(shù)據(jù)*/ 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);//字符串轉(zhuǎn)整數(shù) } //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);//將秒單位時間轉(zhuǎn)換為時間結(jié)構(gòu)體 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獲取一次天氣數(shù)據(jù)*/ { 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);/*創(chuàng)建*/ 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);//更新顯示 }
審核編輯:湯梓紅
-
Linux
+關(guān)注
關(guān)注
87文章
11420瀏覽量
212321 -
SDL
+關(guān)注
關(guān)注
0文章
18瀏覽量
7597 -
開發(fā)庫
+關(guān)注
關(guān)注
0文章
7瀏覽量
4881
發(fā)布評論請先 登錄
相關(guān)推薦
ESP8266簡易WIFI天氣時鐘

SDL編譯安裝圖片顯示

Linux下基于SDL庫貪吃蛇游戲

天氣預(yù)警收音機方案
SDL的交叉編譯問題
移植SDL到JZ2440顯示BMP圖片
OLED屏幕顯示時間天氣設(shè)計實現(xiàn)
STM32+ESP8266獲取網(wǎng)絡(luò)時間和天氣的方法
GUI向?qū)Ь幾g錯誤,找不到“SDL2/SDL.h”文件是怎么回事?
基于SDL的自動售票系統(tǒng)的研發(fā)
SDL1000X系列可編程直流電子負載的產(chǎn)品介紹

SDL下載與配置

評論