Skip to content

Commit

Permalink
Merge pull request #3146 from Starbuck5/system-compat-sdl3
Browse files Browse the repository at this point in the history
SDL3 support for pygame.system
  • Loading branch information
ankith26 authored Oct 6, 2024
2 parents f3f112a + 1aa458d commit 1a57252
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
3 changes: 0 additions & 3 deletions src_c/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,6 @@ newbuffer = py.extension_module(
)

# new/experimental/uncommon stuff, but built by default
# TODO: support SDL3
if sdl_api != 3
system = py.extension_module(
'system',
'system.c',
Expand All @@ -331,7 +329,6 @@ system = py.extension_module(
install: true,
subdir: pg,
)
endif

geometry = py.extension_module(
'geometry',
Expand Down
35 changes: 31 additions & 4 deletions src_c/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,53 @@ pg_system_get_pref_locales(PyObject *self, PyObject *_null)
return NULL;
}

#if SDL_VERSION_ATLEAST(2, 0, 14)
// Sorry about the SDL3 gnarliness here, this was the best way I could
// think of to support SDL2/SDL3 at once. The approach is that each
// version is responsible for coming up with a list and a count,
// then the iteration over the list is shared (except for the indexing
// strategy, where SDL2/3 are different)

PyObject *dict, *val = NULL;
int num_locales;
SDL_Locale *current_locale;

#if SDL_VERSION_ATLEAST(3, 0, 0)
SDL_Locale **locales = SDL_GetPreferredLocales(&num_locales);
if (!locales) {
/* Return an empty list if SDL function does not return any useful
* information */
return ret_list;
}
#elif SDL_VERSION_ATLEAST(2, 0, 14)
SDL_Locale *locales = SDL_GetPreferredLocales();
if (!locales) {
/* Return an empty list if SDL function does not return any useful
* information */
return ret_list;
}

SDL_Locale *current_locale = locales;

num_locales = 0;
current_locale = locales;
/* The array is terminated when the language attribute of the last struct
* in the array is NULL */
while (current_locale->language) {
num_locales++;
current_locale++;
}
#endif

#if SDL_VERSION_ATLEAST(2, 0, 14)
for (int i = 0; i < num_locales; i++) {
dict = PyDict_New();
if (!dict) {
goto error;
}

#if SDL_VERSION_ATLEAST(3, 0, 0)
current_locale = locales[i];
#else
current_locale = locales + i;
#endif
val = PyUnicode_FromString(current_locale->language);
if (!val) {
goto error;
Expand Down Expand Up @@ -145,7 +173,6 @@ pg_system_get_pref_locales(PyObject *self, PyObject *_null)
goto error;
}
Py_DECREF(dict);
current_locale++;
}

SDL_free(locales);
Expand Down

0 comments on commit 1a57252

Please sign in to comment.