Skip to content

Commit 36d7618

Browse files
committed
Change SDL2 inits to subsystems to avoid init/quit issues
1 parent 442d696 commit 36d7618

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

pyboy/plugins/debug.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from pyboy.api.tilemap import TileMap
1515
from pyboy.plugins.base_plugin import PyBoyWindowPlugin
1616
from pyboy.plugins.window_sdl2 import sdl2_event_pump
17-
from pyboy.utils import WindowEvent
17+
from pyboy.utils import WindowEvent, PyBoyAssertException
1818

1919
try:
2020
import sdl2
@@ -78,8 +78,8 @@ def __init__(self, pyboy, mb, pyboy_argv):
7878
return
7979

8080
self.sdl2_event_pump = self.pyboy_argv.get("window") != "SDL2"
81-
if self.sdl2_event_pump:
82-
sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO)
81+
if self.sdl2_event_pump and sdl2.SDL_InitSubSystem(sdl2.SDL_INIT_VIDEO) < 0:
82+
raise PyBoyAssertException(f"SDL_InitSubSystem video failed: {sdl2.SDL_GetError().decode()}")
8383

8484
# self.scale = 2
8585
window_pos = 0
@@ -210,7 +210,7 @@ def stop(self):
210210
for _ in range(3): # At least 2 to close
211211
get_events()
212212
time.sleep(0.1)
213-
sdl2.SDL_Quit()
213+
sdl2.SDL_QuitSubSystem(sdl2.SDL_INIT_VIDEO)
214214

215215
def enabled(self):
216216
if self.pyboy_argv.get("debug"):

pyboy/plugins/window_sdl2.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ctypes import POINTER, c_ubyte, c_void_p, cast
99

1010
from pyboy.plugins.base_plugin import PyBoyWindowPlugin
11-
from pyboy.utils import WindowEvent, WindowEventMouse, cython_compiled
11+
from pyboy.utils import WindowEvent, WindowEventMouse, cython_compiled, PyBoyAssertException
1212

1313
try:
1414
import sdl2
@@ -162,7 +162,9 @@ def __init__(self, pyboy, mb, pyboy_argv):
162162

163163
if not self.enabled():
164164
return
165-
assert sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_GAMECONTROLLER) >= 0
165+
166+
if sdl2.SDL_InitSubSystem(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_GAMECONTROLLER) < 0:
167+
raise PyBoyAssertException("SDL_InitSubSystem video failed: %s", sdl2.SDL_GetError().decode())
166168

167169
self._window = sdl2.SDL_CreateWindow(
168170
b"PyBoy",
@@ -188,7 +190,7 @@ def __init__(self, pyboy, mb, pyboy_argv):
188190

189191
def init_audio(self, mb):
190192
if mb.sound.emulate:
191-
if sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO) >= 0:
193+
if sdl2.SDL_InitSubSystem(sdl2.SDL_INIT_AUDIO) >= 0:
192194
# NOTE: We have to keep spec variables alive to avoid segfault
193195
self.spec_want = sdl2.SDL_AudioSpec(self.mb.sound.sample_rate, sdl2.AUDIO_S8, 2, 128)
194196
self.spec_have = sdl2.SDL_AudioSpec(0, 0, 0, 0)
@@ -217,7 +219,7 @@ def init_audio(self, mb):
217219
logger.warning("SDL_OpenAudioDevice failed: %s", sdl2.SDL_GetError().decode())
218220
else:
219221
self.sound_support = False
220-
logger.warning("SDL_Init audio failed: %s", sdl2.SDL_GetError().decode())
222+
logger.warning("SDL_InitSubSystem audio failed: %s", sdl2.SDL_GetError().decode())
221223
else:
222224
self.sound_support = False
223225

@@ -303,4 +305,4 @@ def stop(self):
303305
for _ in range(3): # At least 2 to close
304306
get_events()
305307
time.sleep(0.1)
306-
sdl2.SDL_Quit()
308+
sdl2.SDL_QuitSubSystem(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_GAMECONTROLLER)

0 commit comments

Comments
 (0)