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


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



extern "C"
{

DLL_EXPORT int * BBCALL iLoadImage( char *cptrFile )
{
    return (int *)driver->createImageFromFile (cptrFile);
}

DLL_EXPORT int * BBCALL iCreateImage( int x, int y, ECOLOR_FORMAT format )
{
	return (int *)driver->createImage( format, dimension2d< u32 >( x, y ) );
}

DLL_EXPORT void BBCALL iFreeImage( video::IImage *image )
{
		image->drop();
}

DLL_EXPORT int * BBCALL iLockImage( video::IImage *image )
{
	return (int *)image->lock();
}

DLL_EXPORT void BBCALL iUnlockImage( video::IImage *image )
{
	image->unlock();
}

DLL_EXPORT void BBCALL iSaveScreenShot( const char * file )
{
	IImage* screenshot = driver->createScreenShot();
	if ( screenshot )
	{
		device->getVideoDriver()->writeImageToFile( screenshot, file );
		screenshot->drop();
	}
}

DLL_EXPORT int * BBCALL iGrabScreenShot( u32 X, u32 Y, u32 W, u32 H )
{
    // grab the screenshot
	IImage *image = driver->createScreenShot();

	// create an image of the same size as the texture
	IImage *transitionImage = driver->createImage( image->getColorFormat(), dimension2d< u32 >( W, H ));

    // copy the pixels from the screenshot to the texture sized image
	image->copyTo( transitionImage, position2d<s32>(0,0), rect<s32>(X,Y,X+W,Y+H));

    // create a texture from the texture sized image
	return (int *)driver->addTexture("screenshot", transitionImage);
}

DLL_EXPORT u32 BBCALL iImageWidth( IImage * image )
{
    core::dimension2d< u32 > size = image->getDimension();
    return size.Width;
}

DLL_EXPORT u32 BBCALL iImageHeight( IImage * image )
{
    core::dimension2d< u32 > size = image->getDimension();
    return size.Height;
}

DLL_EXPORT u32 BBCALL iImagePitch( IImage * image )
{
    core::dimension2d< u32 > size = image->getDimension();
    return image->getPitch();
}

DLL_EXPORT u32 BBCALL iImageFormat( IImage * image )
{
    core::dimension2d< u32 > size = image->getDimension();
    return image->getColorFormat();
}

/* ----------------------------------------------------------------------------
draw the image to the screen
*/
DLL_EXPORT void BBCALL iDrawImage(
                                video::ITexture *texture,
                                int iX, int iY )
{
	//core::dimension2d< u32 > size = texture->getSize();
    //return size.Width;
    driver->draw2DImage(texture, core::position2d<s32>(iX,iY));
	//driver->draw2DImage(texture, core::position2d<s32>(iX,iY),
	//	rect<s32>,NULL,NULL,true);
}

/* ----------------------------------------------------------------------------
draw an image to the screen from a specified rectangle in the source image this
enables you to place lots of images into one texture for 2D drawing. in
addition this function also includes transparency and uses image alpha channels
*/
DLL_EXPORT void BBCALL iDrawImageElement(
                                video::ITexture *texture,
                                int X, int Y,
                                int TX, int TY, int BX, int BY,
                                bool usealpha )
{
    driver->draw2DImage(
            texture,
            position2d< s32 >(X,Y),
            rect< s32 >(TX,TY,BX,BY),
            NULL,
            video::SColor( 255, 255, 255, 255),
            usealpha
    );
}

/* ----------------------------------------------------------------------------
draw an image to the screen from a specified rectangle in the source image  and
into a specified destination rectangle, scaling the image if nessecary.
*/
DLL_EXPORT void BBCALL iDrawImageElementScale(
                                video::ITexture *texture,
                                int dTX, int dTY, int dBX, int dBY,
                                int TX, int TY, int BX, int BY,
                                bool usealpha )
{
    driver->draw2DImage(
            texture,
            rect<s32>(dTX,dTY,dBX,dBY),
            rect<s32>(TX,TY,BX,BY),
            NULL,
            NULL,
            usealpha
    );
}





}