Skip to content

Commit

Permalink
Version-getters SDL2/SDL3
Browse files Browse the repository at this point in the history
  • Loading branch information
Starbuck5 committed Jul 7, 2024
1 parent ec76793 commit 2400f14
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 38 deletions.
8 changes: 8 additions & 0 deletions src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ PG_UnlockMutex(SDL_mutex *mutex)
#define PG_SetJoystickEventsEnabled(enabled) \
SDL_SetJoystickEventsEnabled(enabled)

#define PG_FIND_VNUM_MAJOR(ver) SDL_VERSIONNUM_MAJOR(ver)
#define PG_FIND_VNUM_MINOR(ver) SDL_VERSIONNUM_MINOR(ver)
#define PG_FIND_VNUM_MICRO(ver) SDL_VERSIONNUM_MICRO(ver)

#else /* ~SDL_VERSION_ATLEAST(3, 0, 0)*/
#define PG_ShowCursor() SDL_ShowCursor(SDL_ENABLE)
#define PG_HideCursor() SDL_ShowCursor(SDL_DISABLE)
Expand Down Expand Up @@ -167,6 +171,10 @@ PG_UnlockMutex(SDL_mutex *mutex)
#define PG_EventEnabled(type) SDL_EventState(type, SDL_QUERY)
#define PG_SetJoystickEventsEnabled(enabled) SDL_JoystickEventState(enabled)

#define PG_FIND_VNUM_MAJOR(ver) ver.major
#define PG_FIND_VNUM_MINOR(ver) ver.minor
#define PG_FIND_VNUM_MICRO(ver) ver.patch

#if SDL_VERSION_ATLEAST(2, 0, 14)
#define PG_SurfaceHasRLE SDL_HasSurfaceRLE
#else
Expand Down
46 changes: 32 additions & 14 deletions src_c/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,36 +177,45 @@ pg_EnvShouldBlendAlphaSDL2(void);
static int
pg_CheckSDLVersions(void)
{
#if SDL_VERSION_ATLEAST(3, 0, 0)
int compiled = SDL_VERSION;
int linked = SDL_GetVersion();
#else
SDL_version compiled;
SDL_version linked;

SDL_VERSION(&compiled);
SDL_GetVersion(&linked);
#endif

/* only check the major version, in general major version is bumped for ABI
* incompatible changes */
if (compiled.major != linked.major) {
if (PG_FIND_VNUM_MAJOR(compiled) != PG_FIND_VNUM_MAJOR(linked)) {
PyErr_Format(PyExc_RuntimeError,
"ABI incompatibility detected! SDL compiled with "
"%d.%d.%d, linked to %d.%d.%d (major versions should "
"have matched)",
compiled.major, compiled.minor, compiled.patch,
linked.major, linked.minor, linked.patch);
PG_FIND_VNUM_MAJOR(compiled),
PG_FIND_VNUM_MINOR(compiled),
PG_FIND_VNUM_MICRO(compiled), PG_FIND_VNUM_MAJOR(linked),
PG_FIND_VNUM_MINOR(linked), PG_FIND_VNUM_MICRO(linked));
return 0;
}

/* Basically, this is compiled_version > linked_version case, which we
* don't allow */
if ((linked.minor == compiled.minor && linked.patch < compiled.patch) ||
linked.minor < compiled.minor) {
if ((PG_FIND_VNUM_MINOR(linked) == PG_FIND_VNUM_MINOR(compiled) &&
PG_FIND_VNUM_MICRO(linked) < PG_FIND_VNUM_MICRO(compiled)) ||
PG_FIND_VNUM_MINOR(linked) < PG_FIND_VNUM_MINOR(compiled)) {
/* We do some ifdefs to support different SDL versions at compile time.
We use newer API only when available.
Downgrading via dynamic API probably breaks this.*/
PyErr_Format(PyExc_RuntimeError,
"Dynamic linking causes SDL downgrade! (compiled with "
"version %d.%d.%d, linked to %d.%d.%d)",
compiled.major, compiled.minor, compiled.patch,
linked.major, linked.minor, linked.patch);
PG_FIND_VNUM_MAJOR(compiled),
PG_FIND_VNUM_MINOR(compiled),
PG_FIND_VNUM_MICRO(compiled), PG_FIND_VNUM_MAJOR(linked),
PG_FIND_VNUM_MINOR(linked), PG_FIND_VNUM_MICRO(linked));
return 0;
}

Expand Down Expand Up @@ -373,7 +382,12 @@ static PyObject *
pg_get_sdl_version(PyObject *self, PyObject *args, PyObject *kwargs)
{
int linked = 1; /* Default is linked version. */
SDL_version v;
#if SDL_VERSION_ATLEAST(3, 0, 0)
int version = SDL_VERSION;
#else
SDL_version version;
SDL_VERSION(&version);
#endif

static char *keywords[] = {"linked", NULL};

Expand All @@ -382,12 +396,16 @@ pg_get_sdl_version(PyObject *self, PyObject *args, PyObject *kwargs)
}

if (linked) {
SDL_GetVersion(&v);
}
else {
SDL_VERSION(&v);
#if SDL_VERSION_ATLEAST(3, 0, 0)
version = SDL_GetVersion();
#else
SDL_GetVersion(&version);
#endif
}
return Py_BuildValue("iii", v.major, v.minor, v.patch);

return Py_BuildValue("iii", PG_FIND_VNUM_MAJOR(version),
PG_FIND_VNUM_MINOR(version),
PG_FIND_VNUM_MICRO(version));
}

static PyObject *
Expand Down
23 changes: 15 additions & 8 deletions src_c/font.c
Original file line number Diff line number Diff line change
Expand Up @@ -1233,6 +1233,12 @@ static PyObject *
get_ttf_version(PyObject *self, PyObject *args, PyObject *kwargs)
{
int linked = 1; /* Default is linked version. */
#if SDL_VERSION_ATLEAST(3, 0, 0)
int version = SDL_TTF_VERSION;
#else
SDL_version version;
TTF_VERSION(&version);
#endif

static char *keywords[] = {"linked", NULL};

Expand All @@ -1241,15 +1247,16 @@ get_ttf_version(PyObject *self, PyObject *args, PyObject *kwargs)
}

if (linked) {
const SDL_version *v = TTF_Linked_Version();
return Py_BuildValue("iii", v->major, v->minor, v->patch);
}
else {
/* compiled version */
SDL_version v;
TTF_VERSION(&v);
return Py_BuildValue("iii", v.major, v.minor, v.patch);
#if SDL_VERSION_ATLEAST(3, 0, 0)
version = TTF_Version();
#else
version = *TTF_Linked_Version();
#endif
}

return Py_BuildValue("iii", PG_FIND_VNUM_MAJOR(version),
PG_FIND_VNUM_MINOR(version),
PG_FIND_VNUM_MICRO(version));
}

static PyMethodDef _font_methods[] = {
Expand Down
22 changes: 15 additions & 7 deletions src_c/imageext.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,12 @@ imageext_get_sdl_image_version(PyObject *self, PyObject *args,
PyObject *kwargs)
{
int linked = 1;
#if SDL_VERSION_ATLEAST(3, 0, 0)
int version = SDL_IMAGE_VERSION;
#else
SDL_version version;
SDL_IMAGE_VERSION(&version);
#endif

static char *keywords[] = {"linked", NULL};

Expand All @@ -280,14 +286,16 @@ imageext_get_sdl_image_version(PyObject *self, PyObject *args,
}

if (linked) {
const SDL_version *v = IMG_Linked_Version();
return Py_BuildValue("iii", v->major, v->minor, v->patch);
}
else {
SDL_version v;
SDL_IMAGE_VERSION(&v);
return Py_BuildValue("iii", v.major, v.minor, v.patch);
#if SDL_VERSION_ATLEAST(3, 0, 0)
version = IMG_Version();
#else
version = *IMG_Linked_Version();
#endif
}

return Py_BuildValue("iii", PG_FIND_VNUM_MAJOR(version),
PG_FIND_VNUM_MINOR(version),
PG_FIND_VNUM_MICRO(version));
}

/*
Expand Down
24 changes: 15 additions & 9 deletions src_c/mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,12 @@ static PyObject *
mixer_get_sdl_mixer_version(PyObject *self, PyObject *args, PyObject *kwargs)
{
int linked = 1; /* Default is linked version. */
#if SDL_VERSION_ATLEAST(3, 0, 0)
int version = SDL_MIXER_VERSION;
#else
SDL_version version;
SDL_MIXER_VERSION(&version);
#endif

static char *keywords[] = {"linked", NULL};

Expand All @@ -1593,16 +1599,16 @@ mixer_get_sdl_mixer_version(PyObject *self, PyObject *args, PyObject *kwargs)
/* MIXER_INIT_CHECK() is not required for these methods. */

if (linked) {
/* linked version */
const SDL_version *v = Mix_Linked_Version();
return Py_BuildValue("iii", v->major, v->minor, v->patch);
}
else {
/* compiled version */
SDL_version v;
SDL_MIXER_VERSION(&v);
return Py_BuildValue("iii", v.major, v.minor, v.patch);
#if SDL_VERSION_ATLEAST(3, 0, 0)
version = Mix_Version();
#else
version = *Mix_Linked_Version();
#endif
}

return Py_BuildValue("iii", PG_FIND_VNUM_MAJOR(version),
PG_FIND_VNUM_MINOR(version),
PG_FIND_VNUM_MICRO(version));
}

static int
Expand Down

0 comments on commit 2400f14

Please sign in to comment.