Skip to content

Commit 8cf3aa5

Browse files
committed
Update examples.
1 parent e651ae6 commit 8cf3aa5

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

example.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,25 @@ def main(argc: ctypes.c_int, argv: sdl3.LP_c_char_p) -> ctypes.c_int:
2525
audioDrivers = [sdl3.SDL_GetAudioDriver(i).decode() for i in range(sdl3.SDL_GetNumAudioDrivers())]
2626
print(f"Available audio drivers: {', '.join(audioDrivers)} (current: {sdl3.SDL_GetCurrentAudioDriver().decode()}).")
2727

28-
if currentAudioDevice := sdl3.SDL_OpenAudioDevice(sdl3.SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, None):
29-
sdl3.Mix_Init(sdl3.MIX_INIT_WAVPACK)
30-
sdl3.Mix_OpenAudio(currentAudioDevice, ctypes.byref(audioSpec := sdl3.SDL_AudioSpec()))
31-
print(f"Current audio device: {sdl3.SDL_GetAudioDeviceName(currentAudioDevice).decode()}.")
32-
chunks = [sdl3.Mix_LoadWAV(f"res/voice/{i}".encode()) for i in os.listdir("res/voice")]
33-
currentIndex, channel = 0, 0
28+
if not sdl3.MIX_Init():
29+
print(f"Failed to initialize mixer: {sdl3.SDL_GetError().decode()}.")
30+
return -1
31+
32+
if mixer := sdl3.MIX_CreateMixerDevice(audioDevice := sdl3.SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, None):
33+
# print(f"Current audio device: {sdl3.SDL_GetAudioDeviceName(audioDevice).decode()}.")
34+
track, audios, index = sdl3.MIX_CreateTrack(mixer), [], -1
35+
36+
for file in os.listdir("res/voice"):
37+
if audio := sdl3.MIX_LoadAudio(mixer, f"res/voice/{file}".encode(), False):
38+
audios.append(audio)
39+
40+
else:
41+
print(f"Failed to load audio: {sdl3.SDL_GetError().decode()}.")
42+
return -1
3443

3544
else:
36-
print(f"Failed to open audio device: {sdl3.SDL_GetAudioDeviceName(sdl3.SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK).decode()}, error: {sdl3.SDL_GetError().decode()}.")
45+
print(f"Failed to create mixer device: {sdl3.SDL_GetError().decode()}.")
46+
return -1
3747

3848
surface = sdl3.IMG_Load("res/example.png".encode())
3949
texture = sdl3.SDL_CreateTextureFromSurface(renderer, surface)
@@ -69,9 +79,10 @@ def main(argc: ctypes.c_int, argv: sdl3.LP_c_char_p) -> ctypes.c_int:
6979
frect.w, frect.h = frect.w * width.value / frect.w * scale, frect.h * width.value / frect.w * scale
7080
frect.x, frect.y = width.value / 2 - frect.w / 2, height.value / 2 - frect.h / 2
7181

72-
if currentAudioDevice and not sdl3.Mix_Playing(channel):
73-
channel = sdl3.Mix_PlayChannel(-1, chunks[currentIndex], 1)
74-
if (currentIndex := currentIndex + 1) >= len(chunks): currentIndex = 0
82+
if not sdl3.MIX_TrackPlaying(track):
83+
index = (index + 1) % len(audios)
84+
sdl3.MIX_SetTrackAudio(track, audios[index])
85+
sdl3.MIX_PlayTrack(track, 0)
7586

7687
lastTime, deltaTime = time.time(), time.time() - lastTime
7788
hue, frames = (hue + 0.5 * deltaTime) % 1.0, frames + 1.0
@@ -108,12 +119,12 @@ def main(argc: ctypes.c_int, argv: sdl3.LP_c_char_p) -> ctypes.c_int:
108119
sdl3.SDL_DestroySurface(textSurface)
109120
sdl3.SDL_DestroyTexture(textTexture)
110121

111-
if currentAudioDevice:
112-
for i in chunks:
113-
sdl3.Mix_FreeChunk(i)
122+
for audio in audios:
123+
sdl3.MIX_DestroyAudio(audio)
114124

115-
sdl3.Mix_CloseAudio()
116-
sdl3.Mix_Quit()
125+
sdl3.MIX_DestroyTrack(track)
126+
sdl3.MIX_DestroyMixer(mixer)
127+
sdl3.MIX_Quit()
117128

118129
sdl3.TTF_CloseFont(font)
119130
sdl3.TTF_Quit()

gpu.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ def main(argc: ctypes.c_int, argv: sdl3.LP_c_char_p) -> ctypes.c_int:
6666
transferBuffer = sdl3.SDL_CreateGPUTransferBuffer(device, sdl3.SDL_GPUTransferBufferCreateInfo(sdl3.SDL_GPU_TRANSFERBUFFERUSAGE_UPLOAD, ctypes.sizeof(Vertex) * 3))
6767

6868
transferData = ctypes.cast(sdl3.SDL_MapGPUTransferBuffer(device, transferBuffer, False), sdl3.SDL_POINTER[Vertex])
69-
transferData[0] = Vertex(sdl3.SDL_ARRAY(-0.5, -0.5, 0.0, type = ctypes.c_float)[0])
70-
transferData[1] = Vertex(sdl3.SDL_ARRAY( 0.5, -0.5, 0.0, type = ctypes.c_float)[0])
71-
transferData[2] = Vertex(sdl3.SDL_ARRAY( 0.0, 0.5, 0.0, type = ctypes.c_float)[0])
69+
transferData[0] = Vertex(sdl3.SDL_ARRAY(-0.5, -0.5, 0.0, _type = ctypes.c_float)[0])
70+
transferData[1] = Vertex(sdl3.SDL_ARRAY( 0.5, -0.5, 0.0, _type = ctypes.c_float)[0])
71+
transferData[2] = Vertex(sdl3.SDL_ARRAY( 0.0, 0.5, 0.0, _type = ctypes.c_float)[0])
7272
sdl3.SDL_UnmapGPUTransferBuffer(device, transferBuffer)
7373

7474
commandBuffer = sdl3.SDL_AcquireGPUCommandBuffer(device)
@@ -110,9 +110,9 @@ def main(argc: ctypes.c_int, argv: sdl3.LP_c_char_p) -> ctypes.c_int:
110110
colorTargetInfo = sdl3.SDL_GPUColorTargetInfo(swapChainTexture, load_op = sdl3.SDL_GPU_LOADOP_CLEAR,
111111
clear_color = sdl3.SDL_FColor(*colorsys.hsv_to_rgb(hue := (hue + 0.25 * deltaTime) % 1, 1.0, 0.0), 1.0))
112112

113-
uniformData.color0 = sdl3.SDL_ARRAY(*colorsys.hsv_to_rgb(hue + 0.48, 1.0, 1.0), 1.0, type = ctypes.c_float)[0]
114-
uniformData.color1 = sdl3.SDL_ARRAY(*colorsys.hsv_to_rgb(hue + 0.32, 1.0, 1.0), 1.0, type = ctypes.c_float)[0]
115-
uniformData.color2 = sdl3.SDL_ARRAY(*colorsys.hsv_to_rgb(hue + 0.64, 1.0, 1.0), 1.0, type = ctypes.c_float)[0]
113+
uniformData.color0 = sdl3.SDL_ARRAY(*colorsys.hsv_to_rgb(hue + 0.48, 1.0, 1.0), 1.0, _type = ctypes.c_float)[0]
114+
uniformData.color1 = sdl3.SDL_ARRAY(*colorsys.hsv_to_rgb(hue + 0.32, 1.0, 1.0), 1.0, _type = ctypes.c_float)[0]
115+
uniformData.color2 = sdl3.SDL_ARRAY(*colorsys.hsv_to_rgb(hue + 0.64, 1.0, 1.0), 1.0, _type = ctypes.c_float)[0]
116116

117117
renderPass = sdl3.SDL_BeginGPURenderPass(commandBuffer, ctypes.byref(colorTargetInfo), 1, None)
118118
sdl3.SDL_BindGPUGraphicsPipeline(renderPass, pipeline)

shader.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
0x38, 0x00, 0x00, 0x00, 0x35, 0x00, 0x00, 0x00, 0x36, 0x00, 0x00, 0x00, 0x37, 0x00, 0x00, 0x00,
114114
0x34, 0x00, 0x00, 0x00, 0x41, 0x00, 0x05, 0x00, 0x08, 0x00, 0x00, 0x00, 0x39, 0x00, 0x00, 0x00,
115115
0x2f, 0x00, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x03, 0x00, 0x39, 0x00, 0x00, 0x00,
116-
0x38, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00, type = ctypes.c_uint8
116+
0x38, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00, _type = ctypes.c_uint8
117117
)[::-1]
118118

119119
fragmentData = sdl3.SDL_ARRAY(
@@ -145,5 +145,5 @@
145145
0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xf8, 0x00, 0x02, 0x00, 0x05, 0x00, 0x00, 0x00,
146146
0x3d, 0x00, 0x04, 0x00, 0x07, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
147147
0x3e, 0x00, 0x03, 0x00, 0x09, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x01, 0x00,
148-
0x38, 0x00, 0x01, 0x00, type = ctypes.c_uint8
148+
0x38, 0x00, 0x01, 0x00, _type = ctypes.c_uint8
149149
)[::-1]

0 commit comments

Comments
 (0)