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

天行健 君子當(dāng)自強(qiáng)而不息

使用DirectInput進(jìn)行交互(3)

 

本篇是使用DirectInput進(jìn)行交互(2)的續(xù)篇。


使用鼠標(biāo)玩游戲

鼠標(biāo)的工作原理理解起來并不難,在最底層,鼠標(biāo)通知系統(tǒng)它要移動(dòng)到某個(gè)方向,每次移動(dòng)一個(gè)記號(hào),驅(qū)動(dòng)程序讀取這個(gè)數(shù)據(jù)后,將記號(hào)轉(zhuǎn)化為相對(duì)移動(dòng)值。

在通常的應(yīng)用程序中,windows得到鼠標(biāo)的移動(dòng)并通過消息處理函數(shù)將移動(dòng)作為消息報(bào)告給用戶。使用消息處理函數(shù)有時(shí)速度會(huì)非常慢,因?yàn)閭鬟f給消息處理函數(shù)的每個(gè)消息要被插入到隊(duì)列中,這樣消息就只會(huì)按照他們加入到隊(duì)列中的順序被處理。要加快接收以及處理鼠標(biāo)輸入的過程,就必須直接同鼠標(biāo)的驅(qū)動(dòng)程序進(jìn)行交互,而不采用windows消息處理函數(shù)。

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

如下圖所示,通過鼠標(biāo)距屏幕左上角的像素?cái)?shù)量來衡量鼠標(biāo)的絕對(duì)坐標(biāo)。

相對(duì)指的是從上個(gè)已知位置到當(dāng)前位置所發(fā)生的移動(dòng)量,上個(gè)位置可能位于左邊、右邊、上邊或下邊。

 

使用DirectInput處理鼠標(biāo)

除了指定的是鼠標(biāo)標(biāo)識(shí)符以及鼠標(biāo)數(shù)據(jù)格式外,初始化鼠標(biāo)就和初始化鍵盤幾乎完全相同。

//--------------------------------------------------------------------------------
// 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;
}

要調(diào)用DirectInputDevice8::GetDeviceState,使用諸如相對(duì)移動(dòng)和按鍵狀態(tài)等鼠標(biāo)的相關(guān)信息來填充 DIMOUSESTATE結(jié)構(gòu)體。

DIMOUSESTATE結(jié)構(gòu)體的定義如下:

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.



點(diǎn)擊下載源碼和工程

完整源碼示例:

/***************************************************************************************
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;
}
 

運(yùn)行截圖:



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

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

評(píng)論

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

謝謝您的文章,給了我很大的幫助。希望您能個(gè)寫一些3D碰撞檢測實(shí)現(xiàn)方法的文章。期待中。。。  回復(fù)  更多評(píng)論   


只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


公告

導(dǎo)航

統(tǒng)計(jì)

常用鏈接

隨筆分類(178)

3D游戲編程相關(guān)鏈接

搜索

最新評(píng)論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
            国产一区二区三区高清在线观看| 美女诱惑一区| 国产精品伦子伦免费视频| 欧美精品一区二区视频| 欧美日韩亚洲视频一区| 国产精品久久久久国产精品日日| 国产精品久久久久av免费| 欧美无砖砖区免费| 国产欧美精品日韩区二区麻豆天美| 国产麻豆一精品一av一免费| 国产精品久久午夜夜伦鲁鲁| 国产综合精品| 日韩视频免费观看高清完整版| 一本色道婷婷久久欧美| 欧美一区二区观看视频| 久久综合国产精品| 日韩一级网站| 久久精品视频亚洲| 欧美日韩国语| 国精品一区二区| 亚洲免费av观看| 久久av在线看| 亚洲精品在线视频观看| 欧美影院视频| 欧美无乱码久久久免费午夜一区| 国产一区二区三区四区在线观看| 亚洲高清视频中文字幕| 亚洲欧美乱综合| 亚洲成人中文| 一区二区三区视频在线观看| 欧美亚洲午夜视频在线观看| 欧美精品免费观看二区| 黄色成人在线| 欧美一区二区视频97| 91久久国产综合久久蜜月精品| 亚洲欧美在线aaa| 欧美精品偷拍| 一区二区三区在线视频免费观看| 亚洲视频在线视频| 亚洲国产精品久久久久婷婷老年| 小黄鸭精品aⅴ导航网站入口| 99pao成人国产永久免费视频| 香蕉乱码成人久久天堂爱免费| 久久一区二区三区四区五区| 国产精品亚洲视频| 夜夜嗨av一区二区三区网站四季av | 亚洲国产一区二区在线| 亚洲欧美变态国产另类| 欧美激情国产日韩精品一区18| 国内成人精品视频| 久久er99精品| 午夜欧美大片免费观看 | 裸体女人亚洲精品一区| 国产欧美日韩不卡免费| 亚洲欧美亚洲| a4yy欧美一区二区三区| 欧美黄色日本| 99视频在线观看一区三区| 欧美国产日韩在线观看| 久热成人在线视频| 曰本成人黄色| 久久狠狠久久综合桃花| 亚洲影院在线| 国产精品欧美日韩一区二区| 亚洲午夜激情网站| 亚洲视频一区在线| 国产精品视频你懂的| 羞羞色国产精品| 亚洲一区二区三区在线视频| 国产精品久久久久久久第一福利 | 毛片基地黄久久久久久天堂| 久久久精品五月天| 精品91视频| 欧美国产日韩精品免费观看| 欧美xx69| 亚洲你懂的在线视频| 午夜欧美大片免费观看| 影音先锋中文字幕一区二区| 免费人成网站在线观看欧美高清| 蜜月aⅴ免费一区二区三区| 亚洲区在线播放| 99re热这里只有精品免费视频| 欧美日韩在线一区| 久久大香伊蕉在人线观看热2| 久久精品72免费观看| 亚洲全部视频| 亚洲午夜激情| 黄色影院成人| 亚洲精品国精品久久99热一| 国产精品久久久久永久免费观看 | 久久婷婷国产综合精品青草| 亚洲国产欧美日韩精品| 亚洲激情网址| 国产精品久久久一区二区| 久久久久国产精品人| 欧美不卡在线| 久久爱www| 欧美国产视频日韩| 久久国产福利| 欧美女同在线视频| 久久国产精品毛片| 欧美国产一区二区在线观看| 午夜精品视频一区| 欧美国产日产韩国视频| 久久成人亚洲| 欧美视频第二页| 欧美电影免费| 国产亚洲精品激情久久| 亚洲另类在线一区| 最新中文字幕亚洲| 久久精品卡一| 欧美中文在线观看| 欧美性猛片xxxx免费看久爱| 欧美大片免费看| 国产欧美精品国产国产专区| 亚洲国产精品va在线看黑人动漫| 国产精品丝袜xxxxxxx| 99re6热在线精品视频播放速度| 永久域名在线精品| 欧美一区二区久久久| 亚洲综合色婷婷| 欧美伦理a级免费电影| 老牛影视一区二区三区| 国产精品任我爽爆在线播放| 亚洲精品久久久久久久久久久 | 欧美二区在线观看| 麻豆91精品| 黄色综合网站| 久久国产精品第一页| 亚洲欧美日韩国产一区二区| 欧美日韩国产不卡在线看| 亚洲福利久久| 亚洲人午夜精品免费| 另类尿喷潮videofree | 亚洲欧美激情诱惑| 亚洲一区二区三区免费视频| 欧美区视频在线观看| 亚洲国产一区二区精品专区| 亚洲第一精品夜夜躁人人爽 | 国产精品毛片| 亚洲欧美视频在线观看视频| 亚洲一区二区三区免费在线观看| 欧美经典一区二区| 亚洲精品综合| 亚洲一区二区三区涩| 国产麻豆精品在线观看| 欧美一区在线看| 久久婷婷亚洲| 亚洲国产岛国毛片在线| 欧美国产日韩亚洲一区| 亚洲卡通欧美制服中文| 亚洲欧美国产制服动漫| 国产精品午夜国产小视频| 一本一道久久综合狠狠老精东影业| 久久精品日产第一区二区| 宅男噜噜噜66一区二区66| 欧美日韩精品免费观看| 99在线精品免费视频九九视| 亚洲一区二区在线看| 国产精品网站视频| 久久久久国内| 亚洲裸体视频| 久久久精品五月天| 亚洲毛片在线观看| 国产精品你懂的在线| 久久精品久久99精品久久| 亚洲国产成人高清精品| 亚洲视频在线二区| 国产日韩欧美综合精品| 久久久久久久网站| 亚洲欧洲一区二区三区| 亚洲欧美日韩一区二区三区在线| 韩国v欧美v日本v亚洲v| 欧美日韩国产精品成人| 欧美影院久久久| 99re热精品| 久久综合色播五月| 亚洲午夜电影在线观看| 国内久久精品视频| 欧美三日本三级三级在线播放| 久久av一区二区三区| 亚洲国产一区二区视频| 久久久精品动漫| 亚洲视频网在线直播| 亚洲第一免费播放区| 国产欧美日韩综合| 欧美日韩精品一二三区| 久久亚洲国产成人| 午夜一区二区三视频在线观看| 亚洲国产欧美日韩| 欧美一区二区在线免费播放| 亚洲激情视频| 国产在线精品自拍| 欧美日韩一区二区三区在线| 久久网站免费| 亚洲欧美国产不卡| 亚洲亚洲精品在线观看| 亚洲激情亚洲| 亚洲第一成人在线| 久久综合亚州|