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, &RT, 0, NULL, sdl_flip_none); else if (g_iboard[i][j] = = white) Sdl_rendercopyex (prenderer, pwhitetexture, NULL, &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, &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, &RT); }
The above is the entire content of this article, I hope to help you learn.