forked from cxong/FallingTime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
136 lines (125 loc) · 3.33 KB
/
input.c
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
/*
* Copyright (C) 2014 Nebuleon Fumika <nebuleon@gcw-zero.com>
* 2015 Cong Xu <congusbongus@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "input.h"
#include <stdbool.h>
#include "utils.h"
#define P1_LEFT SDLK_LEFT
#define P1_RIGHT SDLK_RIGHT
#ifdef __GCW0__
#define P2_LEFT0 SDLK_LSHIFT
#define P2_RIGHT0 SDLK_LCTRL
#define P2_LEFT1 SDLK_SPACE
#define P2_RIGHT1 SDLK_LALT
#else
#define P2_LEFT0 SDLK_a
#define P2_RIGHT0 SDLK_d
#define P2_LEFT1 SDLK_z
#define P2_RIGHT1 SDLK_x
#endif
static bool pressed[SDLK_LAST];
// TODO: support joysticks for PC
#ifdef __GCW0__
static SDL_Joystick *analog = NULL;
static SDL_Joystick *gSensor = NULL;
static int16_t gZero = 0;
int JoystickIndex = -1;
#define JOY_DEADZONE 500
#define G_SENSITIVITY 7
#endif
void InputInit(void)
{
memset(pressed, 0, sizeof pressed);
#ifdef __GCW0__
// Look for the right joysticks by name
for (int i = 0; i < SDL_NumJoysticks(); i++)
{
if (strcmp(SDL_JoystickName(i), "linkdev device (Analog 2-axis 8-button 2-hat)") == 0)
analog = SDL_JoystickOpen(i);
else if (strcmp(SDL_JoystickName(i), "mxc6225") == 0)
gSensor = SDL_JoystickOpen(i);
}
#endif
}
void InputFree(void)
{
#ifdef __GCW0__
if (analog) SDL_JoystickClose(analog);
if (gSensor) SDL_JoystickClose(gSensor);
#endif
}
void InputOnEvent(const SDL_Event* event)
{
if (event->type == SDL_KEYUP || event->type == SDL_KEYDOWN)
{
pressed[event->key.keysym.sym] = event->type == SDL_KEYDOWN;
}
}
#ifdef __GCW0__
static int16_t GetJoyX(SDL_Joystick *joy, const int16_t zero);
#endif
int16_t GetMovement(const int player)
{
#ifdef __GCW0__
if (player == 0)
{
if (JoystickIndex == 0)
{
// Analog nub
const int16_t x = GetJoyX(analog, 0);
if (abs(x) > JOY_DEADZONE) return x;
}
else if (JoystickIndex == 1)
{
// G sensor
const int gx = GetJoyX(gSensor, gZero) * G_SENSITIVITY;
return (int16_t)CLAMP(gx, -32768, 32767);
}
}
#endif
const bool left = player == 0 ?
pressed[P1_LEFT] : (pressed[P2_LEFT0] || pressed[P2_LEFT1]);
const bool right = player == 0 ?
pressed[P1_RIGHT] : (pressed[P2_RIGHT0] || pressed[P2_RIGHT1]);
return left ? (right ? 0 : -32768) : (right ? 32767 : 0);
}
#ifdef __GCW0__
static int16_t GetJoyX(SDL_Joystick *joy, const int16_t zero)
{
if (!joy) return 0;
// Read X-axis of chosen joystick
return (int16_t)SDL_JoystickGetAxis(joy, 0) + zero;
}
#endif
void ResetMovement(void)
{
memset(pressed, 0, sizeof pressed);
}
void InputSwitchJoystick(const int inc)
{
#ifdef __GCW0__
JoystickIndex = CLAMP_OPPOSITE(JoystickIndex + inc, -1, 1);
if (JoystickIndex == 1 && gSensor)
{
// Recalibrate G sensor
gZero = (int16_t)SDL_JoystickGetAxis(gSensor, 0);
}
#else
UNUSED(inc);
#endif
}