// ----------------------------------------------------------------------------
// 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.
//
// /////////////////////////////////////////////////////////////////////////////

#include "bIrrlicht.h"
#include "CCloudSceneNode.h"
#include "CLensFlareSceneNode.h"
#include "CGrassPatchSceneNode.h"
#include "CWindGenerator.h"
#include "CBillboardGroupSceneNode.h"
//#include "CSkyDomeColorSceneNode.h"
#include "CLODSceneNode.h"
#include "CZoneSceneNode.h"
#include "CBatchingMesh.h"
#include "CImpostorSceneNode.h"
#include "CBoltSceneNode.h"
#include "CBeamSceneNode.h"



extern IrrlichtDevice*		device;
extern IVideoDriver*		driver;
extern ISceneManager*		smgr;
extern IGUIEnvironment*		guienv;




extern "C"
{

/* ----------------------------------------------------------------------------
adds a scene node for rendering an animated water surface mesh
*/
DLL_EXPORT int * BBCALL iCreateWater ( IAnimatedMesh* mesh, float waveHeight, float waveSpeed, float waveLength, ISceneNode *  parent, int id, float positionX, float positionY, float positionZ, float rotationX, float rotationY, float rotationZ, float scaleX, float scaleY, float scaleZ)
{
    scene::ISceneNode* node = smgr->addWaterSurfaceSceneNode(
                    mesh->getMesh(0),
                    waveHeight,
                    waveSpeed,
                    waveLength,
                    parent,
                    id,
                    core::vector3df(positionX, positionY, positionZ),
                    core::vector3df(rotationX, rotationY, rotationZ),
                    core::vector3df(scaleX, scaleY, scaleZ)
    );
    return (int *)node;
}

/* ----------------------------------------------------------------------------
adds a set of clouds to the scene
*/
DLL_EXPORT int * BBCALL iCreateClouds ( ITexture * txture, u32 lod, u32 depth, u32 density )	//ISceneNode *
{
    // create a cloud node
    scene::CCloudSceneNode* clouds = new scene::CCloudSceneNode(
            smgr->getRootSceneNode(), smgr,
                device->getTimer(), -1, core::vector3df(0,0,0), core::vector3df(0,0,0), core::vector3df(1,1,1));

    // set the level of details to draw at what distance
    clouds->setLOD((f32)lod);

    // set the maximum detail level when recursing
    clouds->setMaxDepth(depth);

    // call a "here's one I made earlier" function
    clouds->makeRandomCloud(density);

 //   clouds->setLOD(1.0f);

    // for some reason ISceneNode material properties cannot be called from here
    clouds->setMaterialTexture(0,txture);
    clouds->getMaterial(0).Lighting = false;
    clouds->getMaterial(0).MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;

    return (int *)clouds;
}

/* ----------------------------------------------------------------------------
Adds a lens flare patch object to the scene based on the supplied parameters
*/
DLL_EXPORT int * BBCALL iCreateFlare ( ITexture * txture )	//CLensFlareSceneNode *
{
    CLensFlareSceneNode* flare = new CLensFlareSceneNode(smgr->getRootSceneNode(),smgr,-1, core::vector3df(300,100,1000));

    // set the texture used for the lens flare effect
    flare->setMaterialTexture(0, txture);

    return (int *)flare;
}

/* ----------------------------------------------------------------------------
Sets the scale of optics in the scene
*/
DLL_EXPORT void BBCALL iFlareScale( CLensFlareSceneNode * flare, float source, float optics )
{
    flare->setOpticsScale( source, optics );
}


/* ----------------------------------------------------------------------------
Adds a grass object to the scene
*/
DLL_EXPORT int * BBCALL iCreateGrass (				//CGrassPatchSceneNode *
                                    scene::ITerrainSceneNode* terrain,
                                    s32 x,
                                    s32 z,
                                    s32 patchSize,
                                    f32 fadeDistance ,
									bool crossed,
                                    f32 grassScale,
									u32 maxDensity,
									u32 dataPositionX,
									u32 dataPositionY,
                                    video::IImage* heightMap,
                                    video::IImage* textureMap,
                                    video::IImage* grassMap,
                                    video::ITexture *grassTexture )
{
    scene::IWindGenerator *wind;
    scene::CGrassPatchSceneNode *grass;

    wind = scene::createWindGenerator( 30.0f, 3.0f );

	core::position2d<s32> dataPosition( dataPositionX, dataPositionY );

	grass = new scene::CGrassPatchSceneNode(
            terrain,
            smgr,
            -1,
            core::vector3d<s32>(x,0,z),
            patchSize,
            grassScale,
			maxDensity,
			dataPosition,
            "grass",
            heightMap,
            textureMap,
            grassMap,
            wind,
			crossed );

    grass->setFadeDistance( fadeDistance );

    grass->setMaterialFlag(video::EMF_LIGHTING,false);
    grass->setMaterialType(video::EMT_TRANSPARENT_ALPHA_CHANNEL);
    grass->setMaterialTexture(0,grassTexture);

    return (int *)grass;
}

/* ----------------------------------------------------------------------------
Set grass density
*/
DLL_EXPORT void BBCALL iGrassDensity( CGrassPatchSceneNode * grass, int density, float distance )
{
    grass->setMaxDensity( density );
    grass->setDrawDistance( distance );
}

/* ----------------------------------------------------------------------------
Adds a grass object to the scene
*/
DLL_EXPORT void BBCALL iGrassWind( CGrassPatchSceneNode * grass, float strength, float res )
{
    grass->getWind()->setStrength( strength );
    grass->getWind()->setRegularity( res );
}

/* ----------------------------------------------------------------------------
get the number of grass objects drawn
*/
DLL_EXPORT u32 BBCALL iGrassDrawCount( CGrassPatchSceneNode * grass )
{
    return grass->getLastDrawCount();
}

/* ----------------------------------------------------------------------------
add a bolt node to the scene
*/
DLL_EXPORT int * BBCALL iCreateBolt( void )	//irr::scene::CBoltSceneNode *
{
	irr::scene::CBoltSceneNode* beam = new irr::scene::CBoltSceneNode(
			smgr->getRootSceneNode(),
			smgr,
			-1 );
	beam->drop();

    return (int *)beam;
}

/* ----------------------------------------------------------------------------
set the properties of a bolt node in the scene
*/
DLL_EXPORT void BBCALL iBoltProperties( irr::scene::CBoltSceneNode* beam,
							    f32 sx, f32 sy, f32 sz,
								f32 ex, f32 ey, f32 ez,
								u32 updateTime,
								u32 height,
								f32 thickness,
								u32 parts,
								u32 bolts,
								bool steddyend,
								u8 R,
								u8 G,
								u8 B,
								u8 alpha )

{
	beam->setLine(irr::core::vector3df(sx,sy,sz), irr::core::vector3df(ex,ey,ez), updateTime, height, parts, bolts, steddyend, thickness, video::SColor(alpha,R,G,B) );
}

/* ----------------------------------------------------------------------------
add a beam node to the scene
*/
DLL_EXPORT int * BBCALL iCreateBeam ( void )	//scene::CBeamSceneNode *
{
	scene::CBeamSceneNode *bolt = new scene::CBeamSceneNode(
			smgr->getRootSceneNode(),
			smgr,
			666,
			core::vector3df(50,0,0),
			core::vector3df(0,20,300),
			10);
	bolt->drop();

	return (int *)bolt;
}

/* ----------------------------------------------------------------------------
set the size of a beam
*/
DLL_EXPORT void BBCALL iBeamSize( scene::CBeamSceneNode* beam, f32 size )
{
   beam->setSize( size );
}

/* ----------------------------------------------------------------------------
set the position of a beam
*/
DLL_EXPORT void BBCALL iPositionBeam( scene::CBeamSceneNode* beam,
									f32 sx, f32 sy, f32 sz,
									f32 ex, f32 ey, f32 ez )
{
   beam->setPosition( core::vector3df( sx,sy,sz ), core::vector3df( ex,ey,ez ));
}

/* ----------------------------------------------------------------------------
adds an empty node To the scene
*/
DLL_EXPORT int * BBCALL iCreatePivot( void )
{
    return (int *)smgr->addEmptySceneNode( );
}

/* ----------------------------------------------------------------------------
add a skybox to the irrlicht scene manager based on six pictures
*/
DLL_EXPORT int * BBCALL iCreateSkybox (
                                       ITexture *texture_up,
                                       ITexture *texture_down,
                                       ITexture *texture_left,
                                       ITexture *texture_right,
                                       ITexture *texture_front,
                                       ITexture *texture_back )
{
	return (int *)smgr->addSkyBoxSceneNode(
            texture_up,
            texture_down,
            texture_left,
            texture_right,
            texture_front,
            texture_back
    );
}

/* ----------------------------------------------------------------------------
add a skydome to the irrlicht scene manager based on six pictures
*//*
DLL_EXPORT int * BBCALL iCreateSkydome (
                                       ITexture *texture,
                                       int horiRes = 16,
                                       int vertRes = 8,
                                       float texturePercentage = 0.9,
                                       float spherePercentage = 2.0,
                                       float radius = 1000.0,
									   ISceneNode* parent = 0,
									   int id = -1)
{
	return (int *)smgr->addSkyDomeSceneNode(
            texture,
            horiRes,
            vertRes,
            texturePercentage,
            spherePercentage,
            radius,
			parent,
			id );
}

DLL_EXPORT int* BBCALL iCreateSkyDome (										//DOES NOT WORK AT ALL IN BLITZ!
                                        ITexture *texture_file,
                                        int  horiRes,
                                        int  vertRes,
                                        float  texturePercentage,
                                        float  spherePercentage,
										float domeRadius )
{
	ISceneNode* node = new CSkyDomeColorSceneNode(texture_file, horiRes, vertRes,
		texturePercentage, spherePercentage, domeRadius, smgr->getRootSceneNode(), smgr, 666);

	node->drop();
	return (int*)node;
}*/

}


