zoukankan      html  css  js  c++  java
  • Nebula3的Input系统

    相对于其他的子系统来说, 输入系统是比较简单的. 很多游戏根本就没有对这一块进行封装, 而直接采用了Win32的消息机制.

    不过经过封装的输入系统使用起来很方便, 呵呵.

    N3中有三种输入设备, 键盘, 鼠标, 手柄. 分别是基于Win32消息, DirectInput, XInput实现的. 这里有一个继承图能够很好的说明输入系统的组织结构:

    基本的消息处理机制是这样的一个流程:

    InputServer里有默认的一个键盘, 一个鼠标, 一个手柄的"handler", 在每帧开始时InputServer会检测当前的输入消息,  得到一个InputEvent, 由相应的InputHandler来处理.  各个InputHandler都保存着当前帧各种输入状态的缓存(如鼠标左键是否按下), 因此, 在程序运行过程中, 我们只要在绘制结束前检测各个InputHandler的状态就相当于知道当前用户是怎样输入的了.

    一般只需要关心这么几个函数就够了:

    1. ////////////////////// Mouse////////////////////////////
    2. /// return true if button is currently pressed
    3. bool ButtonPressed(Input::MouseButton::Code btn) const;
    4. /// return true if button was down at least once in current frame
    5. bool ButtonDown(Input::MouseButton::Code btn) const;
    6. /// return true if button was up at least once in current frame
    7. bool ButtonUp(Input::MouseButton::Code btn) const;
    8. /// return true if a button has been double clicked
    9. bool ButtonDoubleClicked(Input::MouseButton::Code btn) const;
    10. /// return true if mouse wheel rotated forward
    11. bool WheelForward() const;
    12. /// return true if mouse wheel rotated backward
    13. bool WheelBackward() const;
    14. /// get current absolute mouse position (in pixels)
    15. const Math::float2& GetPixelPosition() const;
    16. /// get current screen space mouse position (0.0 .. 1.0)
    17. const Math::float2& GetScreenPosition() const;
    18. /// get mouse movement
    19. const Math::float2& GetMovement() const;
    1. //////////////////////Keyboard//////////////////////
    2. /// return true if a key is currently pressed
    3. bool KeyPressed(Input::Key::Code keyCode) const;
    4. /// return true if key was down at least once in current frame
    5. bool KeyDown(Input::Key::Code keyCode) const;
    6. /// return true if key was up at least once in current frame
    7. bool KeyUp(Input::Key::Code keyCode) const;
    8. /// get character input in current frame
    9. const Util::String& GetCharInput() const;

    GamePad先略过, 原理相同

    测试例子, 在上一次的代码中添加一段:

    1. void OnRenderFrame()
    2.     {
    3. if (this->inputServer->GetDefaultMouse()->ButtonDown(MouseButton::LeftButton))
    4.         {
    5.             MessageBoxA(this->displayDevice->GetHwnd(), "Left Button Down", NULL, 0);
    6.         }
    7. //...//
    8.     }

    效果:

  • 相关阅读:
    java mail
    hibernate 批量处理数据
    动态规划0—1背包问题
    FreeCMS开发过程问题总结(持续更新中)
    RapeLay(电车之狼R)的结局介绍 (隐藏结局攻略)
    笔记本键盘输入错乱,字母都变成数字了
    眼下最好的JSP分页技术
    在一个字符串中找到第一个仅仅出现一次的字符
    央行力保首套房贷背后暗藏何种玄机?
    HDU2149-Good Luck in CET-4 Everybody!(博弈,打表找规律)
  • 原文地址:https://www.cnblogs.com/flying_bat/p/1354934.html
Copyright © 2011-2022 走看看