-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller_test.cpp
More file actions
147 lines (127 loc) · 3.35 KB
/
Copy pathcontroller_test.cpp
File metadata and controls
147 lines (127 loc) · 3.35 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
141
142
143
144
145
146
147
#include <SDL3/SDL.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD))
{
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR,
"SDL Init Error",
SDL_GetError(),
nullptr
);
return 1;
}
SDL_Window* window = SDL_CreateWindow(
"O2EM-NG SDL3 Controller Test",
800,
600,
SDL_WINDOW_RESIZABLE
);
if (!window)
{
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR,
"Window Error",
SDL_GetError(),
nullptr
);
SDL_Quit();
return 1;
}
SDL_Gamepad* gamepad = nullptr;
int count = 0;
SDL_JoystickID* gamepads = SDL_GetGamepads(&count);
if (count > 0)
{
gamepad = SDL_OpenGamepad(gamepads[0]);
if (gamepad)
{
const char* name = SDL_GetGamepadName(gamepad);
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_INFORMATION,
"Controller Detected",
name ? name : "Unknown controller",
window
);
}
}
else
{
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_WARNING,
"No Controller",
"No SDL gamepad detected.",
window
);
}
if (gamepads)
{
SDL_free(gamepads);
}
bool running = true;
SDL_Event e;
while (running)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_EVENT_QUIT)
{
running = false;
}
if (e.type == SDL_EVENT_KEY_DOWN)
{
if (e.key.key == SDLK_ESCAPE)
{
running = false;
}
}
if (e.type == SDL_EVENT_GAMEPAD_BUTTON_DOWN)
{
switch (e.gbutton.button)
{
case SDL_GAMEPAD_BUTTON_SOUTH:
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_INFORMATION,
"Button",
"A / South button pressed",
window
);
break;
case SDL_GAMEPAD_BUTTON_EAST:
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_INFORMATION,
"Button",
"B / East button pressed",
window
);
break;
case SDL_GAMEPAD_BUTTON_DPAD_UP:
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_INFORMATION,
"Button",
"D-Pad Up pressed",
window
);
break;
case SDL_GAMEPAD_BUTTON_DPAD_DOWN:
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_INFORMATION,
"Button",
"D-Pad Down pressed",
window
);
break;
}
}
}
SDL_Delay(16);
}
if (gamepad)
{
SDL_CloseGamepad(gamepad);
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}