从0开发游戏引擎之Win32平台用户输入事件Input类实现

简介: Input类主要实现的功能是 监听用户鼠标键盘事件,当收到操作系统回调之后首先会调到场景管理类,然后通过场景管理类,会调到Input类,然后Input类会传递给KeyInput类和MouseInput处理鼠标和键盘相关的逻辑代码。

Input类主要实现的功能是 监听用户鼠标键盘事件,当收到操作系统回调之后首先会调到场景管理类,然后通过场景管理类,会调到Input类,然后Input类会传递给KeyInput类和MouseInput处理鼠标和键盘相关的逻辑代码。


MouseInput被调起的时候,会记录当前鼠标在屏幕中的坐标点,然后更新鼠标的状态。代码详见程序1.1


KeyInput被调起的时候,会更新当前键盘的状态,按下或者是抬起。详见程序1.2


程序1.1


LRESULT MouseInput::msgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  if (_code == LButton)
  {
    if (msg == WM_LBUTTONDOWN)
    {
      ptLast.x = LOWORD(lParam);
      ptLast.y = HIWORD(lParam);
      _event = MouseDown;
    }
    else if (msg == WM_LBUTTONUP)
    {
      _event = MouseUp;
    }
  }
  else if (_code == MButton)
  {
    if (msg == WM_MBUTTONDOWN)
    {
      ptLast.x = LOWORD(lParam);
      ptLast.y = HIWORD(lParam);
      _event = MouseDown;
    }
    else if (msg == WM_MBUTTONUP)
    {
      _event = MouseUp;
    }
  }
  else if (_code == RButton)
  {
    if (msg == WM_RBUTTONDOWN)
    {
      ptLast.x = LOWORD(lParam);
      ptLast.y = HIWORD(lParam);
      _event = MouseDown;
    }
    else if (msg == WM_RBUTTONUP)
    {
      _event = MouseUp;
    }
  }
  else if (_code == Moving)
  {
    if (msg == WM_MOUSEMOVE)
    {
      ptLast.x = LOWORD(lParam);
      ptLast.y = HIWORD(lParam);
      _event = MouseMove;
    }
  }
  else if (_code == Wheel)
  {
    if (msg == 0x020A/*WM_MOUSEWHEEL*/)
    {
      scroll = HIWORD(wParam);
      _event = WheelScroll;
    }
  }
  return 0;
}


程序1.2


LRESULT KeyInput::msgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  if (msg == WM_KEYDOWN)
  {
    if (_event == None && _code == wParam)
    {
      _event = KeyDown;
    }
  }
  else if (msg == WM_KEYUP)
  {
    if (_code == wParam)
    {
      _event = KeyUp;
    }
  }
  return 0;
}


完整代码


Input.h


#pragma once
enum InputEvent//输入消息的事件
{
  None,
  MouseDown,
  Mouseing,
  MouseUp,
  WheelScroll,
  KeyDown,
  Keying,
  KeyUp,
  MouseMove
};
class InputBase
{
protected:
  InputEvent _event;
  int _code;
public:
  virtual void nextState() = 0;
  virtual LRESULT msgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) = 0;
};
class KeyInput :public InputBase
{
public:
  KeyInput(int code);
  void nextState();
  LRESULT msgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  bool getKey()
  {
    return _event == Keying;
  }
  bool getKeyDown()
  {
    return _event == KeyDown;
  }
  bool getKeyUp()
  {
    return _event == KeyUp;
  }
};
struct MouseData
{
  short scroll;     //滚动码
  POINT delta;      //鼠标根据上一个点的偏移值
  POINT mousePos;     //现在的点
};
enum MouseCode
{
  LButton,
  MButton,
  RButton,
  Wheel,
  Moving
};
class MouseInput :public InputBase
{
private:
  short scroll;
  POINT ptLast;
  POINT pt;
public:
  MouseInput(MouseCode code);
  void nextState();
  LRESULT msgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  bool getMouse(MouseData * mouseData = NULL);
  bool getMouseMove(MouseData * mouseData = NULL);
  bool getMouseDown(MouseData * mouseData = NULL);
  bool getMouseUp(MouseData * mouseData = NULL);
  bool getMouseWheel(MouseData * mouseData = NULL);
};
class Input//管理类
{
private:
  static map<string, InputBase*> inputList;
public:
  static LRESULT msgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  static void flush();
  static bool getKey(int keyCode);
  static bool getKeyDown(int keyCode);
  static bool getKeyUp(int keyCode);
  static bool getMouse(MouseCode mouseCode, MouseData * mouseData = NULL);
  static bool getMouseDown(MouseCode mouseCode, MouseData * mouseData = NULL);
  static bool getMouseUp(MouseCode mouseCode, MouseData * mouseData = NULL);
  static bool getMouseWheel(MouseData * mouseData = NULL);
  static bool getMouseMove(MouseData * mouseData = NULL);
};


Input.cpp


#include "Engine.h"
KeyInput::KeyInput(int code)
{
  _event = None;
  _code = code;
}
void KeyInput::nextState()
{
  if (_event == KeyDown)
  {
    _event = Keying;
  }
  else if (_event == KeyUp)
  {
    _event = None;
  }
}
LRESULT KeyInput::msgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  if (msg == WM_KEYDOWN)
  {
    if (_event == None && _code == wParam)
    {
      _event = KeyDown;
    }
  }
  else if (msg == WM_KEYUP)
  {
    if (_code == wParam)
    {
      _event = KeyUp;
    }
  }
  return 0;
}
MouseInput::MouseInput(MouseCode code)
{
  _event = None;
  _code = code;
}
void MouseInput::nextState()
{
  if (_event == MouseDown)
  {
    _event = Mouseing;
  }
  else if (_event == MouseUp)
  {
    _event = None;
  }
  else if (_event == WheelScroll)
  {
    _event = None;
  }
  else if (_event==MouseMove&&_event==MouseUp)
  {
    _event = MouseMove;
  }
  else if (_event==Mouseing)
  {
    ptLast = pt;
  }
}
LRESULT MouseInput::msgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  if (_code == LButton)
  {
    if (msg == WM_LBUTTONDOWN)
    {
      ptLast.x = LOWORD(lParam);
      ptLast.y = HIWORD(lParam);
      _event = MouseDown;
    }
    else if (msg == WM_LBUTTONUP)
    {
      _event = MouseUp;
    }
  }
  else if (_code == MButton)
  {
    if (msg == WM_MBUTTONDOWN)
    {
      ptLast.x = LOWORD(lParam);
      ptLast.y = HIWORD(lParam);
      _event = MouseDown;
    }
    else if (msg == WM_MBUTTONUP)
    {
      _event = MouseUp;
    }
  }
  else if (_code == RButton)
  {
    if (msg == WM_RBUTTONDOWN)
    {
      ptLast.x = LOWORD(lParam);
      ptLast.y = HIWORD(lParam);
      _event = MouseDown;
    }
    else if (msg == WM_RBUTTONUP)
    {
      _event = MouseUp;
    }
  }
  else if (_code == Moving)
  {
    if (msg == WM_MOUSEMOVE)
    {
      ptLast.x = LOWORD(lParam);
      ptLast.y = HIWORD(lParam);
      _event = MouseMove;
    }
  }
  else if (_code == Wheel)
  {
    if (msg == 0x020A/*WM_MOUSEWHEEL*/)
    {
      scroll = HIWORD(wParam);
      _event = WheelScroll;
    }
  }
  return 0;
}
bool MouseInput::getMouse(MouseData *mouseData)
{
  bool f = _event == Mouseing;
  if (f == true && mouseData != NULL)
  {
    GetCursorPos(&pt);
    ScreenToClient(CPlateForm::getInstance()->getHwnd(), &pt);
    mouseData->mousePos.x = pt.x;
    mouseData->mousePos.y = pt.y;
    mouseData->delta.x = pt.x - ptLast.x;
    mouseData->delta.y = pt.y - ptLast.y;
    mouseData->scroll = 0;
  }
  return f;
}
bool MouseInput::getMouseMove(MouseData *mouseData)
{
  bool f = _event == MouseMove;
  if (f == true && mouseData != NULL)
  {
    GetCursorPos(&pt);
    ScreenToClient(CPlateForm::getInstance()->getHwnd(), &pt);
    mouseData->mousePos.x = pt.x;
    mouseData->mousePos.y = pt.y;
    mouseData->delta.x = pt.x - ptLast.x;
    mouseData->delta.y = pt.y - ptLast.y;
    mouseData->scroll = 0;
    ptLast = pt;
    GLint   viewport[4]; //视口
    glGetIntegerv(GL_VIEWPORT, viewport); //得到的是最后一个设置视口的参数
    POINT st;
    st.x = viewport[2] * 0.5;
    st.y = viewport[3] * 0.5;
    ClientToScreen(CPlateForm::getInstance()->getHwnd(), &st);//应该是吧鼠标锁在窗口的中心点
    SetCursorPos(st.x, st.y);
  }
  return f;
}
bool MouseInput::getMouseDown(MouseData * mouseData)
{
  bool f = _event == MouseDown;
  if (f == true && mouseData != NULL)
  {
    POINT pt;
    GetCursorPos(&pt);
    ScreenToClient(CPlateForm::getInstance()->getHwnd(), &pt);
    memset(mouseData, 0, sizeof(MouseData));
    mouseData->mousePos.x = pt.x;
    mouseData->mousePos.y = pt.y;
  }
  return f;
}
bool MouseInput::getMouseUp(MouseData * mouseData)
{
  bool f = _event == MouseUp;
  if (f == true && mouseData != NULL)
  {
    POINT pt;
    GetCursorPos(&pt);
    ScreenToClient(CPlateForm::getInstance()->getHwnd(), &pt);//转换成像素点
    memset(mouseData, 0, sizeof(MouseData));
    mouseData->mousePos.x = pt.x;
    mouseData->mousePos.y = pt.y;
  }
  return f;
}
bool MouseInput::getMouseWheel(MouseData * mouseData)
{
  bool f = _event == WheelScroll;
  if (f == true && mouseData != NULL)
  {
    POINT pt;
    GetCursorPos(&pt);
    ScreenToClient(CPlateForm::getInstance()->getHwnd(), &pt);
    mouseData->mousePos.x = pt.x;
    mouseData->mousePos.y = pt.y;
    mouseData->scroll = scroll;
    ptLast = pt;
  }
  return f;
}
map<string, InputBase*> Input::inputList;
LRESULT Input::msgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
  map<string, InputBase*>::iterator iter;
  for (iter = inputList.begin(); iter != inputList.end(); iter++)
  {
    iter->second->msgProc(hWnd, msg, wParam, lParam);
  }
  return 0;
}
void Input::flush()
{
  map<string, InputBase*>::iterator iter;
  for (iter = inputList.begin(); iter != inputList.end(); iter++)
  {
    iter->second->nextState();
  }
}
bool Input::getKey(int keyCode)
{
  static char str[20];
  sprintf(str, "key_%d", keyCode);
  if (inputList[str] == NULL)
  {
    inputList[str] = new KeyInput(keyCode);
  }
  return ((KeyInput*)inputList[str])->getKey();
}
bool Input::getKeyDown(int keyCode)
{
  static char str[20];
  sprintf(str, "key_%d", keyCode);
  if (inputList[str] == NULL)
  {
    inputList[str] = new KeyInput(keyCode);
  }
  return ((KeyInput*)inputList[str])->getKeyDown();
}
bool Input::getKeyUp(int keyCode)
{
  static char str[20];
  sprintf(str, "key_%d", keyCode);
  if (inputList[str] == NULL)
  {
    inputList[str] = new KeyInput(keyCode);
  }
  return ((KeyInput*)inputList[str])->getKeyUp();
}
bool Input::getMouse(MouseCode mouseCode, MouseData * mouseData)
{
  static char str[20];
  sprintf(str, "mouse_%d", mouseCode);
  if (inputList[str] == NULL)
  {
    inputList[str] = new MouseInput(mouseCode);
  }
  return ((MouseInput*)inputList[str])->getMouse(mouseData);
}
bool Input::getMouseDown(MouseCode mouseCode, MouseData * mouseData)
{
  static char str[20];
  sprintf(str, "mouse_%d", mouseCode);
  if (inputList[str] == NULL)
  {
    inputList[str] = new MouseInput(mouseCode);
  }
  return ((MouseInput*)inputList[str])->getMouseDown(mouseData);
}
bool Input::getMouseUp(MouseCode mouseCode, MouseData * mouseData)
{
  static char str[20];
  sprintf(str, "mouse_%d", mouseCode);
  if (inputList[str] == NULL)
  {
    inputList[str] = new MouseInput(mouseCode);
  }
  return ((MouseInput*)inputList[str])->getMouseUp(mouseData);
}
bool Input::getMouseWheel(MouseData * mouseData)
{
  if (inputList["wheel"] == NULL)
  {
    inputList["wheel"] = new MouseInput(MouseCode::Wheel);
  }
  return ((MouseInput*)inputList["wheel"])->getMouseWheel(mouseData);
}
bool Input::getMouseMove(MouseData * mouseData)
{
  if (inputList["move"] == NULL)
  {
    inputList["move"] = new MouseInput(MouseCode::Moving);
  }
  return ((MouseInput*)inputList["move"])->getMouseMove(mouseData);
}


相关文章
|
存储 算法 Java
深入理解JVM:内存管理与垃圾回收机制探索
JVM是Java程序的运行核心,实现跨平台、自动内存管理与高效执行。其架构包括类加载、运行时数据区、执行引擎等模块。内存模型历经演变,JDK 8起以元空间替代永久代,优化GC性能。JVM通过分代回收机制,结合标记清除、复制、整理等算法,管理对象生命周期,提升系统稳定性与性能。
|
存储 算法 生物认证
基于Zhang-Suen算法的图像细化处理FPGA实现,包含testbench和matlab验证程序
本项目基于Zhang-Suen算法实现图像细化处理,支持FPGA与MATLAB双平台验证。通过对比,FPGA细化效果与MATLAB一致,可有效减少图像数据量,便于后续识别与矢量化处理。算法适用于字符识别、指纹识别等领域,配套完整仿真代码及操作说明。
|
机器学习/深度学习 人工智能 算法
普通人怎么学人工智能?这些隐藏学习秘籍大揭秘,生成式人工智能认证(GAI认证)来助力
在人工智能(AI)快速发展的今天,普通人学习AI已成为必然趋势。本文从明确学习目标与路径、利用多元化资源、注重实践应用、关注GAI认证及持续自我提升五个方面,为普通人提供系统化的AI学习指南。通过设定目标、学习编程语言、参与项目实践和获取专业认证,普通人可逐步掌握AI技能,在未来职场中占据优势并开启智能时代新篇章。
1292 11
|
12月前
|
安全
《新手零抵触的教学引导设计指南》
本文聚焦新手学习陌生技术体系时的认知抵触问题,提出认知柔化术这一教学引导设计方法论,核心是构建“场景共鸣、微步递进、正向反馈、负荷可控、语言共情、容错包容”的六维体系,将教学从“硬灌输”升级为“认知浸润”。文中详细拆解了场景锚定、正向反馈闭环、认知负荷调控、语言降维转化、容错弹性设计五大核心策略,通过具象化场景嵌入、隐性即时反馈、知识留白缓冲、专业术语转译、错误正向转化等手段,消除新手的“意义断层”与“负荷过载”问题。实践数据显示,该设计能使新手探索时长提升2.3倍,主动复购率高出传统模式47%,实现“学无压力,知有深度”的教学目标。
439 3
|
人工智能 缓存 安全
Claude Code 深度拆解:一个顶级AI编程工具的核心架构
Claude Code是由Anthropic开发的AI编程工具,集成于终端环境,支持代码编写、调试与项目管理。其核心功能包括自然语言交互、代码生成与优化、工具调用、上下文管理及安全控制。Claude Code通过智能处理用户指令,大幅提升开发效率,具备多模型协作与上下文压缩能力,适用于复杂项目场景。
35614 2
|
JSON 前端开发 API
技术解析:58同城房产数据平台 - 根据项目ID获取详情数据的API接口实践
本文探讨如何通过项目ID调用58同城房产API获取详情数据,涵盖接口原理、请求分析、参数结构、Python调用示例及反爬机制,强调合规性与技术风险,为房产数据开发提供参考。(239字)
|
存储 网络协议 网络安全
Hyper-V Win10虚拟机配置常见问题
在配置Hyper-V Win10虚拟机时,用户常面临网络连接、虚拟交换机配置、资源分配及其他问题。例如,虚拟机无法获取IP地址可能源于DHCP服务异常,需检查并启动该服务;外部虚拟交换机配置错误则需确保物理网络适配器正确连接。此外,内存不足或虚拟硬盘性能瓶颈也会影响运行效果。通过合理调整资源配置、优化设置及遵循最佳实践,可有效解决这些问题。
1286 20
|
存储 云安全 运维
云原生技术架构成熟度模型解读 | 学习笔记
快速学习云原生技术架构成熟度模型解读
云原生技术架构成熟度模型解读 | 学习笔记
|
机器学习/深度学习 传感器 算法
XGBOOST回归预测 | Matlab xgboost(XGBOOST) 回归预测
XGBOOST回归预测 | Matlab xgboost(XGBOOST) 回归预测
1166 0