Debian Codegraph @

examples/basics/02-play-with-options/play-with-options.c

raw symbols SCIP
1 /*
2 * This example code creates a mixer, loads a single sound, and plays it, with
3 * several playback options (fade-in, loop, etc).
4 *
5 * This code is public domain. Feel free to use it for any purpose!
6 */
7
8 #define SDL_MAIN_USE_CALLBACKScxx . . $ `examples/basics/02-play-with-options/play-with-options.c:8:9`! 1 /* use the callbacks instead of main() */
9 #include <SDL3/SDL.h>
10 #include <SDL3/SDL_main.h>
11 #include <SDL3_mixer/SDL_mixer.h>cxx . . $ `<file>/include/SDL3_mixer/SDL_mixer.h`/
12
13 /* We will use this renderer to draw into this window every frame. */
14 static SDL_Windowcxx . . $ SDL_Window# *windowcxx . . $ window. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
15 static SDL_Renderercxx . . $ SDL_Renderer# *renderercxx . . $ renderer. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
16 static MIX_Mixercxx . . $ MIX_Mixer# *mixercxx . . $ mixer. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
17 static MIX_Trackcxx . . $ MIX_Track# *trackcxx . . $ track. = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
18
19 /* This function runs once at startup. */
20 SDL_AppResultcxx . . $ SDL_AppResult# SDL_AppInitcxx . . $ SDL_AppInit(35844efa3d58b42c).(void **appstate, int argc, char *argv[])
21 {
22 char *path = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
23 MIX_Audiocxx . . $ MIX_Audio# *audio = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
24 SDL_PropertiesIDcxx . . $ SDL_PropertiesID# options = 0;
25
26 SDL_SetAppMetadatacxx . . $ SDL_SetAppMetadata(709085d428d014ac).("Example Play With Options", "1.0", "com.example.play-with-options");
27
28 /* this doesn't have to run very much, so give up tons of CPU time between iterations. Optional! */
29 SDL_SetHintcxx . . $ SDL_SetHint(b7a7aa4a824b7595).(SDL_HINT_MAIN_CALLBACK_RATEcxx . . $ `<external>/12780769334912691790/SDL_hints.h:2733:9`!, "5");
30
31 /* we don't need video, but we'll make a window for smooth operation. */
32 if (!SDL_Initcxx . . $ SDL_Init(dfa6e707f7696010).(SDL_INIT_VIDEOcxx . . $ `<external>/6225284867037711390/SDL_init.h:81:9`!)) {
33 SDL_Logcxx . . $ SDL_Log(326aeb464d16410c).("Couldn't initialize SDL: %s", SDL_GetErrorcxx . . $ SDL_GetError(33a1f659e94feb42).());
34 return SDL_APP_FAILUREcxx . . $ SDL_APP_FAILURE.;
35 }
36
37 if (!SDL_CreateWindowAndRenderercxx . . $ SDL_CreateWindowAndRenderer(6dc3b8f9fbd6ad9a).("examples/basic/play-with-options", 640, 480, SDL_WINDOW_RESIZABLEcxx . . $ `<external>/7703876559639823225/SDL_video.h:202:9`!, &windowcxx . . $ window., &renderercxx . . $ renderer.)) {
38 SDL_Logcxx . . $ SDL_Log(326aeb464d16410c).("Couldn't create window/renderer: %s", SDL_GetErrorcxx . . $ SDL_GetError(33a1f659e94feb42).());
39 return SDL_APP_FAILUREcxx . . $ SDL_APP_FAILURE.;
40 }
41
42 if (!MIX_Initcxx . . $ MIX_Init(c9857a73d68e59ba).()) {
43 SDL_Logcxx . . $ SDL_Log(326aeb464d16410c).("Couldn't init SDL_mixer library: %s", SDL_GetErrorcxx . . $ SDL_GetError(33a1f659e94feb42).());
44 return SDL_APP_FAILUREcxx . . $ SDL_APP_FAILURE.;
45 }
46
47 /* Create a mixer on the default audio device. Don't care about the specific audio format. */
48 mixercxx . . $ mixer. = MIX_CreateMixerDevicecxx . . $ MIX_CreateMixerDevice(c3e886a66b8c9e8e).(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACKcxx . . $ `<external>/16333709846263140230/SDL_audio.h:385:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!);
49 if (!mixercxx . . $ mixer.) {
50 SDL_Logcxx . . $ SDL_Log(326aeb464d16410c).("Couldn't create mixer on default device: %s", SDL_GetErrorcxx . . $ SDL_GetError(33a1f659e94feb42).());
51 return SDL_APP_FAILUREcxx . . $ SDL_APP_FAILURE.;
52 }
53
54 /* load a sound file */
55 SDL_asprintfcxx . . $ SDL_asprintf(70ad09b5e10823c4).(&path, "%smusic.mp3", SDL_GetBasePathcxx . . $ SDL_GetBasePath(33a1f659e94feb42).()); /* allocate a string of the full file path */
56 audio = MIX_LoadAudiocxx . . $ MIX_LoadAudio(47fba3f94ea049bf).(mixercxx . . $ mixer., path, falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!);
57 if (!audio) {
58 SDL_Logcxx . . $ SDL_Log(326aeb464d16410c).("Couldn't load %s: %s", path, SDL_GetErrorcxx . . $ SDL_GetError(33a1f659e94feb42).());
59 SDL_freecxx . . $ SDL_free(5d22bdacc48458e8).(path);
60 return SDL_APP_FAILUREcxx . . $ SDL_APP_FAILURE.;
61 }
62
63 SDL_freecxx . . $ SDL_free(5d22bdacc48458e8).(path); /* done with this, the file is loaded. */
64
65 /* we need a track on the mixer to play the audio. Each track has audio assigned to it, and
66 all playing tracks are mixed together for the final output. */
67
68 trackcxx . . $ track. = MIX_CreateTrackcxx . . $ MIX_CreateTrack(ab49e1bd0ccce56d).(mixercxx . . $ mixer.);
69 if (!trackcxx . . $ track.) {
70 SDL_Logcxx . . $ SDL_Log(326aeb464d16410c).("Couldn't create a mixer track: %s", SDL_GetErrorcxx . . $ SDL_GetError(33a1f659e94feb42).());
71 return SDL_APP_FAILUREcxx . . $ SDL_APP_FAILURE.;
72 }
73 MIX_SetTrackAudiocxx . . $ MIX_SetTrackAudio(9e37ed25de210b67).(trackcxx . . $ track., audio);
74
75 /* start the audio playing! */
76 options = SDL_CreatePropertiescxx . . $ SDL_CreateProperties(db52760e79bf9054).();
77 if (!options) {
78 SDL_Logcxx . . $ SDL_Log(326aeb464d16410c).("Couldn't create play options: %s", SDL_GetErrorcxx . . $ SDL_GetError(33a1f659e94feb42).());
79 return SDL_APP_FAILUREcxx . . $ SDL_APP_FAILURE.;
80 }
81
82 /* note these all use MILLISECONDS, since the track is generic, but you can use equivalent FRAMES properties, for frame-perfect mixing. */
83 SDL_SetNumberPropertycxx . . $ SDL_SetNumberProperty(bd1d1fabf428684b).(options, MIX_PROP_PLAY_START_MILLISECOND_NUMBERcxx . . $ `include/SDL3_mixer/SDL_mixer.h:1928:9`!, 1000); /* start the first loop 1 second into the audio. */
84 SDL_SetNumberPropertycxx . . $ SDL_SetNumberProperty(bd1d1fabf428684b).(options, MIX_PROP_PLAY_MAX_MILLISECONDS_NUMBERcxx . . $ `include/SDL3_mixer/SDL_mixer.h:1926:9`!, 10000); /* play at most 10 seconds of audio before ending/looping (since we started 1000 in, this will be the eleventh second). */
85 SDL_SetNumberPropertycxx . . $ SDL_SetNumberProperty(bd1d1fabf428684b).(options, MIX_PROP_PLAY_LOOPS_NUMBERcxx . . $ `include/SDL3_mixer/SDL_mixer.h:1924:9`!, 2); /* loop 2 times, 3rd time through doesn't loop. */
86 SDL_SetNumberPropertycxx . . $ SDL_SetNumberProperty(bd1d1fabf428684b).(options, MIX_PROP_PLAY_LOOP_START_MILLISECOND_NUMBERcxx . . $ `include/SDL3_mixer/SDL_mixer.h:1931:9`!, 2000); /* when looping, start again here instead of the start of the track. */
87 SDL_SetNumberPropertycxx . . $ SDL_SetNumberProperty(bd1d1fabf428684b).(options, MIX_PROP_PLAY_FADE_IN_MILLISECONDS_NUMBERcxx . . $ `include/SDL3_mixer/SDL_mixer.h:1933:9`!, 5000); /* fade-in the first loop over five seconds. No fade on the loops, unless the initial fade is still in progress! */
88 MIX_PlayTrackcxx . . $ MIX_PlayTrack(7ace32b420803c).(trackcxx . . $ track., options);
89 SDL_DestroyPropertiescxx . . $ SDL_DestroyProperties(cb2f890c2fdad230).(options); /* MIX_PlayTrack makes a copy of the options, so this can go away. */
90
91 /* we don't save `audio`; SDL_mixer will clean it up for us during MIX_Quit(). */
92
93 return SDL_APP_CONTINUEcxx . . $ SDL_APP_CONTINUE.; /* carry on with the program! */
94 }
95
96 /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
97 SDL_AppResultcxx . . $ SDL_AppResult# SDL_AppEventcxx . . $ SDL_AppEvent(b49e8c0754c2b4d7).(void *appstate, SDL_Eventcxx . . $ SDL_Event# *event)
98 {
99 if (event->typecxx . . $ SDL_Event#type. == SDL_EVENT_QUITcxx . . $ SDL_EVENT_QUIT.) {
100 return SDL_APP_SUCCESScxx . . $ SDL_APP_SUCCESS.; /* end the program, reporting success to the OS. */
101 }
102 return SDL_APP_CONTINUEcxx . . $ SDL_APP_CONTINUE.; /* carry on with the program! */
103 }
104
105 /* This function runs once per frame, and is the heart of the program. */
106 SDL_AppResultcxx . . $ SDL_AppResult# SDL_AppIteratecxx . . $ SDL_AppIterate(142a44099349541e).(void *appstate)
107 {
108 /* draw a blank video frame to keep the OS happy */
109 SDL_RenderClearcxx . . $ SDL_RenderClear(4ede20f9234bd211).(renderercxx . . $ renderer.);
110 SDL_RenderPresentcxx . . $ SDL_RenderPresent(4ede20f9234bd211).(renderercxx . . $ renderer.);
111
112 /* when the track has finished playing, end the program. */
113 if (!MIX_TrackPlayingcxx . . $ MIX_TrackPlaying(e71ee6a29869b04f).(trackcxx . . $ track.)) {
114 return SDL_APP_SUCCESScxx . . $ SDL_APP_SUCCESS.;
115 }
116
117 return SDL_APP_CONTINUEcxx . . $ SDL_APP_CONTINUE.; /* carry on with the program! */
118 }
119
120 /* This function runs once at shutdown. */
121 void SDL_AppQuitcxx . . $ SDL_AppQuit(4db66cc3d193319b).(void *appstate, SDL_AppResultcxx . . $ SDL_AppResult# result)
122 {
123 /* SDL will clean up the window/renderer for us, MIX_Quit() destroys any mixer objects we made. */
124 MIX_Quitcxx . . $ MIX_Quit(49f6e7a06ebc5aa8).();
125 }
126