YSTest: Input.cpp 源文件

YSTest PreAlpha_b400_20130424 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
Input.cpp
浏览该文件的文档.
1 ﻿/*
2 Copyright by FrankHB 2012 - 2013.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "YCLib/Input.h"
29 #if YCL_MULTITHREAD == 1
30 # include <mutex>
31 # define YCL_DEF_LOCKGUARD(_lck, _mutex) \
32 std::lock_guard<std::mutex> _lck(_mutex);
33 #else
34 # define YCL_DEF_LOCKGUARD(...)
35 #endif
36
37 namespace platform
38 {
39
40 void
41 WaitForInput()
42 {
43 while(true)
44 {
45 platform_ex::UpdateKeyStates();
46 if(platform_ex::FetchKeyDownState().any())
47 break;
48 #if YCL_DS
49 ::swiWaitForVBlank();
50 #endif
51 }
52 }
53
54 } // namespace platform;
55
56 namespace platform_ex
57 {
58
59 #if YCL_KEYSTATE_DIRECT
60 platform::KeyInput KeyState, OldKeyState;
61 #else
62 namespace
63 {
65
66 platform::KeyInput KeyStateA, KeyStateAKeyStateB;
67 platform::KeyInput *pKeyState(&KeyStateA), *pOldKeyState(&KeyStateAKeyStateB);
69 #if YCL_MULTITHREAD == 1
70
71 std::mutex CompKeyMutex;
72 std::mutex KeyMutex;
73 #endif
74 } //unnamed namespace;
75
76 const platform::KeyInput&
77 FetchKeyState()
78 {
79 YAssert(pKeyState, "Null pointer found.");
80
81 YCL_DEF_LOCKGUARD(lck, KeyMutex)
82
83 return *pKeyState;
84 }
85
86 const platform::KeyInput&
87 FetchOldKeyState()
88 {
89 YAssert(pOldKeyState, "Null pointer found.");
90
91 YCL_DEF_LOCKGUARD(lck, KeyMutex)
92
93 return *pOldKeyState;
94 }
95
96 platform::KeyInput
97 FetchKeyDownState()
98 {
99 YCL_DEF_LOCKGUARD(comp_lck, CompKeyMutex)
100
101 return FetchKeyState() &~ FetchOldKeyState();
102 }
103
104 platform::KeyInput
105 FetchKeyUpState()
106 {
107 YCL_DEF_LOCKGUARD(comp_lck, CompKeyMutex)
108
109 return (FetchKeyState() ^ FetchOldKeyState()) & ~FetchKeyState();
110 }
111
112 void
113 ClearKeyStates()
114 {
115 YAssert(pKeyState && pOldKeyState, "Null pointer found.");
116
117 YCL_DEF_LOCKGUARD(comp_lck, CompKeyMutex)
118 YCL_DEF_LOCKGUARD(lck, KeyMutex)
119
120 yunseq(pKeyState->reset(), pOldKeyState->reset());
121 }
122
123 #endif
124
125 void
126 UpdateKeyStates()
127 {
128 YCL_DEF_LOCKGUARD(comp_lck, CompKeyMutex)
129 YCL_DEF_LOCKGUARD(lck, KeyMutex)
130
131 #if YCL_KEYSTATE_DIRECT
132 OldKeyState = KeyState;
133 #else
134 std::swap(pKeyState, pOldKeyState);
135 #endif
136 #if YCL_DS
137 KeyState = ::keysCurrent();
138 #elif YCL_MINGW32
139 // NOTE: 0x00 and 0xFF should be invalid.
140 for(std::size_t i(1); i < platform::KeyBitsetWidth - 1; ++i)
141 pKeyState->set(i, ::GetAsyncKeyState(i) & 0x8000);
142 #endif
143 }
144
145
146 void
147 WriteCursor(platform::CursorInfo& tp)
148 {
149 #if YCL_DS
150 ::touchRead(&tp);
151 //修正触摸位置。
152 if(YB_LIKELY(tp.px != 0 && tp.py != 0))
153 yunseq(--tp.px, --tp.py);
154 else
155 // NOTE: %YSLib::Point::Invalid.
156 yunseq(tp.px = std::uint16_t(-1), tp.py = std::uint16_t(-1));
157 #elif YCL_MINGW32
158 ::GetCursorPos(&tp);
159 #else
160 # error Unsupported platform found!
161 #endif
162 }
163
164
165 #if YCL_DS
166 void
167 WaitForKey(platform::KeyInput mask)
168 {
169 while(true)
170 {
171 UpdateKeyStates();
172 if((FetchKeyDownState() & mask).any())
173 break;
174 swiWaitForVBlank();
175 }
176 }
177 #endif
178
179 } // namespace platform_ex;
180