这是一个简单的时钟,用到了 SDL2 的普通颜色板纹理、自定义事件、定时器。
// 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; }