Posted on 2008-12-14 21:53
Condor 閱讀(1551)
評論(0) 編輯 收藏 引用
相對于其他的子系統(tǒng)來說, 輸入系統(tǒng)是比較簡單的. 很多游戲根本就沒有對這一塊進(jìn)行封裝, 而直接采用了Win32的消息機(jī)制.
不過經(jīng)過封裝的輸入系統(tǒng)使用起來很方便, 呵呵.
N3中有三種輸入設(shè)備, 鍵盤, 鼠標(biāo), 手柄. 分別是基于Win32消息, DirectInput, XInput實(shí)現(xiàn)的. 這里有一個(gè)繼承圖能夠很好的說明輸入系統(tǒng)的組織結(jié)構(gòu):
基本的消息處理機(jī)制是這樣的一個(gè)流程:
InputServer里有默認(rèn)的一個(gè)鍵盤, 一個(gè)鼠標(biāo), 一個(gè)手柄的"handler", 在每幀開始時(shí)InputServer會檢測當(dāng)前的輸入消息, 得到一個(gè)InputEvent, 由相應(yīng)的InputHandler來處理. 各個(gè)InputHandler都保存著當(dāng)前幀各種輸入狀態(tài)的緩存(如鼠標(biāo)左鍵是否按下), 因此, 在程序運(yùn)行過程中, 我們只要在繪制結(jié)束前檢測各個(gè)InputHandler的狀態(tài)就相當(dāng)于知道當(dāng)前用戶是怎樣輸入的了.
一般只需要關(guān)心這么幾個(gè)函數(shù)就夠了:
- ////////////////////// Mouse////////////////////////////
-
- /// return true if button is currently pressed
- bool ButtonPressed(Input::MouseButton::Code btn) const;
- /// return true if button was down at least once in current frame
- bool ButtonDown(Input::MouseButton::Code btn) const;
- /// return true if button was up at least once in current frame
- bool ButtonUp(Input::MouseButton::Code btn) const;
- /// return true if a button has been double clicked
- bool ButtonDoubleClicked(Input::MouseButton::Code btn) const;
- /// return true if mouse wheel rotated forward
- bool WheelForward() const;
- /// return true if mouse wheel rotated backward
- bool WheelBackward() const;
- /// get current absolute mouse position (in pixels)
- const Math::float2& GetPixelPosition() const;
- /// get current screen space mouse position (0.0 .. 1.0)
- const Math::float2& GetScreenPosition() const;
- /// get mouse movement
- const Math::float2& GetMovement() const;
- //////////////////////Keyboard//////////////////////
-
- /// return true if a key is currently pressed
- bool KeyPressed(Input::Key::Code keyCode) const;
- /// return true if key was down at least once in current frame
- bool KeyDown(Input::Key::Code keyCode) const;
- /// return true if key was up at least once in current frame
- bool KeyUp(Input::Key::Code keyCode) const;
- /// get character input in current frame
- const Util::String& GetCharInput() const;
GamePad先略過, 原理相同
測試?yán)? 在上一次的代碼中添加一段:
- void OnRenderFrame()
- {
- if (this->inputServer->GetDefaultMouse()->ButtonDown(MouseButton::LeftButton))
- {
- MessageBoxA(this->displayDevice->GetHwnd(), "Left Button Down", NULL, 0);
- }
- //...//
- }
效果:
