Skip to content

Commit 71e5c18

Browse files
authored
Merge pull request #3206 from pygame-community/ankith26-more-sdl3
Port time, surflock and constants to SDL3
2 parents de88561 + f295ce5 commit 71e5c18

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

.github/workflows/build-sdl3.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ jobs:
8787
- name: Build with SDL3
8888
run: python3 dev.py build --sdl3
8989

90+
# eventually we need to run all tests, but for now test that importing pygame
91+
# works
92+
- name: Test import works
93+
run: python3 -c 'import pygame'
94+
9095
# - name: Run tests
9196
# env:
9297
# SDL_VIDEODRIVER: "dummy"

src_c/_pygame.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,10 @@ typedef enum {
284284
SDL_ACTIVEEVENT = SDL_USEREVENT,
285285
SDL_VIDEORESIZE,
286286
SDL_VIDEOEXPOSE,
287+
#if SDL_VERSION_ATLEAST(3, 0, 0)
288+
/* SDL_SYSWMEVENT removed in SDL3, define it here for compat */
289+
SDL_SYSWMEVENT,
290+
#endif
287291

288292
PGE_MIDIIN,
289293
PGE_MIDIOUT,

src_c/meson.build

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ color = py.extension_module(
1818
subdir: pg,
1919
)
2020

21-
# TODO: support SDL3
22-
if sdl_api != 3
2321
constants = py.extension_module(
2422
'constants',
2523
'constants.c',
@@ -28,7 +26,6 @@ constants = py.extension_module(
2826
install: true,
2927
subdir: pg,
3028
)
31-
endif
3229

3330
# TODO: support SDL3
3431
if sdl_api != 3
@@ -147,7 +144,6 @@ surface = py.extension_module(
147144
endif
148145

149146
# TODO: support SDL3
150-
if sdl_api != 3
151147
surflock = py.extension_module(
152148
'surflock',
153149
'surflock.c',
@@ -156,10 +152,7 @@ surflock = py.extension_module(
156152
install: true,
157153
subdir: pg,
158154
)
159-
endif
160155

161-
# TODO: support SDL3
162-
if sdl_api != 3
163156
time = py.extension_module(
164157
'time',
165158
'time.c',
@@ -168,7 +161,6 @@ time = py.extension_module(
168161
install: true,
169162
subdir: pg,
170163
)
171-
endif
172164

173165
# TODO: support SDL3
174166
if sdl_api != 3

src_c/surflock.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,12 @@ pgSurface_LockBy(pgSurfaceObject *surfobj, PyObject *lockobj)
9999
if (surf->subsurface != NULL) {
100100
pgSurface_Prep(surfobj);
101101
}
102-
if (SDL_LockSurface(surf->surf) == -1) {
102+
#if SDL_VERSION_ATLEAST(3, 0, 0)
103+
if (!SDL_LockSurface(surf->surf))
104+
#else
105+
if (SDL_LockSurface(surf->surf) == -1)
106+
#endif
107+
{
103108
PyErr_SetString(PyExc_RuntimeError, "error locking surface");
104109
return 0;
105110
}

src_c/time.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,13 @@ _pg_clear_event_timer_type(int ev_type)
276276

277277
/* Timer callback function
278278
* TODO: This needs better error handling and a way to report to the user */
279+
#if SDL_VERSION_ATLEAST(3, 0, 0)
280+
static Uint32
281+
timer_callback(void *param, SDL_TimerID timerID, Uint32 interval)
282+
#else
279283
static Uint32
280284
timer_callback(Uint32 interval, void *param)
285+
#endif
281286
{
282287
pgEventTimer *evtimer;
283288
PG_LOCK_TIMER_MUTEX
@@ -316,12 +321,14 @@ accurate_delay(Sint64 ticks)
316321
if (ticks <= 0)
317322
return 0;
318323

324+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
319325
if (!SDL_WasInit(SDL_INIT_TIMER)) {
320326
if (SDL_InitSubSystem(SDL_INIT_TIMER)) {
321327
PyErr_SetString(pgExc_SDLError, SDL_GetError());
322328
return -1;
323329
}
324330
}
331+
#endif
325332

326333
funcstart = PG_GetTicks();
327334
if (ticks >= WORST_CLOCK_ACCURACY) {
@@ -342,8 +349,10 @@ accurate_delay(Sint64 ticks)
342349
static PyObject *
343350
time_get_ticks(PyObject *self, PyObject *_null)
344351
{
352+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
345353
if (!SDL_WasInit(SDL_INIT_TIMER))
346354
return PyLong_FromLong(0);
355+
#endif
347356
return PyLong_FromUnsignedLongLong(PG_GetTicks());
348357
}
349358

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

383+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
374384
if (!SDL_WasInit(SDL_INIT_TIMER)) {
375385
if (SDL_InitSubSystem(SDL_INIT_TIMER)) {
376386
return RAISE(pgExc_SDLError, SDL_GetError());
377387
}
378388
}
389+
#endif
379390

380391
ticks = PyLong_AsLongLong(arg);
381392
if (ticks < 0)
@@ -455,13 +466,15 @@ time_set_timer(PyObject *self, PyObject *args, PyObject *kwargs)
455466
goto end;
456467
}
457468

469+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
458470
/* just doublecheck that timer is initialized */
459471
if (!SDL_WasInit(SDL_INIT_TIMER)) {
460472
if (SDL_InitSubSystem(SDL_INIT_TIMER)) {
461473
ecode = PG_TIMER_SDL_ERROR;
462474
goto end;
463475
}
464476
}
477+
#endif
465478

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

538+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
525539
/*just doublecheck that timer is initialized*/
526540
if (!SDL_WasInit(SDL_INIT_TIMER)) {
527541
if (SDL_InitSubSystem(SDL_INIT_TIMER)) {
528542
return RAISE(pgExc_SDLError, SDL_GetError());
529543
}
530544
}
545+
#endif
531546

532547
if (use_accurate_delay)
533548
delay = accurate_delay(delay);
@@ -639,11 +654,13 @@ clock_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
639654
return NULL;
640655
}
641656

657+
#if !SDL_VERSION_ATLEAST(3, 0, 0)
642658
if (!SDL_WasInit(SDL_INIT_TIMER)) {
643659
if (SDL_InitSubSystem(SDL_INIT_TIMER)) {
644660
return RAISE(pgExc_SDLError, SDL_GetError());
645661
}
646662
}
663+
#endif
647664

648665
pgClockObject *self = (pgClockObject *)(type->tp_alloc(type, 0));
649666
self->fps_tick = 0;

0 commit comments

Comments
 (0)