-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
75 lines (68 loc) · 1.92 KB
/
main.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
#include <iostream>
#include <SDL2/SDL.h>
using namespace std;
SDL_Joystick *gamepad;
SDL_Event event;
int joy, indexJoysticks;
int checkJoyStick(){
int i;
if (indexJoysticks == 1) {
return 0;
} else {
for(i = 0; i < indexJoysticks; i++)
{
printf("Select your gamepad");
printf("Name: %s\n", SDL_JoystickNameForIndex(i));
}
cin>>i;
return i-1;
}
}
void detectPressed(){
bool isRunning = true;
while (isRunning) {
while(SDL_PollEvent(&event) != 0){
if(event.type==SDL_QUIT){
isRunning = false;
} else if(event.type == SDL_JOYBUTTONDOWN){
if(event.jbutton.button == 0){
printf("Button Pressed\n");
} else if(event.jbutton.button == 7){
printf("Start Button Pressed, Program exit\n");
isRunning = false;
}
}
}
}
}
int main()
{
if (SDL_Init(SDL_INIT_JOYSTICK) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
SDL_Quit();
return 1;
} else {
SDL_Log("Program Started");
}
if (SDL_NumJoysticks() > 0) {
printf("%d joysticks found\n", indexJoysticks = SDL_NumJoysticks());
if ((gamepad = SDL_JoystickOpen(joy = checkJoyStick()))){
SDL_JoystickEventState(SDL_ENABLE);
SDL_Log("Connected");
SDL_Log("%s\n", SDL_JoystickName(gamepad));
SDL_Log("Number of Axes: %d\n", SDL_JoystickNumAxes(gamepad));
SDL_Log("Number of Buttons: %d\n", SDL_JoystickNumButtons(gamepad));
detectPressed();
} else {
SDL_Log("Couldn't find Joystick\n");
SDL_Quit();
return 1;
}
} else {
SDL_Log("Couldn't find any Joystick\n");
SDL_Quit();
return 1;
}
SDL_Quit();
return 0;
}