Android under SDL2 implementation Gobang game _android

Source: Internet
Author: User
Tags goto sprintf

This article introduces an example of Android with SDL2 to achieve a simple gobang game, to share with you for your reference, the specific contents are as follows

1. five.c

FIVE.C//SDL2 Gobang//gcc-mwindows-o Five five.c fivedata.c Fivedata.h-lsdl2-lsdl2main-lsdl2_image-lsdl2_ttf #define _DEBUG_ #include <stdio.h> #include <string.h> #include <SDL2/SDL.h> #include <sdl2/sdl_ image.h> #include <SDL2/SDL_ttf.h> #include "fivedata.c"//resource file char szbackgroundfile[] = "Resource/background  . jpg ";  Checkerboard Background Map file char szblackfile[] = "resource/blackpiece.jpg";  Black piece diagram file (background color: white) char szwhitefile[] = "resource/whitepiece.jpg"; White piece diagram file (background color: white) char szfontfile[] = "Resource/droidsansfallback.ttf";
Font file//string constant char sztitle[] = "Gobang";
Char szblack[] = "Black side";
Char szwhite[] = "White square";
Char szgametips[] = "%d hand, turn to%s drop";
 
Char szgameover[] = "%s win this Council, please press 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; Checkerboard Line distance Sdl_window *pwindow = NULL; main window Sdl_renderer *prenderer = NULL; Main Window renderer sdl_texture *pbacktexture = NULL; Checkerboard Background Map texture sdl_texture *pblacktexture = NULL; Black piece map Texture sdl_texture *pwhitetexture = NULL; White pieces Figure texture ttf_font *pfont = NULL;         Prompt text font sdl_event Event;           Event _bool BRun = 1;
 
  Keep waiting for event control loop to identify Char szstring[256]; Initialize 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) {#if
def _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 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; }//HeavyPlace chess data, wait for event five_resetdata (); while (BRun && sdl_waitevent (&event)) {switch (event.type) {case Sdl_fingerup://Touch Bounce I F (g_iwho!= NONE) {if OnKeyUp (Event.tfinger.x*nwindowwidth, Event.tfinger.y*nwindowheight, nspacing) && Amp
      Five_isfive ()) g_iwho = NONE;
      else Five_resetdata (); There is no break;
      Fall down redraw the window case sdl_windowevent://Windows message needs redrawing 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://scavenging 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 drop key//Parameters: (x,y) = clicked window coordinates, nspacing = Checkerboard Line distance _bool OnKeyUp (int x, int y, int nspacing) {//Calculate the placement board coordinates int m = (
  x-0.5*nspacing)/nspacing;
  int n = (y-0.5*nspacing)/nspacing; Processing effective placement if (m>=0 && m<max_lines && n>=0 && n<max_lines && g_iboard[m][n]==n
    One) {five_addpiece (M, N, g_iwho);
  return 1;
return 0; ///Draw chessboard//Parameters: Prenderer = renderer, nspacing = Checkerboard line, Pcolor = Color (default black) void Drawboard (Sdl_renderer *prenderer, int nspacing, S
  Dl_color *pcolor) {Sdl_color C;
 
  int r, x, Y, Z;
  if (Pcolor = = NULL) C.R = C.G = c.b = 0; else C = *pcolor;
  Chessboard Line Sdl_setrenderdrawcolor (Prenderer, C.R, C.G, C.B, Sdl_alpha_opaque); for (int i = 1; I <= max_lines i++) {sdl_renderdrawline (Prenderer, nspacing, i*nspacing, max_lines*nspacing, I*n
    Spacing);
  Sdl_renderdrawline (Prenderer, i*nspacing, nspacing, i*nspacing, max_lines*nspacing);        }//Star bit r = nspacing*0.2;         Star radius x = nspacing*4;  Fourth line y = nspacing* (max_lines+1)/2;   Midline z = nspacing* (max_lines-3);
  The penultimate fourth line 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 pieces//parameters: Prenderer = renderer, nspacing = Checkerboard line, pblacktexture = sunspot texture, pwhitetexture = white sub texture void drawpieces (Sdl_renderer *p Renderer, int nspacing, Sdl_texture *pblacktexture, sdl_texture *pwhitetexture) {int r = 0.4*nspacing;//Pawn radius 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, &AMP;RT, 0, NULL, sdl_flip_none); else if (g_iboard[i][j] = = white) Sdl_rendercopyex (prenderer, pwhitetexture, NULL, &AMP;RT, 0, NULL, sdl_flip_n
    one); }}//Prompt text//parameter: szstring = text content, Pfont = font, Pcolor = text color (default black) void Printstring (Sdl_renderer *prenderer, int Nspac
  ING, char *szstring, Ttf_font *pfont, Sdl_color *pcolor) {sdl_texture *ptexttexture;
 
  Sdl_rect RT;
  Rt.x = nspacing;
  Rt.y = nspacing* (max_lines+1);   RT.W = Nspacing*strlen (szstring)/4;
 
  This 4 is concerned with the font size rt.h = nspacing; if (ptexttexture = Getstringtexture (Prenderer, Pfont, szstring, PcoloR)!= null) {Sdl_rendercopyex (prenderer, ptexttexture, NULL, &AMP;RT, 0, NULL, sdl_flip_none);
  Sdl_destroytexture (ptexttexture); //Get picture file texture//Parameters: Szfile = picture filename, btransparent = transparent processing, Pbackgroundcolor = background color (default white)//return value: Texture pointer sdl_texture *getimag Etexture (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 pointer sdl_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) C.R = C.G = c.b = 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 Circle (SDL2 does not draw a circle function, first use rectangular box instead of bar)//parameter: Prenderer = renderer, (x,y) = center coordinates, r = radius, Pcolor = fill color void fillcircle (Sdl_renderer *pre
 
  nderer, 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, &AMP;RT); }

2.fivedata.c     

FIVEDATA.C//Gobang: Data processing module #include "FiveData.h"//public variable int g_nhands;  Total number of hands int g_nlastcrossing;  100*x+y, (x,y) for the last hand of the coordinate enum En_color g_iwho;  Turn to which side drop: 0 drop state, 1 black square, 2 white square int g_iboard[max_lines][max_lines]; Chessboard intersection data: 0 No son, 1 sunspots, 2 white son//judge whether the last hand of the five-child alignment//return value: 1 = form Five sub-alignments, 0 = not formed five-child alignment _bool five_isfive (void) {int I, J, Ncou
 
  NT, 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 bar count ncount = 1;   j = y-1;
    On while (j>=0 && g_iboard[x][y]==g_iboard[x][j]) {ncount++;
  j--;   } j = y + 1;
    The next 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 game data 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;
  //Record a drop Data//parameter: x,y = Pawn coordinates, c = pawn color 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);
 }

3.fivedata.h

FiveData.h
 
//Gobang: Data Processing Module external interface
 
#ifndef _five_data_h
#define _FIVE_DATA_H
 
 
enum En_color  //Pawn Color
{
  none = 0,//    No Child black
  ,     //Sunspot
  white      //Bai Zi
};
 
Chess
#define Max_lines    //Checkerboard line number
extern int g_nhands;      Total hand number
extern int g_nlastcrossing;  100*x+y, (x,y) for the last hand coordinates
extern enum En_color g_iwho;  Turn to which side drop: 0 drop state, 1 black, 2 white
extern int g_iboard[max_lines][max_lines];//Chessboard intersection data: 0 No Child, 1 sunspots, 2 white
 
 
//Judge whether the last hand chess is formed Five sub-alignment
//return value: 1 = form five children, 0 = not formed five children alignment
extern _bool five_isfive (void);
 
Resets the on
-data extern void five_resetdata (void);
 
Record a drop data
//parameter: x,y = Pawn coordinates, c = pawn color
extern void five_addpiece (int x, int y, enum en_color c);
 
 
#endif

The above is the entire content of this article, I hope to help you learn.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.