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

Port time, surflock and constants to SDL3 #3206

Merged
merged 5 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .github/workflows/build-sdl3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ jobs:
-Csetup-args=-Dmixer=disabled
-Csetup-args=-Dfont=disabled

# eventually we need to run all tests, but for now test that importing pygame
# works
- name: Test import works
run: python3 -c 'import pygame'

# - name: Run tests
# env:
# SDL_VIDEODRIVER: "dummy"
Expand Down
4 changes: 4 additions & 0 deletions src_c/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,10 @@ typedef enum {
SDL_ACTIVEEVENT = SDL_USEREVENT,
SDL_VIDEORESIZE,
SDL_VIDEOEXPOSE,
#if SDL_VERSION_ATLEAST(3, 0, 0)
/* SDL_SYSWMEVENT removed in SDL3, define it here for compat */
SDL_SYSWMEVENT,
#endif

PGE_MIDIIN,
PGE_MIDIOUT,
Expand Down
8 changes: 0 additions & 8 deletions src_c/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ color = py.extension_module(
subdir: pg,
)

# TODO: support SDL3
if sdl_api != 3
constants = py.extension_module(
'constants',
'constants.c',
Expand All @@ -28,7 +26,6 @@ constants = py.extension_module(
install: true,
subdir: pg,
)
endif

# TODO: support SDL3
if sdl_api != 3
Expand Down Expand Up @@ -147,7 +144,6 @@ surface = py.extension_module(
endif

# TODO: support SDL3
if sdl_api != 3
surflock = py.extension_module(
'surflock',
'surflock.c',
Expand All @@ -156,10 +152,7 @@ surflock = py.extension_module(
install: true,
subdir: pg,
)
endif

# TODO: support SDL3
if sdl_api != 3
time = py.extension_module(
'time',
'time.c',
Expand All @@ -168,7 +161,6 @@ time = py.extension_module(
install: true,
subdir: pg,
)
endif

# TODO: support SDL3
if sdl_api != 3
Expand Down
4 changes: 4 additions & 0 deletions src_c/surflock.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ pgSurface_LockBy(pgSurfaceObject *surfobj, PyObject *lockobj)
if (surf->subsurface != NULL) {
pgSurface_Prep(surfobj);
}
#if SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_LockSurface(surf->surf)) {
#else
if (SDL_LockSurface(surf->surf) == -1) {
#endif
ankith26 marked this conversation as resolved.
Show resolved Hide resolved
PyErr_SetString(PyExc_RuntimeError, "error locking surface");
return 0;
}
Expand Down
17 changes: 17 additions & 0 deletions src_c/time.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,13 @@ _pg_clear_event_timer_type(int ev_type)

/* Timer callback function
* TODO: This needs better error handling and a way to report to the user */
#if SDL_VERSION_ATLEAST(3, 0, 0)
static Uint32
timer_callback(void *param, SDL_TimerID timerID, Uint32 interval)
#else
static Uint32
timer_callback(Uint32 interval, void *param)
#endif
{
pgEventTimer *evtimer;
PG_LOCK_TIMER_MUTEX
Expand Down Expand Up @@ -316,12 +321,14 @@ accurate_delay(Sint64 ticks)
if (ticks <= 0)
return 0;

#if !SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_WasInit(SDL_INIT_TIMER)) {
if (SDL_InitSubSystem(SDL_INIT_TIMER)) {
PyErr_SetString(pgExc_SDLError, SDL_GetError());
return -1;
}
}
#endif

funcstart = PG_GetTicks();
if (ticks >= WORST_CLOCK_ACCURACY) {
Expand All @@ -342,8 +349,10 @@ accurate_delay(Sint64 ticks)
static PyObject *
time_get_ticks(PyObject *self, PyObject *_null)
{
#if !SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_WasInit(SDL_INIT_TIMER))
return PyLong_FromLong(0);
#endif
return PyLong_FromUnsignedLongLong(PG_GetTicks());
}

Expand Down Expand Up @@ -371,11 +380,13 @@ time_wait(PyObject *self, PyObject *arg)
if (!PyLong_Check(arg))
return RAISE(PyExc_TypeError, "wait requires one integer argument");

#if !SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_WasInit(SDL_INIT_TIMER)) {
if (SDL_InitSubSystem(SDL_INIT_TIMER)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
}
#endif

ticks = PyLong_AsLongLong(arg);
if (ticks < 0)
Expand Down Expand Up @@ -455,13 +466,15 @@ time_set_timer(PyObject *self, PyObject *args, PyObject *kwargs)
goto end;
}

#if !SDL_VERSION_ATLEAST(3, 0, 0)
/* just doublecheck that timer is initialized */
if (!SDL_WasInit(SDL_INIT_TIMER)) {
if (SDL_InitSubSystem(SDL_INIT_TIMER)) {
ecode = PG_TIMER_SDL_ERROR;
goto end;
}
}
#endif

ecode = _pg_add_event_timer(ev_type, ev_dict, loops);
if (ecode != PG_TIMER_NO_ERROR) {
Expand Down Expand Up @@ -522,12 +535,14 @@ clock_tick_base(pgClockObject *self, PyObject *arg, int use_accurate_delay)
self->rawpassed = PG_GetTicks() - self->last_tick;
delay = endtime - self->rawpassed;

#if !SDL_VERSION_ATLEAST(3, 0, 0)
/*just doublecheck that timer is initialized*/
if (!SDL_WasInit(SDL_INIT_TIMER)) {
if (SDL_InitSubSystem(SDL_INIT_TIMER)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
}
#endif

if (use_accurate_delay)
delay = accurate_delay(delay);
Expand Down Expand Up @@ -639,11 +654,13 @@ clock_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
return NULL;
}

#if !SDL_VERSION_ATLEAST(3, 0, 0)
if (!SDL_WasInit(SDL_INIT_TIMER)) {
if (SDL_InitSubSystem(SDL_INIT_TIMER)) {
return RAISE(pgExc_SDLError, SDL_GetError());
}
}
#endif

pgClockObject *self = (pgClockObject *)(type->tp_alloc(type, 0));
self->fps_tick = 0;
Expand Down
Loading