forked from sdlpal/sdlpal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
140 lines (116 loc) · 3.86 KB
/
input.c
File metadata and controls
140 lines (116 loc) · 3.86 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
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
/* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
//
// Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
// Copyright (c) 2011-2024, SDLPAL development team.
// All rights reserved.
//
// This file is part of SDLPAL.
//
// SDLPAL is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 3
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include "main.h"
volatile PALINPUTSTATE g_InputState;
static const int g_KeyMap[][2] = {
{SDLK_UP, kKeyUp}, {SDLK_DOWN, kKeyDown}, {SDLK_LEFT, kKeyLeft}, {SDLK_RIGHT, kKeyRight},
{SDLK_ESCAPE, kKeyMenu}, {SDLK_RETURN, kKeySearch}, {SDLK_SPACE, kKeySearch}, {SDLK_PAGEUP, kKeyPgUp},
{SDLK_PAGEDOWN, kKeyPgDn}, {SDLK_HOME, kKeyHome}, {SDLK_END, kKeyEnd}, {SDLK_R, kKeyRepeat},
{SDLK_A, kKeyAuto}, {SDLK_D, kKeyDefend}, {SDLK_E, kKeyUseItem}, {SDLK_W, kKeyThrowItem},
{SDLK_Q, kKeyFlee}, {SDLK_F, kKeyForce}, {SDLK_S, kKeyStatus}};
static INT PAL_GetCurrDirection(VOID) {
INT iCurrDir = kDirSouth;
for (int i = 1; i < 4; i++)
if (g_InputState.dwKeyOrder[iCurrDir] < g_InputState.dwKeyOrder[i])
iCurrDir = i;
if (!g_InputState.dwKeyOrder[iCurrDir])
iCurrDir = kDirUnknown;
return iCurrDir;
}
static VOID PAL_KeyDown(INT key) {
INT iCurrDir = kDirUnknown;
if (key & kKeyDown)
iCurrDir = kDirSouth;
else if (key & kKeyLeft)
iCurrDir = kDirWest;
else if (key & kKeyUp)
iCurrDir = kDirNorth;
else if (key & kKeyRight)
iCurrDir = kDirEast;
if (iCurrDir != kDirUnknown) {
g_InputState.dwKeyMaxCount++;
g_InputState.dwKeyOrder[iCurrDir] = g_InputState.dwKeyMaxCount;
g_InputState.dir = PAL_GetCurrDirection();
}
g_InputState.dwKeyPress |= key;
}
static VOID PAL_KeyUp(INT key) {
INT iCurrDir = kDirUnknown;
if (key & kKeyDown)
iCurrDir = kDirSouth;
else if (key & kKeyLeft)
iCurrDir = kDirWest;
else if (key & kKeyUp)
iCurrDir = kDirNorth;
else if (key & kKeyRight)
iCurrDir = kDirEast;
if (iCurrDir != kDirUnknown) {
g_InputState.dwKeyOrder[iCurrDir] = 0;
iCurrDir = PAL_GetCurrDirection();
g_InputState.dwKeyMaxCount = (iCurrDir == kDirUnknown) ? 0 : g_InputState.dwKeyOrder[iCurrDir];
g_InputState.dir = iCurrDir;
}
}
static VOID SDLCALL PAL_EventFilter(const SDL_Event *lpEvent) {
switch (lpEvent->type) {
case SDL_EVENT_WILL_ENTER_BACKGROUND:
g_bRenderPaused = TRUE;
break;
case SDL_EVENT_DID_ENTER_FOREGROUND:
g_bRenderPaused = FALSE;
VIDEO_UpdateScreen(NULL);
break;
case SDL_EVENT_KEY_DOWN:
if (lpEvent->key.mod & SDL_KMOD_ALT) {
if (lpEvent->key.key == SDLK_RETURN) {
VIDEO_ToggleFullscreen();
break;
}
}
for (int i = 0; i < sizeof(g_KeyMap) / sizeof(g_KeyMap[0]); i++) {
if (g_KeyMap[i][0] == lpEvent->key.key) {
PAL_KeyDown(g_KeyMap[i][1]);
}
}
break;
case SDL_EVENT_KEY_UP:
for (int i = 0; i < sizeof(g_KeyMap) / sizeof(g_KeyMap[0]); i++) {
if (g_KeyMap[i][0] == lpEvent->key.key) {
PAL_KeyUp(g_KeyMap[i][1]);
}
}
break;
case SDL_EVENT_QUIT:
PAL_Shutdown(0);
}
}
VOID PAL_ClearKeyState(VOID) { g_InputState.dwKeyPress = 0; }
VOID PAL_InitInput(VOID) { g_InputState.dir = kDirUnknown; }
VOID PAL_ProcessEvent(VOID) {
while (1) {
SDL_Event evt;
int ret = SDL_PollEvent(&evt);
if (ret != 0)
PAL_EventFilter(&evt);
else
break;
}
}