示例#1
0
static int tilt_func(void *data)
{
    wiimote_t   wiimote = WIIMOTE_INIT;
    const char *address = config_get_s(CONFIG_WIIMOTE_ADDR);

    if (strlen(address) > 0)
    {
        if (wiimote_connect(&wiimote, address) < 0)
            log_printf("Wiimote error (%s)\n", wiimote_get_error());
        else
        {
            int running = 1;

            wiimote.mode.bits = WIIMOTE_MODE_ACC;
            wiimote.led.one   = 1;

            SDL_mutexP(mutex);
            state.status = running;
            SDL_mutexV(mutex);

            while (mutex && running && wiimote_is_open(&wiimote))
            {
                if (wiimote_update(&wiimote) < 0)
                    break;

                SDL_mutexP(mutex);
                {
                    running = state.status;

                    set_button(&state.A,     wiimote.keys.a);
                    set_button(&state.B,     wiimote.keys.b);
                    set_button(&state.plus,  wiimote.keys.plus);
                    set_button(&state.minus, wiimote.keys.minus);
                    set_button(&state.home,  wiimote.keys.home);
                    set_button(&state.L,     wiimote.keys.left);
                    set_button(&state.R,     wiimote.keys.right);
                    set_button(&state.U,     wiimote.keys.up);
                    set_button(&state.D,     wiimote.keys.down);

                    if (isnormal(wiimote.tilt.y))
                    {
                        state.x = (state.x * (FILTER - 1) +
                                   wiimote.tilt.y) / FILTER;
                    }
                    if (isnormal(wiimote.tilt.x))
                    {
                        state.z = (state.z * (FILTER - 1) +
                                   wiimote.tilt.x) / FILTER;
                    }
                }
                SDL_mutexV(mutex);
            }

            wiimote_disconnect(&wiimote);
        }
    }
    return 0;
}
示例#2
0
int
main (int argc, char **argv)
{
  wiimote_t wiimote = WIIMOTE_INIT;

  if (argc < 2)
    {
      fprintf (stderr, "Usage: test4 BDADDR\n");
      exit (1);
    }

  /* Print help information. */

  printf ("test4 - libcwiimote test application\n\n");
  printf (" Will scan MII slots and save them as files\n");
  printf ("Press buttons 1 and 2 on the wiimote now to connect.\n");

  /* Connect the wiimote specified on the command line. */

  if (wiimote_connect (&wiimote, argv[1]) < 0)
    {
      fprintf (stderr, "unable to open wiimote: %s\n", wiimote_get_error ());
      exit (1);
    }

  const char save_name[] = "wiimotefullmemorydump.txt";

  int slot = 0;

  /* Loop through all 10 mii slots.  Save used slots to filename slot_X.mii */
  while (slot < 10)
    {
      char save_name[10];
      sprintf (save_name, "slot_%d.mii", slot);
      int slotinuse = wiimote_mii_slot_state (&wiimote, slot);
      if (slotinuse)
	{
	  printf ("mii slot %d is in use \n", slot);
	  if (wiimote_mii_dump (&wiimote, save_name, slot) == WIIMOTE_OK)
	    {
	      printf ("Saved mii slot %d . \n", slot);
	    }
	}
      else
	{
	  printf ("mii slot %d is empty \n", slot);
	}
      slot++;
    }

  // Do a dump of all the memory on the wiimote
  wiimote_mii_dump_all (&wiimote, save_name);
  return 0;

}
示例#3
0
int nunchuk_update(wiimote_t *wiimote)
{
	uint8_t data[16];

	if (wiimote_read(wiimote, NUNCHUK_MEM_START, data, 16) < 0) {
		wiimote_set_error("nunchuk_update(): unable to read nunchuk state: %s", wiimote_get_error());
		return WIIMOTE_ERROR;
	}

	nunchuk_decode(&data[8], 6);

	if (!memcpy((uint8_t*)&wiimote->ext.items.nunchuk, &data[8], 6)) {
		wiimote_set_error("nunchuk_update(): memcpy: %s", strerror(errno));
		return WIIMOTE_ERROR;
	}

	return WIIMOTE_OK;
}
/*****************************************************************
  関数名	: InitWindows
  機能	: メインウインドウの表示,設定を行う
  引数	: int	clientID		: クライアント番号
  int	num				: 全クライアント数
  出力	: 正常に設定できたとき0,失敗したとき-1
 *****************************************************************/
int InitWindows(int clientID,int num,char name[][MAX_NAME_SIZE])
{
    int i;
    //SDL_Surface *image;
    //////////////////////////////////////////////////////////////////////////////////////
    /*変更点 画像のファイル名を変更してグー、チョキ、パーが表示できるようにした*/
    char clientButton[4][6]={"0.jpg","1.jpg","2.jpg"};
    char endButton[]="END.jpg";
    char allButton[]="ALL.jpg";


    char *s,title[10];

    /* 引き数チェック */
    assert(0<num && num<=MAX_CLIENTS);
    SDL_EnableKeyRepeat(0,1);
    /* SDLの初期化 */
    if(SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("failed to initialize SDL.\n");
        return -1;
    }

    /* メインのウインドウを作成する */
    if((gMainWindow = SDL_SetVideoMode(600,400, 32, SDL_SWSURFACE)) == NULL) {
        printf("failed to initialize videomode.\n");
        return -1;
    }
    SDL_FillRect(gMainWindow, NULL, 0x000000ff); //to blue
    stringColor(gMainWindow, 0, 0, "Press buttons 1 and 2 on the wiimote now to connect.\n", 0xffffffff);

    SDL_Flip(gMainWindow);
    /* ウインドウのタイトルをセット */
    sprintf(title,"Battle of Soft Character %d",clientID);
    SDL_WM_SetCaption(title,NULL);

    // ***** Wiiリモコン処理 *****
    wiimote_t wiimote = WIIMOTE_INIT;
    data.wiimote=wiimote;
    wiimote_report_t report = WIIMOTE_REPORT_INIT;

    // Wiiリモコンの接続(1つのみ)
    if(strcmp(wiiName,"key") !=0){
        if (wiimote_connect(&data.wiimote, wiiName) < 0) {   // コマンド引数に指定したWiiリモコン識別情報を渡して接続
            printf("unable to open wiimote: %s\n", wiimote_get_error());
            exit(1);
        }
    }
    //Wiiリモコンのレポートタイプを指定する
    if (wiimote_report(&data.wiimote, &report, sizeof(report.status)) < 0) {
        wiimote_disconnect(&data.wiimote);
    }
    data.wiimote.led.one  = 1;   
    data.wiimote.mode.acc = 1;//加速度センサをオンにする
    /* 背景を白にする */
    SDL_FillRect(gMainWindow,NULL,0xffffff);

    /* ボタンの作成 */
    ///////////////////////////////////////////////////////////////////////////////////
    /*変更点 クライアントの数だけボタンが作成されるが、ここで3つしかボタンをつくらないことにする。
      (選択用のボタン3つと終了用のボタン1つ)
     */
    //   for(i=0;i<4;i++){
    /* if (i < 3){
       /gButtonRect[i].x=20+150*i;
       gButtonRect[i].y=10;
       gButtonRect[i].w=135;
       gButtonRect[i].h=135;/
       gButtonRect[i].x=20+150*i;
       gButtonRect[i].y=10;
       gButtonRect[i].w=135;
       gButtonRect[i].h=135;
       }else{
       gButtonRect[i].x=400;
       gButtonRect[i].y=260;
       gButtonRect[i].w=70;
       gButtonRect[i].h=20;
       }*/
    /*if(i==3){
      s=endButton;
      }else{
      s=clientButton[i];
      }

      s = endButton;
      image = IMG_Load(s);
      SDL_BlitSurface(image,NULL,gMainWindow,&(gButtonRect[i]));
      SDL_FreeSurface(image);
     */
    // }
    SDL_Flip(gMainWindow);

    return 0;
}
示例#5
0
int main(int argc,char *argv[])
{


    timers timer;
    int lflag=0;
    int		num;
    int i;//cnum繰り返すため
    char	name[MAX_CLIENTS][MAX_NAME_SIZE];
    char	localHostName[]="localhost";
    char	*serverName;
    char	data[MAX_DATA];
    //int		clientID;

//    SDL_TimerID timer_id1;	// タイマ割り込みを行うためのタイマのID

    // SDL初期化
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        printf("failed to initialize SDL.\n");
        exit(-1);
    }

    /* 引き数チェック */
    if(argc == 2){
    	serverName = localHostName;
        if (wiimote_connect(&wiimote, argv[1]) < 0) {	// コマンド引数に指定したWiiリモコン識別情報を渡して接続
            printf("unable to open wiimote: %s\n", wiimote_get_error());
            exit(1);
    }
    }
    else if(argc == 3){
    	serverName = argv[1];
        if (wiimote_connect(&wiimote, argv[2]) < 0) {	// コマンド引数に指定したWiiリモコン識別情報を渡して接続
            printf("unable to open wiimote: %s\n", wiimote_get_error());
            exit(1);
    }
    }
    else{
		fprintf(stderr, "Usage: %s, Cannot find a Server Name.\n", argv[0]);
		return -1;
    }

    /* サーバーとの接続 */
    if(SetUpClient(serverName,&clientID,name)==-1){
		fprintf(stderr,"setup failed : SetUpClient\n");
		return -1;
	}

    printf("clientnum=%d\n",cnum);

    /* スタート画面の初期化 */
    /*if(InitWindows()==-1){
		fprintf(stderr,"setup failed : InitWindows\n");
		return -1;
                }*/
    
   /*ゲームウィンドウの初期化*/
    /*  if(GameWindows(clientID,name,loop)==-1){
	fprintf(stderr,"setup failed : GameWindows\n");
		return -1;

                }*/

        printf("clientnum=%d\n",cnum);

        wiimote.mode.acc = 1;

        switch(clientID){
        case 0: wiimote.led.one =   1; break;
        case 1: wiimote.led.two   = 1; break; 
        case 2 :wiimote.led.three = 1; break;

        default: break;
        }

    /*wiiリモコンの入力受付開始*/

    // SDL_GetTicks関数を用いる時間管理
	Uint32 next_frame=SDL_GetTicks();	// SDLライブラリの初期化からの経過ミリ秒数を取得
        
        //dflag = 0;
        game.flag = 0;
        thr_net=SDL_CreateThread(thread_net,NULL);
        //thr_time=SDL_CreateThread(thread_time,NULL);
        timer_id1=SDL_AddTimer(100, callbackfunc, NULL);
    /* メインイベントループ */
    while(endFlag){
/*
        timer.now=SDL_GetTicks();//現在時間を取得
        timer.wit=timer.now-timer.lev;//待ち時間を計算

        if(dflag == 0)
        {
            WindowEvent(clientID);
            //timer.lev=SDL_GetTicks();//経過時間を更新
        }
        else if(timer.wit > 16){
            WindowEvent(clientID);
            dflag = 0;
            timer.lev=SDL_GetTicks();//経過時間を更新
        }
*/
        timer.wit=timer.now-timer.lev;//待ち時間を計算

        /*if(timer.wit > 1000){
            game.restTime--;
            timer.lev=SDL_GetTicks();//経過時間を更新
            }*/


        /********メイン画面ループ**************/
        if(game.flag == 0){
            loop=1;
            gametimes=3;
            TopWindow();
            printf("now==%d\n\n",game.flag);
            while(game.flag == 0){
                WindowEvent(clientID,ima);
                if(endFlag == 0)
                    break;
            }
        }
        else if(game.flag == 1){//ゲーム画面作成
            
            if(GameWindows(clientID,name,loop)==-1){
                fprintf(stderr,"setup failed : GameWindows\n");
		return -1;}
        }
        /*********ゲーム画面ループ************/
        else if(game.restTime > 0 && game.flag == 2){
            WindowEvent(clientID,ima);  
            ima=SDL_GetTicks();//現在時間を取得
            //printf("game.restTime:%d\n",game.restTime);
//>>>>>>> color
            DrawChara(clientID,cnum);
        }
        
        else if(game.flag == 3 || game.restTime <= 0)
        {
            game.flag = 3;
            if(gClients[clientID].ADsta==1 && game.restTime<=0){
                gClients[clientID].score -= gClients[clientID].Bflag;
                sprintf(data,"kabaddi,%d,%d,%d,%d,%d,%d,%d,%d\0",SCORE,clientID,gClients[clientID].score,0,0,0,0,0);
                SendData(data);
                }
            WinDisplay(clientID);
            
            printf("now==%dloop==%dcnum==%dgametimes=%d\n\n",game.flag,loop,cnum,gametimes);
            while(game.flag == 3){
                WindowEvent(clientID,ima);
                if(endFlag == 0)
                    break;
            }
        }
        else if(game.flag == 4){
            //loop=0;
            EndWindow();
            for(i=0;i<cnum;i++){
                gClients[i].score=0;
                //gClients[i].restart=0;
                // gametimes=3;
            }
            while(game.flag == 4){
                WindowEvent(clientID,ima);
                if(endFlag == 0)
                    break;
            }    
            
        }
        
        timer.lev=SDL_GetTicks();//経過時間を更新
        
        
        
        
        // timer.now=SDL_GetTicks();//現在時間を取得
        //timer.wit=timer.now-timer.lev;//待ち時間を計算
        
        // if(timer.wit<16)
            //    SDL_Delay(16-timer.wit);//16以下ならCPUを休ませる
        
        //timer.lev=SDL_GetTicks();//経過時間を更新
        
    }
    
    /* 終了処理 */
    SDL_RemoveTimer(timer_id1);
    DestroyWindow();
    CloseSoc();
    
    return 0;
}