@@ -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