//
// Irrlicht Wrapper for Imperative Languages
// Frank Dodd (2006)
//
// Additional Work
// Michael Taupitz (2006)
// Marius Eckardt (2007)
//
// This wrapper DLL encapsulates a sub-set of the features of the powerful
// Irrlicht 3D Graphics Engine exposing the Object Oriented architecture and
// providing a functional 3D SDK for languages that are not object oriented.
//
// This source was created with the help of the great examples in the Irrlicht
// SDK and the excellent Irrlicht documentation. This software was developed
// using the GCC compiler and Code::Blocks IDE
//
// ToDo:
// draw2DLine()
// draw2DPolygon()
// draw2DRectangle()

/* ////////////////////////////////////////////////////////////////////////////
includes
*/
#include "irrlichtwrapper.h"


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

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


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


/****h* /2D
* NAME
*    Loading , drawin and changing Images or Textures.
* DESCRIPTION
*   With these functions you can load, draw and manipulate Images
*   and textures.
****
*/

/****f* 2D/IrrGetTexture
* NAME
*    IrrGetTexture - Load a Texture
* SYNOPSIS
*    IrrGetTexture( TextureFilename.s )
* FUNCTION
*    Loads the Texturefile 'TextureFilename.s'
*    You can all image-formats that Irrlicht know.
* INPUTS
*    TextureFilename.s - Name of the Texturefile
* RESULT
*    A Pointer to the loaded Texture. If the Pointer is 0 then Irrlicht was not able to load the Texture.
* EXAMPLE
*    *mytexture.l = IrrGetTexture( "./media/texture.jpg" )
* NOTES
*    If you have problems with loading Textures, you should compile as
*    Console-Application to see the Irrlicht-Output. Maybe this helps.
****
#PBIMPORT:IrrGetTexture( TextureFilename.p-ascii )
*/

STATIC_EXPORT void * DLL_EXPORT IrrGetTexture( char *cptrFile )
{
	return (void *)driver->getTexture( cptrFile );
}

/****f* 2D/IrrColorKeyTexture
* NAME
*    IrrColorKeyTexture - Color Key the Image
* SYNOPSIS
*    IrrColorKeyTexture( *texture.l, red.l, green.l, blue.l )
* FUNCTION
*    color key the image making parts of it transparent
* INPUTS
*   *texture.l    - pointer to a Texture (loaded with IrrGetTexture())
*   red.l         - define Red-Value of the ColorKey
*   green.l       - define Green-Value of the ColorKey
*   blue.l        - define Blue-Value of the ColorKey
* RESULT
*    nothing
* EXAMPLE
*   *mytexture.l = IrrGetTexture( "./media/texture.jpg" )
*   IrrColorKeyTexture(*mytexture, 255,0,0)
* NOTES
* none, yet!
****
#PBIMPORT:IrrColorKeyTexture( *texture.l, red.l, green.l, blue.l )
*/
STATIC_EXPORT void DLL_EXPORT IrrColorKeyTexture( video::ITexture *texture, int red, int green, int blue )
{
	driver->makeColorKeyTexture(texture, video::SColor(255, red,green,blue));
}

/****f* 2D/IrrColorKeyTextureFromPoint
* NAME
*    IrrColorKeyTextureFromPoint - Color Key the Image
* SYNOPSIS
*    IrrColorKeyTextureFromPoint( *texture.l, x.l, y.l)
* FUNCTION
*   color key the image making parts of it transparent
*   use a pixel of the texture to identify the Color-Value
* INPUTS
*   *texture.l    - pointer to a Texture (loaded with IrrGetTexture())
*   x.l           - x-position of the Pixel
*   y.l           - y-position of the Pixel
* RESULT
*    nothing
* EXAMPLE
*   *mytexture.l = IrrGetTexture( "./media/texture.jpg" )
*   IrrColorKeyTextureFromPoint(*mytexture, 0,0)
* NOTES
* none, yet!
****
#PBIMPORT:IrrColorKeyTextureFromPoint( *texture.l, x.l, y.l)
*/
STATIC_EXPORT void DLL_EXPORT IrrColorKeyTextureFromPoint( video::ITexture *texture, s32 x, s32 y )
{
	driver->makeColorKeyTexture(texture, position2d<s32>(x,y));
}

/****f* 2D/IrrDraw2DImage
* NAME
*    IrrDraw2DImage - Draw the Image
* SYNOPSIS
*    IrrDraw2DImage( *texture.l, x.l, y.l)
* FUNCTION
*   Draw the Image at x,y to the Screen.
* INPUTS
*   *texture.l    - pointer to a Texture (loaded with IrrGetTexture())
*   x.l           - x-position of the Pixel
*   y.l           - y-position of the Pixel
* RESULT
*    nothing
* EXAMPLE
*   IrrDraw2DImage(*texture, 100,100)
* NOTES
* none, yet!
****
#PBIMPORT:IrrDraw2DImage( *texture.l, x.l, y.l)
*/
STATIC_EXPORT void DLL_EXPORT IrrDraw2DImage(
                                video::ITexture *texture,
                                int iX, int iY )
{
    driver->draw2DImage(texture, core::position2d<s32>(iX,iY));
}

/* ----------------------------------------------------------------------------
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
*/
/****f* 2D/IrrDraw2DImageElement
* NAME
*    IrrDraw2DImageElement - Draws a part of a texture into the rectangle optionally using a alpha and color.
* SYNOPSIS
*    IrrDraw2DImageElement(*texture.l, x.l,y.l, source_tx.l,source_ty.l, source_bx.l,source_by.l,use_alpha.l,[color_alpha.l,color_r.l,color_g.l,color_b.l])
* FUNCTION
*   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
* INPUTS
*   *texture.l    - pointer to a Texture (loaded with IrrGetTexture())
*   x.l           - X-position of the Element
*   y.l           - Y-position of the Element
*  source_tx.l	- topX Position of the Source Image
*  source_ty.l	- topY Position of the Source Image
*  source_bx.l	- bottomX Position of the Source Image
*  source_by.l	- bottomY Position of the Source Image
*  use_alpha.l   - Bool use Alpha Mask or not:
*			#IRR_USE_ALPHA
*			#IRR_IGNORE_ALPHA
* Optional:
*  color_alpha.l	 - Alpha value
*  color_r.l	 - Red value
*  color_g.l	 - Green value
*  color_b.l	 - Blue value
*
* RESULT
*    nothing
* EXAMPLE
*   IrrDraw2DImageElement( *PureBasicLogo, screen_width - 109 - 4, 4,  0,0,109,31, #IRR_USE_ALPHA )
* NOTES
* The Optional Color and Alpha values represent the Color with which the image is colored. If the color equals Color(255,255,255,255), the color is ignored. Note that the alpha component is used: If alpha is other than 255, the image will be transparent
****
#PBIMPORT:IrrDraw2DImageElement(*texture.l, x.l,y.l, source_tx.l,source_ty.l, source_bx.l,source_by.l,use_alpha.l=#True,color_alpha.l=255,color_r.l=255,color_g.l=255,color_b.l=255)
*/

STATIC_EXPORT void DLL_EXPORT IrrDraw2DImageElement(
                                video::ITexture *texture,
                                int X, int Y,
                                int TX, int TY, int BX, int BY,
                                bool usealpha, s32 alpha, s32 red, s32 green, s32 blue )
{
    driver->draw2DImage(
            texture,
            position2d< s32 >(X,Y),
            rect< s32 >(TX,TY,BX,BY),
            NULL,
            video::SColor( alpha,red,green,blue),
            usealpha
    );
}
/* ---------------------------------------------------------------------------- */
/****f* 2D/IrrGetFont
* NAME
*    IrrGetFont - Load a Bitmap based Font.
* SYNOPSIS
*    IrrGetFont( Path2Font.s)
* FUNCTION
*   load a bitmap font as a texture.
* INPUTS
*   Path2Font.s    - Path to Bitmap Font.
* RESULT
*    Pointer to FontBitmap
* EXAMPLE
*   *BitmapFont = IrrGetFont ( "media\bitmapfont.bmp" )
* NOTES
* 	For more Info about the Handling: See Example 03
* 	To create such a FontBitmap, check out the FontTool.exe provided in the Tools Folder.
****
#PBIMPORT:IrrGetFont( Path2Font.p-ascii )
*/

STATIC_EXPORT void * DLL_EXPORT IrrGetFont( char * font )
{
    return device->getGUIEnvironment()->getFont( font );
}


/* ----------------------------------------------------------------------------*/
/****f* 2D/Irr2DFontDraw
* NAME
*    Irr2DFontDraw - Draw Text to the Display.
* SYNOPSIS
*    Irr2DFontDraw( *BitmapFont, MyText.p-unicode, Top_X.l, Top_Y.l, Bottom_X.l, Bottom_Y.l , [red.l, green.l, blue.l ])
* FUNCTION
*   draw text to the display at the supplied co-ordinates, using a loaded bitmap font.
* INPUTS
*   *BitmapFont    - pointer to a Irrlicht-Bitmap Font.
*  Top_X.l,Top_Y.l - Top-Left position
*  Bottom_X.l, Bottom_Y.l - Right-Down position
*  red,green,blue - color
* RESULT
*    nothing
* EXAMPLE
*   Irr2DFontDraw ( *BitmapFont, "HELLO WORLD", 120, 80, 250, 96 )
* NOTES
* 	For more Info about the Handling: See Example 03
* 	To create a FontBitmap, check out the FontTool.exe provided in the Tools Folder.
****
#PBIMPORT:Irr2DFontDraw( *BitmapFont, MyText.p-unicode, Top_X.l, Top_Y.l, Bottom_X.l, Bottom_Y.l,red.l=255,green.l=255,blue.l=255 )
*/

STATIC_EXPORT void DLL_EXPORT Irr2DFontDraw ( IGUIFont* font, const wchar_t * wcptrText, int tx, int ty, int bx, int by, int red, int green, int blue )
{
    font->draw( wcptrText, core::rect<s32>(tx,ty,bx,by), video::SColor(255,red,green,blue));
}


/* ----------------------------------------------------------------------------
Create a Image from Data
*/
/* ----------------------------------------------------------------------------*/
/****f* 2D/IrrCreateImageFromData
* NAME
*   IrrCreateImageFromData - Create an Irrlicht Image from Memory
* SYNOPSIS
*    IrrCreateImageFromData(#IRR_ECF_*,ImageWidth.l,ImageHeight.l,*ptr2Image )
* FUNCTION
*   Creates an Irrlicht Image from memory, allowing you to use Images from PB's CatchImage(), CreateImage() functions.
* INPUTS
*   #IRR_ECF*    - Color Format of the Image.
*  ImageWidth.l   - Width of the SourceImage
*  ImageHeight.l  - Height of the SourceImage
*  *ImagePointer - Pointer to the Image in Memory
* RESULT
*    Pointer to the newly created Irrlicht Image
* EXAMPLE
*   IrrCreateImageFromData(#IRR_ECF_A8R8G8B8,ImageWidth(image),ImageHeight(image),*memPtr)
* NOTES
* Possible Color Formats:
* #IRR_ECF_A1R5G5B5
* #IRR_ECF_R5G6B5
* #IRR_ECF_R8G8B8
* #IRR_ECF_A8R8G8B8	<- PB's Standard ( Alpha, Red, Green, Blue ) -> 8Bit Hex ( 1-255 )
*
* For a practical Usage see Example's 31 and 32
****
#PBIMPORT:IrrCreateImageFromData(ColorFormat.l,ImageWidth.l,ImageHeight.l,*ptr2Image )
*/
STATIC_EXPORT void DLL_EXPORT * IrrCreateImageFromData(  ECOLOR_FORMAT  format,
                                         int width, int height,
                                         void *  data )
{

    return (void*) driver->createImageFromData(format, dimension2d<s32>(width,height),data,false);
}

/* ----------------------------------------------------------------------------
Adds a Texture from a Image
*/
/****f* 2D/IrrAddTextureFromImage
* NAME
*   IrrAddTextureFromImage - Create a Texture from an Irrlicht Image
* SYNOPSIS
*   IrrAddTextureFromImage( TextureName.s , *ptr2IrrlichtImage )
* FUNCTION
*   Creates a Texture from an Irrlicht Image. Later calls of IrrGetTexture() with this name will return this texture.
* INPUTS
*   TextureName.s     - Texture Name to identify our Texture.
*  *ptr2irrlichtImage - Pointer to the Source Irrlicht Image
* RESULT
*    Pointer to the Texture
* EXAMPLE
*   *iTex = IrrAddTextureFromImage("Mytexture_"+str(id),*IImage)
*   IrrSetNodeMaterialTexture(*myNode,*iTex,0)
* NOTES
*
* For a practical Usage see Example's 31 and 32
****
#PBIMPORT:IrrAddTextureFromImage( TextureName.p-ascii , *ptr2IrrlichtImage )
*/
STATIC_EXPORT void DLL_EXPORT * IrrAddTextureFromImage( const char* name, IImage *image)
{
    return (void*) driver->addTexture(name,image);
}


/* ----------------------------------------------------------------------------
Remover
*/
/****f* 2D/IrrImageLock
* NAME
*   IrrImageLock - Lock an Irrlicht Image
* SYNOPSIS
*   IrrImageLock( *ptr2IrrlichtImage )
* FUNCTION
*   Lock function. Use this to get a pointer to the image data. After you don't need the pointer anymore, you must call IrrImageUnlock().
* INPUTS
*  *ptr2irrlichtImage - Pointer to an Irrlicht Image
* RESULT
*    Returns pointer to the image data. What type of data is pointed to depends on the color format of the image. For example if the color format is #IRR_ECF_A8R8G8B8. Be sure to call IrrImageUnlock() after you don't need the pointer any more.
* EXAMPLE
*  - none yet
* NOTES
* - none yet
****
#PBIMPORT:IrrImageLock( *ptr2IrrlichtImage )
*/
STATIC_EXPORT void DLL_EXPORT * IrrImageLock( IImage *image)
{
    if (image)
        return image->lock();
    else
        return NULL;
}
/****f* 2D/IrrImageUnLock
* NAME
*   IrrImageUnLock - UnLock an Irrlicht Image
* SYNOPSIS
*   IrrImageUnLock( *ptr2IrrlichtImage )
* FUNCTION
*   UnLock function. Use this to get a pointer to the image data. After you don't need the pointer anymore, you must call unlock().
* INPUTS
*  *ptr2irrlichtImage - Pointer to an Irrlicht Image
* RESULT
*    none
* EXAMPLE
*  - none yet
* NOTES
* - none yet
****
#PBIMPORT:IrrImageUnLock( *ptr2IrrlichtImage )
*/

STATIC_EXPORT void DLL_EXPORT  IrrImageUnlock( IImage *image)
{
    if (image)
        image->unlock();
}
/****f* 2D/IrrImageWidth
* NAME
*   IrrImageWidth - Returns the Width of an Irrlicht Image.
* SYNOPSIS
*   IrrImageWidth( *ptr2IrrlichtImage )
* FUNCTION
*   Use this Function to get the Width of an Irrlicht Image.
* INPUTS
*  *ptr2irrlichtImage - Pointer to an Irrlicht Image
* RESULT
*    Returns the Width of an Irrlicht Image or -1 if the Image isn't valid.
* EXAMPLE
*  - none yet
* NOTES
* - none yet
****
#PBIMPORT:IrrImageWidth( *ptr2IrrlichtImage )
*/
STATIC_EXPORT int DLL_EXPORT  IrrImageWidth( IImage *image)
{
    if (image)
    {
        dimension2d<s32> dim = image->getDimension();
        return dim.Width;
    }
    else
        return -1;
}
/****f* 2D/IrrImageHeight
* NAME
*   IrrImageHeight - Returns the Height of an Irrlicht Image.
* SYNOPSIS
*   IrrImageHeight( *ptr2IrrlichtImage )
* FUNCTION
*   Use this Function to get the Height of an Irrlicht Image.
* INPUTS
*  *ptr2irrlichtImage - Pointer to an Irrlicht Image
* RESULT
*    Returns the Height of an Irrlicht Image or -1 if the Image isn't valid.
* EXAMPLE
*  - none yet
* NOTES
* - none yet
****
#PBIMPORT:IrrImageHeight( *ptr2IrrlichtImage )
*/
STATIC_EXPORT int DLL_EXPORT  IrrImageHeight( IImage *image)
{
    if (image)
    {
        dimension2d<s32> dim = image->getDimension();
        return dim.Height;
    }
    else
        return -1;
}
/****f* 2D/IrrImageBitsPerPixel
* NAME
*   IrrImageBitsPerPixel - Returns the BitDepth of an Irrlicht Image.
* SYNOPSIS
*   IrrImageBitsPerPixel( *ptr2IrrlichtImage )
* FUNCTION
*   Use this Function to get the BitDepth of an Irrlicht Image.
* INPUTS
*  *ptr2irrlichtImage - Pointer to an Irrlicht Image
* RESULT
*    Returns the BitDepth of an Irrlicht Image or -1 if the Image isn't valid.
* EXAMPLE
*  - none yet
* NOTES
* - none yet
****
#PBIMPORT:IrrImageBitsPerPixel( *ptr2IrrlichtImage )
*/

STATIC_EXPORT int DLL_EXPORT  IrrImageBitsPerPixel( IImage *image)
{
    if (image)
    {
        return image->getBitsPerPixel();
    }
    else
        return -1;
}
/****f* 2D/IrrImageBytesPerPixel
* NAME
*   IrrImageBytesPerPixel - Returns bytes per pixel of an Irrlicht Image.
* SYNOPSIS
*   IrrImageBytesPerPixel( *ptr2IrrlichtImage )
* FUNCTION
*   Use this Function to get the Bytes per Pixel from an Irrlicht Image.
* INPUTS
*  *ptr2irrlichtImage - Pointer to an Irrlicht Image
* RESULT
*    Returns bytes per pixel of an Irrlicht Image or -1 if the Image isn't valid.
* EXAMPLE
*  - none yet
* NOTES
* - none yet
****
#PBIMPORT:IrrImageBytesPerPixel( *ptr2IrrlichtImage )
*/

STATIC_EXPORT int DLL_EXPORT  IrrImageBytesPerPixel( IImage *image)
{
    if (image)
    {
        return image->getBytesPerPixel();
    }
    else
        return -1;
}
/****f* 2D/IrrImageDataSizeInBytes
* NAME
*   IrrImageDataSizeInBytes - Returns image data size in bytes.
* SYNOPSIS
*   IrrImageDataSizeInBytes( *ptr2IrrlichtImage )
* FUNCTION
*   Use this Function to get the Data-Size in bytes from an Irrlicht Image.
* INPUTS
*  *ptr2irrlichtImage - Pointer to an Irrlicht Image
* RESULT
*    Returns bytes of an Irrlicht Image or -1 if the Image isn't valid.
* EXAMPLE
*  - none yet
* NOTES
* - none yet
****
#PBIMPORT:IrrImageDataSizeInBytes( *ptr2IrrlichtImage )
*/
STATIC_EXPORT int DLL_EXPORT  IrrImageDataSizeInBytes( IImage *image)
{
    if (image)
    {
        return image->getImageDataSizeInBytes();
    }
    else
        return -1;
}
/****f* 2D/IrrImageDataSizeInPixels
* NAME
*   IrrImageDataSizeInPixels - Returns image data size in pixels.
* SYNOPSIS
*   IrrImageDataSizeInPixels( *ptr2IrrlichtImage )
* FUNCTION
*   Use this Function to get the Data-Size in pixels from an Irrlicht Image.
* INPUTS
*  *ptr2irrlichtImage - Pointer to an Irrlicht Image
* RESULT
*    Returns the Data-Size in pixels from an Irrlicht Image or -1 if the Image isn't valid.
* EXAMPLE
*  - none yet
* NOTES
* - none yet
****
#PBIMPORT:IrrImageDataSizeInPixels( *ptr2IrrlichtImage )
*/
STATIC_EXPORT int DLL_EXPORT  IrrImageDataSizeInPixels( IImage *image)
{
    if (image)
    {
        return image->getImageDataSizeInPixels();
    }
    else
        return -1;
}
/****f* 2D/IrrImageGetPixel
* NAME
*   IrrImageGetPixel - Returns a pixel
* SYNOPSIS
*   IrrImageGetPixel( *ptr2IrrlichtImage, X_Pos.l, Y_Pos.l )
* FUNCTION
*   Use this Function to get the Pixel Color at the specified position from an Irrlicht Image.
* INPUTS
*  *ptr2irrlichtImage - Pointer to an Irrlicht Image
*  X_Pos.l - X Position of the Pixel in the Irrlicht Image
*  Y_Pos.l - Y Position of the Pixel in the Irrlicht Image
* RESULT
*    Returns the color of a pixel at the specified Coordinates from an Irrlicht Image or returns -1 if the Image isn't valid.
* EXAMPLE
*  - none yet
* NOTES
* - none yet
****
#PBIMPORT:IrrImageGetPixel( *ptr2IrrlichtImage, X_Pos.l, Y_Pos.l )
*/
STATIC_EXPORT int DLL_EXPORT  IrrImageGetPixel( IImage *image, s32 x, s32 y)
{
    if (image)
    {
        return image->getPixel(x,y).color;
    }
    else
        return -1;
}
/****f* 2D/IrrImageGetColorFormat
* NAME
*   IrrImageGetColorFormat - Returns the Colorformat of an Irrlicht Image
* SYNOPSIS
*   IrrImageGetColorFormat( *ptr2IrrlichtImage )
* FUNCTION
*   Use this Function to get the Colorfromat from an Irrlicht Image.
* INPUTS
*  *ptr2irrlichtImage - Pointer to an Irrlicht Image
* RESULT
*    Returns the Colorformat from an Irrlicht Image or returns -1 if the Image isn't valid.
* EXAMPLE
*  - none yet
* NOTES
* Valid Color Formats:
* #IRR_ECF_A1R5G5B5 = 0
* #IRR_ECF_R5G6B5     = 1
* #IRR_ECF_R8G8B8     = 2
* #IRR_ECF_A8R8G8B8 = 3
****
#PBIMPORT:IrrImageGetColorFormat( *ptr2IrrlichtImage )
*/
STATIC_EXPORT int DLL_EXPORT  IrrImageGetColorFormat( IImage *image)
{
    if (image)
    {
        return image->getColorFormat();
    }
    else
        return -1;
}
/****f* 2D/IrrImageGetPitch
* NAME
*   IrrImageGetPitch - Returns pitch of image
* SYNOPSIS
*   IrrImageGetPitch( *ptr2IrrlichtImage )
* FUNCTION
*   Returns the pitch of an Irrlicht Image or -1 if the Image isn't valid.
* INPUTS
*  *ptr2irrlichtImage - Pointer to an Irrlicht Image
* RESULT
*    Returns the pitch of an Irrlicht Image or -1 if the Image isn't valid.
* EXAMPLE
*  - none yet
* NOTES
* - none yet
****
#PBIMPORT:IrrImageGetPitch( *ptr2IrrlichtImage )
*/
STATIC_EXPORT int DLL_EXPORT  IrrImageGetPitch( IImage *image)
{
    if (image)
    {
        return image->getPitch();
    }
    else
        return -1;
}


/****f* 2D/IrrChangeTextureAlpha
* NAME
*   IrrChangeTextureAlpha - Changes the Alpha-Value of a Texture
* SYNOPSIS
*   IrrChangeTextureAlpha( *ptr2IrrlichtTexture.l, alpha.l )
* FUNCTION
*   This Function changes the Texture directy. It writes the new Alpha-Value
*   to all pixels of the Texture. So keep that in m ind when using. It is not the best
*   way to add alpha, but sometimes helpfull.
* INPUTS
*  *ptr2irrlichtTexture.l - Pointer to an Irrlicht Textur
*  alpha.l                - new Alpha-Value
* RESULT
*    nothing
* EXAMPLE
*  - none yet
* NOTES
* - none yet
****
#PBIMPORT:IrrChangeTextureAlpha( *ptr2IrrlichtTexture.l, alpha.l )
*/
STATIC_EXPORT void DLL_EXPORT  IrrChangeTextureAlpha( ITexture *tex, u8 alpha)
{
    if (tex)
    {
        u8* tData             = (u8*)tex->lock();
        s32 tPitch              = tex->getPitch();
        dimension2d<s32> texDim = tex->getSize();

        for (int ix = 0; ix < texDim.Width;ix++) {
            for (int iy=0;iy < texDim.Height;iy++) {
                unsigned char * pTLayer = (unsigned char*)tData + (tPitch*ix)+(iy*4);
                (pTLayer)[3] = alpha;
            }
        }
        tex->unlock();
    }
}






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