Merge branch 'master' of https://gitlab.iut-clermont.uca.fr/maribemont/projet-tut
commit
7dd47dcc0d
@ -0,0 +1,7 @@
|
||||
#include "tests/testAudio.h"
|
||||
|
||||
int main(void) {
|
||||
testAudio();
|
||||
return 0;
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
#include "engine/Audio.h"
|
||||
#include <SDL2/SDL_mixer.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
|
||||
void testAudio(void);
|
||||
|
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* \file Audio.h
|
||||
* \brief Audio management
|
||||
* \author Théotime Maillarbaux
|
||||
* \date 13/12/2021
|
||||
*/
|
||||
|
||||
#ifndef AUDIO_H
|
||||
#define AUDIO_H
|
||||
|
||||
#include <SDL2/SDL_mixer.h>
|
||||
#include <SDL2/SDL_thread>
|
||||
|
||||
/**
|
||||
* \brief Fades out a music and plays another one.
|
||||
* \param[in] music A pointer to the Mix_Music struct used to play the current track
|
||||
* \param[in] path The path to the new track to be played
|
||||
* \warning The program won't know if it failed.
|
||||
*
|
||||
* This function creates a thread that will detach on its own completion.
|
||||
*/
|
||||
void switchMusic(Mix_Music* music, char[] path);
|
||||
|
||||
#endif // AUDIO_H
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,28 @@
|
||||
#include "engine/Audio.h"
|
||||
|
||||
struct params {
|
||||
Mix_Music* music; // AA pointer to the Mix_Music struct used for the current track
|
||||
char[] path; // The pparh to the new track to be played
|
||||
};
|
||||
|
||||
static int fadeOut(void* args) {
|
||||
if (args->music != NULL) {
|
||||
Mix_FadeOutMusic(1000);
|
||||
Mix_FreeMusic(args->music);
|
||||
}
|
||||
args->music = Mix_LoadMUS(args->path);
|
||||
return Mix_PlayMusic(args->music,-1);
|
||||
}
|
||||
|
||||
void switchMusic(Mix_Music* music, char[] path) {
|
||||
struct params args = {
|
||||
.music = music,
|
||||
.path = path;
|
||||
};
|
||||
|
||||
SDL_Thread *thread;
|
||||
|
||||
thread = SDL_CreateThread(fadeOut, "Fade out", args);
|
||||
SDL_DetachThread(thread); // Won't wait until thread is done to continue
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
#include "test/testAudio.h"
|
||||
|
||||
void testAudio(void) {
|
||||
char path1[] = "../rsrc/music/testMus.mp3";
|
||||
char path2[] = "../rsrc/music/base_tardi.mp3";
|
||||
Mix_Music* music;
|
||||
|
||||
if (SDL_Init(SDL_INIT_AUDIO) != 0) {
|
||||
perror("SDL");
|
||||
exit(errno);
|
||||
}
|
||||
|
||||
if (Mix_OpenAudio(44100,MIX_DEFAULT_FORMAT,MIX_DEFAULT_CHANNEL,1024) != 0) {
|
||||
SDL_Quit();
|
||||
perror("SDL Mixer");
|
||||
exit(errno);
|
||||
}
|
||||
|
||||
switchMusic(music,path1);
|
||||
SDL_Delay(5000);
|
||||
switchMusic(music,path2);
|
||||
SDL_Delay(5000);
|
||||
|
||||
SDL_FreeMusic(music);
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in new issue