• <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>

            清源游民  gameogre@gmail.com
            在第一部分里分析了 ExampleFrameListener 大部內容,不過沒有窮盡,它其實也包含了輸入部分。因為 CEGUI 本身沒輸入偵測功能,他需要外部力量的幫助。在 ogre1.4 之間的 demo 中,輸入部分是 ogre 自帶的,功能有限(對于我這個菜鳥,暫時還用不到特別的功能 ^_^ )。新版本里,干脆把這部分去掉了,輸入部分采用了新的類庫 OIS - Object-oriented Input Library . 他的作者本身就是 OGRE team 的成員,我們可以相信,他可以與 Ogre 一起工作得很好 .OIS 支持鍵盤,鼠標,游戲桿,最后一項暫時不討論 . 在 windows 平臺下, OIS 提供了對 DirectInput 的封裝,基本的輸入功能是由后者來實現的。既然是學習,我們不防先大概理解一下 DirectInput 基本功能與使用流程以及 OIS 是如何將這些功能封裝進去的。我的目的是要討論 CEGUI 的,現在轉到了 OIS, 又轉到了 DirectxInput, 似乎是離目標越來越遠了。呵,在菜鳥的眼中,什么東西都是菜,都有營養,不防咀嚼個一下下。 CEGUI 固然是好東西,但是如果我們菜鳥整天只會學一堆堆沒完沒了的類庫,那么我們可能永遠就只是個菜鳥了,一句話,知道他做了些什么,比光知道他怎么用那可是相當有效!費話少說了,先看一下 DirectInput, 從 MSDN 上直接抄了一段, 懶得翻譯了: 下面第一段話,說了理解directInput需要了解的一些基本概念與術語,因為以前看過一點,大概知道是些什么。而我現在主要是做學習筆記,不是教程,所以不解釋了。
            To understand DirectInput, it is essential to understand the following terms.?
            DirectInput object: The root DirectInput interface. ·?Device: A keyboard, mouse, joystick, or other input device. ·? DirectInputDevice object: Code representing a keyboard, mouse, joystick, or other input device.·??
            Device object: Code representing a key, button, trigger, and so on found on a DirectInput device object. Also called device object instance.
            第二段話說明了使用 DirectInput 的基本步驟, OIS 把這些東西封裝起來了 .
            The following steps represent a simple implementation of DirectInput in which the application takes responsibility for ascertaining what device object (button, axis, and so on) generated each item of data.

            1.? Create the DirectInput object. You use methods of this object to enumerate devices and create DirectInput device objects.

            2.? Enumerate devices. This is not an essential step if you intend to use only the system mouse or keyboard. To ascertain what other input devices are available on the user's system, have DirectInput enumerate them. Each time DirectInput finds a device that matches the criteria you set, it gives you the opportunity to examine the device's capabilities. It also retrieves a unique identifier that you can use to create a DirectInput device object representing the device.

            3.? Create a DirectInputDevice object for each device you want to use. To do this, you need the unique identifier retrieved during enumeration. For the system mouse or keyboard, you can use a standard GUID.

            4.? Set up the device. For each device, first set the cooperative level, which determines the way the device is shared with other applications or the system. You must also set the data format used for identifying device objects, such as buttons and axes, within data packets. If you intend to retrieve buffered data—that is, events rather than states—you also need to set a buffer size. Optionally, at this stage you can retrieve information about the device and tailor the application's behavior accordingly. You can also set properties such as the range of values returned by joystick axes.

            5.? Acquire the device. At this stage you tell DirectInput that you are ready to receive data from the device.

            6.? Retrieve data. At regular intervals, typically on each pass through the message loop or rendering loop, get either the current state of each device or a record of events that have taken place since the last retrieval. If you prefer, you can have DirectInput notify you whenever an event occurs.

            7.? Act on the data. The application can respond either to the state of buttons and axes or to events such as a key being pressed or released.

            8.? Close DirectInput. Before exiting, your application should unacquire all devices and release them, then release the DirectInput object.

            了解了這些基本步驟,下面我們打開OIS的源碼,看看上面這8步驟封裝到了哪里。然后我們返回 ExampleFrameListener, 看看 demo 中是如何使用 OIS 的 .
            第一個要看的是InputManager類。他提供了平臺無關的接口,負責輸入系統的創建。沒啥好說的,看源碼吧(只列出核心代碼):
            InputManager * InputManager::createInputSystem( ParamList &paramList )
            {????
            ???? ?InputManager* im = 0;
            ????? #elif defined OIS_WIN32_PLATFORM?
            ?????? ??? im = newWin32InputManager();
            ??? #endif?
            ?????? im->_initialize(paramList);?
            ??? return im;
            }
            自然,我們只關心windows平臺下的。于是找到源碼繼續看:
            void Win32InputManager::_initialize( ParamList &paramList )
            {?
            ? //Create the device?
            ?? hr = DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&mDirectInput, NULL );
            ?? /Ok, now we have DirectInput, parse whatever extra settings were sent to us?
            ??? _parseConfigSettings( paramList );?
            ??? _enumerateDevices();
            }

            首先,完成了8步驟中的第1步, Create the DirectInput object 。 在_parseConfigSettings ( paramList ); 中對以后將要創建的設備(鍵盤,鼠標等)的屬性,例如獨占模式,前臺模式等。這些屬性列表paramList?? 經過處理后,之后在創建設備的時候傳入。
            void Win32InputManager::_enumerateDevices()
            {???? //Enumerate all attached devices?
            ??? mDirectInput->EnumDevices(NULL, _DIEnumKbdCallback, this, DIEDFL_ATTACHEDONLY);
            }

            完成了8步驟中的第2 部分 Enumerate devices ,它是針對游戲桿這類設計的,對于鍵盤,鼠標并不需要。InputManager 創建之后,就可以用它來創建鍵盤,鼠標了。它提供了如下的方法:
            Object * Win32InputManager::createInputObject( TypeiType, boolbufferMode )
            {???
            ? Object* obj = 0;???
            ??? switch( iType )?
            ??? {?
            ??? case OISKeyboard: obj = newWin32Keyboard( this, mDirectInput, bufferMode, kbSettings ); break;?
            ??? case OISMouse: obj = newWin32Mouse( this, mDirectInput, bufferMode, mouseSettings ); break;?
            ??? obj->_initialize();?
            ??? return obj;
            }

            鼠標,鍵盤都有各自的類封裝。下面以鍵盤為例,看看它的源碼中都做了些什么:
            void Win32Keyboard::_initialize()
            {

            1????? mDirectInput->CreateDevice(GUID_SysKeyboard, &mKeyboard, NULL);

            2????? mKeyboard->SetDataFormat(&c_dfDIKeyboard)

            3????? HWNDhwin = ((Win32InputManager*)mCreator)->getWindowHandle();

            4????? mKeyboard->SetCooperativeLevel( hwin, coopSetting)))

            5????? mKeyboard ->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph )))

            6????? HRESULThr = mKeyboard->Acquire();?

            }

            這里可是做了不少的工作,看看方法名就可以明白。為說明方便加上了標號。
            1, 它做了8步驟中的第3步, Create a DirectInputDevice object for each device you want to use 。
            2, 3 4,5它們共同做了8步驟中的第4步- Set up the device 。這里面包括:設置鍵盤的數據格式,協作模式,其他屬性。很明顯,6做了8 步步驟中的第5步- Acquire the device 。 意思是告訴DirectInput,我準備從設備上獲取數據了,給我做好生伺侯著。準備工作都做好了,可以獲取數據了,通過設備提供的下面這個方法來做這件事。這就是8 步驟中的第6件事- Retrieve data
            void Win32Keyboard::capture()
            {???? if( mBuffered )?
            ?????? _readBuffered();?
            ???? ? else?
            ?????? _read();
            }

            從源碼中我們可以看到,根據數據的不同,進行了不同的處理。呵,什么不同呢,再從MSDN上抄一段吧,一看就明白: Buffered and Immediate Data
            DirectInput supplies two types of data: buffered and immediate. Buffered data is a record of events that are stored until an application retrieves them. Immediate data is a snapshot of the current state of a device. You might use immediate data in an application that is concerned only with the current state of a device - for example, a flight combat simulation that responds to the current position of the joystick and the state of one or more buttons. Buffered data might be the better choice where events are more important than states - for example, in an application that responds to movement of the mouse and button clicks. You can also use both types of data, as you might, for example, if you wanted to get immediate data for joystick axes but buffered data for the buttons.

            呵,清楚了吧。繼續看代碼, capture()有兩個分枝,分別處理緩沖數據與立即數據。
            首先是緩沖數據,主干代碼如下:
            void Win32Keyboard::_readBuffered()
            {
            ? DIDEVICEOBJECTDATA diBuff[KEYBOARD_DX_BUFFERSIZE];?
            ??? DWORD entries = KEYBOARD_DX_BUFFERSIZE;??
            ?? mKeyboard->GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );?
            ??? //Update keyboard and modifier states.. And, if listener, fire events?
            ??? for(unsignedinti = 0; i < entries; ++i )?
            ??? {?
            ?????? KeyCode kc = (KeyCode)diBuff[ i ].dwOfs;?
            ???????????????????? if( diBuff[ i ].dwData & 0x80 )?
            ?????? {?
            ?? ?????? if( listener )?
            ??????????? istener->keyPressed( KeyEvent( this,kc,_translateText(kc) ) );?
            ?????? }?
            ?????? else
            ?????? {?
            ?????????? //Fire off event?
            ?????????? if( listener )?
            ??????????? listener->keyReleased( KeyEvent( this, kc, 0 ) );?
            ?????? }?
            ??? }
            }

            這段代碼中,GetDeviceData( sizeof(DIDEVICEOBJECTDATA), diBuff, &entries, 0 );取得了緩沖數據,添入了一個數組:尺寸是EYBOARD_DX_BUFFERSIZE,可以看到entries也等于此值。Entries的作用除了開始調用時說明數組的大小,同時在函數調用返回時說明數組里有多少個數據填入(沒有足夠多的緩沖數據,當然填不滿啦,假如entries返回6,說明還有6個按鍵信息在緩沖里等待處理)。很明顯,for循環就是對這些未處理的信息進行處理。KeyCode kc = (KeyCode)diBuff[ i ].dwOfs; 這是看看,這個事件是由哪個鍵引走.KeyCode是個枚舉型:
            enum KeyCode?
            ??? {?
            ???  ? C_1?????????? = 0x02,?
            ?????? KC_2??????????? = 0x03,?
            ?????? KC_3??????????? = 0x04,?
            ?????? KC_A??????????? = 0x1E,?
            ?????? KC_S??????????? = 0x1F,?
            ?????? KC_D??????????? = 0x20,?
            ??? }

            確定了是哪個鍵,接下來就要問了,是按下還是釋放?
            ?if ( diBuff[ i ].dwData & 0x80 )  回答了這個問題,下面做的是
            if ( listener )?
            ??????????? istener->keyPressed( KeyEvent( this,kc,_translateText(kc) ) );

            它把相關的信息內容包裝成KeyEvent,作為參數發給了,已經注冊了的偵聽者.可能不太明白,解釋一下:當我們按下了某個鍵,可以認為是發生了個KeyEvent,程序里會響應這個事件,方法就是把某個類實例注冊為偵聽者(listener),說白了就是告訴OIS,當鍵盤按下釋放的時候你告訴我啊,我要響應他。這里OIS檢測到按鍵事件發生了,根據偵聽者的請求,調用偵聽者的方法(keyPressed)。也許有疑問,OIS 怎么知道偵聽者實現了keypressed()方法呢?不急先看以下代碼,在GUI demo里:
            class GuiFrameListener : publicExampleFrameListener, publicOIS::KeyListener, publicOIS::MouseListener
            看到了吧,它繼承自? OIS:KeyListener ,從OIS的源碼中找到它:
            class _OISExport KeyListener?
            ??? {?
            ??? public:?
            ?????? virtual ~KeyListener() {}?
            ?????? virtual bool keyPressed( constKeyEvent &arg ) = 0;?
            ?????? virtual bool keyReleased( constKeyEvent &arg ) = 0;????
            ??? };
            哈哈,他正是定義了這兩個接口,既然繼承自他,當然也就擁有了這兩個接口,于是GuiFrameListener成了合理合法的Listener了。
            哦,它在哪里注冊的呢?很簡單.在GuiFrameListener的構造函數里我們看到:
            {
            ?????? mMouse ->setEventCallback(this);?
            ?????? mKeyboard->setEventCallback(this);
            }
            繼續挖源碼:
            virtual void Keyboard : :setEventCallback ( KeyListener *keyListener ) {listener=keyListener;}
            這下應該就明白了吧。很明顯GUI Demo里使用了緩沖模式.
            剩下來就是立即模式的數據了,他很好理解:
            void Win32Keyboard::_read()
            {?
            ? mKeyboard->GetDeviceState( sizeof(KeyBuffer), &KeyBuffer );?
            }
            它把鍵盤當下的狀態保存到緩沖中去,要想知道哪個鍵是否按下,只要對照緩沖“按圖索驥”就可以了。
            ?bool Win32Keyboard::isKeyDown( KeyCodekey )
            {?
            ??? return (KeyBuffer[key] & 0x80) != 0;
            }
            這個鍵按下了嗎?那個鍵按下了嗎?那那個呢?呵,真啰嗦,得一個個的問。
            于是我們 GUI Demo 中可以看到下列的代碼:
            virtual bool processUnbufferedKeyInput(const FrameEvent& evt)?
            ?????? {?

            ??????????? if(mKeyboard->isKeyDown(KC_A))?
            ???????????????????? ?mTranslateVector.x = -mMoveScale;??? // Move camera left?
            ??????????? if(mKeyboard->isKeyDown(KC_D))?
            ?????????????????? ?mTranslateVector.x = mMoveScale;????? // Move camera RIGHT?
            ????????????? if(mKeyboard->isKeyDown(KC_UP) || mKeyboard->isKeyDown(KC_W) )?
            ???????????????????? mTranslateVector.z = -mMoveScale;???? // Move camera forward?
            ????????????? if(mKeyboard->isKeyDown(KC_DOWN) || mKeyboard->isKeyDown(KC_S) )??
            ??????? ??????????? mTranslateVector.z = mMoveScale;????? // Move camera backward

            我們用鍵盤要不是緩沖模式,要么立即模式,不可能一起上,這所有在 demo 中都能看到, demo 的作者懶得多寫代碼 ( 嘿嘿 ) ,它繼承了 ExampleFrameListener 的許多功能,而 ExampleFrameListener 也實現了些立即模式的按鍵處理。 Demo 沒有用到。寫了這么多了,還得 8  大步驟?? 該第 7 步了吧: ―― Act on the data , 這有什么好說的呢?愛咋咋地!最后一步,第 8 步――天龍八部 ! 呵錯了,是 Close DirectInput????? 其實很簡單, OIS 把他們封裝在各個類的析構函數里了,想當然,源碼也不用貼了 . 說了 OIS  如何把 DirectInput 封裝起來,參考 8 大步驟,如何使用 OIS 也基本很明白了。OIS ,就學到這里,接下來該主角上場 CEGUI?得休息了,打字,翻資料,看代碼,很累了。


            posted on 2007-03-02 15:27 清源游民 閱讀(3638) 評論(0)  編輯 收藏 引用 所屬分類: OGRE
            <2006年12月>
            262728293012
            3456789
            10111213141516
            17181920212223
            24252627282930
            31123456

            留言簿(35)

            隨筆分類(78)

            隨筆檔案(74)

            文章檔案(5)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            色欲久久久天天天综合网精品| 国产精品久久久久天天影视| 久久性生大片免费观看性| 久久久久亚洲AV无码专区桃色 | 亚洲中文久久精品无码| 久久久久久亚洲精品成人| 久久午夜电影网| 久久久高清免费视频| 日韩一区二区久久久久久| 久久精品国产欧美日韩99热| 久久久久久综合一区中文字幕| 欧美久久综合九色综合| 国内精品久久人妻互换| 亚洲国产成人久久综合区| 久久久久久夜精品精品免费啦| 亚洲精品久久久www| 久久久青草青青亚洲国产免观| 久久九九兔免费精品6| 国内精品久久久久久久影视麻豆 | 久久精品国产亚洲沈樵| 久久人人爽人人爽人人片AV麻烦| 久久91精品国产91久久麻豆 | 久久免费线看线看| 精品综合久久久久久98| 精品久久久久久无码中文野结衣 | 久久免费线看线看| 久久久亚洲欧洲日产国码二区| 亚洲伊人久久成综合人影院| 久久久久久久亚洲精品| 麻豆精品久久精品色综合| 丰满少妇高潮惨叫久久久| 99蜜桃臀久久久欧美精品网站| 欧美日韩精品久久久免费观看| 99久久精品国产一区二区蜜芽| 99久久免费国产精品热| 久久久久亚洲精品天堂| 亚洲成色WWW久久网站| 久久亚洲精精品中文字幕| 无码人妻精品一区二区三区久久久| 久久久久久久久久久精品尤物| 久久婷婷色综合一区二区|