幻想森林

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 3034|回复: 0

[游戏编程] directinput有关的问题

[复制链接]

550

主题

9116

帖子

214748万

积分

超级版主

如同神一般的存在,腿神!拖后腿的神~~

Rank: 8Rank: 8

积分
2147483647
发表于 2011-3-27 23:42:47 | 显示全部楼层 |阅读模式
这是我的测试用的 键盘 缓冲数据 的测试
DIDEVICEOBJECTDATA  是用来存缓冲数据的,我想用messagebox将数组中的每个变量的每个成员都显示出来。自己做了个测试的东西。结果总是没有反应。

上代码

我将DIDEVICEOBJECTDATA  的结构体变量置零,然后做判断,希望messagebox能把DIDEVICEOBJECTDATA每个成员的值都显示出来,为什么这个程序有时候能弹出对话框,有时候谈不出来呢??
  1. #include <windows.h>
  2. #include <dinput.h>
  3. #include <iostream>
  4. #include <string.h>
  5. #include <stdio.h>
  6. using namespace std;
  7. #define DINPUT_BUFFERSIZE 2
  8. HWND hwnd;
  9. LPDIRECTINPUT           lpDirectInput;  // DirectInput object
  10. LPDIRECTINPUTDEVICE     lpKeyboard;     // DirectInput device
  11. LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
  12. void ReleaseDInput()
  13. {
  14. if (lpKeyboard!=NULL)
  15. {
  16.   lpKeyboard->Unacquire();
  17.   lpKeyboard->Release();
  18.   lpKeyboard = NULL;
  19. }
  20. if (lpDirectInput!=NULL)
  21. {
  22.   lpDirectInput->Release();
  23.   lpDirectInput = NULL;
  24. }
  25. }
  26. BOOL InitDInput(HWND hWnd,HINSTANCE hInstance)
  27. {
  28.     HRESULT hr;
  29.     // 创建一个 DIRECTINPUT 对象
  30.     hr = DirectInput8Create(hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&lpDirectInput, NULL);
  31.     if FAILED(hr)
  32.     {
  33.         // 失败
  34.         return FALSE;
  35.     }
  36.     // 创建一个 DIRECTINPUTDEVICE 界面
  37.     hr = lpDirectInput->CreateDevice(GUID_SysKeyboard, &lpKeyboard, NULL);
  38.     if FAILED(hr)
  39.     {
  40.         // 失败
  41.         return FALSE;
  42.     }
  43.     // 设定为通过一个 256 字节的数组返回查询状态值
  44.     hr = lpKeyboard->SetDataFormat(&c_dfDIKeyboard);
  45.     if FAILED(hr)
  46.     {
  47.         // 失败
  48.         return FALSE;
  49.     }
  50.     // 设定协作模式
  51.     hr = lpKeyboard->SetCooperativeLevel(hWnd, DISCL_EXCLUSIVE | DISCL_FOREGROUND);
  52.     if FAILED(hr)
  53.     {
  54.         // 失败
  55.         return FALSE;
  56.     }
  57.     // 设定缓冲区大小
  58.     // 如果不设定,缓冲区大小默认值为 0,程序就只能按立即模式工作
  59.     // 如果要用缓冲模式工作,必须使缓冲区大小超过 0
  60.     DIPROPDWORD     prop;
  61.     prop.diph.dwSize = sizeof(DIPROPDWORD);
  62.     prop.diph.dwHeaderSize = sizeof(DIPROPHEADER);
  63.     prop.diph.dwObj = 0;
  64.     prop.diph.dwHow = DIPH_DEVICE;
  65.     prop.dwData = DINPUT_BUFFERSIZE;
  66.     hr = lpKeyboard->SetProperty(DIPROP_BUFFERSIZE, &prop.diph);
  67.     if FAILED(hr)
  68.     {
  69.         // 失败
  70.         return FALSE;
  71.     }
  72.     hr = lpKeyboard->Acquire();
  73.     if FAILED(hr)
  74.     {
  75.         // 失败
  76.         return FALSE;
  77.     }
  78.     return TRUE;
  79. }
  80. HRESULT UpdateInputState(void)
  81. {
  82.     if(lpKeyboard != NULL)
  83.     {
  84.         DIDEVICEOBJECTDATA  didod[DINPUT_BUFFERSIZE];  // Receives buffered data
  85.         DWORD               dwElements;
  86.         HRESULT             hr;
  87.   ZeroMemory(didod, sizeof(DIDEVICEOBJECTDATA)*DINPUT_BUFFERSIZE);
  88.         hr = DIERR_INPUTLOST;
  89.         while(hr != DI_OK)
  90.         {
  91.             dwElements = DINPUT_BUFFERSIZE;
  92.             hr = lpKeyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), didod, &dwElements, 0);
  93.             if (hr != DI_OK)
  94.             {
  95.                 hr = lpKeyboard->Acquire();
  96.                 if(FAILED(hr))
  97.                 return hr;
  98.             }
  99.         }
  100.         if(FAILED(hr))
  101.             return hr;
  102.   for(int i=0; i<DINPUT_BUFFERSIZE; i++)
  103.   {
  104.    if (didod[i].dwOfs != 0)
  105.    {
  106.    
  107.   DWORD tmp[5*DINPUT_BUFFERSIZE];
  108.   char cBuffer[32];
  109.   VOID* pDest,*pSrc;
  110.   pDest = &tmp;
  111.   pSrc = &didod;
  112.   memcpy( pDest, pSrc, 20*DINPUT_BUFFERSIZE );
  113.   
  114.   string strResult("test:\n");
  115.   for (int i=0;i<5*DINPUT_BUFFERSIZE;i++)
  116.   {
  117.    wsprintf(cBuffer, "%d", tmp[i]);
  118.    strResult+=cBuffer;
  119.    strResult+="\n";
  120.   }
  121.   MessageBox( NULL, strResult.c_str(),"提示!", MB_OK) ;
  122.   break;
  123.    }
  124.     //return S_OK;
  125.    // 此处放入处理代码
  126.    // didod[i].dwOfs 表示那个键被按下或松开
  127.    // didod[i].dwData 记录此键的状态,低字节最高位是 1 表示按下,0 表示松开
  128.    // 一般用 didod[i].dwData&0x80 来测试}
  129.   }
  130. }
  131.     return S_OK;
  132. }
  133. int _stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)  
  134. {  
  135.     HWND hwnd;  
  136.     MSG  msg;  
  137.     WNDCLASS WindowClass;
  138.     if(!hPrevInstance){  
  139.         //not first run, to set the windows class  
  140.         WindowClass.style=NULL;  
  141.         WindowClass.cbClsExtra=0;  
  142.         WindowClass.cbWndExtra=0;
  143.   WindowClass.hbrBackground = HBRUSH(GetStockObject(BLACK_BRUSH));
  144.         WindowClass.hCursor=LoadCursor(hInstance,IDC_ARROW);  
  145.         WindowClass.hIcon=LoadIcon(hInstance,IDI_APPLICATION);  
  146.         WindowClass.hInstance=hInstance;  
  147.         WindowClass.lpfnWndProc=WndProc;  
  148.         WindowClass.lpszClassName="DInput_Test";  
  149.         WindowClass.lpszMenuName=NULL;  
  150.         RegisterClass(&WindowClass);  
  151.     }  
  152.     // start to create the window when registed the window class  
  153.     hwnd=CreateWindow("DInput_Test","DirectInput_Test",WS_OVERLAPPEDWINDOW,  
  154.         CW_USEDEFAULT,  
  155.         CW_USEDEFAULT,  
  156.         CW_USEDEFAULT,  
  157.         CW_USEDEFAULT,NULL,NULL,hInstance,NULL);  
  158.     ShowWindow(hwnd,nShowCmd);  
  159.     UpdateWindow(hwnd);  
  160. if FAILED(InitDInput(hwnd,hInstance))
  161. {
  162.   MessageBox( NULL, "创建DInput设备出错!","Error!", MB_OK) ;
  163.   ReleaseDInput();
  164.   PostQuitMessage( 0 );
  165.   return 0;
  166. }
  167. GetMessage(&msg,NULL,NULL,NULL);
  168.     //process the message quenue
  169. while(msg.message != WM_QUIT)
  170. {
  171.   UpdateInputState();
  172.   if( GetMessage(&msg,NULL,0,0) )
  173.   {
  174.    //解析消息
  175.    TranslateMessage(&msg);
  176.    //分派消息
  177.    DispatchMessage(&msg);
  178.   }
  179. }
  180. ReleaseDInput();
  181. UnregisterClass( "DInput_Test", hInstance);
  182.     return msg.wParam;
  183. }  
  184. //window message process function   
  185.     LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wparam,LPARAM lparam)  
  186.     {  
  187.         switch(msg)
  188.   {
  189.    //关闭窗口消息的处理
  190.   case WM_CLOSE:
  191.    DestroyWindow( hwnd );
  192.    break;
  193.    //销毁窗口消息的处理
  194.   case WM_DESTROY:
  195.    //退出当前进程
  196.    PostQuitMessage( 0 );
  197.    break;
  198.         default:  
  199.             return DefWindowProc(hwnd,msg,wparam,lparam);  
  200.         }  
  201.         return NULL;  
  202.     }
复制代码
我就是你们的神,庶民们,追随我吧!跟着我一起拖后腿!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|幻想森林

GMT+8, 2024-3-29 16:23 , Processed in 0.018599 second(s), 20 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表