Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDL3 support for pygame.system #3146

Merged
merged 1 commit into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading