Add menu audio controls and menu music

This commit is contained in:
John Doe
2026-05-07 20:35:15 +02:00
parent a908b50019
commit 2367f4fb1c
10 changed files with 348 additions and 25 deletions
+30 -2
View File
@@ -128,6 +128,7 @@ class GameWindow:
self.audio_devs["base"] = sdl2.SDL_OpenAudioDevice(None, 0, audio_spec, None, 0)
self.audio_devs["effects"] = sdl2.SDL_OpenAudioDevice(None, 0, audio_spec, None, 0)
self.audio_devs["music"] = sdl2.SDL_OpenAudioDevice(None, 0, audio_spec, None, 0)
self.sound_volume = sdl2.SDL_MIX_MAXVOLUME
self.music_enabled = False
self.music_volume = 128
self.music_track = None
@@ -149,12 +150,20 @@ class GameWindow:
self.music_enabled = True
sdlmixer.Mix_VolumeMusic(self.music_volume)
def set_volume(self, volume_percent):
def set_sound_volume(self, volume_percent):
clamped = max(0, min(int(volume_percent), 100))
self.sound_volume = int(round(clamped * sdl2.SDL_MIX_MAXVOLUME / 100))
def set_music_volume(self, volume_percent):
clamped = max(0, min(int(volume_percent), 100))
self.music_volume = int(round(clamped * 128 / 100))
if self.music_enabled and sdlmixer is not None:
sdlmixer.Mix_VolumeMusic(self.music_volume)
def set_volume(self, volume_percent):
self.set_sound_volume(volume_percent)
self.set_music_volume(volume_percent)
def play_music(self, music_file, loop=True):
if not self.audio or not self.music_enabled or sdlmixer is None:
return False
@@ -498,12 +507,31 @@ class GameWindow:
spec = SDL_AudioSpec(freq=22050, aformat=sdl2.AUDIO_U8, channels=1, samples=2048)
if sdl2.SDL_LoadWAV_RW(rw, 1, byref(spec), byref(_buf), byref(_length)) == None:
raise RuntimeError("Failed to load WAV")
if self.sound_volume <= 0:
sdl2.SDL_FreeWAV(_buf)
return
devid = self.audio_devs[tag]
# Clear any queued audio
sdl2.SDL_ClearQueuedAudio(devid)
buffer_length = int(_length.value)
if self.sound_volume < sdl2.SDL_MIX_MAXVOLUME:
scaled_buffer = create_string_buffer(buffer_length)
sdl2.SDL_MixAudioFormat(
cast(scaled_buffer, POINTER(sdl2.Uint8)),
_buf,
spec.format,
buffer_length,
self.sound_volume,
)
sdl2.SDL_QueueAudio(devid, cast(scaled_buffer, POINTER(sdl2.Uint8)), buffer_length)
else:
sdl2.SDL_QueueAudio(devid, _buf, buffer_length)
sdl2.SDL_FreeWAV(_buf)
# Start playing audio
sdl2.SDL_QueueAudio(devid, _buf, _length)
sdl2.SDL_PauseAudioDevice(devid, 0)
def stop_sound(self):