Skip to content

Commit 1a57252

Browse files
authored
Merge pull request #3146 from Starbuck5/system-compat-sdl3
SDL3 support for pygame.system
2 parents f3f112a + 1aa458d commit 1a57252

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src_c/meson.build

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,6 @@ newbuffer = py.extension_module(
321321
)
322322

323323
# new/experimental/uncommon stuff, but built by default
324-
# TODO: support SDL3
325-
if sdl_api != 3
326324
system = py.extension_module(
327325
'system',
328326
'system.c',
@@ -331,7 +329,6 @@ system = py.extension_module(
331329
install: true,
332330
subdir: pg,
333331
)
334-
endif
335332

336333
geometry = py.extension_module(
337334
'geometry',

src_c/system.c

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,25 +96,53 @@ pg_system_get_pref_locales(PyObject *self, PyObject *_null)
9696
return NULL;
9797
}
9898

99-
#if SDL_VERSION_ATLEAST(2, 0, 14)
99+
// Sorry about the SDL3 gnarliness here, this was the best way I could
100+
// think of to support SDL2/SDL3 at once. The approach is that each
101+
// version is responsible for coming up with a list and a count,
102+
// then the iteration over the list is shared (except for the indexing
103+
// strategy, where SDL2/3 are different)
104+
100105
PyObject *dict, *val = NULL;
106+
int num_locales;
107+
SDL_Locale *current_locale;
108+
109+
#if SDL_VERSION_ATLEAST(3, 0, 0)
110+
SDL_Locale **locales = SDL_GetPreferredLocales(&num_locales);
111+
if (!locales) {
112+
/* Return an empty list if SDL function does not return any useful
113+
* information */
114+
return ret_list;
115+
}
116+
#elif SDL_VERSION_ATLEAST(2, 0, 14)
101117
SDL_Locale *locales = SDL_GetPreferredLocales();
102118
if (!locales) {
103119
/* Return an empty list if SDL function does not return any useful
104120
* information */
105121
return ret_list;
106122
}
107123

108-
SDL_Locale *current_locale = locales;
109-
124+
num_locales = 0;
125+
current_locale = locales;
110126
/* The array is terminated when the language attribute of the last struct
111127
* in the array is NULL */
112128
while (current_locale->language) {
129+
num_locales++;
130+
current_locale++;
131+
}
132+
#endif
133+
134+
#if SDL_VERSION_ATLEAST(2, 0, 14)
135+
for (int i = 0; i < num_locales; i++) {
113136
dict = PyDict_New();
114137
if (!dict) {
115138
goto error;
116139
}
117140

141+
#if SDL_VERSION_ATLEAST(3, 0, 0)
142+
current_locale = locales[i];
143+
#else
144+
current_locale = locales + i;
145+
#endif
118146
val = PyUnicode_FromString(current_locale->language);
119147
if (!val) {
120148
goto error;
@@ -145,7 +173,6 @@ pg_system_get_pref_locales(PyObject *self, PyObject *_null)
145173
goto error;
146174
}
147175
Py_DECREF(dict);
148-
current_locale++;
149176
}
150177

151178
SDL_free(locales);

0 commit comments

Comments
 (0)