青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

天行健 君子當自強而不息

使用DirectInput進行交互(3)

 

本篇是使用DirectInput進行交互(2)的續篇。


使用鼠標玩游戲

鼠標的工作原理理解起來并不難,在最底層,鼠標通知系統它要移動到某個方向,每次移動一個記號,驅動程序讀取這個數據后,將記號轉化為相對移動值。

在通常的應用程序中,windows得到鼠標的移動并通過消息處理函數將移動作為消息報告給用戶。使用消息處理函數有時速度會非常慢,因為傳遞給消息處理函數的每個消息要被插入到隊列中,這樣消息就只會按照他們加入到隊列中的順序被處理。要加快接收以及處理鼠標輸入的過程,就必須直接同鼠標的驅動程序進行交互,而不采用windows消息處理函數。

不論采用哪種移動方式接收鼠標移動,都要從跟蹤鼠標在屏幕上的坐標開始,可以選擇追蹤絕對鼠標坐標或相對鼠標坐標。絕對表示當前的鼠標位置都是基于某個固定點(通常是屏幕的左上角)。

如下圖所示,通過鼠標距屏幕左上角的像素數量來衡量鼠標的絕對坐標。

相對指的是從上個已知位置到當前位置所發生的移動量,上個位置可能位于左邊、右邊、上邊或下邊。

 

使用DirectInput處理鼠標

除了指定的是鼠標標識符以及鼠標數據格式外,初始化鼠標就和初始化鍵盤幾乎完全相同。

//--------------------------------------------------------------------------------
// Initialize mouse interface, return a mouse interface pointer.
//--------------------------------------------------------------------------------
IDirectInputDevice8* Init_Mouse(HWND hwnd, IDirectInput8* directinput)
{
    IDirectInputDevice8* directinput_device;

    
// create the device object
    if(FAILED(directinput->CreateDevice(GUID_SysMouse, &directinput_device, NULL)))
        
return NULL;

    
// set the data format
    if(FAILED(directinput_device->SetDataFormat(&c_dfDIMouse)))
    {
        directinput_device->Release();
        
return NULL;
    }

    
// set the coooperative mode
    if(FAILED(directinput_device->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
    {
        directinput_device->Release();
        
return NULL;
    }

    
// acquire the device for use
    if(FAILED(directinput_device->Acquire()))
    {
        directinput_device->Release();
        
return NULL;
    }

    
// everything well, so return a vaild pointer.
    return directinput_device;
}

要調用DirectInputDevice8::GetDeviceState,使用諸如相對移動和按鍵狀態等鼠標的相關信息來填充 DIMOUSESTATE結構體。

DIMOUSESTATE結構體的定義如下:

Describes the state of a mouse device that has up to four buttons, or another device that is being accessed as if it were a mouse device. This structure is used with the IDirectInputDevice8::GetDeviceState method.

typedef struct DIMOUSESTATE {
LONG lX;
LONG lY;
LONG lZ;
BYTE rgbButtons[4];
} DIMOUSESTATE, *LPDIMOUSESTATE;

Members

lX
X-axis.
lY
Y-axis.
lZ
Z-axis, typically a wheel. If the mouse does not have a z-axis, the value is 0.
rgbButtons
Array of buttons. The high-order bit of the byte is set if the corresponding button is down.

Remarks

You must prepare the device for mouse-style access by calling the IDirectInputDevice8::SetDataFormat method, passing the c_dfDIMouse global data format variable.

The mouse is a relative-axis device, so the absolute axis positions for mouse axes are accumulated relative motion. Therefore, the value of the absolute axis position is not meaningful except in comparison with other absolute axis positions.

If an axis is in relative mode, the appropriate member contains the change in position. If it is in absolute mode, the member contains the absolute axis position.



點擊下載源碼和工程

完整源碼示例:

/***************************************************************************************
PURPOSE:
    Mouse device Demo
 ***************************************************************************************/


#define DIRECTINPUT_VERSION 0x0800

#include <windows.h>
#include <stdio.h>
#include <dinput.h>
#include "resource.h"

#pragma comment(lib, "dxguid.lib")
#pragma comment(lib, "dinput8.lib")

#pragma warning(disable : 4996)

#define Safe_Release(p) if((p)) (p)->Release();

// window handles, class and caption text.
HWND g_hwnd;
char g_class_name[] = "MouseClass";

IDirectInput8* g_directinput;               
// directinput component
IDirectInputDevice8* g_directinput_device;  // mouse device

//--------------------------------------------------------------------------------
// Window procedure.
//--------------------------------------------------------------------------------
long WINAPI Window_Proc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    
switch(msg)
    {
    
case WM_DESTROY:
        PostQuitMessage(0);
        
return 0;
    }

    
return (long) DefWindowProc(hwnd, msg, wParam, lParam);
}

//--------------------------------------------------------------------------------
// Initialize mouse interface, return a mouse interface pointer.
//--------------------------------------------------------------------------------
IDirectInputDevice8* Init_Mouse(HWND hwnd, IDirectInput8* directinput)
{
    IDirectInputDevice8* directinput_device;

    
// create the device object
    if(FAILED(directinput->CreateDevice(GUID_SysMouse, &directinput_device, NULL)))
        
return NULL;

    
// set the data format
    if(FAILED(directinput_device->SetDataFormat(&c_dfDIMouse)))
    {
        directinput_device->Release();
        
return NULL;
    }

    
// set the coooperative mode
    if(FAILED(directinput_device->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE)))
    {
        directinput_device->Release();
        
return NULL;
    }

    
// acquire the device for use
    if(FAILED(directinput_device->Acquire()))
    {
        directinput_device->Release();
        
return NULL;
    }

    
// everything well, so return a vaild pointer.
    return directinput_device;
}

//--------------------------------------------------------------------------------
// Read mouse buffer.
//--------------------------------------------------------------------------------
BOOL Read_Device(IDirectInputDevice8* directinput_device, void* buffer, long buffer_size)
{
    HRESULT rv;

    
while(1)
    {
        
// poll device
        g_directinput_device->Poll();

        
// read in state
        if(SUCCEEDED(rv = g_directinput_device->GetDeviceState(buffer_size, buffer)))
            
break;

        
// return when an unknown error
        if(rv != DIERR_INPUTLOST || rv != DIERR_NOTACQUIRED)
            
return FALSE;

        
// re-acquire and try again
        if(FAILED(g_directinput_device->Acquire()))
            
return FALSE;
    }

    
return TRUE;
}

//--------------------------------------------------------------------------------
// Main function, routine entry.
//--------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
    WNDCLASS        win_class;
    MSG             msg;
    DIMOUSESTATE    mouse_state = {0};
    
char            text[256];
    
long            x_pos = 0, y_pos = 0;

    
// create window class and register it
    win_class.style         = CS_HREDRAW | CS_VREDRAW;
    win_class.lpfnWndProc   = Window_Proc;
    win_class.cbClsExtra    = 0;
    win_class.cbWndExtra    = DLGWINDOWEXTRA;
    win_class.hInstance     = inst;
    win_class.hIcon         = LoadIcon(inst, IDI_APPLICATION);
    win_class.hCursor       = LoadCursor(NULL, IDC_ARROW);
    win_class.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
    win_class.lpszMenuName  = NULL;
    win_class.lpszClassName = g_class_name;    

    
if(! RegisterClass(&win_class))
        
return FALSE;

    
// create the main window
    g_hwnd = CreateDialog(inst, MAKEINTRESOURCE(IDD_MOUSE), 0, NULL);

    ShowWindow(g_hwnd, cmd_show);
    UpdateWindow(g_hwnd);

    
// initialize directinput and get keyboard device
    DirectInput8Create(inst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **) &g_directinput, NULL);

    
// initialize mouse
    g_directinput_device = Init_Mouse(g_hwnd, g_directinput);

    
// start message pump, waiting for signal to quit.
    ZeroMemory(&msg, sizeof(MSG));

    
while(msg.message != WM_QUIT)
    {
        
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        
        
// read in mouse and display coordinates
        Read_Device(g_directinput_device, &mouse_state, sizeof(DIMOUSESTATE));

        x_pos += mouse_state.lX;
        y_pos += mouse_state.lY;

        
if(mouse_state.lX != 0 || mouse_state.lY != 0)
        {
            sprintf(text, "%ld, %ld", x_pos, y_pos);
            SetWindowText(GetDlgItem(g_hwnd, IDC_COORDINATES), text);
        }
    }

    
// release directinput objects
    g_directinput_device->Unacquire();
    g_directinput_device->Release();
    g_directinput->Release();

    UnregisterClass(g_class_name, inst);
    
    
return (int) msg.wParam;
}
 

運行截圖:



閱讀下篇:使用DirectInput進行交互(4)

posted on 2007-07-25 15:14 lovedday 閱讀(1409) 評論(1)  編輯 收藏 引用

評論

# re: 使用DirectInput進行交互(3) 2007-07-26 00:08 小禹

謝謝您的文章,給了我很大的幫助。希望您能個寫一些3D碰撞檢測實現方法的文章。期待中。。。  回復  更多評論   

公告

導航

統計

常用鏈接

隨筆分類(178)

3D游戲編程相關鏈接

搜索

最新評論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            日韩一级片网址| 99riav国产精品| 亚洲日本成人| 黄色工厂这里只有精品| 国产亚洲激情| 在线观看成人小视频| 亚洲成人在线网站| 亚洲日本电影在线| 亚洲午夜久久久久久久久电影网| 一二美女精品欧洲| 午夜日韩在线| 久久中文在线| 欧美sm重口味系列视频在线观看| 亚洲电影在线免费观看| 亚洲第一精品夜夜躁人人躁| 亚洲国产女人aaa毛片在线| 亚洲美女淫视频| 亚洲尤物在线| 免费91麻豆精品国产自产在线观看| 久久青草欧美一区二区三区| 久久伊人亚洲| 欧美激情视频给我| 国产乱码精品一区二区三区忘忧草 | 欧美高清你懂得| 欧美日韩亚洲系列| 国产女人精品视频| 欧美韩日亚洲| 欧美在线观看视频在线| 欧美成人精品| 亚洲淫片在线视频| 蜜臀av性久久久久蜜臀aⅴ四虎| 欧美视频在线观看一区| 136国产福利精品导航网址| 亚洲一区二区免费| 欧美成人免费一级人片100| 中文在线资源观看视频网站免费不卡| 午夜视频久久久| 国产精品xxxav免费视频| 亚洲第一黄色| 久久久国产一区二区三区| 一本色道久久99精品综合| 久久精品日产第一区二区| 国产精品扒开腿爽爽爽视频| 亚洲日韩中文字幕在线播放| 久久久久久久一区| 亚洲一级影院| 欧美日韩免费一区二区三区视频| 尤物九九久久国产精品的特点 | 永久久久久久| 久久电影一区| 亚洲视频一区二区| 欧美日韩精品一区二区在线播放| 悠悠资源网亚洲青| 噜噜噜在线观看免费视频日韩| 国产日韩av在线播放| 99视频精品在线| 亚洲国产成人精品久久| 久久精品论坛| 伊人成人网在线看| 葵司免费一区二区三区四区五区| 亚洲欧美一区二区三区在线| 国产精品乱码久久久久久| 亚洲五月婷婷| 亚洲一区激情| 国产精品综合视频| 欧美在线观看一区二区| 亚洲欧美视频| 国产香蕉久久精品综合网| 欧美一区二区视频网站| 午夜国产精品视频| 国产日韩欧美一区在线| 久久精品国产亚洲精品| 欧美中文字幕在线观看| 激情婷婷亚洲| 亚洲第一二三四五区| 欧美日精品一区视频| 亚洲综合视频1区| 亚洲午夜三级在线| 国产日韩欧美日韩| 榴莲视频成人在线观看| 欧美国产激情二区三区| 国产精品99久久久久久人| 日韩小视频在线观看| 一本色道久久综合亚洲精品不| 亚洲毛片一区| 亚洲一区不卡| 久久久久国产精品人| 久久久久高清| 久久精品30| 欧美v日韩v国产v| 亚洲视频综合| 91久久久精品| 国产日产欧美a一级在线| 亚洲二区视频| 国产日韩在线不卡| 欧美日韩中文字幕在线| 亚洲欧美一区二区原创| 欧美日本成人| 亚洲精品乱码久久久久久蜜桃91| 在线看不卡av| 亚洲免费影院| 亚洲欧美日韩国产| 国产麻豆午夜三级精品| 亚洲精品视频啊美女在线直播| 国内在线观看一区二区三区 | 日韩亚洲欧美一区| 国产视频亚洲| 亚洲一区亚洲| 麻豆成人在线| 一本色道久久综合亚洲精品婷婷| 老司机午夜精品| 亚洲一区二区三区在线观看视频| 亚洲一区二区三区国产| 久久五月婷婷丁香社区| 久久精品国产一区二区三区免费看| 国产精品视频一二| 欧美一级片一区| 免费成人av在线看| 亚洲一区二区三区四区视频| 99在线|亚洲一区二区| 一区二区三区久久精品| 欧美成人在线网站| 久久亚洲精品伦理| 国产偷国产偷亚洲高清97cao | 欧美另类高清视频在线| 你懂的一区二区| 欧美理论在线播放| 欧美日韩在线视频首页| 欧美视频精品在线观看| 欧美午夜片在线观看| 国产精品久久久一本精品| 国产欧美在线视频| 国产一区二区丝袜高跟鞋图片| 国产伦精品一区二区三区视频孕妇| 亚洲在线观看| 亚洲制服欧美中文字幕中文字幕| 性欧美超级视频| 久久综合久久综合久久| 亚洲二区精品| 亚洲欧美日韩一区二区三区在线| 久久精品亚洲一区| 国产精品xvideos88| 亚洲日本中文字幕免费在线不卡| 亚洲嫩草精品久久| 亚洲激情成人网| 欧美亚洲综合网| 国产精品人成在线观看免费| 娇妻被交换粗又大又硬视频欧美| 国产精品久久久久久av福利软件 | 欧美一级在线视频| 亚洲成人资源网| 亚洲免费视频网站| 欧美日韩成人| 精品999在线观看| 亚洲一区三区电影在线观看| 欧美1级日本1级| 亚洲伦理在线| 亚洲激情午夜| 免费欧美网站| 亚洲国产日韩精品| 欧美91精品| 久久av一区二区三区| 国产欧美日韩精品a在线观看| 亚洲午夜91| 在线视频你懂得一区二区三区| 美女黄网久久| 亚洲精品综合精品自拍| 亚洲第一页在线| 欧美国产成人精品| 一本高清dvd不卡在线观看| 亚洲经典视频在线观看| 欧美激情1区2区| 久久精品一二三区| 亚洲在线免费观看| 免费成人高清视频| 亚洲高清av在线| 鲁大师影院一区二区三区| 欧美激情国产日韩| 欧美va天堂在线| 欧美日韩精品在线| 在线一区日本视频| 亚洲深夜福利在线| 久久精品日产第一区二区三区 | 日韩视频免费观看高清完整版| 在线观看欧美激情| 亚洲国产欧美国产综合一区| 久久免费视频在线| 亚洲精品久久久蜜桃| 久久久91精品国产| 欧美v日韩v国产v| 国产精品日韩欧美一区| 亚洲免费电影在线| 欧美国产日韩二区| 亚洲婷婷在线| 欧美二区乱c少妇| 欧美成人亚洲| 国产精品啊啊啊| 亚洲大片av| 99成人免费视频| 日韩一区二区免费看|