/* ============================================================================
 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL DINGOO GAMES OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ============================================================================ */ 

#include "../inc/GameEngine.h"

//--------------------------------------------------------------------------------------------------------
GameEngine::GameEngine( void* userdata [] ) : 
					Engine( userdata )
{
	m_pAudioData = NULL;
}
//--------------------------------------------------------------------------------------------------------
GameEngine::~GameEngine( void )
{
	Free();
}
//--------------------------------------------------------------------------------------------------------
/// init all resources
result	GameEngine::Init( void )
{
	InitDLRes();

	// 16 FPS
	SetFPS( 24 );

	m_pImgS2d =new Texture();
    m_pImgS2d->CreateFromFile(_LS("ui_logo_part1.s3dtex"));

	m_pImgS2d_alpha =new Texture();
    m_pImgS2d_alpha->CreateFromFile(_LS("ui_logo_part1_alpha.s3dtex"));
	
	pos[0] = 0;
	pos[1] = 0;

	return S2D_OK;
}
//--------------------------------------------------------------------------------------------------------
/// free all resources
void	GameEngine::Free( void )
{
	ReleaseDLRes();
	SAFE_DELETE_ARRAY( m_pAudioData );
}
//--------------------------------------------------------------------------------------------------------
/// game logic before render
void	GameEngine::Exec( void )
{
}
//--------------------------------------------------------------------------------------------------------
/// game logic after render
void	GameEngine::Exec2( void )
{
}
//--------------------------------------------------------------------------------------------------------
/// pre-render proccess
void	GameEngine::PreRender( void )
{

}
//--------------------------------------------------------------------------------------------------------
/// render
void	GameEngine::Render( void )
{

	////------------DrawOverlay
    //m_pDraw2D->SetTexture( m_pImgS2d );
    //m_pDraw2D->DrawOverlay(0,0,NULL);

	////------------SetFilpType
	//m_pDraw2D->SetFilpType(FLIP_HORIZONTAL);
	//m_pDraw2D->SetTexture( m_pImgS2d );
	//m_pDraw2D->DrawOverlay(0,0,NULL);


	////------------DrawOverlay & SetOverlayBlendMode
	//m_pDraw2D->SetTexture( m_pImgS2d );
	//m_pDraw2D->DrawOverlay(10,0,NULL);
	//m_pDraw2D->SetOverlayBlendMode( OBM_SRC50);
	//m_pDraw2D->DrawOverlay(50,0,NULL);

	////------------DrawStretchOverlay
	m_pDraw2D->SetTexture( m_pImgS2d );

	Rect rcTest; 

	pos[0] += 1;
	pos[1] += 1;
	rcTest.left =  pos[0];
	rcTest.top =  pos[1];

	rcTest.right = rcTest.left + m_pImgS2d->GetWidth()/3;
	rcTest.bottom = rcTest.top + m_pImgS2d->GetHeight()/3;

	m_pDraw2D->DrawStretchOverlay(rcTest,NULL);

	////------------
	//Rect rcTest; 
	//Rect module;

	//rcTest.left =  0;
	//rcTest.top =  0;
	//rcTest.right = rcTest.left + m_pImgS2d->GetWidth() /2 ;
	//rcTest.bottom = rcTest.top + m_pImgS2d->GetHeight()/2 ;

	//module.left=0;
	//module.right=module.left+m_pImgS2d->GetWidth() /2   ;
	//module.top=0;
	//module.bottom=module.top + m_pImgS2d->GetHeight()/2 ;


	//m_pDraw2D->SetOverlayBlendMode(OBM_NONE );
	//m_pDraw2D->SetTexture( m_pImgS2d );

	//m_pDraw2D->DrawStretchOverlay(rcTest,&module);
	//m_pDraw2D->SetOverlayBlendMode( OBM_NONE );



	//// --------------alphachanel
	//Rect rcTest; 
	//Rect module;

	//rcTest.left =  0;
	//rcTest.top =  0;
	//rcTest.right = rcTest.left + m_pImgS2d->GetWidth()   ;
	//rcTest.bottom = rcTest.top + m_pImgS2d->GetHeight()  ;


	//module.left=0;
	//module.right=module.left+m_pImgS2d->GetWidth()/2     ;
	//module.top=0;
	//module.bottom=module.top + m_pImgS2d->GetHeight()/2  ;


	//m_pDraw2D->SetOverlayBlendMode(OBM_NONE );
	//m_pDraw2D->SetTexture( m_pImgS2d );

	//m_pDraw2D->SetAlphaChannels(m_pImgS2d_alpha);
	//m_pDraw2D->DrawStretchOverlay(rcTest,&module);
	//m_pDraw2D->SetOverlayBlendMode( OBM_NONE );


	
}
//--------------------------------------------------------------------------------------------------------
void	GameEngine::PostRender( void )
{
}

//--------------------------------------------------------------------------------------------------------
void	GameEngine::DispDebugInfo( void )
{
}

//--------------------------------------------------------------------------------------------------------
void	GameEngine::EnterGame( s32 MapId )
{ 

}
//--------------------------------------------------------------------------------------------------------
void	GameEngine::PlayMusic(u32 ID,bool bRep)
{
	if(!m_bUseAudio){
		return;
	}
	if(ID>=m_AudioCnt){
		return;
	}
	m_pAudioPlayer->Play(&m_pAudioData[ID],0,m_pAudioData[ID].GetLength(),255,bRep);
}


//--------------------------------------------------------------------------------------------------------
void	GameEngine::StopMusic()
{
	m_pAudioPlayer->Stop(0);
}

//--------------------------------------------------------------------------------------------------------
void	GameEngine::PlaySound(u32 ID)
{
	if(!m_bUseAudio){
		return;
	}
	if(ID>=m_AudioCnt){
		return;
	}
	for(s32 i=1;i<AudioPlayer::MaxChannels;i++){
		if(m_pAudioPlayer->GetState(i) != AUDIO_PLAYING){
			m_pAudioPlayer->Play(&m_pAudioData[ID],i,m_pAudioData[ID].GetLength(),255,false);
			return;
		}
	}
}	

//--------------------------------------------------------------------------------------------------------
void	GameEngine::StopAllSound()
{
	m_bUseAudio = false;

	m_pAudioPlayer->StopAll();
}

//--------------------------------------------------------------------------------------------------------
void	GameEngine::ExitGame( void )
{
	Exit();
}

void GameEngine::LoadCfg(void)
{
	// get full filename with path
	Char fullname[256];
	s2d::strcpy( fullname, _LS(".\\lltconfig.sdt") );

	FILE* m_fp = fopen( fullname, _LS("rb") );

	if(m_fp == NULL){
		return;
	}

	// read file to memory
	fseek( m_fp, 0, SEEK_END );
	s32 size =  ftell(m_fp);

	s32* mem = (s32*)  malloc( size );
	s32* pstar = mem;

	fseek(m_fp, 0, SEEK_SET );
	fread( mem, size,m_fp );
	fclose(m_fp);

	s32 Scene = *(s32*)mem;

	free( pstar );
}
//--------------------------------------------------------------------------------------------------------
void GameEngine::SaveCfg(void)
{
	// get full filename with path
	Char fullname[256];
	s2d::strcpy( fullname, _LS(".\\lltconfig.sdt") );

	FILE* m_fp = fopen( fullname, _LS("wb+") );

	if(m_fp == NULL){
		return;
	}

	s32 Scene=5;
	fwrite(&Scene, sizeof(s32),m_fp );

	// close file
	fclose(m_fp);
}