Commit MetaInfo

Revisãof892d8d7bc8b069128f250dc9f8164f081510f19 (tree)
Hora2020-03-02 04:50:46
AutorDavid Ludwig <dludwig@pobo...>
CommiterDavid Ludwig

Mensagem de Log

more code cleanups

Mudança Sumário

Diff

diff -r d3f769baecc0 -r f892d8d7bc8b main.cpp
--- a/main.cpp Sun Mar 01 14:11:19 2020 -0500
+++ b/main.cpp Sun Mar 01 14:50:46 2020 -0500
@@ -63,11 +63,50 @@
6363 );
6464 }
6565
66-struct JoystickTypeInfo {
67- SDL_JoystickType type;
68- const char * name;
66+
67+template <typename EnumType>
68+struct EnumTypeInfo
69+{
70+ EnumType value;
71+ const char *name;
72+
73+ static const EnumType defaultValue;
74+ static const EnumTypeInfo<EnumType> infos[];
6975 };
70-static const JoystickTypeInfo joystick_type_infos[] = {
76+
77+template <typename EnumType>
78+constexpr const char * NameOf(EnumType value)
79+{
80+ for (int i = 0; i < SDL_arraysize(EnumTypeInfo<EnumType>::infos); ++i) {
81+ if (value == EnumTypeInfo<EnumType>::infos[i].value) {
82+ return EnumTypeInfo<EnumType>::infos[i].name;
83+ }
84+ }
85+ return NameOf(EnumTypeInfo<EnumType>::defaultValue);
86+}
87+
88+template <typename EnumType>
89+constexpr bool NamesForImGuiCombo(void *data, int idx, const char **outText)
90+{
91+ if (idx < 0 || idx >= SDL_arraysize(EnumTypeInfo<EnumType>::infos)) {
92+ return false;
93+ }
94+ *outText = EnumTypeInfo<EnumType>::infos[idx].name;
95+ return true;
96+}
97+
98+template <typename EnumType>
99+constexpr int NumNames()
100+{
101+ return SDL_arraysize(EnumTypeInfo<EnumType>::infos);
102+}
103+
104+
105+template <>
106+const SDL_JoystickType EnumTypeInfo<SDL_JoystickType>::defaultValue = SDL_JOYSTICK_TYPE_UNKNOWN;
107+
108+template <>
109+const EnumTypeInfo<SDL_JoystickType> EnumTypeInfo<SDL_JoystickType>::infos[] = {
71110 { SDL_JOYSTICK_TYPE_UNKNOWN, "Unknown" },
72111 { SDL_JOYSTICK_TYPE_GAMECONTROLLER, "Game controller" },
73112 { SDL_JOYSTICK_TYPE_WHEEL, "Wheel" },
@@ -80,30 +119,12 @@
80119 { SDL_JOYSTICK_TYPE_THROTTLE, "Throttle" }
81120 };
82121
83-static bool GetJoystickTypeInfosForImGuiCombo(void *, int idx, const char ** out_text)
84-{
85- if (idx < 0 || idx >= SDL_arraysize(joystick_type_infos)) {
86- return false;
87- }
88- *out_text = joystick_type_infos[idx].name;
89- return true;
90-}
91122
92-constexpr const char * JoystickTypeName(SDL_JoystickType type)
93-{
94- for (int i = 0; i < SDL_arraysize(joystick_type_infos); ++i) {
95- if (type == joystick_type_infos[i].type) {
96- return joystick_type_infos[i].name;
97- }
98- }
99- return JoystickTypeName(SDL_JOYSTICK_TYPE_UNKNOWN);
100-}
123+template <>
124+const SDL_GameControllerType EnumTypeInfo<SDL_GameControllerType>::defaultValue = SDL_CONTROLLER_TYPE_UNKNOWN;
101125
102-struct ControllerTypeInfo {
103- SDL_GameControllerType type;
104- const char * name;
105-};
106-static const ControllerTypeInfo controller_type_infos[] = {
126+template <>
127+const EnumTypeInfo<SDL_GameControllerType> EnumTypeInfo<SDL_GameControllerType>::infos[] = {
107128 { SDL_CONTROLLER_TYPE_UNKNOWN, "Unknown" },
108129 { SDL_CONTROLLER_TYPE_XBOX360, "XBox 360" },
109130 { SDL_CONTROLLER_TYPE_XBOXONE, "XBox One" },
@@ -115,21 +136,12 @@
115136 #endif
116137 };
117138
118-constexpr const char * ControllerTypeName(SDL_GameControllerType type)
119-{
120- for (int i = 0; i < SDL_arraysize(controller_type_infos); ++i) {
121- if (type == controller_type_infos[i].type) {
122- return controller_type_infos[i].name;
123- }
124- }
125- return ControllerTypeName(SDL_CONTROLLER_TYPE_UNKNOWN);
126-}
127139
128-struct GameControllerButtonInfo {
129- SDL_GameControllerButton button;
130- const char *name;
131-};
132-static const GameControllerButtonInfo controller_button_type_infos[] = {
140+template <>
141+const SDL_GameControllerButton EnumTypeInfo<SDL_GameControllerButton>::defaultValue = SDL_CONTROLLER_BUTTON_INVALID;
142+
143+template <>
144+const EnumTypeInfo<SDL_GameControllerButton> EnumTypeInfo<SDL_GameControllerButton>::infos[] = {
133145 { SDL_CONTROLLER_BUTTON_INVALID, "Invalid" },
134146 { SDL_CONTROLLER_BUTTON_A, "A" },
135147 { SDL_CONTROLLER_BUTTON_B, "B" },
@@ -149,22 +161,12 @@
149161 // { SDL_CONTROLLER_BUTTON_MAX, "Max" }, // don't convert this to a value
150162 };
151163
152-constexpr const char * GameControllerButtonName(SDL_GameControllerButton button)
153-{
154- for (int i = 0; i < SDL_arraysize(controller_button_type_infos); ++i) {
155- if (button == controller_button_type_infos[i].button) {
156- return controller_button_type_infos[i].name;
157- }
158- }
159- return GameControllerButtonName(SDL_CONTROLLER_BUTTON_INVALID);
160-}
161164
165+template <>
166+const SDL_GameControllerAxis EnumTypeInfo<SDL_GameControllerAxis>::defaultValue = SDL_CONTROLLER_AXIS_INVALID;
162167
163-struct GameControllerAxisInfo {
164- SDL_GameControllerAxis axis;
165- const char *name;
166-};
167-static const GameControllerAxisInfo controller_axis_type_infos[] = {
168+template <>
169+const EnumTypeInfo<SDL_GameControllerAxis> EnumTypeInfo<SDL_GameControllerAxis>::infos[] = {
168170 { SDL_CONTROLLER_AXIS_INVALID, "Invalid" },
169171 { SDL_CONTROLLER_AXIS_LEFTX, "Left X" },
170172 { SDL_CONTROLLER_AXIS_LEFTY, "Left Y" },
@@ -175,16 +177,6 @@
175177 // { SDL_CONTROLLER_AXIS_MAX, "MAX" }, // don't convert this to a value
176178 };
177179
178-constexpr const char * GameControllerAxisName(SDL_GameControllerAxis axis)
179-{
180- for (int i = 0; i < SDL_arraysize(controller_axis_type_infos); ++i) {
181- if (axis == controller_axis_type_infos[i].axis) {
182- return controller_axis_type_infos[i].name;
183- }
184- }
185- return GameControllerAxisName(SDL_CONTROLLER_AXIS_INVALID);
186-}
187-
188180
189181 void ShowJoystickSubWindow(int device_index);
190182
@@ -220,7 +212,7 @@
220212
221213 #if SDL_JOYSTICK_VIRTUAL
222214 static struct VJoyParams {
223- int type_index = 1; // index into 'joystick_type_infos'
215+ int type_index = 1; // index into 'EnumTypeInfo<SDL_JoystickType>::infos'
224216 int naxes = 2;
225217 int nballs = 0;
226218 int nbuttons = 4;
@@ -238,7 +230,7 @@
238230 ImGui::SetNextWindowPos(pos);
239231 }
240232 if (ImGui::BeginPopupModal("New Virtual Joystick", NULL, ImGuiWindowFlags_AlwaysAutoResize)) {
241- ImGui::Combo("Type", &vjoy_params.type_index, &GetJoystickTypeInfosForImGuiCombo, NULL, IM_ARRAYSIZE(joystick_type_infos));
233+ ImGui::Combo("Type", &vjoy_params.type_index, &NamesForImGuiCombo<SDL_JoystickType>, NULL, NumNames<SDL_JoystickType>());
242234 ImGui::InputInt("Num Axes", &vjoy_params.naxes, 1, 0);
243235 ImGui::InputInt("Num Balls", &vjoy_params.nballs);
244236 ImGui::InputInt("Num Buttons", &vjoy_params.nbuttons);
@@ -249,9 +241,9 @@
249241 if (ImGui::Button("Create", ImVec2(120,0))) {
250242 SDL_JoystickType vjoy_type = SDL_JOYSTICK_TYPE_UNKNOWN;
251243 if (vjoy_params.type_index >= 0 &&
252- vjoy_params.type_index < SDL_arraysize(joystick_type_infos))
244+ vjoy_params.type_index < NumNames<SDL_JoystickType>())
253245 {
254- vjoy_type = joystick_type_infos[vjoy_params.type_index].type;
246+ vjoy_type = EnumTypeInfo<SDL_JoystickType>::infos[vjoy_params.type_index].value;
255247 printf("vjoy_type: %d\n", (int)vjoy_type);
256248 }
257249 int vjoy_device_index = \
@@ -349,7 +341,7 @@
349341 ImGui_TextGUID(SDL_JoystickGetDeviceGUID(device_index));
350342 ImGui::NextColumn();
351343
352- ImGui::Text("%s", JoystickTypeName(SDL_JoystickGetDeviceType(device_index)));
344+ ImGui::Text("%s", NameOf(SDL_JoystickGetDeviceType(device_index)));
353345 ImGui::NextColumn();
354346
355347 ImGui::Text("%d", (int)SDL_JoystickGetDeviceInstanceID(device_index));
@@ -818,7 +810,7 @@
818810 ImGui::NextColumn();
819811
820812 // Type
821- ImGui::Text("%s", ControllerTypeName(SDL_GameControllerTypeForIndex(device_index)));
813+ ImGui::Text("%s", NameOf(SDL_GameControllerTypeForIndex(device_index)));
822814 ImGui::NextColumn();
823815
824816 // Mapping
@@ -968,7 +960,7 @@
968960
969961 ImGui::Text("Type");
970962 ImGui::NextColumn();
971- ImGui::Text("%s", ControllerTypeName(SDL_GameControllerGetType(controller)));
963+ ImGui::Text("%s", NameOf(SDL_GameControllerGetType(controller)));
972964 ImGui::NextColumn();
973965
974966 ImGui::Text("Player Index");
@@ -1022,7 +1014,7 @@
10221014 ImGui::PushStyleColor(ImGuiCol_Button, (ImVec4)ImColor(0,0,0));
10231015 }
10241016 ImGui::PushStyleColor(ImGuiCol_Border, (ImVec4)ImColor(255,255,255));
1025- ImGui::Text("Button, %s", GameControllerButtonName(button));
1017+ ImGui::Text("Button, %s", NameOf(button));
10261018 ImGui::NextColumn();
10271019 ImGui::Text("%d", (int)buttonState);
10281020 ImGui::NextColumn();
@@ -1049,7 +1041,7 @@
10491041 ImGui::PushID(i);
10501042 const SDL_GameControllerAxis key = (SDL_GameControllerAxis)i;
10511043 Sint16 value = SDL_GameControllerGetAxis(controller, key);
1052- ImGui::Text("Axis, %s", GameControllerAxisName(key));
1044+ ImGui::Text("Axis, %s", NameOf(key));
10531045 ImGui::NextColumn();
10541046 ImGui::Text("%d", (int)value);
10551047 ImGui::NextColumn();
Show on old repository browser