• <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 大部內(nèi)容,不過沒有窮盡,它其實也包含了輸入部分。因為 CEGUI 本身沒輸入偵測功能,他需要外部力量的幫助。在 ogre1.4 之間的 demo 中,輸入部分是 ogre 自帶的,功能有限(對于我這個菜鳥,暫時還用不到特別的功能 ^_^ )。新版本里,干脆把這部分去掉了,輸入部分采用了新的類庫 OIS - Object-oriented Input Library . 他的作者本身就是 OGRE team 的成員,我們可以相信,他可以與 Ogre 一起工作得很好 .OIS 支持鍵盤,鼠標(biāo),游戲桿,最后一項暫時不討論 . 在 windows 平臺下, OIS 提供了對 DirectInput 的封裝,基本的輸入功能是由后者來實現(xiàn)的。既然是學(xué)習(xí),我們不防先大概理解一下 DirectInput 基本功能與使用流程以及 OIS 是如何將這些功能封裝進(jìn)去的。我的目的是要討論 CEGUI 的,現(xiàn)在轉(zhuǎn)到了 OIS, 又轉(zhuǎn)到了 DirectxInput, 似乎是離目標(biāo)越來越遠(yuǎn)了。呵,在菜鳥的眼中,什么東西都是菜,都有營養(yǎng),不防咀嚼個一下下。 CEGUI 固然是好東西,但是如果我們菜鳥整天只會學(xué)一堆堆沒完沒了的類庫,那么我們可能永遠(yuǎn)就只是個菜鳥了,一句話,知道他做了些什么,比光知道他怎么用那可是相當(dāng)有效!費話少說了,先看一下 DirectInput, 從 MSDN 上直接抄了一段, 懶得翻譯了: 下面第一段話,說了理解directInput需要了解的一些基本概念與術(shù)語,因為以前看過一點,大概知道是些什么。而我現(xiàn)在主要是做學(xué)習(xí)筆記,不是教程,所以不解釋了。
            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類。他提供了平臺無關(guān)的接口,負(fù)責(zé)輸入系統(tǒng)的創(chuàng)建。沒啥好說的,看源碼吧(只列出核心代碼):
            InputManager * InputManager::createInputSystem( ParamList &paramList )
            {????
            ???? ?InputManager* im = 0;
            ????? #elif defined OIS_WIN32_PLATFORM?
            ?????? ??? im = newWin32InputManager();
            ??? #endif?
            ?????? im->_initialize(paramList);?
            ??? return im;
            }
            自然,我們只關(guān)心windows平臺下的。于是找到源碼繼續(xù)看:
            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 ); 中對以后將要創(chuàng)建的設(shè)備(鍵盤,鼠標(biāo)等)的屬性,例如獨占模式,前臺模式等。這些屬性列表paramList?? 經(jīng)過處理后,之后在創(chuàng)建設(shè)備的時候傳入。
            void Win32InputManager::_enumerateDevices()
            {???? //Enumerate all attached devices?
            ??? mDirectInput->EnumDevices(NULL, _DIEnumKbdCallback, this, DIEDFL_ATTACHEDONLY);
            }

            完成了8步驟中的第2 部分 Enumerate devices ,它是針對游戲桿這類設(shè)計的,對于鍵盤,鼠標(biāo)并不需要。InputManager 創(chuàng)建之后,就可以用它來創(chuàng)建鍵盤,鼠標(biāo)了。它提供了如下的方法:
            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;
            }

            鼠標(biāo),鍵盤都有各自的類封裝。下面以鍵盤為例,看看它的源碼中都做了些什么:
            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();?

            }

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

            從源碼中我們可以看到,根據(jù)數(shù)據(jù)的不同,進(jìn)行了不同的處理。呵,什么不同呢,再從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.

            呵,清楚了吧。繼續(xù)看代碼, capture()有兩個分枝,分別處理緩沖數(shù)據(jù)與立即數(shù)據(jù)。
            首先是緩沖數(shù)據(jù),主干代碼如下:
            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 );取得了緩沖數(shù)據(jù),添入了一個數(shù)組:尺寸是EYBOARD_DX_BUFFERSIZE,可以看到entries也等于此值。Entries的作用除了開始調(diào)用時說明數(shù)組的大小,同時在函數(shù)調(diào)用返回時說明數(shù)組里有多少個數(shù)據(jù)填入(沒有足夠多的緩沖數(shù)據(jù),當(dāng)然填不滿啦,假如entries返回6,說明還有6個按鍵信息在緩沖里等待處理)。很明顯,for循環(huán)就是對這些未處理的信息進(jìn)行處理。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) ) );

            它把相關(guān)的信息內(nèi)容包裝成KeyEvent,作為參數(shù)發(fā)給了,已經(jīng)注冊了的偵聽者.可能不太明白,解釋一下:當(dāng)我們按下了某個鍵,可以認(rèn)為是發(fā)生了個KeyEvent,程序里會響應(yīng)這個事件,方法就是把某個類實例注冊為偵聽者(listener),說白了就是告訴OIS,當(dāng)鍵盤按下釋放的時候你告訴我啊,我要響應(yīng)他。這里OIS檢測到按鍵事件發(fā)生了,根據(jù)偵聽者的請求,調(diào)用偵聽者的方法(keyPressed)。也許有疑問,OIS 怎么知道偵聽者實現(xiàn)了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;????
            ??? };
            哈哈,他正是定義了這兩個接口,既然繼承自他,當(dāng)然也就擁有了這兩個接口,于是GuiFrameListener成了合理合法的Listener了。
            哦,它在哪里注冊的呢?很簡單.在GuiFrameListener的構(gòu)造函數(shù)里我們看到:
            {
            ?????? mMouse ->setEventCallback(this);?
            ?????? mKeyboard->setEventCallback(this);
            }
            繼續(xù)挖源碼:
            virtual void Keyboard : :setEventCallback ( KeyListener *keyListener ) {listener=keyListener;}
            這下應(yīng)該就明白了吧。很明顯GUI Demo里使用了緩沖模式.
            剩下來就是立即模式的數(shù)據(jù)了,他很好理解:
            void Win32Keyboard::_read()
            {?
            ? mKeyboard->GetDeviceState( sizeof(KeyBuffer), &KeyBuffer );?
            }
            它把鍵盤當(dāng)下的狀態(tài)保存到緩沖中去,要想知道哪個鍵是否按下,只要對照緩沖“按圖索驥”就可以了。
            ?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 也實現(xiàn)了些立即模式的按鍵處理。 Demo 沒有用到。寫了這么多了,還得 8  大步驟?? 該第 7 步了吧: ―― Act on the data , 這有什么好說的呢?愛咋咋地!最后一步,第 8 步――天龍八部 ! 呵錯了,是 Close DirectInput????? 其實很簡單, OIS 把他們封裝在各個類的析構(gòu)函數(shù)里了,想當(dāng)然,源碼也不用貼了 . 說了 OIS  如何把 DirectInput 封裝起來,參考 8 大步驟,如何使用 OIS 也基本很明白了。OIS ,就學(xué)到這里,接下來該主角上場 CEGUI?得休息了,打字,翻資料,看代碼,很累了。


            posted on 2007-03-02 15:27 清源游民 閱讀(3656) 評論(0)  編輯 收藏 引用 所屬分類: OGRE
            <2007年3月>
            25262728123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            留言簿(35)

            隨筆分類(78)

            隨筆檔案(74)

            文章檔案(5)

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            丁香狠狠色婷婷久久综合| 精品久久久久久无码中文字幕| 亚洲国产日韩欧美久久| 狠狠色丁香婷婷久久综合| 一本一本久久a久久综合精品蜜桃 一本一道久久综合狠狠老 | 亚洲国产成人久久精品影视| 99久久国产亚洲高清观看2024| 久久人人爽人人爽人人片AV麻豆| 亚洲精品无码久久千人斩| 久久久精品午夜免费不卡| 人人狠狠综合久久亚洲| 国产∨亚洲V天堂无码久久久| 国产国产成人久久精品| 亚洲国产精品高清久久久| 品成人欧美大片久久国产欧美...| 久久人人爽人人爽人人片AV高清| 国产成人精品久久二区二区| 伊人久久一区二区三区无码| 国产午夜久久影院| 色婷婷久久综合中文久久蜜桃av| 久久99精品久久久久久不卡| 久久精品国产第一区二区三区 | 亚洲AⅤ优女AV综合久久久| 亚洲一区中文字幕久久| www久久久天天com| 色偷偷88888欧美精品久久久| 久久影视综合亚洲| 久久久久国产一区二区| 人人狠狠综合久久亚洲88| 久久ZYZ资源站无码中文动漫| 精品国产乱码久久久久久人妻| 亚洲精品国产综合久久一线| 久久国产精品一区| 日本国产精品久久| 伊人久久大香线蕉综合网站| 亚洲精品美女久久久久99小说| 久久国产三级无码一区二区| 亚洲综合婷婷久久| 久久噜噜久久久精品66| 久久综合久久性久99毛片| 久久无码一区二区三区少妇|