-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInput.cpp
More file actions
87 lines (81 loc) · 1.36 KB
/
Input.cpp
File metadata and controls
87 lines (81 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "Input.h"
int Input::CreateInput()
{
//初始化
const Uint8* temp = SDL_GetKeyboardState(NULL);
memcpy(nowKeyState, temp, SDL_NUM_SCANCODES);
//将前一帧的数组初始化为0
memset(beforeKeyState, 0, SDL_NUM_SCANCODES);
return 0;
}
int Input::Updata()
{
//交换键盘值
memcpy(beforeKeyState, nowKeyState, SDL_NUM_SCANCODES);
//获取新的键盘状态
const Uint8* temp = SDL_GetKeyboardState(NULL);
memcpy(nowKeyState, temp, SDL_NUM_SCANCODES);
//交换鼠标状态
beforeMouseState = nowMouseState;
beforePoint = nowPoint;
//获取新的鼠标状态
nowMouseState = SDL_GetMouseState(&nowPoint.x, &nowPoint.y);
return 0;
}
int Input::GetKeyState(SDL_Scancode key)
{
//判断键盘状态
if (beforeKeyState[key] == 0)
{
if (nowKeyState[key] == 0)
{
return Key_None;
}
else
{
return Key_Down;
}
}
else
{
if (nowKeyState[key] == 0)
{
return Key_Up;
}
else
{
return Key_Keep;
}
}
}
int Input::GetMouseState(int button)
{
//判断鼠标状态
if ((beforeMouseState & SDL_BUTTON(button)) == 0)
{
if ((nowMouseState & SDL_BUTTON(button)) == 0)
{
return Key_None;
}
else
{
return Key_Down;
}
}
else
{
if ((nowMouseState & SDL_BUTTON(button)) == 0)
{
return Key_Up;
}
else
{
return Key_Keep;
}
}
}
bool Input::GetMouseMove()
{
if (beforePoint.x != nowPoint.x || beforePoint.y != nowPoint.y) { return true; }
return false;
}