Created
August 28, 2012 10:40
-
-
Save armornick/3497064 to your computer and use it in GitHub Desktop.
Using SDL_Mixer with SDL2
This file contains 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
#include <SDL2/SDL.h> | |
#include <SDL2/SDL_mixer.h> | |
#define WAV_PATH "Roland-GR-1-Trumpet-C5.wav" | |
#define MUS_PATH "HR2_Friska.ogg" | |
// Our wave file | |
Mix_Chunk *wave = NULL; | |
// Our music file | |
Mix_Music *music = NULL; | |
int main(int argc, char* argv[]){ | |
// Initialize SDL. | |
if (SDL_Init(SDL_INIT_AUDIO) < 0) | |
return -1; | |
//Initialize SDL_mixer | |
if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 ) | |
return -1; | |
// Load our sound effect | |
wave = Mix_LoadWAV(WAV_PATH); | |
if (wave == NULL) | |
return -1; | |
// Load our music | |
music = Mix_LoadMUS(MUS_PATH); | |
if (music == NULL) | |
return -1; | |
if ( Mix_PlayChannel(-1, wave, 0) == -1 ) | |
return -1; | |
if ( Mix_PlayMusic( music, -1) == -1 ) | |
return -1; | |
while ( Mix_PlayingMusic() ) ; | |
// clean up our resources | |
Mix_FreeChunk(wave); | |
Mix_FreeMusic(music); | |
// quit SDL_mixer | |
Mix_CloseAudio(); | |
return 0; | |
} |
recommend also calling Mix_Quit();
after Mix_CloseAudio();
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the example !