/*NAMEread-readfromafiledescriptorSYNOPSIS#includeunistd.hssize_tread(intfd,void*buf,size_tcount);从一个文件中读取相关的数据存储到buf所指向的内存空间fd:文件描述符open函数的返回值buf:存储数据的空间count:读取的个数linux下的触摸事件:在linux下,触摸事件、蓝牙事件、键盘事件都叫做inputevent而且,在linux下,所有的inputevent都用一个structinput_event的结构体来进行描述。structinput_event{structtime_valtime;__u16type;//事件的类型__u16code;//ABS_XABS_Y__s32value;};*/#includesys/types.h#includesys/stat.h#includefcntl.h#includeerrno.h//perror,errno#includeunistd.h#includesys/mman.h#includestdio.h#includemath.h#includelinux/input.h//structinput_eventint*plcd=NULL;intDET=0;intrand_arr[3]={2,4,2};//枚举类型表明手指滑动方向enumfinger_move{MOVE_RIGHT=1,MOVE_LEFT,MOVE_UP,MOVE_DOWN,};intmatrix[4][4]={2,0,2,0,0,2,4,0,0,2,0,2,2,0,4,0,};//指针数组用来存储图片名称constchar*file_name[]={NULL,military_font_7_2.bmp,military_font_7_4.bmp,military_font_7_8.bmp,military_font_7_16.bmp,military_font_7_32.bmp,military_font_7_64.bmp,military_font_7_128.bmp,military_font_7_256.bmp,military_font_7_512.bmp,military_font_7_1024.bmp,military_font_7_2048bmp,NULL,};/*根据不同的数字从指针数组内获得对应的图片名字返回值:图片名字参数:num二维数组内的数字*/constchar*get_filename_by_digital(intnum){intindex=0;switch(num){case2:index=1;break;case4:index=2;break;case8:index=3;break;case16:index=4;break;case32:index=5;break;case64:index=6;break;case128:index=7;break;case256:index=8;break;case512:index=9;break;case1024:index=10;break;case2048:index=11;break;case4096:index=12;break;case8192:index=13;break;case16384:index=14;break;case32768:index=15;break;case65536:index=16;break;default:index=0;break;}returnfile_name[index];}/*功能:在lcd的(x,y)点显示color这个颜色@x:点的横坐标@y:点的纵坐标@color:颜色值*/voidlcd_draw_point(intx,inty,intcolor){int*p=plcd;*(p+y*800+x)=color;}//清屏操作voidlcd_clear_screen(intcolor){intx,y;for(y=0;y480;y++){for(x=0;x800;x++)lcd_draw_point(x,y,color);}}/*功能:画一个矩形@x0x坐标@y0y坐标@w宽@h高@color颜色值*/voidlcd_draw_rect(intx0,inty0,intw,inth,intcolor){intx,y;for(y=y0;y(y0+h);y++){for(x=x0;x(x0+w);x++)lcd_draw_point(x,y,color);}}/*在指定的位置(x0,y0)显示一张指定的BMP图片参数:@file_name:bmp图片名字@x0:指定位置的x坐标@y0:指定位置的y坐标返回值:void*/voidlcd_draw_bmp(constchar*file_name,intx0,inty0){intfd;fd=open(file_name,O_RDONLY);if(fd==-1){printf(openfile:%serror!\n,file_name);return;}//跳过bmp图片的文件头部54个字节lseek(fd,54,SEEK_SET);charbuf[100*100*3];read(fd,buf,100*100*3);close(fd);inti=0,x,y;for(y=0;y100;y++){for(x=0;x100;x++){unsignedcharb,g,r;intcolor;b=buf[i++];g=buf[i++];r=buf[i++];color=(r16)|(g8)|b;lcd_draw_point(x+x0,y0+99-y,color);}}}/*根据定点坐标以及指定的矩形宽度,画一个num*num的矩阵参数:@x0:指定位置的x坐标@y0:指定位置的y坐标@num:矩阵大小num*num@pice_size:每一个矩形的宽度@blank_size:每个矩形之间的间隔大小*/voidlcd_draw_matrix(intx0,inty0,intnum,intpice_size,intblank_size,intcolor){intn,m;intx,y;for(n=0;nnum;n++){y=y0+n*(pice_size+blank_size);for(m=0;mnum;m++){x=x0+m*(pice_size+blank_size);if(matrix[n][m]==0)lcd_draw_rect(x,y,pice_size,pice_size,color);elselcd_draw_bmp(get_filename_by_digital(matrix[n][m]),x,y);}}}//识别手指滑动的方向intget_finger_move_direction(){intfd,r;intx1,y1,x2,y2;fd=open(/dev/event0,O_RDONLY);if(fd==-1){printf(openevent0error!\n);return-1;}structinput_eventev;x1=-1;y1=-1;while(1){r=read(fd,&ev,sizeof(ev));if(r!=sizeof(ev))continue;if((ev.type==EV_ABS)&&(ev.code==ABS_X)){if(x1==-1)x1=ev.value;x2=ev.value;}if((ev.type==EV_ABS)&&(ev.code==ABS_Y)){if(y1==-1)y1=ev.value;y2=ev.value;}if((ev.type==EV_ABS)&&(ev.code==ABS_PRESSURE)&&(ev.value==0)){intdelta_x=abs(x2-x1);intdelta_y=abs(y2-y1);//printf(%d%d\n,delta_x,delta_y);//lrudif(delta_x2*delta_y){if(x2x1){printf(right\n);close(fd);returnMOVE_RIGHT;//向右}elseif(x2x1){printf(left\n);close(fd);returnMOVE_LEFT;//向左}}elseif(delta_x2*delta_y){if(y2y1){printf(up\n);close(fd);returnMOVE_UP;//向上}elseif(y2y1){printf(down\n);close(fd);returnMOVE_DOWN;//向下}}else//该手势不能识别恭喜你再来一次{x1=-1;y1=-1;}}}close(fd);return0;}//获得当前棋盘上空格子的个数intget_blank_num(){intjishu=0;inti,j;for(i=0;i4;i++){for(j=0;j4;j++){if(matrix[i][j]==0){jishu++;}}}returnjishu;}//通过传入的数字产生随机数intget_rand_num(intsrand_num){//srand();returnrand()%srand_num;}intif2n(intx)//判断x是否是2的n次方{intflag=0;intn;for(n=1;n=11;n++){if(x==pow(2,n)){flag=1;returnflag;}}returnflag;}voidhandle_left_move(){//想办法把矩阵内的所有数字全部往左边移动inti,j,m,n,tp=0,counter,g,h,temp,L,Z;for(i=0;i4;i++){for(j=0;j4;j++){if(matrix[i][j]!=0){tp=j;for(tp;tp0&&(matrix[i][tp-1]==0);tp--){matrix[i][tp-1]=matrix[i][tp];matrix[i][tp]=0;}}}}//把相邻的相同的两个数字进行合并for(m=0;m4;m++){for(n=0;n3;n++){if(if2n(matrix[m][n]+matrix[m][n+1])==1){matrix[m][n]=matrix[m][n]+matrix[m][n+1];matrix[m][n+1]=0;}}}//根据新的矩阵画出最新的游戏矩阵lcd_draw_matrix(50,20,4,100,20,0x00ff0000);//求出当前矩阵中空格子的个数/*counter=get_blank_num();//根据空格子的个数生成一个随机数intnum=get_rand_num(counter);for(g=0;g4;g++){for(h=0;h4;h++){if(matrix[g][h]==0){temp++;if(temp==num){L=g;Z=h;break;}}}}intjtx=counter%3;matrix[L][Z]=rand_arr[jtx];lcd_draw_bmp(get_filename_by_digital(rand_arr[jtx]),50+120*L,20+120*Z);*///lcd_draw_matrix(50,20,4,100,20,0x00ff0000);//遍历matrix[4][4]二维数组,查找新生成的随机数的具体位置//真正目的是为了求出新的位置的顶点坐标//随机生成2/4///intrand_num=get_rand_num(3);//根据随机生成的2/4以及位置在对应的格子生显示出对应的图?//lcd_draw_bmp(get_filename