Add background music

This commit is contained in:
Mylloon 2022-05-17 22:02:35 +02:00
parent d8608bc7b1
commit 2befbd4a69
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 48 additions and 1 deletions

View file

@ -49,7 +49,7 @@ else
endif
CPPFLAGS += $(shell sdl2-config --cflags)
LDFLAGS += -lGL4Dummies $(shell sdl2-config --libs)
LDFLAGS += -lGL4Dummies $(shell sdl2-config --libs) -lSDL2_mixer
%.o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

14
includes/audio.h Normal file
View file

@ -0,0 +1,14 @@
/* TP7 */
#ifndef DEMO_AUDIO_HPP
#define DEMO_AUDIO_HPP 1
#include <SDL_mixer.h>
// Charge et lance la musique
void initialisationMusique(Mix_Music *, const char *);
// Libère la musique de la mémoire
void liberationMusique(Mix_Music *);
#endif

26
src/audio.c Normal file
View file

@ -0,0 +1,26 @@
/* TP7 */
#include "../includes/audio.h"
void initialisationMusique(Mix_Music *musique, const char *filename) {
Mix_Init(MIX_INIT_OGG);
Mix_OpenAudio(44100, AUDIO_S16LSB, 2, 1024);
musique = Mix_LoadMUS(filename);
Mix_VolumeMusic(MIX_MAX_VOLUME / 10);
Mix_PlayMusic(musique, 1);
}
void liberationMusique(Mix_Music *musique) {
if(musique) {
if(Mix_PlayingMusic()) {
Mix_HaltMusic();
}
Mix_FreeMusic(musique);
musique = NULL;
}
Mix_CloseAudio();
Mix_Quit();
}

View file

@ -1,9 +1,14 @@
#include <GL4D/gl4duw_SDL2.h>
#include "includes/animations.h"
#include "includes/audio.h"
static Mix_Music * ambiance = NULL;
// Comportement à la fermeture du programme
void fermeture(void) {
liberationMusique(ambiance);
gl4duClean(GL4DU_ALL);
}
@ -23,6 +28,8 @@ int main(int argc, char *argv[]) {
{ 0, NULL, NULL, NULL }
};
initialisationMusique(ambiance, "audio/ambiance.ogg");
gl4dhInit(animations, dimensions[0], dimensions[1], NULL);
atexit(fermeture);