#include "Player.h"

using namespace GSL;

GSLPlayer *_player = 0;

#ifdef WIN32
void CALLBACK AudioOutProc( HWAVEOUT hOut, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2 )
{
	_player->sendSoundMSG( hOut, uMsg, dwInstance, dwParam1, dwParam2 );
}
#endif

// Inhalt der GSLPlayer Klasse aus Player.h
// Konstruktor
GSLPlayer::GSLPlayer()
{
	InitializeCriticalSection( &cs );
}

// Destruktor
GSLPlayer::~GSLPlayer()
{
	sounds.leeren();
	DeleteCriticalSection( &cs );
}

// nicht constant
bool GSLPlayer::addSound( GSLSound *snd )
{
	EnterCriticalSection( &cs );
	int anz = sounds.getEintragAnzahl();
	for( int i = 0; i < anz; i++ )
	{
		if( sounds.z( i ) == snd )
		{
			snd->release();
			LeaveCriticalSection( &cs );
			return 0;
		}
	}
	sounds.add( snd );
	LeaveCriticalSection( &cs );
	return 1;
}

void GSLPlayer::removeSound( GSLSound *zSnd )
{
	EnterCriticalSection( &cs );
	int anz = sounds.getEintragAnzahl();
	for( int i = 0; i < anz; i++ )
	{
		if( sounds.z( i ) == zSnd )
		{
			LeaveCriticalSection( &cs );
			sounds.remove( i );
			return;
		}
	}
	LeaveCriticalSection( &cs );
}

#ifdef WIN32
void GSLPlayer::sendSoundMSG( HWAVEOUT hOut, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2 )
{
	EnterCriticalSection( &cs );
	int anz = sounds.getEintragAnzahl();
	for( int i = 0; i < anz; i++ )
	{
		if( sounds.z( i ) && sounds.z( i )->getHandle() == hOut )
		{
			sounds.z( i )->msg( uMsg, dwInstance, dwParam1, dwParam2 );
			break;
		}
	}
	LeaveCriticalSection( &cs );
}
#endif