-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
142 lines (125 loc) · 3.52 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
137
138
139
140
141
142
#include "input.h"
void listAvailablesJoy (){
SDL_Joystick* pJoystick = NULL;
printf("Availables Joysticks\n");
printf("======================\n\n");
for (char i = 0; i < SDL_NumJoysticks(); i++) {
printf("Name : %s\n",SDL_JoystickNameForIndex(i));
/* open the selected gamepad */
pJoystick = SDL_JoystickOpen(i);
printf("Axis : %d\n",SDL_JoystickNumAxes(pJoystick));
printf("Hats : %d\n",SDL_JoystickNumHats(pJoystick));
printf("Trackballs : %d\n",SDL_JoystickNumBalls(pJoystick));
printf("Buttons : %d\n\n",SDL_JoystickNumButtons(pJoystick));
}
}
/* get the next gamepad event
* return 1 if program ended */
int getEvent(struct joy_event *joy_event){
SDL_Event event;
if(SDL_WaitEvent(&event)){
switch (event.type) {
case SDL_QUIT:
return 1;
break;
case SDL_JOYAXISMOTION:
joy_event->gamepad_number = event.jaxis.which;
joy_event->type = AXIS;
joy_event->wich = event.jaxis.axis;
joy_event->value = (event.jaxis.value >> 9 << 1) + 1;
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
joy_event->gamepad_number = event.jbutton.which;
joy_event->type = BUTTON;
joy_event->wich = event.jbutton.button;
if (event.jbutton.state == SDL_PRESSED)
joy_event->value = KEY_PRESSED;
else
joy_event->value = KEY_RELEASED;
break;
case SDL_JOYHATMOTION:
joy_event->gamepad_number = event.jhat.which;
joy_event->type = HAT;
joy_event->wich = event.jhat.hat;
switch (event.jhat.value) {
case SDL_HAT_CENTERED:
joy_event->value = CENTER;
break;
case SDL_HAT_LEFT:
joy_event->value = LEFT;
break;
case SDL_HAT_RIGHT:
joy_event->value = RIGHT;
break;
case SDL_HAT_UP:
joy_event->value = UP;
break;
case SDL_HAT_DOWN:
joy_event->value = DOWN;
break;
case SDL_HAT_LEFTDOWN:
joy_event->value = LEFT | DOWN;
break;
case SDL_HAT_LEFTUP:
joy_event->value = LEFT | UP;
break;
case SDL_HAT_RIGHTDOWN:
joy_event->value = RIGHT | DOWN;
break;
case SDL_HAT_RIGHTUP:
joy_event->value = RIGHT | UP;
break;
}
break;
}
}
else return 1;
return 0;
}
/* get the boolean value from a button/ hat/ axis
* this function return 1 if the button is the same
* that the event then the digital value is stored in value,
* 0 if nothing was done */
int digitalRead (struct btn *button, struct joy_event *event, enum key_value *value){
if ( (button->type == event->type) && (button->wich == event->wich) ){
/* the button is the same that the event, so read the value */
switch (event->type) {
case BUTTON:
*value = event->value;
break;
case HAT:
if ((event->value & button->direction) != 0)
*value = KEY_PRESSED;
else
*value = KEY_RELEASED;
break;
case AXIS:
if ((event->value * button->reverse) > button->true_value)
*value = KEY_PRESSED;
else
*value = KEY_RELEASED;
break;
}
return 1;
}
return 0;
}
/* get the coordinates of a joystick from an event:
* return 1 if it is the good joystick (and read value)
* return 0 if nothing was done */
int joystickRead (struct joy *joystick,
struct joy_event *event, struct vec2 *coordinates){
if (event->type == AXIS) {// && (joystick->wich == event->wich) ){
if (event->wich == joystick->x_axis){
coordinates->x = event->value * joystick->reverse_x;
return 1;
}
else if (event->wich == joystick->y_axis){
coordinates->y = event->value * joystick->reverse_y;
return 1;
}
else return 0;
}
return 0;
}