Created
November 23, 2024 12:52
-
-
Save 8Observer8/7133520ecc2db970c7043a14e74ca148 to your computer and use it in GitHub Desktop.
Play sound effect my mouse click using SDL3_mixer and C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* This example code $WHAT_IT_DOES. | |
* | |
* This code is public domain. Feel free to use it for any purpose! | |
*/ | |
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ | |
#include <SDL3/SDL.h> | |
#include <SDL3/SDL_main.h> | |
#include <SDL3_mixer/SDL_mixer.h> | |
/* We will use this renderer to draw into this window every frame. */ | |
static SDL_Window *window = NULL; | |
static SDL_Renderer *renderer = NULL; | |
static Mix_Chunk *g_wave = NULL; | |
/* This function runs once at startup. */ | |
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | |
{ | |
SDL_SetAppMetadata("Example HUMAN READABLE NAME", "1.0", "com.example.CATEGORY-NAME"); | |
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) { | |
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); | |
return SDL_APP_FAILURE; | |
} | |
if (!SDL_CreateWindowAndRenderer("examples/CATEGORY/NAME", 300, 300, 0, &window, &renderer)) { | |
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | |
return SDL_APP_FAILURE; | |
} | |
SDL_AudioSpec spec; | |
spec.freq = MIX_DEFAULT_FREQUENCY; | |
spec.format = MIX_DEFAULT_FORMAT; | |
spec.channels = MIX_DEFAULT_CHANNELS; | |
// Open the audio device | |
if (!Mix_OpenAudio(0, &spec)) { | |
SDL_Log("Couldn't open audio: %s", SDL_GetError()); | |
return SDL_APP_FAILURE; | |
} | |
// Load the WAV file | |
const char *soundFilePath = "assets/audio/picked-coin-echo-2.wav"; | |
g_wave = Mix_LoadWAV(soundFilePath); | |
if (g_wave == NULL) { | |
SDL_Log("Couldn't load WAV file %s: %s", soundFilePath, SDL_GetError()); | |
return SDL_APP_FAILURE; | |
} | |
return SDL_APP_CONTINUE; /* carry on with the program! */ | |
} | |
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ | |
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | |
{ | |
if (event->type == SDL_EVENT_QUIT) { | |
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ | |
} else if (event->type == SDL_EVENT_MOUSE_BUTTON_DOWN) { | |
// Play the sound effect | |
Mix_PlayChannel(-1, g_wave, 0); | |
} | |
return SDL_APP_CONTINUE; /* carry on with the program! */ | |
} | |
/* This function runs once per frame, and is the heart of the program. */ | |
SDL_AppResult SDL_AppIterate(void *appstate) | |
{ | |
// Clear the screen | |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | |
SDL_RenderClear(renderer); | |
// Update the screen | |
SDL_RenderPresent(renderer); | |
return SDL_APP_CONTINUE; /* carry on with the program! */ | |
} | |
/* This function runs once at shutdown. */ | |
void SDL_AppQuit(void *appstate, SDL_AppResult result) | |
{ | |
/* SDL will clean up the window/renderer for us. */ | |
if (g_wave) { | |
Mix_FreeChunk(g_wave); | |
g_wave = NULL; | |
} | |
SDL_Quit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment