Using Agar with SDL Mixer

From Agar

Jump to: navigation, search

Agar works with the sound library SDL_Mixer without a hitch. Call these example functions after Agar has been initialized. Use these functions to open and close the audio device:

#include <stdbool.h>
#include <SDL.h>
#include <SDL_mixer.h>
 
bool inited = 0;
int audio_rate = 22050;
int audio_channels = 2;
int audio_buffers = 1024;
Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
int mixer_channels = 128;
 
void
init_audio()
{
	if (inited) {
		close_audio();
	}
	inited = 1;
 
	/*
	 * This is where we open up our audio device.  Mix_OpenAudio
	 * takes as its parameters the audio format we'd /like/ to
	 * have.
	 */
	Mix_OpenAudio(audio_rate, audio_format, audio_channels,
	    audio_buffers);
 
	/*
	 * If we actually care about what we got, we can ask here.  In this
	 * program we don't, but I'm showing the function call here anyway
	 * in case we'd want to know later.
	 */
	Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
 
	mixer_channels = Mix_AllocateChannels(mixer_channels);
 
	/* Load sounds directory ... */
}
 
/* This is the cleaning up part */
void
close_audio()
{
	if (inited)
		Mix_CloseAudio();
}

Elsewhere you may wish to attach an event that plays a sound too, in this example, a button press. Since Agar triggers the AG_Event by default after the mouse button is released, this is how we can use an AG_Event function to trigger percussively (on mouse down):

void
PlaySample(AG_Event *event)
{
	Mix_Chunk *sample = AG_PTR(1);
	int loop = AG_INT(2);
	int channel;
 
	if (!inited) {
		return;
	}
 
	channel = Mix_PlayChannel(-1, sample, loop);
 
	/* Unreg fx, set default panning/position. */
	Mix_SetPanning(channel, 255,255);
	Mix_SetPosition( channel, 0,0);
}
 
AG_Button *btn;
Mix_Chunk *mySample;
 
...
 
btn = AG_ButtonNewFn(window, 0, "Play sound once",
    PlaySample, "%p,%i", mySample, 0);
btn = AG_ButtonNewFn(window, 0, "Play sound twice",
    PlaySample, "%p,%i", mySample, 1);

SEE ALSO

Personal tools