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

#include "giofx.h"

#if TARGET_OS_IPHONE
	#import <AVFoundation/AVFoundation.h>
#else
	#import <Cocoa/Cocoa.h>
#endif

static float	oldEndPointx = 0;
static float	oldEndPointy = 0;

static float	music_volume    = 0.35;
static float    extra_volume    = 0.35;
static float	speech_volume   = 1.0;

UInt32 _sounds[MAX_SOUNDS];

#if TARGET_OS_IPHONE
	AVAudioPlayer *audioPlayer      = NULL;
    AVAudioPlayer *speechPlayer     = NULL;
    AVAudioPlayer *extraPlayer      = NULL;
    AVAudioRecorder *audioRecorder  = NULL;
    AVPlayer *videoPlayer           = NULL;
    AVPlayerLayer *videolayer       = NULL;
#else
	NSSound *audioPlayer            = NULL;
    NSSound *speechPlayer           = NULL;
    NSSound *extraPlayer            = NULL;
#endif

//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
/*
void PlayVideo(char * videoChar)
{

#if TARGET_OS_IPHONE

    if (videoPlayer)
        StopVideo();
    
    NSString* video = [NSString stringWithUTF8String: videoChar];
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], video]];
	if (videoPlayer == NULL && url)
	{
        videoPlayer = [AVPlayer playerWithURL:url];
        videolayer = [AVPlayerLayer layer];

        extern CAEAGLLayer* mainLayer;
        [videolayer setPlayer:videoPlayer];
        [videolayer setFrame:[mainLayer frame]];
        [videolayer setBackgroundColor:[UIColor whiteColor].CGColor];
        [videolayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
        [mainLayer addSublayer:videolayer];

        [videoPlayer play];
    }

#else

#endif

}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void StopVideo()
{

#if TARGET_OS_IPHONE

    if (videoPlayer != nil)
	{
        //[videoPlayer release];
        videoPlayer = NULL;
        [videolayer removeFromSuperlayer];
        
    }
	videoPlayer = nil;

#else

#endif

}

float  GetVideoStatus(void)
{

#if TARGET_OS_IPHONE
    if (videoPlayer)
    {
        AVPlayerItem *currentItem = videoPlayer.currentItem;
        NSTimeInterval currentTime = CMTimeGetSeconds(currentItem.currentTime);
        //NSLog(@" Capturing Time :%f ",currentTime);
        return currentTime;
    }
#else

#endif

    return 0;
}
*/
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void RecordStart()
{

#if TARGET_OS_IPHONE

    if (!audioRecorder)
    {
        NSURL *audioFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"audioRecording.m4a"]];
        NSDictionary *audioSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithFloat:44100],AVSampleRateKey,
                               [NSNumber numberWithInt: kAudioFormatAppleLossless],AVFormatIDKey,
                               [NSNumber numberWithInt: 1],AVNumberOfChannelsKey,
                               [NSNumber numberWithInt:AVAudioQualityMedium],AVEncoderAudioQualityKey,nil];

        audioRecorder = [[AVAudioRecorder alloc]
                      initWithURL:audioFileURL
                      settings:audioSettings
                      error:nil];
    }
    
    [audioRecorder record];

#endif

}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void RecordStop()
{
#if TARGET_OS_IPHONE

    if (audioRecorder)
    {
        [audioRecorder stop];
        [audioRecorder release];
    }
    
    audioRecorder = nil;

#endif

}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void RecordPlay()
{

#if TARGET_OS_IPHONE

    if (audioPlayer)
        StopMusic();
    
    NSURL *audioFileURL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:@"audioRecording.m4a"]];
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileURL error:nil];
    [audioPlayer play];

#endif

}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void PlayMusic(char * musicChar,float startTime,bool onLoop)
{

#ifndef USE_SOUNDS
    return;
#endif

    if (audioPlayer)
        StopMusic();
    
    NSString* music = [NSString stringWithUTF8String: musicChar];	
	
#if TARGET_OS_IPHONE
   
	NSError *error;
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], music]];
	if (audioPlayer == NULL && url)
	{
		audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        if (audioPlayer)
        {
            audioPlayer.numberOfLoops = (onLoop) ? -1 : 0;
            audioPlayer.volume = music_volume;
            audioPlayer.currentTime = startTime;
        }
    }

#else

	if (audioPlayer == NULL)
	{
		audioPlayer = [[NSSound alloc] initWithContentsOfFile: [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], music] byReference:YES];
        
		[audioPlayer setLoops : (onLoop) ? YES : NO];
		audioPlayer.volume = music_volume;
        audioPlayer.currentTime = startTime;
        //[audioPlayer setCurrentTime:startTime];
	}
    
    //printf("playing <%s> : ",musicChar); if (!audioPlayer) printf("ERROR\n"); else printf("ok\n");
    
#endif

	if (audioPlayer)
		[audioPlayer play];

}

//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void PlayExtra(char * musicChar,float startTime,bool onLoop)
{

#ifndef USE_SOUNDS
    return;
#endif

    if (extraPlayer)
        StopExtra();
    
    NSString* music = [NSString stringWithUTF8String: musicChar];	
	
#if TARGET_OS_IPHONE
   
	NSError *error;
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], music]];
	if (extraPlayer == NULL && url)
	{
		extraPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        if (extraPlayer)
        {
            extraPlayer.numberOfLoops = (onLoop) ? -1 : 0;
            extraPlayer.volume = extra_volume;
            extraPlayer.currentTime = startTime;
        }
    }

#else

	if (extraPlayer == NULL)
	{
		extraPlayer = [[NSSound alloc] initWithContentsOfFile: [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], music] byReference:YES];
        
		[extraPlayer setLoops : (onLoop) ? YES : NO];
		extraPlayer.volume = extra_volume;
        extraPlayer.currentTime = startTime;
	}
    
    //printf("playing <%s> : ",musicChar); if (!extraPlayer) printf("ERROR\n"); else printf("ok\n");
    
#endif

	if (extraPlayer)
		[extraPlayer play];

}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------

void PlaySpeech(char* speechChar)
{

#ifndef USE_SOUNDS
    return;
#endif

    if (speechPlayer)
        StopSpeech();
        
    NSString* speechPath = [NSString stringWithUTF8String: speechChar];
    
#if TARGET_OS_IPHONE

	NSError *error;
    NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], speechPath]];
	if (speechPlayer == NULL && url)
	{
		speechPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error];
        if (speechPlayer)
        {
            speechPlayer.numberOfLoops = 0;
            speechPlayer.volume = speech_volume;
        }
	}
    
#else

    if (speechPlayer == NULL)
	{
		speechPlayer = [[NSSound alloc] initWithContentsOfFile: [NSString stringWithFormat:@"%@/%@", [[NSBundle mainBundle] resourcePath], speechPath] byReference:YES];
		[speechPlayer setLoops : NO];
		speechPlayer.volume = speech_volume;
	}
    
    //printf("playing <%s> : ",speechChar); if (!speechPlayer) printf("ERROR\n"); else printf("ok\n");
    
#endif
    
	if (speechPlayer)
		[speechPlayer play];
}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void PauseMusic(bool onPause)
{

#ifndef USE_SOUNDS
    return;
#endif

	if (audioPlayer)
    {
        if (!onPause)
#if TARGET_OS_IPHONE
            [audioPlayer play];
#else
            [audioPlayer resume];
#endif
        else
            [audioPlayer pause];
    }
}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void PauseSpeech(bool onPause)
{

#ifndef USE_SOUNDS
    return;
#endif

	if (speechPlayer)
    {
        if (!onPause)
#if TARGET_OS_IPHONE
            [speechPlayer play];
#else
            [speechPlayer resume];
#endif
        else
            [speechPlayer pause];
    }
}

//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void StopExtra()
{

#ifndef USE_SOUNDS
    return;
#endif

    if (extraPlayer != nil)
	{	
        [extraPlayer stop];
        [extraPlayer release];
    }
	extraPlayer = nil;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
void StopMusic()
{

#ifndef USE_SOUNDS
    return;
#endif

    if (audioPlayer != nil)
	{	
        [audioPlayer stop];
        [audioPlayer release];
    }
	audioPlayer = nil;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
void StopSpeech()
{

#ifndef USE_SOUNDS
    return;
#endif

    if (speechPlayer != nil)
	{	
        [speechPlayer stop];
        [speechPlayer release];
    }
    speechPlayer = nil;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
void SetMusicVolume(float volume)
{

#ifndef USE_SOUNDS
    return;
#endif

	music_volume = volume;
	if (audioPlayer != nil)
		audioPlayer.volume = music_volume;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
void SetExtraVolume(float volume)
{

#ifndef USE_SOUNDS
    return;
#endif

	extra_volume = volume;
	if (extraPlayer != nil)
		extraPlayer.volume = extra_volume;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
void SetSpeechVolume(float volume)
{

#ifndef USE_SOUNDS
    return;
#endif

	speech_volume = volume;
	if (speechPlayer != nil)
		speechPlayer.volume = speech_volume;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
int GetSpeechStatus()
{

#ifndef USE_SOUNDS
    return true;
#endif

    if (speechPlayer)
        return speechPlayer.isPlaying;

    return false;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
int GetExtraStatus()
{

#ifndef USE_SOUNDS
    return true;
#endif

    if (extraPlayer)
        return extraPlayer.isPlaying;

    return false;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
int GetMusicStatus()
{

#ifndef USE_SOUNDS
    return true;
#endif

    if (audioPlayer)
        return audioPlayer.isPlaying;

    return false;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
float GetSpeechTime()
{

#ifndef USE_SOUNDS
    return 0;
#endif

    if (speechPlayer)
        return (float)speechPlayer.currentTime;
    
    return 0;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
float GetMusicTime()
{

#ifndef USE_SOUNDS
    return 0;
#endif

    if (audioPlayer)
        return (float)audioPlayer.currentTime;
    
    return 0;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
float GetExtraTime()
{

#ifndef USE_SOUNDS
    return 0;
#endif

    if (extraPlayer)
        return (float)extraPlayer.currentTime;
    
    return 0;
}
//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
float GetMusicVolume()
{

#ifndef USE_SOUNDS
    return 0;
#endif

	return music_volume;
}

//----------------------------------------------------------------------------------
// 
//----------------------------------------------------------------------------------
/*
bool WasShaked(cScene* scene, float accX,float accY,float accZ, float currentTimer)
{

	if (startShakeTimer == 0 || (currentTimer - startShakeTimer > 50))
	{	
		startShakeTimer = currentTimer;
		oldaccX = accX;
		oldaccY = accY;
		oldaccZ = accZ;
		return false;
	}

	bool ret = false;
	if (
		(accX > oldaccX +scene->shakeSensitivity || accX < oldaccX -scene->shakeSensitivity) ||
		(accY > oldaccY +scene->shakeSensitivity || accY < oldaccY -scene->shakeSensitivity) ||
		(accZ > oldaccZ +scene->shakeSensitivity || accZ < oldaccZ -scene->shakeSensitivity)
		)
		{
			if (++shaked > 4)
			{
				ret = true;
				startShakeTimer = currentTimer;
				shaked = 0;
				
			}
		}
		
	oldaccX = accX;
	oldaccY = accY;
	oldaccZ = accZ;
	return ret;
}
*/
//----------------------------------------------------------------------------------
// controlla gli accelerometri, se richiesto aggiorna la posizione della camera
//----------------------------------------------------------------------------------
			
bool IdleAccelerators(cScene* scene,int flag)
{
	if ((scene->accelerometerX || scene->accelerometerY) && (scene->acceleration.x || scene->acceleration.y))
	{

		if (scene->acceleration.x > 0)
        {
            if ((scene->acceleration.x -= scene->accelerometerFactor.x) < 0) scene->acceleration.x = 0;
		}
        else if (scene->acceleration.x < 0)
        {
            if ((scene->acceleration.x += scene->accelerometerFactor.x) > 0) scene->acceleration.x = 0;
        }
        
		if (scene->acceleration.y > 0)
        {
            if ((scene->acceleration.y -= scene->accelerometerFactor.y) < 0) scene->acceleration.y = 0;
		}
        else if (scene->acceleration.y < 0)
        {
            if ((scene->acceleration.y += scene->accelerometerFactor.y) > 0) scene->acceleration.y = 0;
        }
		UpdateCameraPositionFromAccelerators(scene,flag,scene->acceleration.x,scene->acceleration.y);
		return true;
	}
	return false;
}

//----------------------------------------------------------------------------------
// controlla i drag, se richiesto aggiorna la posizione della camera
//----------------------------------------------------------------------------------
bool IdleTouches(cScene* scene,int flag)
{
	if (scene->touch && (scene->touchpos.x || scene->touchpos.y))
	{
		if (scene->touchpos.x > 0) if ((scene->touchpos.x -= scene->touchAccelFactor.x) < 0) scene->touchpos.x = 0;
		if (scene->touchpos.x < 0) if ((scene->touchpos.x += scene->touchAccelFactor.x) > 0) scene->touchpos.x = 0;
		if (scene->touchpos.y > 0) if ((scene->touchpos.y -= scene->touchAccelFactor.y) < 0) scene->touchpos.y = 0;
		if (scene->touchpos.y < 0) if ((scene->touchpos.y += scene->touchAccelFactor.y) > 0) scene->touchpos.y = 0;
		UpdateCameraPositionFromTouches(scene,flag,scene->touchpos.x,scene->touchpos.y);
		return true;
	}
	return false;
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
int BuildCube(cScene* scene,float dim,char* tex1,char* tex2,char* tex3,char* tex4,char* tex5,char* tex6)
{
    for (int i=0;i< MAX_ENTITIES;i++)
		if (scene->pObject[i] == NULL)
		{
			scene->pObject[i] = new cObj(scene);
			
			cObj* tObj = scene->pObject[i];
            
			//sub
			tObj->totObjects = 6;
			tObj->subObj     = (cSub*)malloc( sizeof(cSub) * tObj->totObjects );
			memset(tObj->subObj,0,sizeof(cSub) * tObj->totObjects);
			
            for (int o = 0; o < tObj->totObjects; o++)
            {
                cSub* subObj = &tObj->subObj[o];
                
                subObj->totMaps		= 4;
                subObj->totVertex   = 4;
                subObj->vertex		= (cVertex*)malloc(sizeof(cVertex)*subObj->totVertex );
                subObj->normal      = (cVertex*)malloc(sizeof(cVertex)*subObj->totVertex );
                subObj->map			= (cTex*)malloc(sizeof(cTex)*subObj->totMaps);
                
                //vertici FRONTE
                if (o == 0)
                {
                    subObj->vertex[0].Set(-dim,+dim,+dim);
                    subObj->vertex[1].Set(+dim,+dim,+dim);
                    subObj->vertex[2].Set(+dim,-dim,+dim);
                    subObj->vertex[3].Set(-dim,-dim,+dim);
                }
                //DESTRA
                else if (o == 1)
                {
                    subObj->vertex[0].Set(+dim,+dim,+dim);
                    subObj->vertex[1].Set(+dim,+dim,-dim);
                    subObj->vertex[2].Set(+dim,-dim,-dim);
                    subObj->vertex[3].Set(+dim,-dim,+dim);
                }
                //SINISTRA
                else if (o == 2)
                {
                    subObj->vertex[0].Set(-dim,+dim,-dim);
                    subObj->vertex[1].Set(-dim,+dim,+dim);
                    subObj->vertex[2].Set(-dim,-dim,+dim);
                    subObj->vertex[3].Set(-dim,-dim,-dim);
                }
                //DIETRO
                else if (o == 3)
                {
                    subObj->vertex[0].Set(+dim,+dim,-dim);
                    subObj->vertex[1].Set(-dim,+dim,-dim);
                    subObj->vertex[2].Set(-dim,-dim,-dim);
                    subObj->vertex[3].Set(+dim,-dim,-dim);
                }
                //SOPRA
                else if (o == 4)
                {
                    subObj->vertex[0].Set(-dim,+dim,-dim);
                    subObj->vertex[1].Set(-dim,+dim,+dim);
                    subObj->vertex[2].Set(+dim,+dim,+dim);
                    subObj->vertex[3].Set(+dim,+dim,-dim);
                }
                //SOTTO
                else if (o == 5)
                {
                    subObj->vertex[0].Set(-dim,-dim,+dim);
                    subObj->vertex[1].Set(+dim,-dim,+dim);
                    subObj->vertex[2].Set(+dim,-dim,-dim);
                    subObj->vertex[3].Set(-dim,-dim,-dim);
                }
                
                //ciclo textures
                subObj->map[0].Set(0,1);
                subObj->map[1].Set(1,1);
                subObj->map[2].Set(1,0);
                subObj->map[3].Set(0,0);
                
                //materiale
                subObj->totMaterials = 1;
                subObj->mat = (cMat*)malloc(sizeof(cMat) * subObj->totMaterials);
                subObj->mat[0].totFaces = 2;
                subObj->mat[0].face   = (cFace*)  malloc(sizeof(cFace)    *subObj->mat[0].totFaces);
                subObj->mat[0].normal = (cVertex*)malloc(sizeof(cVertex)  *subObj->mat[0].totFaces);
                subObj->mat[0].diffuse.Set(1,1,1,1);
                subObj->mat[0].ambient.Set(1,1,1,1);
                subObj->mat[0].specular.Set(1,1,1,1);
                
                subObj->mat[0].tex_diffuse = 0;
                
                if (o == 0)
                    subObj->mat[0].tex_diffuse = LoadTexture(scene,tex1,GL_CLAMP_TO_EDGE,true);
                else if  (o == 1)
                    subObj->mat[0].tex_diffuse = LoadTexture(scene,tex2,GL_CLAMP_TO_EDGE,true);
                else if  (o == 2)
                    subObj->mat[0].tex_diffuse = LoadTexture(scene,tex3,GL_CLAMP_TO_EDGE,true);
                else if  (o == 3)
                    subObj->mat[0].tex_diffuse = LoadTexture(scene,tex4,GL_CLAMP_TO_EDGE,true);
                else if  (o == 4)
                    subObj->mat[0].tex_diffuse = LoadTexture(scene,tex5,GL_CLAMP_TO_EDGE,true);
                else if  (o == 5)
                    subObj->mat[0].tex_diffuse = LoadTexture(scene,tex6,GL_CLAMP_TO_EDGE,true);

                subObj->mat[0].face[0].v[0] = 0;
                subObj->mat[0].face[0].v[1] = 1;
                subObj->mat[0].face[0].v[2] = 2;
                
                subObj->mat[0].face[1].v[0] = 0;
                subObj->mat[0].face[1].v[1] = 2;
                subObj->mat[0].face[1].v[2] = 3;
			
            }
            
			tObj->cullMode = 2;//GL_CW;
            tObj->SetColor(1,1,1,1);
			tObj->SetAngle(0,0,0);
			tObj->SetPosition(0,0,0);
			tObj->depthTest = true;
			tObj->SetActive(true);
			
			++scene->totObjs;
			
			return i;
		}
    
	return 0;
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------

int LoadTexture(cScene* scene,char* texName,int texMode,bool useFilter)
{
    if (strstr(texName,"null"))
        return scene->LoadTexture((char*)"null.png",texMode,useFilter);
    
    char fullfName[255];
    sprintf(fullfName,"%s.png",texName);
    int ret = scene->LoadTexture(fullfName,texMode,useFilter);
    if (ret == -1)
    {
        sprintf(fullfName,"%s.jpg",texName);
        ret = scene->LoadTexture(fullfName,texMode,useFilter);
    }
    
    if (ret == -1)
        printf("TEXTURE ERROR LOADING <%s>",texName);
    
    return ret;
} 

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

int SwapTexture(cScene* scene, int idx,char* texName,int texMode,bool useFilter)
{ 
	if (scene->pTexture[idx])
		SAFE_DELETE(scene->pTexture[idx]);
    
	return LoadTexture(scene,texName,texMode,useFilter);
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void ResizeObject(cScene* scene,int numObj,float dimx,float dimy)
{
    cSub* subObj = &scene->pObject[numObj]->subObj[0];
    
    float zVal = subObj[0].vertex[0].z;
    
    subObj[0].vertex[0].Set(-dimx,+dimy,zVal);
    subObj[0].vertex[1].Set(+dimx,+dimy,zVal);
    subObj[0].vertex[2].Set(+dimx,-dimy,zVal);
    subObj[0].vertex[3].Set(-dimx,-dimy,zVal);
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
float MapSize(float imageSize)
{
    return ((float)imageSize * 1.5f) / 1024.0f;
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void InvertUMaps(cScene* scene, int objId)
{
    if (!scene->pObject[objId])
        return;
    
    cSub* subObj = &scene->pObject[objId]->subObj[0];

    subObj[0].map[0].u = 1;
    subObj[0].map[1].u = 0;
    subObj[0].map[2].u = 0;
    subObj[0].map[3].u = 1;
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
int BuildObject(cScene* scene,float px,float py,float pz,float dimx,float dimy,char* texName,int texMode,bool useFilter)
{
    for (int i=0;i< MAX_ENTITIES;i++)
		if (scene->pObject[i] == NULL)
		{
			scene->pObject[i] = new cObj(scene);
			
			cObj* tObj = scene->pObject[i];

			//sub
			tObj->totObjects = 1;
			tObj->subObj  = (cSub*)malloc(sizeof(cSub)*(tObj->totObjects));
			memset(tObj->subObj,0,sizeof(cSub)*(tObj->totObjects));
			
			cSub* subObj = &tObj->subObj[0];

			subObj[0].totMaps		= 4;
			subObj[0].totVertex		= 4;
			subObj[0].vertex		= (cVertex*)malloc(sizeof(cVertex)*subObj[0].totVertex );
			subObj[0].normal		= (cVertex*)malloc(sizeof(cVertex)*subObj[0].totVertex );
			subObj[0].map			= (cTex*)malloc(sizeof(cTex)*subObj[0].totMaps);
			
			//ciclo vertici
			subObj[0].vertex[0].Set(-dimx,+dimy,0.0f);
			subObj[0].vertex[1].Set(+dimx,+dimy,0.0f);
			subObj[0].vertex[2].Set(+dimx,-dimy,0.0f);
			subObj[0].vertex[3].Set(-dimx,-dimy,0.0f);
		
			//ciclo textures
			subObj[0].map[0].Set(0,1);
			subObj[0].map[1].Set(1,1);
			subObj[0].map[2].Set(1,0);
			subObj[0].map[3].Set(0,0);

			//materiale
			subObj[0].totMaterials = 1;
			subObj[0].mat = (cMat*)malloc(sizeof(cMat) * subObj[0].totMaterials);
			subObj[0].mat[0].totFaces = 2;
			subObj[0].mat[0].face   = (cFace*)  malloc(sizeof(cFace)    *subObj[0].mat[0].totFaces);
			subObj[0].mat[0].normal = (cVertex*)malloc(sizeof(cVertex)  *subObj[0].mat[0].totFaces);
			
			subObj[0].mat[0].diffuse.Set(1,1,1,1);
			subObj[0].mat[0].ambient.Set(1,1,1,1);
			subObj[0].mat[0].specular.Set(1,1,1,1);
			
			subObj[0].mat[0].tex_diffuse = 0;
			if (texName)
				subObj[0].mat[0].tex_diffuse = LoadTexture(scene,texName,texMode,useFilter);
			
			subObj[0].mat[0].face[0].v[0] = 0;
			subObj[0].mat[0].face[0].v[1] = 1;
			subObj[0].mat[0].face[0].v[2] = 2;
			
			subObj[0].mat[0].face[1].v[0] = 0;
			subObj[0].mat[0].face[1].v[1] = 2;
			subObj[0].mat[0].face[1].v[2] = 3;
			
			tObj->cullMode = 2;

			tObj->SetColor(1,1,1,1);
			tObj->SetAngle(0,0,0);
			tObj->SetPosition(px, py, pz);
			
			tObj->CalcNormals();
			tObj->CreateBoundBoxes();
			tObj->depthTest = true;
			
			tObj->SetActive(true);
			
			//ricalcola end-object
            for (int p=0;p< MAX_ENTITIES;p++)
                if (scene->pObject[p])
                    scene->totObjs = p+1;
        
            //old
			//++scene->totObjs;
			
			return i;
		}
		
	return 0;
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
int BuildDoubleObject(cScene* scene,float px,float py,float pz,float dimx,float dimy,char* texName1,char* texName2, int texMode,bool useFilter)
{

	for (int i=0;i< MAX_ENTITIES;i++)
		if (scene->pObject[i] == NULL)
		{
			scene->pObject[i] = new cObj(scene);
			
			cObj* tObj = scene->pObject[i];

			//sub
			tObj->totObjects    = 1;
			tObj->subObj        = (cSub*)malloc(sizeof(cSub)*(tObj->totObjects));
			memset(tObj->subObj,0,sizeof(cSub)*(tObj->totObjects));
			
            cSub* subObj = &tObj->subObj[0];
                
            subObj[0].totMaps		= 4;
            subObj[0].totVertex     = 4;
            subObj[0].vertex		= (cVertex*)malloc(sizeof(cVertex)*subObj[0].totVertex );
            subObj[0].normal		= (cVertex*)malloc(sizeof(cVertex)*subObj[0].totVertex );
            subObj[0].map			= (cTex*)malloc(sizeof(cTex)*subObj[0].totMaps);
            
            //ciclo vertici
            subObj[0].vertex[0].Set(-dimx,+dimy,0.0f);
            subObj[0].vertex[1].Set(+dimx,+dimy,0.0f);
            subObj[0].vertex[2].Set(+dimx,-dimy,0.0f);
            subObj[0].vertex[3].Set(-dimx,-dimy,0.0f);
            
            //ciclo textures
            subObj[0].map[0].Set(0,1);
            subObj[0].map[1].Set(1,1);
            subObj[0].map[2].Set(1,0);
            subObj[0].map[3].Set(0,0);
            
            //sottooggetti (2)
            subObj[0].totMaterials = 2;
            subObj[0].mat = (cMat*)malloc(sizeof(cMat) * subObj[0].totMaterials);
            
            for (int mat=0; mat<2; mat++)
            {
                //materiale
                
                subObj[0].mat[mat].totFaces = 2;
                subObj[0].mat[mat].face    = (cFace*)  malloc(sizeof(cFace)    *subObj[0].mat[mat].totFaces);
                subObj[0].mat[mat].normal  = (cVertex*)malloc(sizeof(cVertex)  *subObj[0].mat[mat].totFaces);
                
                subObj[0].mat[mat].diffuse.Set(1,1,1,1);
                subObj[0].mat[mat].ambient.Set(1,1,1,1);
                subObj[0].mat[mat].specular.Set(1,1,1,1);

                subObj[0].mat[mat].tex_diffuse = LoadTexture(scene,((mat==0) ? texName2 : texName1),texMode,useFilter);
                
                /*
                0-----------1
                |           |
                |           |
                |           |
                3-----------2
                */
                
                //front
                if (mat == 0)
                {
                    subObj[0].mat[mat].face[0].v[0] = 0;
                    subObj[0].mat[mat].face[0].v[1] = 1;
                    subObj[0].mat[mat].face[0].v[2] = 2;
                
                    subObj[0].mat[mat].face[1].v[0] = 0;
                    subObj[0].mat[mat].face[1].v[1] = 2;
                    subObj[0].mat[mat].face[1].v[2] = 3;
                }
                //rear
                else if (mat == 1)
                {
                    subObj[0].mat[mat].face[0].v[0] = 1;
                    subObj[0].mat[mat].face[0].v[1] = 0;
                    subObj[0].mat[mat].face[0].v[2] = 3;
                
                    subObj[0].mat[mat].face[1].v[0] = 1;
                    subObj[0].mat[mat].face[1].v[1] = 3;
                    subObj[0].mat[mat].face[1].v[2] = 2;
                }
			}
            
			tObj->cullMode = GL_CW;

			tObj->SetColor(1,1,1,1);
			tObj->SetAngle(0,0,0);
			tObj->SetPosition(px, py, pz);
			
			tObj->CalcNormals();
			tObj->CreateBoundBoxes();
			tObj->depthTest = true;
			
			tObj->SetActive(true);
			
			++scene->totObjs;
			
			return i;
		}
		
	return 0;
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
int BuildGrid(cScene* scene,float px,float py,float pz,float dimx,float dimy,int numVertices,char* texName,int texMode,bool useFilter)
{

	for (int i=0;i< MAX_ENTITIES;i++)
		if (scene->pObject[i] == NULL)
		{
			scene->pObject[i] = new cObj(scene);
			
			cObj* tObj = scene->pObject[i];

			//sub
			tObj->totObjects = 1;
			tObj->subObj  = (cSub*)malloc(sizeof(cSub)*(tObj->totObjects));
			memset(tObj->subObj,0,sizeof(cSub)*(tObj->totObjects));
			
			cSub* subObj = &tObj->subObj[0];

			//printf("begin grid as obj %d, step %d\n",i,numVertices);
			
			subObj[0].totVertex		= (int)(numVertices * numVertices);
			subObj[0].totMaps		= (int)(numVertices * numVertices);
			subObj[0].vertex		= (cVertex*)malloc(sizeof(cVertex)*subObj[0].totVertex );
			subObj[0].normal		= (cVertex*)malloc(sizeof(cVertex)*subObj[0].totVertex );
			subObj[0].map			= (cTex*)malloc(sizeof(cTex)*subObj[0].totMaps);
			
			//ciclo vertici e uvmap 
			float deltax  = (dimx*2)/(float)(numVertices-1);
			float deltay  = (dimy*2)/(float)(numVertices-1);
			
			float deltam  = 1.0f/(float)(numVertices-1);
			
			float currm_u = 0;
			float currm_v = 0;
			
			int numVertex = -1;
			for (float y = -dimy; y<= +dimy; y+=deltay)
			{	
				for (float x = -dimx; x<= +dimx; x+=deltax)
				{
					if ((++numVertex) >= subObj[0].totVertex)
						break;
					
					subObj[0].vertex[numVertex].Set(x,y,0.0f);
					subObj[0].map[numVertex].Set(currm_u,currm_v);
					
					//printf("vertex %d coord %.2f %.2f uvmap %.2f %.2f\n",numVertex,x,y,currm_u,currm_v);
					
					currm_u += deltam;
				}
				currm_u = 0;
				currm_v += deltam;
			}
			
			//printf("allocated %d vertices\n",subObj[0].totVertex);
			
			//materiale
			subObj[0].totMaterials = 1;
			subObj[0].mat = (cMat*)malloc(sizeof(cMat) * subObj[0].totMaterials);
			subObj[0].mat[0].totFaces = ((numVertices-1) * (numVertices-1)) * 2;
			subObj[0].mat[0].face   = (cFace*)  malloc(sizeof(cFace)    *subObj[0].mat[0].totFaces);
			subObj[0].mat[0].normal = (cVertex*)malloc(sizeof(cVertex)  *subObj[0].mat[0].totFaces);
			
			subObj[0].mat[0].diffuse.Set(1,1,1,1);
			subObj[0].mat[0].ambient.Set(1,1,1,1);
			subObj[0].mat[0].specular.Set(1,1,1,1);
			
			subObj[0].mat[0].tex_diffuse = 0;
			if (texName)
				subObj[0].mat[0].tex_diffuse = LoadTexture(scene,texName,texMode,useFilter);
			
			int f = 0;
			for (int y=0; y<(numVertices-1);y++)
				for (int x=0; x<(numVertices-1);x++)
				{
					int currVertex = (y*(float)numVertices) + x;
					if (currVertex+1+numVertices >= subObj[0].totVertex)
						break;
						
					subObj[0].mat[0].face[f].v[0]	= currVertex;
					subObj[0].mat[0].face[f].v[1]	= currVertex+1;
					subObj[0].mat[0].face[f].v[2]	= currVertex+1+numVertices;
			
					//printf("face %d vertices %d %d %d\n",f,subObj[0].mat[0].face[f].v[0],subObj[0].mat[0].face[f].v[1],subObj[0].mat[0].face[f].v[2]);
					
					subObj[0].mat[0].face[f+1].v[0] = currVertex;
					subObj[0].mat[0].face[f+1].v[1] = currVertex+1+numVertices;
					subObj[0].mat[0].face[f+1].v[2] = currVertex+numVertices;
					
					//printf("face %d vertices %d %d %d\n",f+1,subObj[0].mat[0].face[f+1].v[0],subObj[0].mat[0].face[f+1].v[1],subObj[0].mat[0].face[f+1].v[2]);
					
					if ((f+=2) >= subObj[0].mat[0].totFaces)
						break;
				}

			//printf("allocated %d faces\n",subObj[0].mat[0].totFaces);
			tObj->cullMode = 2;

			tObj->SetColor(1,1,1,1);
			tObj->SetAngle(0,0,0);
			tObj->SetPosition(px, py, pz);
			
			//tObj->CalcNormals();
			tObj->CreateBoundBoxes();
			tObj->depthTest = true;
			
			tObj->SetActive(true);
			
			++scene->totObjs;
			
			return i;
		}
		
	return 0;
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void OffsetSubObj(cScene* scene,int numObj,int numSub,float amountX,float amountY)
{
    cObj* tObj = scene->pObject[numObj];
    
    for (int i=0; i<4; i++)
    {
        tObj->subObj[numSub].vertex[i].x += amountX;
        tObj->subObj[numSub].vertex[i].y += amountY;
    }
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void RotateSubObj(cScene* scene,int numObj,int numSub,float amountZ)
{
    cObj* tObj = scene->pObject[numObj];
    
    for (int i=0; i<4; i++)
         tObj->subObj[numSub].vertex[i].RotateZ(amountZ);
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
int AddSubObj(cScene* scene,int numObj,float px,float py,float pz,float dimx,float dimy,char* texName,int texMode,bool useFilter)
{

	cObj* tObj = scene->pObject[numObj];
	
	int idx = tObj->totObjects;
	tObj->subObj = (cSub*)realloc(tObj->subObj,sizeof(cSub) * (tObj->totObjects+1));
	memset(&tObj->subObj[idx],0,sizeof(cSub));
	++tObj->totObjects;
	
	tObj->subObj[idx].totMaps		= 4;
	tObj->subObj[idx].totVertex		= 4;
	tObj->subObj[idx].vertex		= (cVertex*)malloc(sizeof(cVertex)*tObj->subObj[idx].totVertex );
	tObj->subObj[idx].normal		= (cVertex*)malloc(sizeof(cVertex)*tObj->subObj[idx].totVertex );
	tObj->subObj[idx].map			= (cTex*)malloc(sizeof(cTex)*tObj->subObj[idx].totMaps);
	
	//ciclo vertici
	tObj->subObj[idx].vertex[0].Set(px -dimx,py +dimy, pz+0.0f);
	tObj->subObj[idx].vertex[1].Set(px +dimx,py +dimy, pz+0.0f);
	tObj->subObj[idx].vertex[2].Set(px +dimx,py -dimy, pz+0.0f);
	tObj->subObj[idx].vertex[3].Set(px -dimx,py -dimy, pz+0.0f);
	
	//ciclo textures
	tObj->subObj[idx].map[0].Set(0,1);
	tObj->subObj[idx].map[1].Set(1,1);
	tObj->subObj[idx].map[2].Set(1,0);
	tObj->subObj[idx].map[3].Set(0,0);
	
	//materiale
	tObj->subObj[idx].totMaterials = 1;
	tObj->subObj[idx].mat = (cMat*)malloc(sizeof(cMat));
	tObj->subObj[idx].mat[0].totFaces = 2;
	tObj->subObj[idx].mat[0].face   = (cFace*)  malloc(sizeof(cFace)    * tObj->subObj[idx].mat[0].totFaces);
	tObj->subObj[idx].mat[0].normal = (cVertex*)malloc(sizeof(cVertex)  * tObj->subObj[idx].mat[0].totFaces);
	
	tObj->subObj[idx].mat[0].diffuse.Set(1,1,1,1);
	tObj->subObj[idx].mat[0].ambient.Set(1,1,1,1);
	tObj->subObj[idx].mat[0].specular.Set(1,1,1,1);
	
	tObj->subObj[idx].mat[0].tex_diffuse = 0;
	if (texName)
		tObj->subObj[idx].mat[0].tex_diffuse = LoadTexture(scene,texName,texMode,useFilter);
	
	tObj->subObj[idx].mat[0].face[0].v[0] = 0;
	tObj->subObj[idx].mat[0].face[0].v[1] = 1;
	tObj->subObj[idx].mat[0].face[0].v[2] = 2;
	
	tObj->subObj[idx].mat[0].face[1].v[0] = 0;
	tObj->subObj[idx].mat[0].face[1].v[1] = 2;
	tObj->subObj[idx].mat[0].face[1].v[2] = 3;

	tObj->CalcNormals();
	tObj->CreateBoundBoxes();

	return scene->pObject[numObj]->totObjects;
}

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void TileTexture(cObj* pObject, float repX,float repY,float compensateDeltaX,float compensateDeltaY)
{
	//compensa i bordi fottuti 
	float deltaBorderX	= compensateDeltaX;
	float deltaBorderY	= compensateDeltaY;
	
	pObject->subObj[0].map[0].u = 0    + deltaBorderX;  pObject->subObj[0].map[0].v = repY - deltaBorderY;
	pObject->subObj[0].map[1].u = repX - deltaBorderX;	pObject->subObj[0].map[1].v = repY - deltaBorderY;
	pObject->subObj[0].map[2].u = repX - deltaBorderX;	pObject->subObj[0].map[2].v = 0    + deltaBorderY;
	pObject->subObj[0].map[3].u = 0    + deltaBorderX;	pObject->subObj[0].map[3].v = 0    + deltaBorderY;
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void TileSubTexture(cObj* pObject, int sub, float repX,float repY,float compensateDeltaX,float compensateDeltaY)
{
	//compensa i bordi fottuti 
	float deltaBorderX	= compensateDeltaX;
	float deltaBorderY	= compensateDeltaY;
	
	pObject->subObj[sub].map[0].u = 0 + deltaBorderX;		pObject->subObj[sub].map[0].v = repY - deltaBorderY;
	pObject->subObj[sub].map[1].u = repX - deltaBorderX;	pObject->subObj[sub].map[1].v = repY - deltaBorderY;
	pObject->subObj[sub].map[2].u = repX - deltaBorderX;	pObject->subObj[sub].map[2].v = 0 + deltaBorderY;
	pObject->subObj[sub].map[3].u = 0 + deltaBorderX;		pObject->subObj[sub].map[3].v = 0 + deltaBorderY;
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void SwapTextureCoord(cObj* pObject, int sub, int senseX, int senseY)
{

	pObject->subObj[sub].map[0].u = (senseX)?0:1;		pObject->subObj[sub].map[0].v = (senseY)?1:0;
	pObject->subObj[sub].map[1].u = (senseX)?1:0;       pObject->subObj[sub].map[1].v = (senseY)?1:0;
	pObject->subObj[sub].map[2].u = (senseX)?1:0;       pObject->subObj[sub].map[2].v = (senseY)?0:1;
	pObject->subObj[sub].map[3].u = (senseX)?0:1;		pObject->subObj[sub].map[3].v = (senseY)?0:1;
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------

void InitSoundEngine()
{
#ifdef USE_SOUNDS
	
    #define kListenerDistance 1.0
    
	SoundEngine_Initialize(0);
	SoundEngine_SetListenerPosition(0.0, 0.0, kListenerDistance);
	SoundEngine_SetMasterVolume(1.0f);
	
	for (int i=0;i<MAX_SOUNDS;i++)
		_sounds[i] = 0;
		
#endif
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void SoundEffectVolume(int soundID, float volumeVal)
{
#ifdef USE_SOUNDS

    SoundEngine_SetEffectLevel(_sounds[soundID], volumeVal);

#endif
}
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
void SoundEffectPitch(int soundID, float pitchVal)
{
#ifdef USE_SOUNDS

    SoundEngine_SetEffectPitch(_sounds[soundID], pitchVal);

#endif
}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
/*
void LoadSounds(char* dataPath,int set,int localization)
{

    LoadSounds(dataPath,set,localization);
}
*/
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void EndSoundEngine()
{
#ifdef USE_SOUNDS
	
    for (int i=0;i<MAX_SOUNDS;i++)
        if (_sounds[i])
        {
            SoundEngine_StopEffect(_sounds[i], false);
            SoundEngine_UnloadEffect(_sounds[i]);
        }

	SoundEngine_Teardown();	

#endif

}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void SoundEffect(int soundID, bool start,bool vibrate)
{
#ifdef USE_SOUNDS

	if (start)
		SoundEngine_StartEffect(_sounds[soundID]);
	else
		SoundEngine_StopEffect(_sounds[soundID], false);

#endif
}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void TouchResponder(cScene* scene,int px,int py)
{
	if (!scene || !scene->renderActive || !scene->touch)
		return;

	scene->touchpos.x		= 0;
	scene->touchpos.y		= 0;
	oldEndPointx			= px;
	oldEndPointy			= py;

	scene->selectedObject = -1;
    scene->PickObject(px,py);
    
#ifdef USE_BOX2D

    int i = scene->selectedObject;
    if (i >= 0 && scene->pObject[i])
        if (!scene->m_mouseJoint && scene->pObject[i]->body && !scene->pObject[i]->p_static)
        {
            b2MouseJointDef md;
            md.bodyA		= scene->m_mouseBody;
            md.bodyB		= scene->pObject[i]->body;
            md.target		= scene->PhysicScreenToWorld(px,py,true);
            md.maxForce		= scene->m_mouseForce;
            md.dampingRatio = 0;
            md.frequencyHz	= 10;
            scene->m_mouseJoint = (b2MouseJoint*)scene->world->CreateJoint(&md);
            scene->pObject[i]->body->SetAwake(true);
        }

#endif

    
#ifdef DEBUG
    b2Vec2 bt   = scene->PhysicScreenToWorld(px,py,true);
    printf("%.2f,%.2f [%d]\n",bt.x,bt.y,scene->selectedObject);
#endif

    SceneTouchResponder(px,py);
    
}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void DragResponder(cScene* scene,int sx,int sy,int ex,int ey)
{
	if (!scene || !scene->renderActive || !scene->touch)
		return;

	scene->touchpos.x = (oldEndPointx - (float)ex) / scene->touchDivider.x;
	scene->touchpos.y = ((float)ey - oldEndPointy) / scene->touchDivider.y;
	oldEndPointx = ex;
	oldEndPointy = ey;

#ifdef USE_BOX2D
	int i = scene->selectedObject;
	if (i >= 0 && !scene->pObject[i]->p_static && scene->pObject[i]->body)
        if (scene->m_mouseJoint)
            scene->m_mouseJoint->SetTarget(scene->PhysicScreenToWorld(ex,ey,true));
#endif
    
    SceneDragResponder(sx,sy,ex,ey);

}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void ZoomResponder(cScene* scene,float distance)
{

}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------

void MultiTouchResponder(cScene* scene,int opCode,int p1x,int p1y,int p2x,int p2y)
{
	if (!scene || !scene->renderActive || !scene->touch)
		return;

	SceneMultiTouchResponder(opCode,p1x,p1y,p2x,p2y);
}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
#ifdef USE_BOX2D
void ContactResponder(cScene* scene,b2Body* bodyA,b2Body* bodyB)
{
    if (bodyA && bodyB)
        SceneContactResponder(bodyA,bodyB);
}
#endif
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------
void ReleaseResponder(cScene* scene,int px,int py)
{
	if (!scene || !scene->renderActive || !scene->touch)
		return;

	if (scene->selectedObject < 0)
	{
		float delta = 1;
		if (scene->touchpos.x > delta) scene->touchpos.x = delta; else if (scene->touchpos.x < -delta) scene->touchpos.x = -delta;
		if (scene->touchpos.y > delta) scene->touchpos.y = delta; else if (scene->touchpos.y < -delta) scene->touchpos.y = -delta;
	}
	else
	{
		scene->touchpos.x = 0;
		scene->touchpos.y = 0;
	}
	
#ifdef USE_BOX2D
	if (scene->m_mouseJoint)
	{
		scene->world->DestroyJoint(scene->m_mouseJoint);
		scene->m_mouseJoint = NULL;
	}
#endif

    SceneReleaseResponder(px,py);
	scene->selectedObject = -1;
    
}
//---------------------------------------------------------------------------------
//
//---------------------------------------------------------------------------------

