-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeyfilter.cpp
310 lines (271 loc) · 8.79 KB
/
keyfilter.cpp
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <assert.h>
#include "keyfilter.h"
#include "ftgkeys.h"
namespace dragonfighting {
Command::Command(const char *name, const unsigned char *cmd, size_t length)
{
assert(length <= 16);
unsigned int i=0;
for (i=0; i<sizeof(this->name)-1 && name[i]!='\0'; i++) {
this->name[i] = name[i];
}
this->name[i] = '\0';
for (i=0; i<length; i++) {
this->ftgKeyArray[i] = cmd[i];
}
this->length = length;
}
static bool commandCompare(Command *c1, Command *c2)
{
if (c1->length == c2->length) {
// because: 3A must be parsed before 2A or 6A
return c1->ftgKeyArray[c1->length-2] > c2->ftgKeyArray[c2->length-2];
} else {
return c1->length > c2->length;
}
}
KeyFilter::KeyFilter() :
commandTable(),
ftgKeyCurIndex(0),
ftgKeyPreIndex(sizeof(ftgKeyStateBuffer) - 1),
beginIndex(0)
{
memset(ftgKeyStateBuffer, 0, sizeof(ftgKeyStateBuffer));
memset(curCommandName, 0, sizeof(curCommandName));
}
KeyFilter::~KeyFilter()
{
for (list<Command*>::iterator i = commandTable.begin(); i != commandTable.end(); ++i ){
delete *i;
}
}
void KeyFilter::addCommand(const char *name, const unsigned char *cmd, size_t length)
{
if (length < 2) {
throw "command length must at lest 2";
}
Command *command = new Command(name, cmd, length);
commandTable.push_back(command);
commandTable.sort(commandCompare);
//debug
/*
for (list<Command*>::iterator i = commandTable.begin(); i != commandTable.end(); ++i ){
printf("%s\n", (*i)->name);
}
printf("--------------\n");
*/
}
void KeyFilter::updateKeys(unsigned char currentFtgKeyState)
{
// save key in buffer
if (ftgKeyStateBuffer[ftgKeyCurIndex].ftgkey == currentFtgKeyState) {
ftgKeyStateBuffer[ftgKeyCurIndex].numFrames += 1;
} else {
ftgKeyPreIndex = ftgKeyCurIndex;
ftgKeyCurIndex = (ftgKeyCurIndex + 1) % KEY_BUFFER_LEN;
ftgKeyStateBuffer[ftgKeyCurIndex].ftgkey = currentFtgKeyState;
ftgKeyStateBuffer[ftgKeyCurIndex].numFrames = 1;
}
// if not a 'command end', skip parse
// A 'command end' is: current is a button key and pre is not the same button key
if ((ftgKeyStateBuffer[ftgKeyCurIndex].ftgkey & FTG_BUTTON_KEYS) == 0) {
return;
} else if ((ftgKeyStateBuffer[ftgKeyPreIndex].ftgkey & ftgKeyStateBuffer[ftgKeyCurIndex].ftgkey & FTG_BUTTON_KEYS) != 0) {
return;
}
// parse command
list<Command*>::iterator i;
for (i = commandTable.begin(); i != commandTable.end(); ++i ){
int j;
int temp_p_cur = ftgKeyCurIndex;
for (j=(*i)->length-1; j>=0; j--) {
if (temp_p_cur == beginIndex) {
break;
}
// faulttolerant
if (ftgKeyStateBuffer[temp_p_cur].ftgkey == 0 && ftgKeyStateBuffer[temp_p_cur].numFrames < 8) {
temp_p_cur--;
if (temp_p_cur == -1)
temp_p_cur += KEY_BUFFER_LEN;
}
// if interval too long
if (j != 0 && ftgKeyStateBuffer[temp_p_cur].numFrames > 8) {
break;
}
if (j == (int)(*i)->length-1) {
// if 'command end' mismatch
if ( (ftgKeyStateBuffer[temp_p_cur].ftgkey & FTG_BUTTON_KEYS) != (*i)->ftgKeyArray[j] ) {
break;
}
} else {
bool passed = true;
int loop = 2;
// test if mismatch
while ( loop > 0 && (ftgKeyStateBuffer[temp_p_cur].ftgkey != (*i)->ftgKeyArray[j]) ) {
// faulttolerant
unsigned char keytest = (*i)->ftgKeyArray[j] & ftgKeyStateBuffer[temp_p_cur].ftgkey;
if ( keytest != 0 && (keytest == ftgKeyStateBuffer[temp_p_cur].ftgkey || keytest == (*i)->ftgKeyArray[j]) ) {
// skip one key and test again
temp_p_cur--;
if (temp_p_cur == -1) {
temp_p_cur += KEY_BUFFER_LEN;
}
loop --;
} else {
passed = false;
break;
}
}
if (!passed) {
break;
}
}
temp_p_cur--;
if (temp_p_cur == -1) {
temp_p_cur += KEY_BUFFER_LEN;
}
}
if (j < 0) {
break;
}
}
if (i != commandTable.end()) {
strncpy(curCommandName, (*i)->name, sizeof(curCommandName));
beginIndex = ftgKeyCurIndex;
//debug
static int hitnumber = 0;
printf("%s hit %d\n", (*i)->name, hitnumber++);
/*int i;
for (i=0; i<KEY_BUFFER_LEN; i++) {
if (i == ftgKeyCurIndex) {
printf("*");
}
printf("%d,", ftgKeyStateBuffer[i].ftgkey);
}
printf("\n");
*/
}
}
bool KeyFilter::pollCurrentCommandName(char *cmdname, size_t bufflen)
{
assert(cmdname != NULL && bufflen > 0);
if (curCommandName[0] != '\0') {
strncpy(cmdname, curCommandName, bufflen);
memset(curCommandName, 0, sizeof(curCommandName));
return true;
} else {
return false;
}
}
Uint32 KeyFilter::getKeyState(unsigned char key)
{
if ( (ftgKeyStateBuffer[ftgKeyCurIndex].ftgkey & key) != 0 ) {
return ftgKeyStateBuffer[ftgKeyCurIndex].numFrames;
}
return 0;
}
void KeyFilter::flipHorizontal()
{
bool key1down = false, key2down = false;
if ( (ftgKeyStateBuffer[ftgKeyCurIndex].ftgkey & FTGKEY_6) != 0 ) {
key1down = true;
}
if ( (ftgKeyStateBuffer[ftgKeyCurIndex].ftgkey & FTGKEY_4) != 0 ) {
key2down = true;
}
if (key1down) {
ftgKeyStateBuffer[ftgKeyCurIndex].ftgkey |= FTGKEY_4;
} else {
ftgKeyStateBuffer[ftgKeyCurIndex].ftgkey &= ~FTGKEY_4;
}
if (key2down) {
ftgKeyStateBuffer[ftgKeyCurIndex].ftgkey |= FTGKEY_6;
} else {
ftgKeyStateBuffer[ftgKeyCurIndex].ftgkey &= ~FTGKEY_6;
}
}
}
#ifdef FTG_TEST
#include <stdlib.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <math.h>
#include "keyfilter.h"
using namespace dragonfighting;
int main(int argc, char **argv)
{
int exited = 0;
SDL_Surface *screen = NULL;
Uint32 interval = 1000/60;
Uint32 delay = 0;
Uint32 frame = 0;
Uint32 oldtime = SDL_GetTicks();
Uint32 newtime = oldtime;
SDL_Init(SDL_INIT_EVERYTHING);
atexit(SDL_Quit);
screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
if (screen == NULL)
{
fprintf(stderr, "error set video mode.\n");
exit(1);
}
// Init key map
unsigned char key_map[SDLK_LAST];
memset(key_map, 0, sizeof(key_map));
key_map[SDLK_w] = FTGKEY_8;
key_map[SDLK_s] = FTGKEY_2;
key_map[SDLK_a] = FTGKEY_4;
key_map[SDLK_d] = FTGKEY_6;
key_map[SDLK_j] = FTGKEY_A;
key_map[SDLK_k] = FTGKEY_B;
key_map[SDLK_l] = FTGKEY_C;
key_map[SDLK_i] = FTGKEY_D;
// Init key filter
KeyFilter keyFilter;
unsigned char cmd[][8] = {
{FTGKEY_6, FTGKEY_3, FTGKEY_2, FTGKEY_3, FTGKEY_A},
{FTGKEY_2, FTGKEY_3, FTGKEY_6, FTGKEY_A},
{FTGKEY_2, FTGKEY_3, FTGKEY_6, FTGKEY_B},
{FTGKEY_2, FTGKEY_2, FTGKEY_A},
{FTGKEY_6, FTGKEY_A},
{FTGKEY_2, FTGKEY_A},
{FTGKEY_6, FTGKEY_3, FTGKEY_2, FTGKEY_3, FTGKEY_6, FTGKEY_A},
{FTGKEY_6, FTGKEY_2, FTGKEY_3, FTGKEY_A},
};
//keyFilter.addCommand("63236A", cmd[6], 6);
keyFilter.addCommand("6323A", cmd[0], 5);
//keyFilter.addCommand("623A", cmd[7], 4);
keyFilter.addCommand("236A", cmd[1], 4);
keyFilter.addCommand("236B", cmd[2], 4);
keyFilter.addCommand("22A", cmd[3], 3);
keyFilter.addCommand("6A", cmd[4], 2);
keyFilter.addCommand("2A", cmd[5], 2);
unsigned char current_key_state = 0;
while(exited==0)
{
SDL_Event event;
if (SDL_PollEvent(&event)==1){
if((event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_ESCAPE) || (event.type==SDL_QUIT)) exited=1;
else if (event.type==SDL_KEYDOWN)
{
current_key_state |= key_map[event.key.keysym.sym];
}
else if (event.type==SDL_KEYUP)
{
current_key_state &= ~key_map[event.key.keysym.sym];
}
}
keyFilter.updateKeys(current_key_state);
SDL_FillRect( screen, NULL, 0 );
//SDL_FillRect( screen, &rect, color );
frame++;
SDL_Flip(screen);
newtime = SDL_GetTicks();
if (newtime - oldtime < interval)
SDL_Delay(interval - newtime + oldtime);
oldtime = SDL_GetTicks();
}
SDL_Quit();
return 0;
}
#endif