Posted on 2008-12-14 21:53
Condor 閱讀(1551)
評論(0) 編輯 收藏 引用
相對于其他的子系統(tǒng)來說, 輸入系統(tǒng)是比較簡單的. 很多游戲根本就沒有對這一塊進行封裝, 而直接采用了Win32的消息機制.
不過經(jīng)過封裝的輸入系統(tǒng)使用起來很方便, 呵呵.
N3中有三種輸入設備, 鍵盤, 鼠標, 手柄. 分別是基于Win32消息, DirectInput, XInput實現(xiàn)的. 這里有一個繼承圖能夠很好的說明輸入系統(tǒng)的組織結構:
基本的消息處理機制是這樣的一個流程:
InputServer里有默認的一個鍵盤, 一個鼠標, 一個手柄的"handler", 在每幀開始時InputServer會檢測當前的輸入消息, 得到一個InputEvent, 由相應的InputHandler來處理. 各個InputHandler都保存著當前幀各種輸入狀態(tài)的緩存(如鼠標左鍵是否按下), 因此, 在程序運行過程中, 我們只要在繪制結束前檢測各個InputHandler的狀態(tài)就相當于知道當前用戶是怎樣輸入的了.
一般只需要關心這么幾個函數(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先略過, 原理相同
測試例子, 在上一次的代碼中添加一段:
- void OnRenderFrame()
- {
- if (this->inputServer->GetDefaultMouse()->ButtonDown(MouseButton::LeftButton))
- {
- MessageBoxA(this->displayDevice->GetHwnd(), "Left Button Down", NULL, 0);
- }
- //...//
- }
效果:
