// ----------------------------------------------------------------------------
// Blitz-Irrlicht 1.8.3 Wrapper by Ed Musolino 2015
// Based off of Frank Dodd's Irrlicht 1.7 Freebasic Wrapper (2006-2010)
// ----------------------------------------------------------------------------

//  This software is provided 'as-is', without any express Or implied
//  warranty.  In no event will the authors be held liable For any damages
//  arising from the use of this software.

//  Permission is granted To anyone To use this software For any purpose,
//  including commercial applications, And To alter it And redistribute it
//  freely, subject To the following restrictions:

//  1. The Origin of this software must Not be misrepresented; you must not
//     claim that you wrote the original software. If you use this software
//     in a product, an acknowledgement in the product documentation would be
//     appreciated but is Not required.
//  2. Altered source versions must be clearly marked as such, And must Not be
//     misrepresented as being the original software.
//  3. This notice may Not be removed Or altered from any source distribution.
//
// /////////////////////////////////////////////////////////////////////////////

/* ////////////////////////////////////////////////////////////////////////////
includes
*/

#include <stdio.h>
#include "bIrrlicht.h"

/* ////////////////////////////////////////////////////////////////////////////
global variables
*/

/* ////////////////////////////////////////////////////////////////////////////
external variables
*/
extern IrrlichtDevice*		device;
extern IVideoDriver*		driver;
extern ISceneManager*		smgr;
extern IGUIEnvironment*		guienv;

extern ISoundEngine * engine;
extern ISoundSource * audiosource;
extern ISound * audios;

/* ////////////////////////////////////////////////////////////////////////////
Global Function Declarations

all of the below functions are declared as C functions and are exposed without
any mangled names so that they can be easily imported into imperative
languages like FreeBasic
*/
extern "C"
{

/* ////////////////////////////////////////////////////////////////////////////
SYSTEM FUNCTIONS
*/
/* ----------------------------------------------------------------------------
Get the length of the sound in milliseconds

DLL_EXPORT unsigned int BBCALL IrrKlangGetPlayLength( ISound * sound )
{
    return sound->getPlayLength();
}*/
DLL_EXPORT ISceneNode * BBCALL iAddSoundNode(
													const char * file,
													float min_dist,
													bool loop,
													ISceneNode * parent = 0)
{
	CIrrKlangSceneNode* soundNode = new CIrrKlangSceneNode(engine, smgr->getRootSceneNode(), smgr, 666);

	soundNode->setSoundFileName( file );
	soundNode->setMinMaxSoundDistance( min_dist, 10000000.0 );
	if(loop)
	{
		soundNode->setLoopingStreamMode();
	}
	else
	{
		soundNode->setPlayOnceMode();
	}
	if(parent)
		soundNode->setParent(parent);
	
	soundNode->drop();
	return soundNode;
}

/* ----------------------------------------------------------------------------
Set the minimum distance at which the sound can be heard
*/
DLL_EXPORT void BBCALL iSoundDistance( ISound * sound, float distance )
{
    sound->setMinDistance( distance );
}

/* ----------------------------------------------------------------------------
Set the position of the sound in 3D space
*/
DLL_EXPORT void BBCALL iPositionSound( ISound * sound, float posX, float posY, float posZ )
{
    sound->setPosition( vec3df( posX, posY, posZ ));
}

/* ----------------------------------------------------------------------------
Play a 3D sound
*/
DLL_EXPORT ISound * BBCALL iPlay3DSound (
								const char * file,
								float posX,
								float posY,
								float posZ,
								bool playLooped,
								bool startPaused,
								bool track,
								E_STREAM_MODE streamMode,
								bool enableSoundEffects )
{
	return engine->play3D( file, vec3df(posX, posY, posZ), playLooped, startPaused, track, streamMode,enableSoundEffects );
}


/* ----------------------------------------------------------------------------
Set the position of the listener for 3D sounds
*/
DLL_EXPORT void BBCALL iListenerPosition (
												float posX,
												float posY,
												float posZ,
												float lookX,
												float lookY,
												float lookZ,
												float velocityX,
												float velocityY,
												float velocityZ,
												float upX,
												float upY,
												float upZ )
{
    // draw the gui interface
    engine->setListenerPosition  (
			vec3df( posX, posY, posZ ),
			vec3df( lookX, lookY, lookZ ),
			vec3df( velocityX, velocityY, velocityZ ),
			vec3df( upX, upY, upZ));
}

DLL_EXPORT void BBCALL iParentNode( CIrrKlangSceneNode* snode, ISceneNode* node )
{
	//engine->
    snode->setParent(node);
}



/* ////////////////////////////////////////////////////////////////////////////
all of the above functions are declared as C functions and are exposed without
any mangled names
*/
}
