Chinaunix首页 | 论坛 | 博客
  • 博客访问: 130052
  • 博文数量: 30
  • 博客积分: 972
  • 博客等级: 中士
  • 技术积分: 332
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-08 10:04
文章分类

全部博文(30)

文章存档

2012年(30)

分类: LINUX

2012-07-05 21:00:02


 

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<unistd.h>
  3. #include<stdlib.h>
  4. #include<signal.h>
  5. #include"TFT_API.h"
  6. #include"ts_calibrate.h"
  7. #include<pthread.h>
  8. #include"judge.h"
  9. #include <sys/types.h>
  10. #include<sys/time.h>
  11. #include<semaphore.h>
  12. int chess[8][8];
  13. WIN_HANDLE Chess_back_window;
  14. WIN_HANDLE Cal_Black;
  15. WIN_HANDLE Cal_White;
  16. WIN_HANDLE Cal_Time;
  17. int player=1,timeout=0,start=1;
  18. int timer=20,flags;
  19. sem_t sem1,sem2;
  20. void inittft(void)
  21. {
  22.  getFBP("/dev/fb0");
  23.  TFT_Init();
  24.  ts_cal_init();

  25.  Chess_back_window=TFT_CreateWindowEx(0,0,320,240,COLOR_BLACK);
  26.  Cal_Black=TFT_CreateWindowEx(18,60,40,20,COLOR_BLACK);
  27.  Cal_White=TFT_CreateWindowEx(18,150,40,20,COLOR_BLACK);
  28.  Cal_Time=TFT_CreateWindowEx(18,213,40,20,COLOR_BLACK);
  29.  TFT_File_Picture(Chess_back_window,0,0,"../picture/fan.bmp",1);

  30. }
  31. void display(void)
  32. {
  33.  int i,j;
  34.  for(i=0;i<8;i++)
  35.  {
  36.   for(j=0;j<8;j++)
  37.   {
  38.    if(chess[i][j] == 0);
  39.    //printf("There is no chess\n");
  40.    
  41.    if(chess[i][j] == 1)
  42.    TFT_File_Picture(Chess_back_window,i*28+90,j*28+10,"../picture/black.bmp",0);

  43.    if(chess[i][j] == 2)
  44.    TFT_File_Picture(Chess_back_window,i*28+90,j*28+10,"../picture/white.bmp",0);
  45.   }
  46.  }
  47. }
  48. void initChess(void)
  49. {
  50.  TFT_SetColor(Cal_Black,COLOR_WHITE);

  51.  TFT_SetColor(Cal_White,COLOR_WHITE);

  52.  chess[3][3]=2;
  53.  chess[3][4]=1;
  54.  chess[4][3]=1;
  55.  chess[4][4]=2;

  56.  display();
  57. }
  58. void *display_chess(void *a)
  59. {
  60.  while(1)
  61.  {
  62.   display();
  63.   usleep(5*1000);
  64.  }
  65.  return NULL;
  66. }
  67. void *play_chess(void *a)
  68. {
  69.  TS_RET ts;
  70.  int ret;
  71.  int temp_x;
  72.  int temp_y;

  73.  while(1)
  74.  {
  75.   sem_wait(&sem1);
  76.   ret=ts_read(&ts);
  77.   int ret=0;
  78.   start=1;
  79.   if(ret < 0)
  80.   printf("Pls touch the chess board\n");
  81.   //printf("press pad x y padis %d %d %d\n",ts.x,ts.y,ts.pressure);
  82.   if(ts.x > 85 && ts.x < 309 && ts.y > 8 && ts.y < 232)
  83.   {
  84.    temp_x=(ts.x-85)/28 ;temp_y=(ts.y-5)/28 ;
  85.    
  86.    ret=judgeRule(temp_x,temp_y,8,chess,player,1);
  87.    flags=0;
  88.    
  89.    if(ret > 0)
  90.    {
  91.     if((chess[temp_x][temp_y] == 0))
  92.     {
  93.      if(player == 1)
  94.      {
  95.      if((chess[temp_x+1][temp_y])==2||(chess[temp_x -1][temp_y]==2)||(chess[temp_x][temp_y+1]==2)\
  96.         ||(chess[temp_x][temp_y-1]==2)||(chess[temp_x-1][temp_y-1]==2)||(chess[temp_x+1][temp_y+1]==2)\
  97.         ||(chess[temp_x-1][temp_y+1]==2)||(chess[temp_x+1][temp_y-1]==2))
  98.       chess[temp_x][temp_y]=1;
  99.      }
  100.      if(player == 2)
  101.      {
  102.      if((chess[temp_x+1][temp_y])==1||(chess[temp_x -1][temp_y]==1)||(chess[temp_x][temp_y+1]==1)\
  103.         ||(chess[temp_x][temp_y-1]==1)||(chess[temp_x-1][temp_y-1]==1)||(chess[temp_x+1][temp_y+1]==1)\
  104.         ||(chess[temp_x-1][temp_y+1]==1)||(chess[temp_x+1][temp_y-1]==1))
  105.       chess[temp_x][temp_y]=2;
  106.      }
  107.     }
  108.    flags=1;
  109.    }
  110.   sem_post(&sem2);
  111.   }
  112.  }
  113.  return NULL;
  114. }
  115. void switchplayer(int signo)
  116. {
  117.  switch (player)
  118.  {
  119.  case 1:
  120.    player=2;
  121.    break;
  122.  case 2:
  123.    player=1;
  124.    break;
  125.  default:
  126.    break;
  127.  }
  128. }
  129. void * player_handler(void *a)
  130. {
  131.  while(1)
  132.  {
  133.   if(timeout)
  134.   {
  135.    //raise(SIGQUIT);
  136.    //sem_wait(&sem2);
  137.    sem_post(&sem1);
  138.    timeout=0;
  139.   }
  140.   else
  141.   {
  142.   sem_wait(&sem2);
  143.   if(flags ==1)
  144.    {
  145.    raise(SIGQUIT);
  146.    timer=20;
  147.    }
  148.   sem_post(&sem1);
  149.   }
  150.  }
  151.  return NULL;
  152. }
  153. void * cal_chess(void *a)
  154. {
  155.  int i,j;
  156.  char buf[50]="";
  157.  while(1)
  158.  {
  159.  int black=0,white=0;
  160.  TFT_SetColor(Cal_Black,COLOR_WHITE);
  161.  for(i=0;i<8;i++)
  162.  {
  163.   for(j=0;j<8;j++)
  164.   {
  165.    if(chess[i][j] == 1)
  166.    black++;
  167.    if(chess[i][j] == 2)
  168.    white++;
  169.   }
  170.  }
  171.  if(black < 64 )
  172.  {
  173.   sprintf(buf,"../picture/number/%d.bmp",black/10);
  174.   TFT_File_Picture(Cal_Black,0,0,buf,1);
  175.   sprintf(buf,"../picture/number/%d.bmp",black%10);
  176.   TFT_File_Picture(Cal_Black,20,0,buf,1);
  177.  }
  178.  if(white < 64 )
  179.  {
  180.   sprintf(buf,"../picture/number/%d.bmp",white/10);
  181.   TFT_File_Picture(Cal_White,0,0,buf,1);
  182.   sprintf(buf,"../picture/number/%d.bmp",white%10);
  183.   TFT_File_Picture(Cal_White,20,0,buf,1);
  184.  }
  185.  if(black + white == 64)
  186.  {
  187.   if(black > white)
  188.    TFT_File_Picture(Chess_back_window,145,90,"../picture/startBlack.bmp",0);
  189.   if(white > black)
  190.    TFT_File_Picture(Chess_back_window,145,90,"../picture/startWhite.bmp",0);
  191.   if(white == black)
  192.    TFT_File_Picture(Chess_back_window,145,90,"../picture/startEqual.bmp",0);
  193.  }
  194.  usleep(5*1000);
  195.  }
  196.  return NULL;
  197. }
  198. void * get_time(void *a)
  199. {
  200.  char buf[50]="";
  201.  while(1)
  202.  {
  203.   while(start)
  204.   {
  205.   if(timer >= 0)
  206.    {
  207.    TFT_SetColor(Cal_Time,COLOR_WHITE);
  208.    sprintf(buf,"../picture/number/%d.bmp",timer/10);
  209.    TFT_File_Picture(Cal_Time,0,0,buf,1);
  210.    sprintf(buf,"../picture/number/%d.bmp",timer%10);
  211.    TFT_File_Picture(Cal_Time,20,0,buf,1);
  212.    sleep(1);
  213.    timer--;
  214.    if(timer < 0)
  215.     {
  216.     timeout=1;
  217.     raise(SIGQUIT);
  218.     break;
  219.     }
  220.    }
  221.   }
  222.  }
  223.  return NULL;
  224. }

  225. int main(void)
  226. {
  227.  pthread_t tid_t,tid_p,tid_c,tid_d,tid_g;
  228.  inittft();
  229.  initChess();
  230.  signal(SIGQUIT,switchplayer);
  231.  sem_init(&sem1,0,1);
  232.  sem_init(&sem2,0,0);

  233.  pthread_create(&tid_t,NULL,display_chess,NULL);
  234.  pthread_create(&tid_p,NULL,player_handler,NULL);
  235.  pthread_create(&tid_c,NULL,cal_chess,NULL);
  236.  pthread_create(&tid_d,NULL,play_chess,NULL);
  237.  pthread_create(&tid_g,NULL,get_time,NULL);

  238.  while(1);
  239.  return 0;
  240. }


 

阅读(1725) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~