|
1 | 1 | 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 | +) |
4 | 7 |
|
5 | 8 | _HASSDLTTF = True |
6 | 9 | try: |
|
16 | 19 | __all__ = ["SDLError", "init", "quit", "get_events", "TestEventProcessor"] |
17 | 20 |
|
18 | 21 |
|
| 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 | + |
19 | 34 | class SDLError(Exception): |
20 | 35 | """An SDL2-specific exception class.""" |
21 | 36 |
|
@@ -48,19 +63,71 @@ def raise_sdl_err(desc=None): |
48 | 63 | raise SDLError(e) |
49 | 64 |
|
50 | 65 |
|
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. |
53 | 96 |
|
54 | 97 | See :ref:`pygamers_pygame` for a comparison between this function and |
55 | 98 | ``pygame.init()``. |
56 | 99 |
|
57 | 100 | Raises: |
58 | | - :exc:`SDLError`: If the SDL2 video subsystem cannot be initialized. |
| 101 | + :exc:`SDLError`: If a requested SDL subsystem cannot be initialized. |
59 | 102 |
|
60 | 103 | """ |
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)) |
64 | 131 |
|
65 | 132 |
|
66 | 133 | def quit(): |
|
0 commit comments