//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------

#include "giofx.h"

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------

cTexture::~cTexture()
{
	glDeleteTextures(1,&memAddr);
	strcpy(name,"noname\0");
	depth   = 0;
	memAddr = 0;
	size.h  = 0;
	size.v  = 0;
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------

cTexture::cTexture(char* texturefName,int wrapMode,bool useFilter)
{
    strcpy(name,texturefName);
	size.h  = 0;
	size.v  = 0;
	memAddr = 0;
	GLubyte* tBuffer = NULL;
	NSString* texName = [NSString stringWithUTF8String: texturefName];

#if TARGET_OS_IPHONE
    UIImage* image      = [UIImage imageNamed:texName];
    CGImageRef imageRef = [image CGImage];
#else	
    NSURL*            url = [NSURL fileURLWithPath:texName];
    CGImageRef        imageRef = NULL;
    CGImageSourceRef  sourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
    if (sourceRef)
    {
        imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, NULL);
        CFRelease(sourceRef);
    }
#endif
    
    if (!imageRef)
        return;
    
    size.h = (int)CGImageGetWidth(imageRef);
    size.v = (int)CGImageGetHeight(imageRef);
    depth  = CGImageGetBitsPerPixel(imageRef) / 8;
    
#ifdef DEBUG
    if (size.h > 2048 || size.v > 2048)
        printf("WARNING %s is over 2048 pixels",texturefName);
#endif

    //printf("loading %s (%dx%dx%d)\n",texturefName,(int)size.h,(int)size.v,depth);
    
    CGRect rect   = {{0, 0}, {size.h, size.v}};
    tBuffer = (GLubyte*) malloc (size.h * 4 * size.v);
    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
    CGContextRef myBitmapContext = CGBitmapContextCreate (tBuffer, size.h, size.v, 8, size.h*4 , space, kCGImageAlphaPremultipliedLast);
    CGAffineTransform vFlip = CGAffineTransformMake(1, 0, 0, -1, 0, size.v);
    CGContextConcatCTM(myBitmapContext, vFlip);
    CGContextSetBlendMode(myBitmapContext, kCGBlendModeCopy);
    CGContextDrawImage(myBitmapContext,rect , imageRef);
    CGContextRelease(myBitmapContext);

#if TARGET_OS_IPHONE
    //[image release];
#else
    CGImageRelease(imageRef);
#endif

	if (size.h % 2)
		glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	else 
		glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
	
    glEnable(GL_TEXTURE_2D);
	glGenTextures(1,&memAddr);
	glBindTexture(GL_TEXTURE_2D,memAddr);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);
    
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,(useFilter)?GL_LINEAR:GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,(useFilter)?GL_LINEAR:GL_NEAREST);
    
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)size.h,(int)size.v, 0, GL_RGBA, GL_UNSIGNED_BYTE, tBuffer);	
	free(tBuffer);
}	

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
