// Multitron.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "Multitron.h"

#include "MainFrm.h"
#include "ChildFrm.h"
#include "MultitronFrm.h"
#include "MultitronDoc.h"
#include "TextureDoc.h"
#include "MultitronView.h"
#include "TextureView.h"

#include <GameOS\GameOS.hpp>
#include <DLLPlatform\DLLPlatform.hpp>
#include <stuff\stuff.hpp>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMultitronApp

BEGIN_MESSAGE_MAP(CMultitronApp, CWinApp)
	//{{AFX_MSG_MAP(CMultitronApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	ON_COMMAND(ID_FILE_SAVE, OnFileSave)
	ON_COMMAND(ID_FILE_SAVE_AS, OnFileSave)
	//}}AFX_MSG_MAP
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMultitronApp construction

CMultitronApp::CMultitronApp()
{
	m_pMainDoc = NULL;
	m_FileOPendlg = NULL;
	m_mapHintParams["mipmap"]="Default|None|Explicit";
	m_mapHintParams["reloadfromdisk"]="false|true";
	m_mapHintParams["readonly"]="true|false";
	m_mapHintParams["memory"]="Default|AGP|Video";
	m_mapHintParams["pageout"]="Default|First|Last";
	m_mapHintParams["mipfilter"]="Box|Point";
	m_mapHintParams["pinkisalpha"]="false|true";
	m_mapHintParams["blueisalpha"]="false|true";
	m_mapHintParams["nogamma"]="false|true";
	m_mapHintParams["format"]="Alpha|Solid|Keyed|Bump";
	m_mapHintParams["resourcify"]="true|false";
	m_mapHintParams["bias"]="-3|-2|-1|0|1|2|3";
}

CMultitronApp::~CMultitronApp() 
{
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CMultitronApp object

CMultitronApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CMultitronApp initialization

BOOL CMultitronApp::InitInstance()
{
	char buf[MAX_PATH];
	GetCurrentDirectory(MAX_PATH,buf);

	InitGameOS( AfxGetInstanceHandle(), NULL, "\0" );
	Stuff::ArmorLevel = 4;
	Stuff::InitializeClasses();

	SetCurrentDirectory(buf);
	m_strRunDir = buf;	


	AfxEnableControlContainer();

	// Standard initialization

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings(8);  // Load standard INI file options (including MRU)

	// Register document templates

	CMultiDocTemplate* pDocTemplate;

	pDocTemplate = new CMultiDocTemplate(
		IDR_MULTITTYPE,
		RUNTIME_CLASS(CTextureDoc),
		RUNTIME_CLASS(CChildFrame), // custom MDI child frame
		RUNTIME_CLASS(CTextureView));
	AddDocTemplate(pDocTemplate);
	m_mapTemplate[ID_FILE_NEW] = pDocTemplate;

	// ****Be sure that this doc template is last so that it is closed last and all previous documents that use the
	// pages dependent on the main doc don't assert that they are corrupted ****
	m_pMainDoc = pDocTemplate = new CMultiDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CMultitronDoc),
		RUNTIME_CLASS(CMultitronFrm), // custom MDI child frame
		RUNTIME_CLASS(CMultitronView));
	AddDocTemplate(pDocTemplate);

	// create main MDI Frame window
	CMainFrame* pMainFrame = new CMainFrame;
	if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	m_pMainWnd = pMainFrame;

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);
	
	CRect rc;
	pMainFrame->GetWindowRect(&rc);
	rc.right = rc.right-rc.left;
	rc.bottom = rc.bottom-rc.top;
	rc.left = 0;
	rc.top = 0;
	pMainFrame->MoveWindow(rc);
	pMainFrame->ShowWindow(m_nCmdShow);
	pMainFrame->UpdateWindow();

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	return TRUE;
}


int CMultitronApp::ExitInstance() 
{	
	if (m_FileOPendlg) {
		delete m_FileOPendlg;
		m_FileOPendlg = NULL;
	}

	Stuff::TerminateClasses();
	ExitGameOS();
	return CWinApp::ExitInstance();
}

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CMultitronApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CMultitronApp message handlers


void CMultitronApp::OnFileNew() 
{
	WPARAM wParam = MAKELPARAM(ID_FILE_MRU_FILE1,0);
	AfxGetMainWnd()->SendMessage(WM_COMMAND,wParam,NULL);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void __stdcall GetGameOSEnvironment(char* CommandLine)
{
		//
	//----------------------------------------------------------
	// This is where to set GOS environment settings.
	//----------------------------------------------------------
	//
	Environment.applicationName			= "Multitron";
	Environment.screenWidth				= 640;
	Environment.screenHeight			= 480;
	Environment.bitDepth				= 16;
}

void CMultitronApp::OnFileOpen() 
{
	if (!m_FileOPendlg) {
		m_FileOPendlg = new CFileOpenDlg;
	}

	if (IDOK == m_FileOPendlg->DoModal()) {
		OpenDocumentFile(m_FileOPendlg->GetFileName());
	}
}

CDocument* CMultitronApp::OpenDocumentFile(LPCTSTR lpszFileName) 
{
	return m_pMainDoc->OpenDocumentFile(lpszFileName);
}

void CMultitronApp::OnFileSave() 
{
	if (m_pMainDoc)
	{
		POSITION pos = m_pMainDoc->GetFirstDocPosition();
		while (pos)
		{
			CDocument *pDoc = m_pMainDoc->GetNextDoc(pos);
			pDoc->SaveModified();
		}
	}
}

void CMultitronApp::RefreshFiles() 
{
	if (m_pMainDoc)
	{
		POSITION pos = m_pMainDoc->GetFirstDocPosition();
		while (pos)
		{
			CDocument *pDoc = m_pMainDoc->GetNextDoc(pos);
			pDoc->UpdateAllViews(NULL);
		}
	}
}
