這是一個簡單的時鐘,用到了 SDL2 的普通顏色板紋理、自定義事件、定時器。windows
// Time.c // SDL2 時鐘:自定義消息與計時器 // gcc -mwindows -o Time Time.c -lSDL2 -lSDL2main -lSDL2_ttf // C4droid //#define _DEBUG_ #include <stdio.h> #include <string.h> #include <time.h> #include <SDL2/SDL.h> #include <SDL2/SDL_ttf.h> // 字符串常量 char szWindowTitle[] = "SDL2 時鐘"; char *szWeekCN[7] = {"天", "一", "二", "三", "四", "五", "六"}; // 星期漢字 char szTimeFormat[] = "%d年%02d月%02d日 星期%s %02d點%02d分%02d秒"; // 時鐘格式 char *szFontFile[2] = {"/system/fonts/DroidSansFallback.ttf", // 安卓系統中字體文件 "C:/Windows/Fonts/msyh.ttf"}; // Windows系統中字體文件 Uint32 TimerFunc(Uint32 nInterval, void *param); SDL_Texture *GetColorTexture(SDL_Renderer *pRenderer, int nWidth, int nHeight, int color); SDL_Texture *GetStringTexture(SDL_Renderer *pRenderer, char *szString, TTF_Font *pFont, int color); void PrintString(SDL_Renderer *pRenderer, SDL_Rect *prt, char *szString, TTF_Font *pFont, int color); #undef main int main(int argc, char *argv[]) { int nWindowWidth, nWindowHeight; // 窗口尺寸 SDL_Window *pWindow = NULL; // 窗口 SDL_Renderer *pRenderer = NULL; // 渲染器 SDL_Texture *pBackTexture = NULL; // 背景紋理 SDL_Texture *pTextTexture = NULL; // 文字紋理 TTF_Font *pFont = NULL; // 文字字體 SDL_Rect rt; // 文字位置 SDL_TimerID iTimeID; // 定時器ID Uint32 SDL_MyTimerEvent; // 自定義事件類型ID Uint32 nInterval = 1000; // 定時間隔 SDL_Event event; _Bool bRun = 1; char szMsg[256]; time_t nRawTime; struct tm *pt; // 初始化 if(SDL_Init(SDL_INIT_EVERYTHING) == -1 || TTF_Init() == -1) { #ifdef _DEBUG_ fprintf(stderr, "1 %s", SDL_GetError()); #endif goto label_error; } // 建立主窗口及其渲染器、背景板 if(SDL_CreateWindowAndRenderer(0, 0, SDL_WINDOW_FULLSCREEN, &pWindow, &pRenderer) == -1) { #ifdef _DEBUG_ fprintf(stderr, "2 %s", SDL_GetError()); #endif goto label_error; } SDL_SetWindowTitle(pWindow, szWindowTitle); SDL_GetWindowSize(pWindow, &nWindowWidth, &nWindowHeight); rt.x = 0; rt.y = nWindowHeight/2-20; rt.w = nWindowWidth; rt.h = 40; if(NULL == (pBackTexture = GetColorTexture(pRenderer, nWindowWidth, nWindowHeight, 0xFFFF))) { #ifdef _DEBUG_ fprintf(stderr, "3 %s", SDL_GetError()); #endif goto label_error; } // 加載字體文件 if(strcmp(SDL_GetPlatform(), "Android") == 0) pFont = TTF_OpenFont(szFontFile[0], 20); else if(strcmp(SDL_GetPlatform(), "Windows") == 0) pFont = TTF_OpenFont(szFontFile[1], 20); if(NULL == pFont) { #ifdef _DEBUG_ fprintf(stderr, "4 %s", SDL_GetError()); #endif goto label_error; } // 取得定義定時器觸發的事件類型號 if((SDL_MyTimerEvent = SDL_RegisterEvents(1)) == -1) { #ifdef _DEBUG_ fprintf(stderr, "5 %s", SDL_GetError()); #endif goto label_error; } // 啓動定時器 if((iTimeID = SDL_AddTimer(nInterval, TimerFunc, &SDL_MyTimerEvent)) == 0) { #ifdef _DEBUG_ fprintf(stderr, "6 %s", SDL_GetError()); #endif goto label_error; } while(bRun && SDL_WaitEvent(&event)) { switch (event.type) { case SDL_USEREVENT : if(event.type == SDL_MyTimerEvent) { time(&nRawTime); pt = localtime(&nRawTime); sprintf(szMsg, szTimeFormat, pt->tm_year+1900, pt->tm_mon+1, pt->tm_mday, szWeekCN[pt->tm_wday], pt->tm_hour, pt->tm_min, pt->tm_sec); SDL_RenderClear(pRenderer); SDL_RenderCopyEx(pRenderer, pBackTexture, NULL, NULL, 0, NULL, SDL_FLIP_NONE); PrintString(pRenderer, &rt, szMsg, pFont, 0); SDL_RenderPresent(pRenderer); } break; case SDL_QUIT : bRun = 0; break; } } // 異常退出 label_error: if(pRenderer != NULL) SDL_DestroyRenderer(pRenderer); if(pWindow != NULL) SDL_DestroyWindow(pWindow); if(pFont != NULL) TTF_CloseFont(pFont); TTF_Quit(); SDL_Quit(); return 0; } // 顯示文字時鐘 // 參數:pRenderer = 渲染器;prt = 顯示文字的位置;szString = 文字內容;pFont = 字體;pColor = 文字顏色 void PrintString(SDL_Renderer *pRenderer, SDL_Rect *prt, char *szString, TTF_Font *pFont, int color) { SDL_Texture *pTextTexture; if((pTextTexture = GetStringTexture(pRenderer, szString, pFont, color)) != NULL) { SDL_RenderCopyEx(pRenderer, pTextTexture, NULL, prt, 0, NULL, SDL_FLIP_NONE); SDL_DestroyTexture(pTextTexture); } } // 定時器處理過程 // 參數:nInterval = 定時間隔;param = 參數(這裏給的是自定義事件類型ID) Uint32 TimerFunc(Uint32 nInterval, void *param) { SDL_Event event; SDL_zero(event); event.type = *((Uint32 *)param); SDL_PushEvent(&event); return nInterval; } // 取得字符串紋理 // 參數:pRenderer = 渲染器;szString = 字符串內容;pFont = 字體;color = 文字顏色 // 返回值:紋理指針 SDL_Texture *GetStringTexture(SDL_Renderer *pRenderer, char *szString, TTF_Font *pFont, int color) { SDL_Texture *pTexture; SDL_Surface *pSurface; SDL_Color c = {color>>16, (color>>8)&0xFF, color&0xFF}; if((pSurface = TTF_RenderUTF8_Blended(pFont, szString, c)) == NULL) return NULL; pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface); SDL_FreeSurface(pSurface); return pTexture; } // 取得顏色板紋理 // 參數:pRenderer = 渲染器;nWidth,nHeight = 顏色板尺寸;color = 顏色 // 返回值:紋理指針 SDL_Texture *GetColorTexture(SDL_Renderer *pRenderer, int nWidth, int nHeight, int color) { SDL_Texture *pTexture; SDL_Surface *pSurface; if((pSurface = SDL_CreateRGBSurface(0, nWidth, nHeight, 32, 0, 0, 0, 0)) == NULL) return NULL; SDL_FillRect(pSurface, NULL, color); pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface); SDL_FreeSurface(pSurface); return pTexture; }