簡單的五子棋,示例 SDL2 的圖片紋理、文字紋理、聲音、幾何繪圖、鼠標事件

這是一個簡單的五子棋代碼,用到了圖片紋理、文字紋理、聲音、幾何繪圖、鼠標事件。windows

// Five.c
// SDL2 五子棋
// gcc -mwindows -o Five Five.c FiveData.c FiveData.h -lSDL2 -lSDL2main -lSDL2_image -lSDL2_ttf -lSDL2_mixer

//#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.h"

// 資源文件
char szBackGroundFile[] = "Resource/BackGround.jpg";    // 棋盤背景圖文件
char szBlackFile[]      = "Resource/BlackPiece.jpg";    // 黑棋子圖文件(背景色:白色)
char szWhiteFile[]      = "Resource/WhitePiece.jpg";    // 白棋子圖文件(背景色:白色)
int  BackColor          = 0xFFFFFF;                     // 棋子圖片的背景色
char szFontFile[]       = "C:/Windows/Fonts/msyh.ttf";  // 字體文件
char szMusicFile[]      = "Resource/Stone.mp3";         // 落子音效文件
// 字符串常量
char szTitle[]    = "五子棋";
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);

#undef main
int main(int argc, char **argv)
{
    int nWindowWidth  = 640;            // 屏幕尺寸(默認640*480)
    int nWindowHeight = 480;
    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];

    // 初始化:SDL二、SDL_Image(jpg)、SDL_ttf、SDL_Mixer
    if(SDL_Init(SDL_INIT_VIDEO) == -1 || IMG_Init(IMG_INIT_JPG) == -1 || TTF_Init() == -1
            || Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
    {
#ifdef _DEBUG_
        fprintf(stderr, "1 %s", SDL_GetError());
#endif
        return 1;
    }
    // 建立主窗口及其渲染器
    if(SDL_CreateWindowAndRenderer(nWindowWidth, nWindowHeight, SDL_WINDOW_RESIZABLE, &pWindow, &pRenderer)==-1)
    {
#ifdef _DEBUG_
        fprintf(stderr, "2 %s", SDL_GetError());
#endif
        goto label_error;
    }
    SDL_SetWindowTitle(pWindow, szTitle);
    // 加載圖片文件
    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_MOUSEBUTTONUP :    // 鼠標按鍵彈起
            if(g_iWho != NONE)
            {
                if(OnKeyUp(event.button.x, event.button.y, nSpacing))
                {
                    Mix_PlayMusic(pMusic, 0);
                    if(Five_isFive())
                        g_iWho = NONE;
                }
            }
            else
                Five_ResetData();
            // 這裏沒有 break; 往下墜落重繪窗口

        case SDL_WINDOWEVENT :      //  有窗口消息需重繪窗口
            SDL_GetWindowSize(pWindow, &nWindowWidth, &nWindowHeight);
            nSpacing = SDL_min(nWindowWidth, nWindowHeight)/(MAX_LINES+2);

            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);
}
// FiveData.h
// 五子棋:數據處理模塊對外接口

#ifndef _FIVE_DATA_H
#define _FIVE_DATA_H


enum en_COLOR   // 棋子顏色
{
    NONE = 0,       // 無子
    BLACK,          // 黑子
    WHITE           // 白子
};

// 棋局
#define MAX_LINES      15       // 棋盤線數
extern int  g_nHands;           // 總手數
extern int  g_nLastCrossing;    // 100*x+y,(x,y)爲最後一手的座標
extern enum en_COLOR g_iWho;    // 輪到哪方落子:0 不可落子狀態,1 黑方,2 白方
extern int  g_iBoard[MAX_LINES][MAX_LINES]; // 棋盤交叉點數據:0 無子,1 黑子,2 白子


// 判斷最後一手棋是否造成五子連珠
// 返回值:1 = 造成五子連珠, 0 = 未造成五子連珠
extern _Bool Five_isFive(void);

// 重置對局數據
extern void Five_ResetData(void);

// 記錄一個落子數據
// 參數:x,y = 棋子座標,c = 棋子顏色
extern void Five_AddPiece(int x, int y, enum en_COLOR c);


#endif
// FiveData.c
// 五子棋:數據處理模塊

#include "FiveData.h"


// 公共變量
int  g_nHands;           // 總手數
int  g_nLastCrossing;    // 100*x+y,(x,y)爲最後一手的座標
enum en_COLOR g_iWho;    // 輪到哪方落子:0 不可落子狀態,1 黑方,2 白方
int  g_iBoard[MAX_LINES][MAX_LINES];    // 棋盤交叉點數據:0 無子,1 黑子,2 白子


// 判斷最後一手棋是否造成五子連珠
// 返回值:1 = 造成五子連珠, 0 = 未造成五子連珠
_Bool Five_isFive(void)
{
    int i, j, nCount, x, y;

    if(g_nLastCrossing < 0)
        return 0;
    x = g_nLastCrossing/100;
    y = g_nLastCrossing%100;

    // 橫線計數
    nCount = 1;
    i = x - 1;      // 左
    while(i>=0 && g_iBoard[x][y]==g_iBoard[i][y])
    {
        nCount++;
        i--;
    }
    i = x + 1;      // 右
    while(i<MAX_LINES && g_iBoard[x][y]==g_iBoard[i][y])
    {
        nCount++;
        i++;
    }
    if(nCount >= 5)
        return 1;

    // 豎線計數
    nCount = 1;
    j = y - 1;      // 上
    while(j>=0 && g_iBoard[x][y]==g_iBoard[x][j])
    {
        nCount++;
        j--;
    }
    j = y + 1;      // 下
    while(j<MAX_LINES && g_iBoard[x][y]==g_iBoard[x][j])
    {
        nCount++;
        j++;
    }
    if(nCount >= 5)
        return 1;

    // 左斜線計數
    nCount = 1;
    i = x - 1;      // 左上
    j = y - 1;
    while(i>=0 && j>=0 && g_iBoard[x][y]==g_iBoard[i][j])
    {
        nCount++;
        i--;
        j--;
    }
    i = x + 1;      // 右下
    j = y + 1;
    while(i<MAX_LINES && j<MAX_LINES && g_iBoard[x][y]==g_iBoard[i][j])
    {
        nCount++;
        i++;
        j++;
    }
    if(nCount >= 5)
        return 1;

    // 右斜線計數
    nCount = 1;
    i = x + 1;      // 右上
    j = y - 1;
    while(i<MAX_LINES && j>=0 && g_iBoard[x][y]==g_iBoard[i][j])
    {
        nCount++;
        i++;
        j--;
    }
    i = x - 1;      // 左下
    j = y + 1;
    while(i>=0 && j<MAX_LINES && g_iBoard[x][y]==g_iBoard[i][j])
    {
        nCount++;
        i--;
        j++;
    }
    if(nCount >= 5)
        return 1;

    return 0;
}

// 重置對局數據
void Five_ResetData(void)
{
    for(int i=0; i<MAX_LINES; i++)
        for(int j=0; j<MAX_LINES; j++)
            g_iBoard[i][j] = NONE;
    g_nHands = 0;
    g_nLastCrossing = -1;
    g_iWho = BLACK;
}

// 記錄一個落子數據
// 參數:x,y = 棋子座標,c = 棋子顏色
void Five_AddPiece(int x, int y, enum en_COLOR c)
{
    g_iBoard[x][y] = c;
    g_nHands++;
    g_nLastCrossing = 100*x + y;
    g_iWho = (g_iWho == BLACK ? WHITE : BLACK);
}

用到了三張圖片:函數

聲音文件本身隨便找個吧。字體

 注意:int main(int argc, char *argv[]) 函數前面必需要有 #undef mainui

否則會報錯:undefined reference to `WinMain@16'spa

相關文章
相關標籤/搜索