// BlinkGame - Hunt for the Lost Pixels
// an SDL game in honor of the BlinkenArea crew
//
// (C) 2005-06 Rene Schickbauer
//
// See License.txt for licensing information
//
#ifdef NOP_CODE
/*--------------------------------------------------------------------
CVS AUTO-GENERATED INFO
$Author: cavac $
$Date: 2006/03/02 13:03:43 $
$Name: rel-0-3-patches $
$Revision: 1.14 $
$State: Exp $

$Log: sound.cpp,v $
Revision 1.14  2006/03/02 13:03:43  cavac
Re-formatted all .cpp and .h files a bit to get rid of the TAB/SPACE mess
that makes troubles with XCode's "re-format" feature

Revision 1.13  2006/02/27 00:12:07  cavac
Reduced volume of background music

Revision 1.12  2006/02/24 20:25:39  cavac
Added sound-support to scripting engine

Revision 1.11  2006/02/17 19:07:59  cavac
Added rudimentary sound effects (FX) support

Revision 1.10  2006/02/17 10:49:42  cavac
Added error-handling

Revision 1.9  2006/02/10 18:56:17  cavac
Code Audit: Fixed many 64Bit based problems (mostly untested)

Revision 1.8  2006/02/10 17:50:26  cavac
Added ActionCapture feature (define ALLOW_ACTIONCAPTURE to allow this
							 resource-hungry extra)

Revision 1.7  2006/02/05 22:24:57  cavac
Added bool to sound.cpp to check for initSound()
ifdef'ed out calls to popt

Revision 1.6  2006/02/05 19:07:57  dauti
Added command line option support.

Revision 1.5  2006/01/23 23:29:06  cavac
Added define to disable sound
Changed "VERSION" to LONGVERSION"
Added new VERSION which is numeric for RPM and such

Revision 1.4  2006/01/09 14:06:59  cavac
Disabled Sound and fullscreen mode for WIN32

Revision 1.3  2006/01/09 13:00:29  cavac
Added Project-files for VisualC++ 6 (VC6)
Added needed SDL libraries for WIN32
Ported Code to compile on Win32 (some problems remain)
TODO: Add OGG-Support for Win32 (libogg + libvorbis)
Added Installer-Script for Inno-Setup (Win32)

Revision 1.2  2006/01/09 00:50:34  cavac
Bugfixed CVS autogenerated entries in background.cpp
Added CVS autogenerated entries to remaining .cpp and .h files
Updated copyright from "2005 Rene Schickbauer" to "2005-06 Rene Schickbauer"


--------------------------------------------------------------------*/
#endif //NOP_CODE

#include <stdlib.h>
#include "globals.h"
#include "sound.h"
#include "errorhandler.h"

int audio_rate = 22050;
Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
int audio_channels = 2;
Uint32 audio_buffers = 4096;
extern bool soundOK ;
bool soundInitOK = false;

Mix_Music *levelMusic = NULL;
Mix_Chunk *sound_fx[FX_MAX_PREDEF + MAX_FX_SAMPLES];
Uint32 sndfxcnt = 0;

void initSound()
{
	if (soundOK==false) { return ; } // Sound support not compiled or disabled by command line switch -s
	
	/* This is where we open up our audio device.  Mix_OpenAudio takes
	as its parameters the audio format we'd /like/ to have. */
	if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
		printf("Unable to open audio!\n");
		soundInitOK = false;
		return;
	} else {
		soundInitOK = true;
	}
	
	/* If we actually care about what we got, we can ask here.  In this
	program we don't... yet */
	Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);
	sound_fx[FX_COLLECT_PIXEL] = Mix_LoadWAV(RESPATH "fx_collect_pixel.ogg");
	sound_fx[FX_KILL_MONSTER] = Mix_LoadWAV(RESPATH "fx_kill_monster.ogg");
	sound_fx[FX_KILL_PLAYER] = Mix_LoadWAV(RESPATH "fx_killed.ogg");
	sound_fx[FX_LEVEL_FINISHED] = Mix_LoadWAV(RESPATH "fx_level_finished.ogg");
	
}

void deInitSound()
{
	if(!soundOK || !soundInitOK) {
		return;
	}
	soundStopMusic();
	levelMusic = NULL;
	
	Mix_FreeChunk(sound_fx[FX_COLLECT_PIXEL]);
	Mix_FreeChunk(sound_fx[FX_KILL_MONSTER]);
	Mix_FreeChunk(sound_fx[FX_KILL_PLAYER]);
	Mix_FreeChunk(sound_fx[FX_LEVEL_FINISHED]);
	
	deInitSoundFXLua();
	
	Mix_CloseAudio();
	soundInitOK = false;
}


void soundPlayFX(Uint32 fx) {
	if(!soundOK || !soundInitOK) {
		return;
	}
	
	Mix_PlayChannel(-1, sound_fx[fx], 0);
}


void soundStartMusic(char* fname)
{
	if(!soundOK || !soundInitOK) {
		return;
	}
	
	soundStopMusic();
	
	char fullfname[MAX_FNAME_LENGTH];
	sprintf(fullfname, "%s%s", RESPATH, fname);
	levelMusic = Mix_LoadMUS(fullfname);	
	if(!levelMusic) {
		DIE(ERROR_SOUND_READ, fullfname);
	}
	Mix_PlayMusic(levelMusic, -1);
	Mix_VolumeMusic(MIX_MAX_VOLUME / 2);
	
}

void soundStopMusic()
{
	if(!soundOK || !soundInitOK) {
		return;
	}
	
	Mix_HaltMusic();
	if(levelMusic) {
		Mix_FreeMusic(levelMusic);
		levelMusic = 0;
	}
}

Uint32 addSoundFXLua(char* fname) {
	if(!soundOK || !soundInitOK) {
		return 0;
	}
	
	char fullfname[MAX_FNAME_LENGTH];
	sprintf(fullfname, "%s%s", RESPATH, fname);

	if(sndfxcnt >= MAX_FX_SAMPLES) {
		DIE(ERROR_BOUNDARY, "too many FX samples");
	}
	sound_fx[sndfxcnt + FX_MAX_PREDEF] = Mix_LoadWAV(fullfname);
	if(!sound_fx[sndfxcnt + FX_MAX_PREDEF]) {
		DIE(ERROR_SOUND_READ, fname);
	}
	sndfxcnt++;
	return (sndfxcnt-1);
}

void soundPlayFXLua(Uint32 fx) {
	if(!soundOK || !soundInitOK) {
		return;
	}
	
	if(fx >= sndfxcnt) {
		DIE(ERROR_INDEX_INVALID, "Unknown sample");
	}
	soundPlayFX(fx + FX_MAX_PREDEF);
}

void initSoundFXLua() {
	if(!soundOK || !soundInitOK) {
		return;
	}
	
	sndfxcnt = 0;
}

void deInitSoundFXLua() {
	if(!soundOK || !soundInitOK) {
		return;
	}
	
	for(Uint32 i = 0; i < sndfxcnt; i++) {
		Mix_FreeChunk(sound_fx[i + FX_MAX_PREDEF]);
	}
}
