-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathtest_sdl3_mixer.c
More file actions
81 lines (64 loc) · 1.87 KB
/
test_sdl3_mixer.c
File metadata and controls
81 lines (64 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Copyright 2025 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/
#include <stdio.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
#include <SDL3_mixer/SDL_mixer.h>
#include <emscripten.h>
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
MIX_Audio *audio = NULL;
MIX_Track *track = NULL;
MIX_Mixer *mixer = NULL;
#define WIDTH 640
#define HEIGHT 480
#ifndef SOUND_PATH
#error "must define SOUND_PATH"
#endif
void sound_loop_then_quit() {
if (MIX_TrackPlaying(track))
return;
MIX_DestroyAudio(audio);
MIX_DestroyTrack(track);
MIX_DestroyMixer(mixer);
emscripten_cancel_main_loop();
printf("Shutting down\n");
exit(0);
}
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_VIDEO);
if (!MIX_Init()) {
printf("MIX_Init failed: %s\n", SDL_GetError());
return 1;
}
if (!SDL_CreateWindowAndRenderer("SDL3 MIXER", WIDTH, HEIGHT, 0, &window, &renderer)) {
printf("SDL_CreateWindowAndRenderer: %s\n", SDL_GetError());
return 1;
}
mixer = MIX_CreateMixerDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, NULL);
if (!mixer) {
printf("Couldn't create mixer on default device: %s", SDL_GetError());
return 1;
}
audio = MIX_LoadAudio(mixer, SOUND_PATH, false);
if (!audio) {
printf("MIX_LoadAudio: %s\n", SDL_GetError());
return 1;
}
track = MIX_CreateTrack(mixer);
if (!track) {
printf("MIX_CreateTrack: %s\n", SDL_GetError());
return 1;
}
MIX_SetTrackAudio(track, audio);
SDL_PropertiesID props = SDL_CreateProperties();
SDL_SetNumberProperty(props, MIX_PROP_PLAY_LOOPS_NUMBER, 0);
printf("Starting sound play loop\n");
MIX_PlayTrack(track, props);
emscripten_set_main_loop(sound_loop_then_quit, 0, 1);
return 0;
}