⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 junqi_client.cpp

📁 linux下开发的四国军旗源代码,包括客户端和服务器端
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//###################################################//#Created: LIP//#Description:	The Client code for JunQi(four country)//#OS :		linux.//#My QQ number: 13066543//#My e_mail: lip94101@btamail.net.cn//####################################################include <stdio.h>#include <stdlib.h>#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/Xos.h>#include <X11/Xatom.h>#include <X11/keysym.h>#include <X11/Intrinsic.h>#include <sys/socket.h>#include <netinet/in.h>#include <arpa/inet.h>#include <sys/time.h>#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <signal.h>#include <pthread.h>#include <string.h> #include <errno.h>#include "junqi_type.h"#include "cli_loadconfig.h"char showStr[]="Game JunQi";//-misc-zysong18030-bold-r-normal--0-0-0-0-c-0-gb18030.2000-1		StarOffice	ZYSong18030//-misc-zysong18030-bold-r-normal--0-0-0-0-c-0-gb18030.2000-1		StarOffice	Zysong18030//-misc-zysong18030-medium-r-normal--0-0-0-0-c-0-gb18030.2000-1/*XChar2b	glb_name_str[32]={	{0x5d,0xe5},{0x51,0x75},//工兵\u24037 \u20853	{0x63,0x92},{0x95,0x7f},//排长\u25490 \u38271	{0x8f,0xde},{0x95,0x7f},//连长\u36830 \u38271	{0x84,0x25},{0x95,0x7f},//营长\u33829 \u38271	{0x56,0xe2},{0x95,0x7f},//团长\u22242 \u38271	{0x65,0xc5},{0x95,0x7f},//旅长\u26053 \u38271	{0x5e,0x08},{0x95,0x7f},//师长\u24072 \u38271	{0x51,0x9b},{0x95,0x7f},//军长\u20891 \u38271	{0x53,0xf8},{0x4e,0xe4},//司令\u21496 \u20196	{0x57,0x30},{0x96,0xf7},//地雷\u22320 \u38647	{0x70,0xb8},{0x5f,0x39},//炸弹\u28856 \u24377	{0x51,0x9b},{0x68,0xcb},//军棋\u20891 \u26827	{0x51,0x75},{0x7a,0xd9},//兵站\u20853 \u31449	{0x88,0x4c},{0x84,0x25},//行营\u34892 \u33829	{0x59,0x27},{0x67,0x2c},{0x84,0x25}//大本营\u22823 \u26412 \u33829};*/extern unsigned char GUO_GB2312_12X12_FONT_BITMAP[];XChar2b	glb_name_str[32]={	{0xB9,0xA4},{0xB1,0xF8},//工兵	{0xC5,0xC5},{0xB3,0xA4},//排长	{0xC1,0xac},{0xb3,0xa4},//连长	{0xd3,0xaa},{0xb3,0xa4},//营长	{0xcd,0xc5},{0xb3,0xa4},//团长	{0xc2,0xc3},{0xb3,0xa4},//旅长	{0xca,0xa6},{0xb3,0xa4},//师长	{0xbe,0xfc},{0xb3,0xa4},//军长	{0xcb,0xbe},{0xc1,0xee},//司令	{0xb5,0xd8},{0xc0,0xd7},//地雷	{0xd5,0xa8},{0xb5,0xaf},//炸弹	{0xbe,0xfc},{0xc6,0xe5},//军棋	{0xb1,0xf8},{0xd5,0xbe},//兵站	{0xd0,0xd0},{0xd3,0xaa},//行营	{0xb4,0xf3},{0xb1,0xbe},{0xd3,0xaa}//大本营};Fly_Next_Point_s	fly_center_link_point[25+1];int			fly_search_point[25];int			fly_search_num;int map_run[17][17]={	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0,	0,0,0,0,0,0,  0,0,0,0,0,  0,0,0,0,0,0};Display *dis;Window win;GC global_gc;XColor green_col;XColor blue_col;XColor red_col;XColor yellow_col;XColor orange_col;XColor black_col;XColor gray_col;Colormap colormap;XFontStruct *font_info;Font	my_font;XImage *pbak_image;XColor play_a_col;XColor play_b_col;XColor play_c_col;XColor play_d_col;#define PLAYER_A_COLOR	play_a_col.pixel#define PLAYER_B_COLOR	play_b_col.pixel#define PLAYER_C_COLOR	play_c_col.pixel#define PLAYER_D_COLOR	play_d_col.pixelint TCP_sockFD;int glb_prg_Exit;int self_userID=1;int self_player_side = 1;int self_qiju_id=0;int run_flag;int qi_selected_flag;int select_qi_xindex;int select_qi_yindex;char user_name_str[32];char user_passwd_str[16];char glb_svr_ip[16];int  glb_svr_port;char glb_self_ip[16];int  glb_self_port;#include "cli_qiju_func.h"#include "const_define.h"CONST_VAR_DEFINECliUserInfo_s  glb_user_list[MAX_USER_INFO_NUM+1];CliQI_Ju_s	glb_qiju_list[MAX_QI_JU_NUM+1];char join_qiju_map_flag[15][15]={	' ','C',' ',' ','C',' ',' ','C',' ',' ','C',' ',' ','C',' ',	'D','T','B','D','T','B','D','T','B','D','T','B','D','T','B',	' ','A',' ',' ','A',' ',' ','A',' ',' ','A',' ',' ','A',' ',	' ','C',' ',' ','C',' ',' ','C',' ',' ','C',' ',' ','C',' ',	'D','T','B','D','T','B','D','T','B','D','T','B','D','T','B',	' ','A',' ',' ','A',' ',' ','A',' ',' ','A',' ',' ','A',' ',	' ','C',' ',' ','C',' ',' ','C',' ',' ','C',' ',' ','C',' ',	'D','T','B','D','T','B','D','T','B','D','T','B','D','T','B',	' ','A',' ',' ','A',' ',' ','A',' ',' ','A',' ',' ','A',' ',	' ','C',' ',' ','C',' ',' ','C',' ',' ','C',' ',' ','C',' ',	'D','T','B','D','T','B','D','T','B','D','T','B','D','T','B',	' ','A',' ',' ','A',' ',' ','A',' ',' ','A',' ',' ','A',' ',	' ','C',' ',' ','C',' ',' ','C',' ',' ','C',' ',' ','C',' ',	'D','T','B','D','T','B','D','T','B','D','T','B','D','T','B',	' ','A',' ',' ','A',' ',' ','A',' ',' ','A',' ',' ','A',' '};int init_fly_center_const_data();int InitColors();int deal_inputkey(int ikey);int deal_button_press(XButtonEvent event);int deal_button_release(XButtonEvent event);int deal_motion_notify(XMotionEvent event);int deal_connect_svr_button_event(XButtonEvent event);int deal_select_qiju_button_event(XButtonEvent event);int deal_init_qi_button_event(XButtonEvent event);int deal_run_qi_button_event(XButtonEvent event);int redraw_all();extern int join_qiju_init_draw();extern int join_qiju_re_drawall();extern int join_qiju_draw_chair(int x,int y,int user_index);extern int join_qiju_draw_table(int table_index);extern int join_qiju_draw_join_qiju(int userID,int qijuID,int playerID);int connect_svr_init_draw();int InitTCPSocket();void* TCPThread(void *TCPThreadpara);int deal_sock_cmd(int msgLen,char *rbuff);int deal_svr_move_ring(char *rbuff);int deal_svr_del_qi(char *rbuff);int deal_svr_eat_qi(char *rbuff);int deal_svr_kill_player(char *rbuff);int deal_svr_game_over(char *rbuff);int deal_svr_leave_qiju(char *rbuff);int deal_svr_join_qiju(char *rbuff);int deal_svr_move_qi(char *rbuff);int deal_svr_start_game(char *rbuff);int deal_svr_player_start(char *rbuff);int deal_svr_show_qi(char *rbuff);int deal_svr_ack(char *rbuff);int deal_svr_ack_login(char *rbuff);int deal_svr_ack_logout(char *rbuff);int deal_svr_ack_join_qiju(char *rbuff);int deal_svr_ack_start_game(char *rbuff);int deal_svr_ack_move_qi(char *rbuff);int deal_svr_ack_ask_peace(char *rbuff);int deal_svr_ack_give_up(char *rbuff);int deal_svr_ack_leave_qiju(char *rbuff);int data_para(char *buff,int index,char *para);int decode_cmd_name(char *cmd_str);int Send_login_to_svr(int sock,char *namestr,char *passwdstr);int Send_join_qiju_to_svr(int sock,int user_id,int qiju_id,int play_side);int Send_peace_to_svr(int sock);int Send_give_up_to_svr(int sock);int Send_move_qi_to_svr(int sock,int s_xindex,int s_yindex,int d_xindex,int d_yindex);int Send_start_game_to_svr(int sock,int user_id,int qiju_id,int play_side);int Send_logout_to_svr(int sock,int user_id,char *user_passwd_str);int Send_leave_qiju_to_svr(int sock,int user_id,int qiju_id,int player_id);int writenTCP(int fd, void *vptr, int n);int Draw_gb2312font16(Display *display,Drawable d,GC gc,int x,int y,XChar2b *string,int length);void main(){	int ikey;	XEvent event_rtn;	pthread_t           pth_TCP;	//freopen("log_junqi_client.txt", "a+", stdout);	LoadConfig("config.txt");	char	*display_str;	display_str=getenv("DISPLAY");	printf("The display_str=%s\n",display_str);	dis = XOpenDisplay(display_str);	if(dis == NULL){		perror("XOpenDisplay Err: dis = NULL\n");		exit(0);	}	printf("XOpenDisplay OK: dis = %d\n",dis);	//Init the color	InitColors();	win = XCreateSimpleWindow(dis,RootWindow(dis,0),0,0,790,590,5,blue_col.pixel,gray_col.pixel);	XMapWindow(dis,win);	printf("XMapWindow OK\n");	//Init the Sys Font:	char **path;	int	ipath;	path=XGetFontPath(dis,&ipath);	printf("the ipath=%d\n",ipath);	for(int itmp=0;itmp<ipath;itmp++){		printf("The font path[%d] =%s\n",itmp,path[itmp]);	}	//path=XListFonts(dis,"*zysong18030*",500,&ipath);	path=XListFonts(dis,"*song*",500,&ipath);	printf("the font num=%d\n",ipath);	for(int itmp=0;itmp<ipath;itmp++){		printf("The font name[%d] =%s\n",itmp,path[itmp]);	}	if((font_info = XLoadQueryFont(dis,"-misc-zysong18030-medium-r-normal--0-0-0-0-c-0-gb18030.2000-1"))==NULL){	//if((font_info = XLoadQueryFont(dis,"-iscas-song ti-medium-r-normal--12-120-72-72-c-120-gb2312.1980-0"))==NULL){		perror("Use 9x15 font error.use fixed font\n");		font_info = XLoadQueryFont(dis,"fixed");	}	printf("the font_info = %d\n",font_info);	printf("The font_info->direction=%d\n",font_info->direction);	printf("The font_info->min_char_or_byte2=%d\n",font_info->min_char_or_byte2);	printf("The font_info->max_char_or_byte2=%d\n",font_info->max_char_or_byte2);	printf("The font_info->min_byte1=%d\n",font_info->min_byte1);	printf("The font_info->max_byte1=%d\n",font_info->max_byte1);	printf("The font_info->all_chars_exist=%d\n",font_info->all_chars_exist);	printf("The font_info->default_char=%d\n",font_info->default_char);	printf("The font_info->n_properties=%d\n",font_info->n_properties);	printf("The font_info->ascent=%d\n",font_info->ascent);	printf("The font_info->descent=%d\n",font_info->descent);	printf("The font_info->per_char=%d\n",font_info->per_char);	printf("The font_info->min_bounds.lbearing=%d\n",font_info->min_bounds.lbearing);	printf("The font_info->min_bounds.rbearing=%d\n",font_info->min_bounds.rbearing);	printf("The font_info->min_bounds.width=%d\n",font_info->min_bounds.width);	printf("The font_info->min_bounds.ascent=%d\n",font_info->min_bounds.ascent);	printf("The font_info->min_bounds.descent=%d\n",font_info->min_bounds.descent);	printf("The font_info->min_bounds.attributes=%d\n",font_info->min_bounds.attributes);	printf("The font_info->max_bounds.lbearing=%d\n",font_info->max_bounds.lbearing);	printf("The font_info->max_bounds.rbearing=%d\n",font_info->max_bounds.rbearing);	printf("The font_info->max_bounds.width=%d\n",font_info->max_bounds.width);	printf("The font_info->max_bounds.ascent=%d\n",font_info->max_bounds.ascent);	printf("The font_info->max_bounds.descent=%d\n",font_info->max_bounds.descent);	printf("The font_info->max_bounds.attributes=%d\n",font_info->max_bounds.attributes);				//my_font=XLoadFont(dis,"hz16.pcf");	global_gc = XCreateGC(dis,win,(unsigned long)0,0);	XSetFont(dis,global_gc,font_info->fid);	XSetForeground(dis,global_gc,green_col.pixel);	printf("XSetForeground OK\n");	//Redefine the mouse Cursor:	XUndefineCursor(dis,win);//	Cursor itmp_cursor=XCreateFontCursor(dis,10);//	XRecolorCursor(dis,itmp_cursor,&blue_col,&yellow_col);//	XDefineCursor(dis,win,itmp_cursor);	unsigned int bmp_w,bmp_h;	int x_hot_rtn,y_hot_rtn;	int itmprtn;	Pixmap bmp1_rtn;	Pixmap bmp2_rtn;	itmprtn = XReadBitmapFile(dis,win,"sword.xbm",&bmp_w,&bmp_h,&bmp1_rtn,&x_hot_rtn,&y_hot_rtn);	printf("The ReadBitmap rtn =[%d]\n",itmprtn);	itmprtn = XReadBitmapFile(dis,win,"sword.xbm",&bmp_w,&bmp_h,&bmp2_rtn,&x_hot_rtn,&y_hot_rtn);	printf("The ReadBitmap rtn =[%d]\n",itmprtn);	Cursor itmp_cursor;	itmp_cursor=XCreatePixmapCursor(dis,bmp1_rtn,bmp2_rtn,&yellow_col,&gray_col,0,0);	XDefineCursor(dis,win,itmp_cursor);	XDrawRectangle(dis,win,global_gc,0,0,789,589);	//Reset the user eventMask:	//XSelectInput(dis,win,KeyPressMask|ButtonPressMask|ExposureMask|Button1MotionMask|ButtonReleaseMask);	XSelectInput(dis,win,KeyPressMask|ButtonPressMask|ExposureMask|ButtonReleaseMask);	XFlush(dis);	printf("!!!!!Now wait to input\n");	glb_prg_Exit = 0;	self_player_side = PLAYER_SIDE_A;	qi_selected_flag = 0;	init_fly_center_const_data();	run_flag=RUN_FLAG_CONNECT_SVR;	connect_svr_init_draw();	if (pthread_create(&pth_TCP,NULL,TCPThread,NULL) != 0){		printf("Could not create TCP thread.exit\n");		exit(0);	}	     	//freopen("log.txt", "a+", stdout);	//freopen("log.txt", "a+", stderr);	while(!glb_prg_Exit){		XNextEvent(dis,&event_rtn);		switch(event_rtn.type){		case Expose:			redraw_all();			XFlush(dis);			break;		case KeyPress:			ikey=XLookupKeysym(&event_rtn.xkey,0);			deal_inputkey(ikey);			XFlush(dis);			if(ikey == XK_Escape){				glb_prg_Exit = 1;				printf("This key == Escape!\n");			}			break;		case ButtonPress:			deal_button_press(event_rtn.xbutton);			break;		case ButtonRelease:			deal_button_release(event_rtn.xbutton);			break;		//case MotionNotify:			//deal_motion_notify(event_rtn.xmotion);			//break;		default:			printf("This is Event type=%d\n",event_rtn.type);			//glb_prg_Exit = 1;			break;		}	}	XFreePixmap(dis,bmp1_rtn);	XFreeCursor(dis,itmp_cursor);/*	for(int row=0;row<17;row++){		for(int col=0;col<17;col++){			printf("%3d,",map_run[col][row]);		}		printf("\n");	}	for(int row=0;row<15;row++){		for(int col=0;col<15;col++){			printf("%3c,",join_qiju_map_flag[row][col]);		}		printf("\n");	}*/}int InitColors(){	if(dis == NULL){		printf("InitColors Err: dis = NULL\n");		return -1;	}	colormap=DefaultColormap(dis,0);	XParseColor(dis,colormap,"#00FF00",&green_col);	XAllocColor(dis,colormap,&green_col);	XParseColor(dis,colormap,"#0000FF",&blue_col);	XAllocColor(dis,colormap,&blue_col);	XParseColor(dis,colormap,"#FF0000",&red_col);	XAllocColor(dis,colormap,&red_col);	XParseColor(dis,colormap,"yellow",&yellow_col);	XAllocColor(dis,colormap,&yellow_col);	XParseColor(dis,colormap,"orange",&orange_col);	XAllocColor(dis,colormap,&orange_col);	XParseColor(dis,colormap,"black",&black_col);	XAllocColor(dis,colormap,&black_col);	XParseColor(dis,colormap,"gray",&gray_col);	XAllocColor(dis,colormap,&gray_col);	XParseColor(dis,colormap,"#062190",&play_a_col);	XAllocColor(dis,colormap,&play_a_col);	XParseColor(dis,colormap,"#7e6d43",&play_b_col);	XAllocColor(dis,colormap,&play_b_col);	XParseColor(dis,colormap,"#2D8502",&play_c_col);	XAllocColor(dis,colormap,&play_c_col);	XParseColor(dis,colormap,"#7E207E",&play_d_col);	XAllocColor(dis,colormap,&play_d_col);	printf("XAllocColor OK\n");	return 0;}int redraw_all(){	switch(run_flag){	case RUN_FLAG_CONNECT_SVR:		connect_svr_init_draw();		break;	case RUN_FLAG_SELECT_QIJU:		join_qiju_re_drawall();		break;	case RUN_FLAG_INIT_QI:		qiju_redraw_screen();		break;	case RUN_FLAG_PLAY_QI:		qiju_redraw_screen();		break;	case RUN_FLAG_RUN_QI:		qiju_redraw_screen();		break;	default:		break;	}	return 0;}int deal_button_press(XButtonEvent event){	return 0;}int deal_button_release(XButtonEvent event){	int x,y;	int x_index,y_index;	if(event.button!=1){		return 0;	}	x=event.x;	y=event.y;	x_index=x/35;	y_index=y/35;	if(x_index>=17 || y_index>=17){//not the qi_map		switch(run_flag){		case RUN_FLAG_CONNECT_SVR:			break;		case RUN_FLAG_SELECT_QIJU:			if(x_index>=BUTTON_EXIT_XINDEX && x_index<=BUTTON_EXIT_XINDEX+1 && y_index>=BUTTON_EXIT_YINDEX && y_index<=BUTTON_EXIT_YINDEX+1){				//Send logout to svr.				Send_logout_to_svr(TCP_sockFD,self_userID,user_passwd_str);				close(TCP_sockFD);				TCP_sockFD = 0;				run_flag = RUN_FLAG_CONNECT_SVR;				qi_selected_flag = 0;				memset(glb_user_list,0,sizeof(glb_user_list));				memset(glb_qiju_list,0,sizeof(glb_qiju_list));				connect_svr_init_draw();			}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -