SoFunction
Updated on 2025-03-02

SDL2 is used to implement Gozi chess game on Android

This article introduces the implementation of a simple Gozi game using SDL2 on Android. It is shared with you for your reference. The specific content is as follows

1.

// 
 
// SDL2 Goko Chess// gcc -mwindows -o Five    -lSDL2 -lSDL2main -lSDL2_image -lSDL2_ttf
 
//#define _DEBUG_
 
#include <>
#include <>
#include <SDL2/>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include ""
 
// Resource filechar szBackGroundFile[] = "Resource/";  // Chessboard background image filechar szBlackFile[]   = "Resource/";  // Black chess piece image file (background color: white)char szWhiteFile[]   = "Resource/";  // White chess piece image file (background color: white)char szFontFile[]    = "Resource/"; // Font file// String constantschar szTitle[]  = "Gozi";
char szBlack[]  = "Black Side";
char szWhite[]  = "Bai Fang";
char szGameTips[] = "The %d hand, it's %s turn to make it";
char szGameOver[] = "%s wins this game, please press the button to continue";
 
_Bool OnKeyUp(int x, int y, int nSpacing);
void DrawBoard(SDL_Renderer *pRenderer, int nSpacing, SDL_Color *pColor);
void DrawPieces(SDL_Renderer *pRenderer, int nSpacing, SDL_Texture *pBlackTexture, SDL_Texture *pWhiteTexture);
void PrintString(SDL_Renderer *pRenderer, int nSpacing, char *szString, TTF_Font *pFont, SDL_Color *pColor);
void FillCircle(SDL_Renderer *pRenderer, int x, int y, int r, SDL_Color *pColor);
SDL_Texture *GetImageTexture(SDL_Renderer *pRenderer, char *szFile, _Bool bTransparent, SDL_Color *pBackGroundColor);
SDL_Texture *GetStringTexture(SDL_Renderer *pRenderer, TTF_Font *pFont, char *szString, SDL_Color *pColor);
 
#undef main
int main(int argc, char **argv)
{
  int nWindowWidth, nWindowHeight;  // Screen size  int nSpacing;            // Chessboard line distance  SDL_Window  *pWindow    = NULL; // Main window  SDL_Renderer *pRenderer   = NULL; // Main window renderer  SDL_Texture *pBackTexture = NULL; // Checkerboard background image texture  SDL_Texture *pBlackTexture = NULL; // Black chess piece drawing texture  SDL_Texture *pWhiteTexture = NULL; // White chess piece drawing texture  TTF_Font   *pFont     = NULL; // Prompt text font  SDL_Event  event;         // event  _Bool bRun = 1;           // Continuously wait for event control loop identification  char szString[256];
 
  // Initialization  if(SDL_Init(SDL_INIT_EVERYTHING)==-1 || IMG_Init(IMG_INIT_JPG)==-1 || TTF_Init()==-1)
  {
#ifdef _DEBUG_
    fprintf(stderr, "%s", SDL_GetError());
#endif
    return 1;
  }
  // Create the main window and its renderer  if(SDL_CreateWindowAndRenderer(0, 0, SDL_WINDOW_FULLSCREEN, &pWindow, &pRenderer)==-1)
  {
#ifdef _DEBUG_
    fprintf(stderr, "%s", SDL_GetError());
#endif
    goto label_error;
  }
  SDL_SetWindowTitle(pWindow, szTitle);
  SDL_GetWindowSize(pWindow, &nWindowWidth, &nWindowHeight);
  nSpacing = SDL_min(nWindowWidth, nWindowHeight)/(MAX_LINES+2);
 
  // Load the picture file  if(NULL==(pBackTexture = GetImageTexture(pRenderer, szBackGroundFile, 0, NULL))
      || NULL==(pBlackTexture = GetImageTexture(pRenderer, szBlackFile, 1, NULL))
      || NULL==(pWhiteTexture = GetImageTexture(pRenderer, szWhiteFile, 1, NULL)))
  {
#ifdef _DEBUG_
    fprintf(stderr, "%s", SDL_GetError());
#endif
    goto label_error;
  }
  // Load font file  if(NULL == (pFont = TTF_OpenFont(szFontFile, 20))) // This 20 is the font size  {
#ifdef _DEBUG_
    fprintf(stderr, "%s", SDL_GetError());
#endif
    goto label_error;
  }
 
  // Reset the chess game data and wait for events  Five_ResetData();
  while(bRun && SDL_WaitEvent(&event))
  {
    switch()
    {
    case SDL_FINGERUP :  // Touch pops up      if(g_iWho != NONE)
      {
        if(OnKeyUp(*nWindowWidth, *nWindowHeight, nSpacing) && Five_isFive())
          g_iWho = NONE;
      }
      else
        Five_ResetData();
      // There is no break here; Fall down and repaint the window 
    case SDL_WINDOWEVENT :   // If there is a window message, you need to repaint the window      SDL_RenderClear(pRenderer);
      SDL_RenderCopyEx(pRenderer, pBackTexture, NULL, NULL, 0, NULL, SDL_FLIP_NONE);
      DrawBoard(pRenderer, nSpacing, NULL);
      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, NULL);
      SDL_RenderPresent(pRenderer);
      break;
 
    case SDL_QUIT :
      bRun = 0;
      break;
 
    default :
      break;
    }
  }
 
label_error:
// Clean up  if(pBackTexture != NULL) SDL_DestroyTexture(pBackTexture);
  if(pBlackTexture != NULL) SDL_DestroyTexture(pBlackTexture);
  if(pWhiteTexture != NULL) SDL_DestroyTexture(pWhiteTexture);
  if(pFont != NULL)     TTF_CloseFont(pFont);
  TTF_Quit();
  IMG_Quit();
  SDL_Quit();
  return 0;
}
 
// Response to the drop button// Parameters: (x, y) = the clicked window coordinates, nSpacing = the chess line distance_Bool OnKeyUp(int x, int y, int nSpacing)
{
  // Calculate the coordinates of the landing point  int m = (x - 0.5*nSpacing)/nSpacing;
  int n = (y - 0.5*nSpacing)/nSpacing;
  // Process effective landing points  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;
}
 
// Draw the chessboard// Parameters: pRenderer = Renderer, nSpacing = Checkerboard line spacing, pColor = Color (default black)void DrawBoard(SDL_Renderer *pRenderer, int nSpacing, SDL_Color *pColor)
{
  SDL_Color c;
  int r, x, y, z;
 
  if(pColor == NULL)
     =  =  = 0;
  else
    c = *pColor;
 
  // Chessboard line  SDL_SetRenderDrawColor(pRenderer, , , , 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);
  }
 
  // Star position  r = nSpacing*0.2;        // Star radius  x = nSpacing*4;         // The fourth line  y = nSpacing*(MAX_LINES+1)/2;  // Center line  z = nSpacing*(MAX_LINES-3);   // The fourth line last  FillCircle(pRenderer, x, x, r, &c);
  FillCircle(pRenderer, y, x, r, &c);
  FillCircle(pRenderer, z, x, r, &c);
  FillCircle(pRenderer, x, y, r, &c);
  FillCircle(pRenderer, y, y, r, &c);
  FillCircle(pRenderer, z, y, r, &c);
  FillCircle(pRenderer, x, z, r, &c);
  FillCircle(pRenderer, y, z, r, &c);
  FillCircle(pRenderer, z, z, r, &c);
}
 
// Draw chess pieces// Parameters: pRenderer = Renderer, nSpacing = Chessboard line spacing, pBlackTexture = Blackspot texture, pWhiteTexture = Whitespot texturevoid DrawPieces(SDL_Renderer *pRenderer, int nSpacing, SDL_Texture *pBlackTexture, SDL_Texture *pWhiteTexture)
{
  int r = 0.4*nSpacing; // radius of chess piece  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++)
    {
       = (i+1)*nSpacing - r;
       = (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);
    }
  }
}
 
// Prompt text// Parameters: szString = text content, pFont = font, pColor = text color (default black)void PrintString(SDL_Renderer *pRenderer, int nSpacing, char *szString, TTF_Font *pFont, SDL_Color *pColor)
{
  SDL_Texture *pTextTexture;
  SDL_Rect rt;
 
   = nSpacing;
   = nSpacing*(MAX_LINES+1);
   = nSpacing*strlen(szString)/4;   // This 4 is related to the font size   = nSpacing;
 
  if((pTextTexture = GetStringTexture(pRenderer, pFont, szString, pColor)) != NULL)
  {
    SDL_RenderCopyEx(pRenderer, pTextTexture, NULL, &rt, 0, NULL, SDL_FLIP_NONE);
    SDL_DestroyTexture(pTextTexture);
  }
}
 
// Get the image file texture// Parameters: szFile = picture file name, bTransparent = whether to process transparently, pBackGroundColor = background color (default white)// Return value: texture pointerSDL_Texture *GetImageTexture(SDL_Renderer *pRenderer, char *szFile, _Bool bTransparent, SDL_Color *pBackGroundColor)
{
  SDL_Texture *pTexture;
  SDL_Surface *pSurface;
  int r, g, b;
 
  if((pSurface = IMG_Load(szFile)) == NULL)
    return NULL;
  if(bTransparent)
  {
    if(pBackGroundColor == NULL)
    {
      r = g = b = 255;
    }
    else
    {
      r = pBackGroundColor->r;
      g = pBackGroundColor->g;
      b = pBackGroundColor->b;
    }
    SDL_SetColorKey(pSurface, 1, SDL_MapRGB(pSurface->format, r, g, b));
  }
  pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface);
 
  SDL_FreeSurface(pSurface);
  return pTexture;
}
 
// Get string texture// Parameters: szString = string content, pFont = font, pColor = text color (default black)// Return value: texture pointerSDL_Texture *GetStringTexture(SDL_Renderer *pRenderer, TTF_Font *pFont, char *szString, SDL_Color *pColor)
{
  SDL_Texture *pTexture;
  SDL_Surface *pSurface;
  SDL_Color c;
 
  if(pColor == NULL)
     =  =  = 0;
  else
    c = *pColor;
 
  if((pSurface = TTF_RenderUTF8_Blended(pFont, szString, c)) == NULL)
    return NULL;
  pTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface);
 
  SDL_FreeSurface(pSurface);
  return pTexture;
}
 
// Draw circles (SDL2 does not have a function to draw circles, just use a rectangular box instead)// Parameters: pRenderer = Renderer, (x, y) = Center coordinate, r = Radius, pCOlor = Fill colorvoid FillCircle(SDL_Renderer *pRenderer, int x, int y, int r, SDL_Color *pColor)
{
  SDL_Rect rt = {x-r, y-r, 2*r, 2*r};
 
  SDL_SetRenderDrawColor(pRenderer, pColor->r, pColor->g, pColor->b, SDL_ALPHA_OPAQUE);
  SDL_RenderFillRect(pRenderer, &rt);
}

    

// 
 
// Goziqi: Data processing module 
#include ""
 
 
// Public variablesint g_nHands;      // Total lotint g_nLastCrossing;  // 100*x+y, (x, y) is the coordinates of the last handenum en_COLOR g_iWho;  // Which side is the turn to make a move: 0 Cannot make a move, 1 Black side, 2 White sideint g_iBoard[MAX_LINES][MAX_LINES];  // Checkerboard intersection data: 0 No child, 1 blackspot, 2 whitespot 
 
// Determine whether the last move forms a five-piece bead// Return value: 1 = Five-subunit beads formed, 0 = Five-subunit beads not formed_Bool Five_isFive(void)
{
  int i, j, nCount, x, y;
 
  if(g_nLastCrossing < 0)
    return 0;
  x = g_nLastCrossing/100;
  y = g_nLastCrossing%100;
 
  // Horizontal line count  nCount = 1;
  i = x - 1;   // Left  while(i>=0 && g_iBoard[x][y]==g_iBoard[i][y])
  {
    nCount++;
    i--;
  }
  i = x + 1;   // Right  while(i<MAX_LINES && g_iBoard[x][y]==g_iBoard[i][y])
  {
    nCount++;
    i++;
  }
  if(nCount >= 5)
    return 1;
 
  // Vertical line count  nCount = 1;
  j = y - 1;   // superior  while(j>=0 && g_iBoard[x][y]==g_iBoard[x][j])
  {
    nCount++;
    j--;
  }
  j = y + 1;   // Down  while(j<MAX_LINES && g_iBoard[x][y]==g_iBoard[x][j])
  {
    nCount++;
    j++;
  }
  if(nCount >= 5)
    return 1;
 
  // Left slash count  nCount = 1;
  i = x - 1;   // Top left  j = y - 1;
  while(i>=0 && j>=0 && g_iBoard[x][y]==g_iBoard[i][j])
  {
    nCount++;
    i--;
    j--;
  }
  i = x + 1;   // Lower right  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;
 
  // Right slash count  nCount = 1;
  i = x + 1;   // Top right  j = y - 1;
  while(i<MAX_LINES && j>=0 && g_iBoard[x][y]==g_iBoard[i][j])
  {
    nCount++;
    i++;
    j--;
  }
  i = x - 1;   // Lower left  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;
}
 
// Reset the game datavoid 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;
}
 
// Record a write-in data// Parameters: x, y = chess piece coordinates, c = chess piece colorvoid 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);
}

  

// 
 
// Goziqi: external interface of data processing module 
#ifndef _FIVE_DATA_H
#define _FIVE_DATA_H
 
 
enum en_COLOR  // Chess piece color{
  NONE = 0,    // No children  BLACK,     // Heizi  WHITE      // Bai Zi};
 
// Chess game#define MAX_LINES 15 // Number of chessboard linesextern int g_nHands; // Total lotsextern int g_nLastCrossing;  // 100*x+y, (x, y) is the coordinates of the last handextern enum en_COLOR g_iWho;  // Which side is the turn to make a move: 0 Cannot make a move, 1 Black side, 2 White sideextern int g_iBoard[MAX_LINES][MAX_LINES]; // Checkerboard intersection data: 0 No child, 1 blackspot, 2 whitespot 
 
// Determine whether the last move forms a five-piece bead// Return value: 1 = Five-subunit beads formed, 0 = Five-subunit beads not formedextern _Bool Five_isFive(void);
 
// Reset the game dataextern void Five_ResetData(void);
 
// Record a write-in data// Parameters: x, y = chess piece coordinates, c = chess piece colorextern void Five_AddPiece(int x, int y, enum en_COLOR c);
 
 
#endif

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.