
#include "MainCPA.h"
#include "ITF.h"
#include "SND.h"
#ifdef USE_PROFILER
#include "prf.h"
#endif
#include "GameITF.h"

#include "DlgOpt.h"
#include "acp_driver.h"

// ANNECY AV {
#ifdef __cplusplus
    extern "C" {
#endif

extern void MOR_fn_vRemoveAllMorphedObjects (void);

#ifdef __cplusplus
    };
#endif
// ENDANNECY AV }

//************************************************************************************
			 
void fn_vChangeWindowTitle(char *szWindowName)
{
 M_GetMainWnd()->SetWindowText(szWindowName);
#ifdef ACTIVE_EDITOR
 CString act;
 char actc[255], *p;
 g_oBaseFrame.GetWindowText(act);
 strcpy(actc, (char *) (LPCSTR) act);
 p = strchr(actc, ']') + 1;
 *p = '\0';
 act = actc;
 act += "  ("+CString(szWindowName)+")";
 g_oBaseFrame.SetWindowText(act);
#endif
}

//************************************************************************************

void fn_vForceToCloseApplication()
{
 M_GetMainWnd()->PostMessage(WM_CLOSE);
 Sleep(0);
}

//************************************************************************************

void fn_vSetEngineWorldForITF(HIE_tdxHandleToSuperObject _hActualWorld)
{
 M_GetMainDevice3D()->SetEngineWorld(_hActualWorld);
}

//************************************************************************************

GLD_tdhDevice fn_hGetGLDDevice(unsigned long _ulValue)
{
 return(M_GetMainGLDDevice(_ulValue));
}

//************************************************************************************

GLD_tdhViewport fn_hGetGLDViewport(unsigned long _ulValue)
{
 return(M_GetMainGLDViewport(_ulValue));
}

//************************************************************************************

void * fn_hGetDrawSemaphore(void)
{
 return(M_GetMainApp()->m_hDrawSem);
}

//************************************************************************************

void GAMITF_fn_vResizeGameViewport(void)
{
 GAM_fn_vActualizeAllGameViewportSize();
}

//************************************************************************************

#if defined(ACTIVE_EDITOR)
void fn_vEditorDrawObjectExpect0(void)
{
 M_GetMainDevice()->SendMessage(CPA_MSG_DRAWOBJECTEXCEPT0);
}
#endif /* ACTIVE_EDITOR */

//************************************************************************************

#if defined(ACTIVE_EDITOR)
void fn_vEditorDataHasChange(void)
{
	M_GetMainApp()->fn_vDataHasChanged();
}
#endif /* ACTIVE_EDITOR */

//************************************************************************************

#if defined(ACTIVE_EDITOR)
void fn_vEnableEditorsMainWnd(unsigned char _ulValue)
{
 M_GetMainWnd()->fn_vEnableEditors(_ulValue);
}
#endif /* ACTIVE_EDITOR */

//************************************************************************************

#if defined(ACTIVE_EDITOR)
void fn_vEditorSetCurrentMap(char *_szCurrentMap)
{
 M_GetMainApp()->fn_vSetCurrentMap(_szCurrentMap);
}
#endif /* ACTIVE_EDITOR */

//************************************************************************************
HWND fn_hGetBaseFrameWnd(void)
{
#if defined(ACTIVE_EDITOR)
	return g_oBaseFrame . m_hWnd;
#else
	return fn_hGetApplicationWindow();
#endif /* ACTIVE_EDITOR */
}
//************************************************************************************
void fn_vInitSoundStruct(void *SndInit)
{
 ((SND_tdstInitStruct*)SndInit)->hProcessInstance=AfxGetInstanceHandle();
#if defined(ACTIVE_EDITOR)
 ((SND_tdstInitStruct*)SndInit)->hMainWindow=g_oBaseFrame.GetSafeHwnd();
#else
 ((SND_tdstInitStruct*)SndInit)->hMainWindow=fn_hGetApplicationWindow();
#endif /* ACTIVE_EDITOR */
 ((SND_tdstInitStruct*)SndInit)->hMutexDX=NULL;
}
//************************************************************************************
void fn_vSwapDeviceToFullScreen(void)
{
	M_GetMainApp()->PostThreadMessage(WM_USER,USERM_SWAP_FULLSCREEN,0);
}

void fn_vChangeDeviceResolution(void)
{
}

//************************************************************************************
BOOL fn_bInFullScreen()	// MR1106
{
	return M_GetMainDevice()->GetDevice(0)->IsFullScreen();
}
//************************************************************************************
BOOL fn_bBadRes()
{
	return M_GetMainApp()->m_bBadResolution;
}
//ANNECY VL SPOFLAG 04/12/97{
//************************************************************************************
BOOL fn_bIsEditorsActive(void)
{
	return M_GetMainApp()->m_bEditorsAreActive;
}
//ENDANNECY VL SPOFLAG}
//************************************************************************************
HINSTANCE fn_hGetApplicationInstance(void)
{
 return(AfxGetInstanceHandle());
}
//************************************************************************************
#ifndef  USE_DIRECTX 
HWND fn_hGetApplicationWindow(void)
{
 return(AfxGetMainWnd()->m_hWnd);
}
#else
// In fact, this should work also under DX7 version
HWND fn_hGetApplicationWindow(void)
{
	HWND hResult;
	CWnd * hCwnd;

	hCwnd = (CWnd *)M_GetMainDevice()->GetDevice(0)->GetViewPort();;
	hResult = hCwnd->m_hWnd;

	HWND  hwnd0=(AfxGetMainWnd()->m_hWnd);
static int kk=0;
	if(kk==0)
	{
		return( hResult );
	}else
		return hwnd0;
	
}
HWND fn_hGetApplicationWindowWithChoise(BOOL _isFullScreen)
{
	HWND hResult;
	CWnd * hCwnd;

	hCwnd = (CWnd *)M_GetMainDevice()->GetDevice(0)->GetViewPort();;
	hResult = hCwnd->m_hWnd;

	HWND  hwnd0=(AfxGetMainWnd()->m_hWnd);
	
	if(_isFullScreen)
	{
		return hwnd0;
	}else
		return( hResult );

}

#endif /* USE8DX8 */
//************************************************************************************
char *fn_szGetExeCommandLine(void)
{
	return (AfxGetApp()->m_lpCmdLine);
}
//************************************************************************************
unsigned long g_ulDeviceWidth,g_ulDeviceHeight;
//VL0
unsigned long g_ulDeviceMode;
//EVL

void MAIN_fn_vSetDeviceWidth(unsigned long ulWidth,unsigned long ulHeight)
{
    g_ulDeviceWidth = ulWidth;
    g_ulDeviceHeight = ulHeight;
}


//VL0
void MAIN_fn_vSetDeviceMode(unsigned long ulIsFullScreen )
{
    g_ulDeviceMode = ulIsFullScreen;
}
//EVL


//************************************************************************************
unsigned long MAIN_fn_ulGetDeviceWidth(void)
{
    return(g_ulDeviceWidth);
}

//************************************************************************************
unsigned long MAIN_fn_ulGetDeviceHeight(void)
{
    return(g_ulDeviceHeight);
}

//VL
//************************************************************************************
unsigned long MAIN_fn_ulDeviceIsFullScreen(void)
{
    return(g_ulDeviceMode);
}
//EVL

//************************************************************************************

//************************************************************************************
//************************************************************************************
//************************** nouvelle synchro ****************************************
//************************************************************************************
//************************************************************************************
//************************************************************************************
//************************************************************************************


//************************************************************************************
//************************************************************************************
// check if appli  asked to end the game
// if not, return
// if yes, change the engine mode to stop program
// tell the appli that engine is leaving
// return...
void fn_vCheckEnd()
{
	// if no close was asked, return
	if (M_GetMainApp()->m_bAppliAskedToEndGame == FALSE)
		return;

	// change engine mode to stop program
	fn_vChangeEngineMode(EM_ModeStoppingProgram);

	// tells the appli that the engine is beginning the closing procedure
	M_GetMainApp()->PostThreadMessage(WM_USER,USERM_ENGINE_IS_CLOSING,0);
	// and that's all
}	

// Call fn_vCheckEnd and return TRUE if detected "End of Appli"
//		return FALSE otherelse
ACP_tdxBool fn_bTestIfEndOfAppliAsked()
{
	fn_vCheckEnd();

	if (fn_eGetEngineMode()==EM_ModeStoppingProgram)
		return TRUE;
	else
		return FALSE;
}


//************************************************************************************
//**********************************************************************************
// tells the appli that the engine has finished
// it will tell the appli that 
// this function will return when appli is OK
void fn_vEngineHasFinished()
{
	// tells the appli that the engine has finished
	M_GetMainApp()->PostThreadMessage(WM_USER,USERM_ENGINE_IS_DONE,0);
	// and wait until the engine is OK
	while (M_GetMainApp()->m_bFinishEngine == FALSE)
		Sleep(0);
}


//************************************************************************************
//************************************************************************************
// check if appli  asked for a pause
// if no pause --> return
// if pause, stop engine and return when engine restart
void fn_vCheckPause()
{
	// if no pause was asked, return
	if (M_GetMainApp()->m_bAppliAskedToStopEngine == FALSE)
		return;

	fn_vSetEngineInPaused();

	// reset one step pulse
	M_GetMainApp()->m_bAppliPulsedOneStep = FALSE;

	// tells appli to stop engine, and wait to restart
	M_GetMainApp()->PostThreadMessage(WM_USER,USERM_STOP_ENGINE,0);
	while ((M_GetMainApp()->m_bAppliAskedToStopEngine) && (M_GetMainApp()->m_bAppliPulsedOneStep == FALSE))
	{
		Sleep(0);
	}
	// reset one step pulse
	M_GetMainApp()->m_bAppliPulsedOneStep = FALSE;

 fn_vResetEngineFromPaused();

    fn_vCheckEditor();
}	


//************************************************************************************
//************************************************************************************
// check if appli asked to go in editor
// if no editor --> return
// if editor, stop engine, pass in editor and return when engine restart
void fn_vCheckEditor()
{
	// if appli didn't ask to launch editor, return
	if (M_GetMainApp()->m_bAppliAskedToActivateEditors == FALSE)
		return;

// ANNECY AV {
    // Remove all the morphed objects
    MOR_fn_vRemoveAllMorphedObjects ();
#if defined(ACTIVE_EDITOR)
	GAM_fn_vFillHierarchyBeforeEditor();
#endif
// ENDANNECY AV }

	// Notify the engine for timer reinit
	fn_vSetEngineInPaused();

	// tells appli to go in editors.
	// this will stop the engine
	M_GetMainApp()->PostThreadMessage(WM_USER,USERM_START_EDITORS,0);
	// wait for editors to stop
	while (M_GetMainApp()->m_bAppliAskedToActivateEditors)
	{
		Sleep(0);
	}
#if defined(ACTIVE_EDITOR)
	if (g_DEMO_KeybTest == DEMO_PLAY)
	{
		DEMO_fn_vStopWhenPlaying();
		if (! M_GetMainApp() -> m_bAutoReinitTheMap)
			fn_vReinitTheMap ();
	}
	GAM_fn_vFillHierarchyAfterEditor();
#endif
#ifdef USE_PROFILER
	// stop rasters until beginning of next frame
	PRF_fn_vReinitAllChrono();
#endif

 fn_vResetEngineFromPaused();
}	


//************************************************************************************
//************************************************************************************
// force appli to start editor
// stop engine, pass in editor and return when engine restart
void fn_vForceEditor()
{
#if defined(ACTIVE_EDITOR)
	// tells engine that the appli wants to go in editor
	// the editors will be activated as soon as possible
	M_GetMainApp()->m_bEngineAskedToForceEditors = TRUE;
	M_GetMainApp()->m_bAppliAskedToActivateEditors = TRUE;
#endif
}	

//************************************************************************************
//************************************************************************************
ACP_tdxBool fn_bTestIfEditorRunning()
{
	return (unsigned char)M_GetMainApp()->m_bAppliAskedToActivateEditors;
}
//************************************************************************************
//************************************************************************************
//************************************************************************************
//************************************************************************************
void fn_vChoicePreferences(void)
{
	CDlgUpdateOptions dlgPreferences;
	dlgPreferences.DoModal();
}
