Skip to content

Commit 425acc4

Browse files
committed
Extended sdl2.ext.init to support all subsystems
1 parent 6d9c1bc commit 425acc4

File tree

3 files changed

+113
-14
lines changed

3 files changed

+113
-14
lines changed

doc/news.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ This describes the latest changes between the PySDL2 releases.
55
0.9.14 (Unreleased)
66
-------------------
77

8+
New Features:
9+
810
* Added a new function :func:`~sdl2.ext.get_displays` and class
911
:class:`~sdl2.ext.DisplayInfo` to provide an easier and more Pythonic API for
1012
getting information about the names, locations, and supported video modes
1113
of the connected displays.
14+
* Extended :func:`~sdl2.ext.init` to allow initializing all SDL2 subsystems
15+
individually using keyword arguments (e.g. ``sdl2.ext.init(audio=True)`` to
16+
initialize the audio subsystem as well as the default video subsystem).
17+
Previously, this function only initailized the video subsystem.
1218

1319

1420
0.9.13
@@ -46,7 +52,6 @@ New Features:
4652
that will not break when other SDL2 modules are eventually migrated to similar
4753
Python-wrapped bindings.
4854

49-
5055
Fixed Bugs:
5156

5257
* Fixed a bug in :func:`~sdl2.rw_from_object` where calling

sdl2/ext/common.py

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import ctypes
2-
from .. import SDL_Init, SDL_Quit, SDL_QuitSubSystem, SDL_WasInit, \
3-
SDL_INIT_VIDEO, error, events, timer
2+
from .. import (events, timer, error, dll,
3+
SDL_Init, SDL_InitSubSystem, SDL_Quit, SDL_QuitSubSystem, SDL_WasInit,
4+
SDL_INIT_VIDEO, SDL_INIT_AUDIO, SDL_INIT_TIMER, SDL_INIT_HAPTIC,
5+
SDL_INIT_JOYSTICK, SDL_INIT_GAMECONTROLLER, SDL_INIT_SENSOR, SDL_INIT_EVENTS,
6+
)
47

58
_HASSDLTTF = True
69
try:
@@ -16,6 +19,18 @@
1619
__all__ = ["SDLError", "init", "quit", "get_events", "TestEventProcessor"]
1720

1821

22+
_sdl_subsystems = {
23+
'timer': SDL_INIT_TIMER,
24+
'audio': SDL_INIT_AUDIO,
25+
'video': SDL_INIT_VIDEO,
26+
'joystick': SDL_INIT_JOYSTICK,
27+
'haptic': SDL_INIT_HAPTIC,
28+
'gamecontroller': SDL_INIT_GAMECONTROLLER,
29+
'events': SDL_INIT_EVENTS,
30+
'sensor': SDL_INIT_SENSOR,
31+
}
32+
33+
1934
class SDLError(Exception):
2035
"""An SDL2-specific exception class."""
2136

@@ -48,19 +63,71 @@ def raise_sdl_err(desc=None):
4863
raise SDLError(e)
4964

5065

51-
def init():
52-
"""Initializes the SDL2 video subsystem.
66+
def init(
67+
video=True, audio=False, timer=False, joystick=False, controller=False,
68+
haptic=False, sensor=False, events=True
69+
):
70+
"""Initializes SDL and its optional subsystems.
71+
72+
By default, only the video and events subsystems are initialized. Note that
73+
the sensor subsystem requires SDL 2.0.9 or later.
74+
75+
Args:
76+
video (bool, optional): Whether to initialize the SDL video subsystem.
77+
If True, the events subsystem will also be initialized. Defaults
78+
to True.
79+
audio (bool, optional): Whether to initialize the SDL audio subsystem.
80+
Defaults to False.
81+
timer (bool, optional): Whether to initialize the SDL timer subsystem.
82+
Defaults to False.
83+
joystick (bool, optional): Whether to initialize the SDL joystick
84+
subsystem. If True, the events subsystem will also be initialized.
85+
Defaults to False.
86+
controller (bool, optional): Whether to initialize the SDL gamecontroller
87+
subsystem. If True, the joystick subsystem will also be initialized.
88+
Defaults to False.
89+
haptic (bool, optional): Whether to initialize the SDL haptic (force
90+
feedback) subsystem. Defaults to False.
91+
sensor (bool, optional): Whether to initialize the SDL sensor subsystem.
92+
Defaults to False.
93+
events (bool, optional): Whether to initialize the SDL events subsystem.
94+
Will automatically be initialized if the video, joystick, or
95+
gamecontroller subsystems are enabled. Defaults to False.
5396
5497
See :ref:`pygamers_pygame` for a comparison between this function and
5598
``pygame.init()``.
5699
57100
Raises:
58-
:exc:`SDLError`: If the SDL2 video subsystem cannot be initialized.
101+
:exc:`SDLError`: If a requested SDL subsystem cannot be initialized.
59102
60103
"""
61-
# TODO: More subsystems?
62-
if SDL_Init(SDL_INIT_VIDEO) != 0:
63-
raise SDLError()
104+
subsystems = []
105+
if events:
106+
subsystems.append("events")
107+
if video:
108+
subsystems.append("video")
109+
if audio:
110+
subsystems.append("audio")
111+
if timer:
112+
subsystems.append("timer")
113+
if joystick:
114+
subsystems.append("joystick")
115+
if controller:
116+
subsystems.append("gamecontroller")
117+
if haptic:
118+
subsystems.append("haptic")
119+
if sensor:
120+
subsystems.append("sensor")
121+
122+
if SDL_Init(0) != 0:
123+
raise_sdl_err("initializing SDL2")
124+
125+
for s in subsystems:
126+
if s == "sensor" and dll.version < 2009:
127+
e = "The sensor subsystem requires SDL 2.0.9 or later."
128+
raise RuntimeError(e)
129+
if SDL_InitSubSystem(_sdl_subsystems[s]) != 0:
130+
raise_sdl_err("initializing the {0} subsystem".format(s))
64131

65132

66133
def quit():

sdl2/test/sdl2ext_test.py

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import sys
22
import pytest
3+
import sdl2
34
from sdl2 import ext as sdl2ext
4-
from sdl2 import SDL_WasInit, SDL_INIT_VIDEO, SDL_FlushEvent, SDL_USEREVENT, \
5+
from sdl2 import SDL_Quit, SDL_WasInit, SDL_FlushEvent, SDL_USEREVENT, \
56
SDL_FIRSTEVENT, SDL_LASTEVENT, SDL_Event, SDL_UserEvent, SDL_PushEvent
67

78
@pytest.fixture(scope="module")
89
def with_sdl_ext():
10+
if SDL_WasInit(0) != 0:
11+
SDL_Quit()
912
sdl2ext.init()
1013
yield
1114
sdl2ext.quit()
@@ -18,15 +21,39 @@ def test_init_quit():
1821
sdl2ext.init()
1922
except sdl2ext.SDLError:
2023
raise pytest.skip('Video subsystem not supported')
21-
assert SDL_WasInit(SDL_INIT_VIDEO) == SDL_INIT_VIDEO
24+
assert SDL_WasInit(sdl2.SDL_INIT_VIDEO) == sdl2.SDL_INIT_VIDEO
25+
assert SDL_WasInit(sdl2.SDL_INIT_EVENTS) == sdl2.SDL_INIT_EVENTS
2226
sdl2ext.quit()
23-
assert SDL_WasInit(SDL_INIT_VIDEO) != SDL_INIT_VIDEO
27+
assert SDL_WasInit(sdl2.SDL_INIT_VIDEO) != sdl2.SDL_INIT_VIDEO
2428
sdl2ext.init()
2529
sdl2ext.init()
2630
sdl2ext.init()
27-
assert SDL_WasInit(SDL_INIT_VIDEO) == SDL_INIT_VIDEO
31+
assert SDL_WasInit(sdl2.SDL_INIT_VIDEO) == sdl2.SDL_INIT_VIDEO
32+
sdl2ext.quit()
33+
assert SDL_WasInit(sdl2.SDL_INIT_VIDEO) != sdl2.SDL_INIT_VIDEO
34+
35+
# Test initializing other subsystems
36+
sdl2ext.init(video=False, events=True)
37+
assert SDL_WasInit(sdl2.SDL_INIT_VIDEO) != sdl2.SDL_INIT_VIDEO
38+
assert SDL_WasInit(sdl2.SDL_INIT_EVENTS) == sdl2.SDL_INIT_EVENTS
39+
sdl2ext.init(video=True, audio=True, timer=True)
40+
assert SDL_WasInit(sdl2.SDL_INIT_VIDEO) == sdl2.SDL_INIT_VIDEO
41+
assert SDL_WasInit(sdl2.SDL_INIT_AUDIO) == sdl2.SDL_INIT_AUDIO
42+
assert SDL_WasInit(sdl2.SDL_INIT_TIMER) == sdl2.SDL_INIT_TIMER
43+
sdl2ext.init(joystick=True, haptic=True)
44+
assert SDL_WasInit(sdl2.SDL_INIT_VIDEO) == sdl2.SDL_INIT_VIDEO
45+
assert SDL_WasInit(sdl2.SDL_INIT_JOYSTICK) == sdl2.SDL_INIT_JOYSTICK
46+
assert SDL_WasInit(sdl2.SDL_INIT_HAPTIC) == sdl2.SDL_INIT_HAPTIC
47+
assert SDL_WasInit(sdl2.SDL_INIT_GAMECONTROLLER) != sdl2.SDL_INIT_GAMECONTROLLER
48+
sdl2ext.init(controller=True)
49+
assert SDL_WasInit(sdl2.SDL_INIT_GAMECONTROLLER) == sdl2.SDL_INIT_GAMECONTROLLER
50+
if sdl2.dll.version < 2009:
51+
with pytest.raises(RuntimeError):
52+
sdl2ext.init(sensor=True)
53+
else:
54+
sdl2ext.init(sensor=True)
55+
assert SDL_WasInit(sdl2.SDL_INIT_SENSOR) == sdl2.SDL_INIT_SENSOR
2856
sdl2ext.quit()
29-
assert SDL_WasInit(SDL_INIT_VIDEO) != SDL_INIT_VIDEO
3057

3158
def test_get_events(with_sdl_ext):
3259
SDL_FlushEvent(SDL_FIRSTEVENT, SDL_LASTEVENT)

0 commit comments

Comments
 (0)