SDL2 跨越多种平台,那个五子棋很容易移至到 Android 上去,只需要调整下 Five.c 中的消息响应(把鼠标消息改成触摸消息),在调整下窗口风格为全屏即可。另外,安卓下SDL_mixer 支持 wav、ogg,好像不支持 MP3 。
// Five.c // SDL2 五子棋 // C4droid #define _DEBUG_ #include <stdio.h> #include <string.h> #include <SDL2/SDL.h> #include <SDL2/SDL_image.h> #include <SDL2/SDL_ttf.h> #include <SDL2/SDL_mixer.h> #include "FiveData.c" // 资源文件 char szBackGroundFile[] = "Resource/BackGround.jpg"; // 棋盘背景图文件 char szBlackFile[] = "Resource/BlackPiece.jpg"; // 黑棋子图文件(背景色:白色) char szWhiteFile[] = "Resource/WhitePiece.jpg"; // 白棋子图文件(背景色:白色) int BackColor = 0xFFFFFF; // 棋子图片的背景色 char szFontFile[] = "/system/fonts/DroidSansFallback.ttf"; // 字体文件 char szMusicFile[] = "/system/media/audio/ui/Effect_Tick.ogg"; // 落子音效文件 // 字符串常量 char szTitle[] = "SDL2 五子棋"; char szBlack[] = "黑方"; char szWhite[] = "白方"; char szGameTips[] = "第 %d 手,轮到 %s 落子"; char szGameOver[] = "%s 取得本局胜利,请按键继续"; _Bool OnKeyUp(int x, int y, int nSpacing); void PrintString(SDL_Renderer *pRenderer, int nSpacing, char *szString, TTF_Font *pFont, int color); void DrawBoard(SDL_Renderer *pRenderer, int nSpacing, int color); void DrawPieces(SDL_Renderer *pRenderer, int nSpacing, SDL_Texture *pBlackTexture, SDL_Texture *pWhiteTexture); void FillCircle(SDL_Renderer *pRenderer, int x, int y, int r, int color); SDL_Texture *GetImageTexture(SDL_Renderer *pRenderer, char *szFile, _Bool bTransparent, int color); SDL_Texture *GetStringTexture(SDL_Renderer *pRenderer, char *szString, TTF_Font *pFont, int color); int main(int argc, char **argv) { int nWindowWidth, nWindowHeight; // 屏幕尺寸 int nSpacing; // 棋盘线距 SDL_Window *pWindow = NULL; // 主窗口 SDL_Renderer *pRenderer = NULL; // 主窗口渲染器 SDL_Texture *pBackTexture = NULL; // 棋盘背景图纹理 SDL_Texture *pBlackTexture = NULL; // 黑棋子图纹理 SDL_Texture *pWhiteTexture = NULL; // 白棋子图纹理 TTF_Font *pFont = NULL; // 提示文字字体 Mix_Music *pMusic = NULL; // 音效 SDL_Event event; // 事件 _Bool bRun = 1; // 持续等待事件控制循环标识 char szString[256]; // 初始化:SDL2、SDL_Image(jpg)、SDL_ttf、SDL_Mixer if(SDL_Init(SDL_INIT_EVERYTHING) == -1 || IMG_Init(IMG_INIT_JPG) == -1 || TTF_Init() == -1 || Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096) == -1) { #ifdef _DEBUG_ fprintf(stderr, "1 %s", SDL_GetError()); #endif return 1; } // 创建主窗口及其渲染器 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, szTitle); SDL_GetWindowSize(pWindow, &nWindowWidth, &nWindowHeight); nSpacing = SDL_min(nWindowWidth, nWindowHeight)/(MAX_LINES+2); // 加载图片文件 if(NULL==(pBackTexture = GetImageTexture(pRenderer, szBackGroundFile, 0, 0)) || NULL==(pBlackTexture = GetImageTexture(pRenderer, szBlackFile, 1, BackColor)) || NULL==(pWhiteTexture = GetImageTexture(pRenderer, szWhiteFile, 1, BackColor))) { #ifdef _DEBUG_ fprintf(stderr, "3 %s", IMG_GetError()); #endif goto label_error; } // 加载字体文件 if(NULL == (pFont = TTF_OpenFont(szFontFile, 20))) { #ifdef _DEBUG_ fprintf(stderr, "4 %s", TTF_GetError()); #endif goto label_error; } // 加载声音文件 if((pMusic = Mix_LoadMUS(szMusicFile)) == NULL) { #ifdef _DEBUG_ fprintf(stderr, "5 %s", Mix_GetError()); #endif goto label_error; } // 重置棋局数据,等待事件 Five_ResetData(); while((bRun) && SDL_WaitEvent(&event)) { switch(event.type) { case SDL_FINGERUP : // 触摸手指松开 if(g_iWho != NONE) { if(OnKeyUp(event.tfinger.x*nWindowWidth, event.tfinger.y*nWindowHeight, nSpacing)) { Mix_PlayMusic(pMusic, 0); if(Five_isFive()) g_iWho = NONE; } } else Five_ResetData(); // 这里没有 break; 往下坠落重绘窗口 case SDL_WINDOWEVENT : // 有窗口消息需重绘窗口 SDL_RenderClear(pRenderer); SDL_RenderCopyEx(pRenderer, pBackTexture, NULL, NULL, 0, NULL, SDL_FLIP_NONE); DrawBoard(pRenderer, nSpacing, 0); DrawPieces(pRenderer, nSpacing, pBlackTexture, pWhiteTexture); if(g_iWho == NONE) sprintf(szString, szGameOver, g_nHands%2==1 ? szBlack : szWhite); else sprintf(szString, szGameTips, g_nHands+1, g_iWho==BLACK ? szBlack : szWhite); PrintString(pRenderer, nSpacing, szString, pFont, 0); SDL_RenderPresent(pRenderer); break; case SDL_QUIT : bRun = 0; break; default : break; } } label_error: // 清理 if(pBackTexture != NULL) SDL_DestroyTexture(pBackTexture); if(pBlackTexture != NULL) SDL_DestroyTexture(pBlackTexture); if(pWhiteTexture != NULL) SDL_DestroyTexture(pWhiteTexture); if(pFont != NULL) TTF_CloseFont(pFont); if(pMusic != NULL) Mix_FreeMusic(pMusic); Mix_CloseAudio(); TTF_Quit(); IMG_Quit(); SDL_Quit(); return 0; } // 响应落子按键 // 参数:(x,y) = 被点击的窗口坐标;nSpacing = 棋盘线距 _Bool OnKeyUp(int x, int y, int nSpacing) { // 计算落点棋盘坐标 int m = (x - 0.5*nSpacing)/nSpacing; int n = (y - 0.5*nSpacing)/nSpacing; // 处理有效落点 if(m>=0 && m<MAX_LINES && n>=0 && n<MAX_LINES && g_iBoard[m][n]==NONE) { Five_AddPiece(m, n, g_iWho); return 1; } return 0; } // 画棋盘 // 参数:pRenderer = 渲染器;nSpacing = 棋盘线距;color = 线及星颜色 void DrawBoard(SDL_Renderer *pRenderer, int nSpacing, int color) { int r, x, y, z; // 棋盘线 SDL_SetRenderDrawColor(pRenderer, color>>16, (color>>8)&0xFF, color&0xFF, SDL_ALPHA_OPAQUE); for(int i = 1; i <= MAX_LINES; i++) { SDL_RenderDrawLine(pRenderer, nSpacing, i*nSpacing, MAX_LINES*nSpacing, i*nSpacing); SDL_RenderDrawLine(pRenderer, i*nSpacing, nSpacing, i*nSpacing, MAX_LINES*nSpacing); } // 星位 r = nSpacing*0.2; // 星半径 x = nSpacing*4; // 第四线 y = nSpacing*(MAX_LINES+1)/2; // 中线 z = nSpacing*(MAX_LINES-3); // 倒数第四线 FillCircle(pRenderer, x, x, r, color); FillCircle(pRenderer, y, x, r, color); FillCircle(pRenderer, z, x, r, color); FillCircle(pRenderer, x, y, r, color); FillCircle(pRenderer, y, y, r, color); FillCircle(pRenderer, z, y, r, color); FillCircle(pRenderer, x, z, r, color); FillCircle(pRenderer, y, z, r, color); FillCircle(pRenderer, z, z, r, color); } // 画棋子 // 参数:pRenderer = 渲染器;nSpacing = 棋盘线距;pBlackTexture = 黑子纹理;pWhiteTexture = 白子纹理 void DrawPieces(SDL_Renderer *pRenderer, int nSpacing, SDL_Texture *pBlackTexture, SDL_Texture *pWhiteTexture) { int r = 0.4*nSpacing; // 棋子半径 SDL_Rect rt = {0, 0, 2*r, 2*r}; if(g_nHands <= 0) return; for(int i=0; i<MAX_LINES; i++) { for(int j=0; j<MAX_LINES; j++) { rt.x = (i+1)*nSpacing - r; rt.y = (j+1)*nSpacing - r; if(g_iBoard[i][j] == BLACK) SDL_RenderCopyEx(pRenderer, pBlackTexture, NULL, &rt, 0, NULL, SDL_FLIP_NONE); else if(g_iBoard[i][j] == WHITE) SDL_RenderCopyEx(pRenderer, pWhiteTexture, NULL, &rt, 0, NULL, SDL_FLIP_NONE); } } } // 提示文字 // 参数:pRenderer = 渲染器;nSpacing = 棋盘线距;szString = 文字内容;pFont = 字体;color = 文字颜色 void PrintString(SDL_Renderer *pRenderer, int nSpacing, char *szString, TTF_Font *pFont, int color) { SDL_Texture *pTextTexture; SDL_Rect rt; rt.x = nSpacing; rt.y = nSpacing*(MAX_LINES+1); rt.w = nSpacing*strlen(szString)/4; // 这个 4 和字体大小有关 rt.h = nSpacing; if((pTextTexture = GetStringTexture(pRenderer, szString, pFont, color)) != NULL) { SDL_RenderCopyEx(pRenderer, pTextTexture, NULL, &rt, 0, NULL, SDL_FLIP_NONE); SDL_DestroyTexture(pTextTexture); } } // 取得图片文件纹理 // 参数:pRenderer = 渲染器;szFile = 图片文件名;bTransparent = 是否透明处理;color = 背景色 // 返回值:纹理指针 SDL_Texture *GetImageTexture(SDL_Renderer *pRenderer, char *szFile, _Bool bTransparent, int color) { SDL_Texture *pTexture; SDL_Surface *pSurface; if((pSurface = IMG_Load(szFile)) == NULL) return NULL; if(bTransparent) SDL_SetColorKey(pSurface, 1, SDL_MapRGB(pSurface->format, color>>16, (color>>8)&0xFF, color&0xFF)); pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface); SDL_FreeSurface(pSurface); return pTexture; } // 取得字符串纹理 // 参数: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; } // 画圆(SDL2 没有画圆的函数,先用矩形框代替吧) // 参数:pRenderer = 渲染器;(x,y) = 圆心坐标;r = 半径;color = 填充色 void FillCircle(SDL_Renderer *pRenderer, int x, int y, int r, int color) { SDL_Rect rt = {x-r, y-r, 2*r, 2*r}; SDL_SetRenderDrawColor(pRenderer, color>>16, (color>>8)&0xFF, color&0xFF, SDL_ALPHA_OPAQUE); SDL_RenderFillRect(pRenderer, &rt); }
注意:安卓下文件名严格区分大小写
如果把 #include <SDL2/SDL_mixer.h> 写成 #include <SDL2/SDL_Mixer.h>
会报错:fatal error: SDL2/SDL_Mixer.h: No such file or directory
把 #include "FiveData.h"
改成 #include "FiveData.c"
是为了实现 C4droid 单文件编译。
那个时钟就更简单了,都不用改,直接编译就行。
// 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; }
实际上 SDL2 提供了一个函数检测操作系统:
const char *SDL_GetPlatform(void);
该函数返回一个操作系统名的字符串:
Windows
Mac OS X
Linux
iOS
Android
或者 Unknown
可以在程序里直接判断操作系统,然后进行相应的处理,这里可以一次编写,到处编译了。