#include "stdafx.h"
#include "Menu.h"
#include "Game.h"
#include <io.h>
#include <sys\stat.h>

map<string, IEngBaseObj*> mapResources;
string ini, work_dir;

ENG_DYNAMIC_FUNC

IEngineCore	*pEngineCore = NULL;

CMenu *pMenu = NULL;
CGame *pGame = NULL;

string IntToStr(int val)
{
	char res[16];
	sprintf(res, "%d", val);
	return string(res);
}

bool FileExists(const char *pcName)
{
	if(pcName[strlen(pcName)-1]=='\\')
	{
		struct _stat statBuffer;
		return (_stat(pcName, &statBuffer) == 0 && statBuffer.st_mode & S_IFDIR); 
	}

	return _access(pcName, 0) != -1;
}

void CALLBACK Init(void *pParametr)
{
	IInput *p_input;
	pEngineCore->GetSubSystem(ESS_INPUT, (IEngineSubSystem *&)p_input);
	p_input->Configure(ICF_HIDE_CURSOR);

	IResourceManager *p_res_man;
	pEngineCore->GetSubSystem(ESS_RESOURCE_MANAGER, (IEngineSubSystem *&)p_res_man);

	IEngBaseObj *p_obj;
	string res_name;

	if (p_res_man->Load(RESOURCE_PATH("Bragga_40.dft"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["MainFont"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("BankGothic_14.dft"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["BaseFont"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("sky.jpg"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Sky"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("cursor.png"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Cursor"] = p_obj;
	((ITexture*)p_obj)->SetFrameSize(20, 20);

	if (p_res_man->Load(RESOURCE_PATH("blood.png"), p_obj, TEXTURE_LOAD_DEFAULT_2D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Blood"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("ball.png"), p_obj, TEXTURE_LOAD_DEFAULT_2D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Ball"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("spark.bmp"), p_obj, TEXTURE_LOAD_DEFAULT_2D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Spark"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("MenuForms.dgui"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Forms"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("plane.dmd"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["MeshPlane"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("man.dmd"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Man"] = p_obj;
	
	if (p_res_man->Load(RESOURCE_PATH("man.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["ManTex"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("asphalt.dds"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexAsphaltDiff"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("asphalt_n.dds"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexAsphaltNormal"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("asphalt_s.dds"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexAsphaltSpec"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("block\\diff.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexBlockDiff"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("block\\normal.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexBlockNormal"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("block\\spec.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexBlockSpec"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("block\\simple.dmd"), p_obj, MLF_CALCULATE_TANGENT_SPACE) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["MeshBlockSimple"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("block\\soplo.dmd"), p_obj, MLF_CALCULATE_TANGENT_SPACE) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["MeshBlockSoplo"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house1\\mesh.dmd"), p_obj, MLF_CALCULATE_TANGENT_SPACE) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["MeshHouse1"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house1\\diff.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse1Diff"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house1\\normal.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse1Normal"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house1\\spec.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse1Spec"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house2\\mesh.dmd"), p_obj, MLF_CALCULATE_TANGENT_SPACE) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["MeshHouse2"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house2\\diff.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse2Diff"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house2\\normal.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse2Normal"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house2\\spec.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse2Spec"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house3\\mesh.dmd"), p_obj, MLF_CALCULATE_TANGENT_SPACE) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["MeshHouse3"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house3\\diff.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse3Diff"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house3\\normal.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse3Normal"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house3\\spec.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse3Spec"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house4\\mesh.dmd"), p_obj, MLF_CALCULATE_TANGENT_SPACE) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["MeshHouse4"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house4\\diff.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse4Diff"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house4\\normal.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse4Normal"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house4\\spec.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse4Spec"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house5\\mesh.dmd"), p_obj, MLF_CALCULATE_TANGENT_SPACE) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["MeshHouse5"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house5\\diff.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse5Diff"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house5\\normal.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse5Normal"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("house5\\spec.jpg"), p_obj, TEXTURE_LOAD_DEFAULT_3D) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["TexHouse5Spec"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("sounds\\rotate.wav"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Rotate"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("sounds\\multikill.wav"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Multikill"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("sounds\\line_done.wav"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["LineDone"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("sounds\\excelnt.wav"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Excelnt"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("sounds\\down.wav"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Down"] = p_obj;

	if (p_res_man->Load(RESOURCE_PATH("sounds\\death.wav"), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Death"] = p_obj;

	if (p_res_man->Load((work_dir+"\\music.mp3").c_str(), p_obj) != S_OK)
		pEngineCore->AddToLogEx("Failed to load requested resource!", LT_FATAL, __FILE__, __LINE__);
	mapResources["Music"] = p_obj;

	((IMusic*)p_obj)->Play();
	((IMusic*)p_obj)->SetVolume(GetPrivateProfileIntA("System", "MusicVolume", 80, ini.c_str()));

	ISound *p_snd;
	pEngineCore->GetSubSystem(ESS_SOUND, (IEngineSubSystem *&)p_snd);

	p_snd->SetMasterVolume(GetPrivateProfileIntA("System", "SoundVolume", 100, ini.c_str()));

	StartScene(GS_MENU);
}

void CALLBACK Free(void *pParametr)
{
	if(pMenu)
	{
		delete pMenu;
		pMenu = NULL;
	}

	if(pGame)
	{
		delete pGame;
		pGame = NULL;
	}
}

void StartScene(E_GAME_SCENE eScene)
{
	Free(NULL);

	switch(eScene)
	{
	case GS_MENU:
		pMenu = new CMenu(pEngineCore);
		break;
	case GS_GAME:
		pGame = new CGame(pEngineCore,  GetPrivateProfileIntA("System", "PostProcess", 0, ini.c_str()) == 1, GetPrivateProfileIntA("System", "HighDetail", 1, ini.c_str()) == 1);
		break;
	}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	if(GetEngine(DLL_PATH,pEngineCore))
	{
		char txt[255];
		GetModuleFileName(NULL, txt, 255);
		work_dir = string(txt);
		int idx = 0;
	
		for(int i = 0; i < work_dir.length(); i ++)
			if(work_dir[i] == '\\') idx = i;

		work_dir.erase(idx, work_dir.length() - idx);

		ini = work_dir + "\\settings.ini";

		if(!FileExists(ini.c_str()))
		{
			int desktop_width = 0, desktop_height = 0;
			HDC desktop_dc	= GetDC(GetDesktopWindow());
			desktop_width	= GetDeviceCaps(desktop_dc, HORZRES);
			desktop_height	= GetDeviceCaps(desktop_dc, VERTRES);
			ReleaseDC(GetDesktopWindow(), desktop_dc);

			if((desktop_width == 800 && desktop_height == 600) || (desktop_width == 1024 && desktop_height == 768) || (desktop_width == 1280 && desktop_height == 1024) || (desktop_width == 1600 && desktop_height == 1200) ||
				(desktop_width == 1280 && desktop_height == 720) || (desktop_width == 1366 && desktop_height == 768) || (desktop_width == 1280 && desktop_height == 800) || (desktop_width == 1440 && desktop_height == 900) || 
				(desktop_width == 1680 && desktop_height == 1050) || (desktop_width == 1920 && desktop_height == 1080) || (desktop_width == 1920 && desktop_height == 1200) )
			{
				WritePrivateProfileStringA("System", "ResX", IntToStr(desktop_width).c_str(), ini.c_str());
				WritePrivateProfileStringA("System", "ResY", IntToStr(desktop_height).c_str(), ini.c_str());
			}
			else
			{
				WritePrivateProfileStringA("System", "ResX", "1024", ini.c_str());
				WritePrivateProfileStringA("System", "ResY", "768", ini.c_str());
			}

			WritePrivateProfileStringA("System", "MSAA", "4", ini.c_str());
			WritePrivateProfileStringA("System", "FScreen", "1", ini.c_str());
			WritePrivateProfileStringA("System", "VSync", "0", ini.c_str());
			WritePrivateProfileStringA("System", "HighDetail", "1", ini.c_str());
			WritePrivateProfileStringA("System", "PostProcess", "0", ini.c_str());
			WritePrivateProfileStringA("System", "SoundVolume", "100", ini.c_str());
			WritePrivateProfileStringA("System", "MusicVolume", "80", ini.c_str());


		}

		TEngWindow w;
		w.uiWidth = GetPrivateProfileIntA("System", "ResX", 1024, ini.c_str());
		w.uiHeight = GetPrivateProfileIntA("System", "ResY", 768, ini.c_str());
		w.bFullScreen = GetPrivateProfileIntA("System", "FScreen", 1, ini.c_str());
		w.bVSync = GetPrivateProfileIntA("System", "VSync", 0, ini.c_str());
		w.uiSamplesCount = GetPrivateProfileIntA("System", "MSAA", 4, ini.c_str());

		if(SUCCEEDED(pEngineCore->InitializeEngine(NULL, APP_CAPTION, w)))
		{
			pEngineCore->AddProcedure(EPT_INIT,	&Init);
			pEngineCore->AddProcedure(EPT_FREE,	&Free);
			pEngineCore->StartEngine();
		}
		FreeEngine();
	}
	else MessageBox(NULL,"Couldn't load \""DLL_PATH"\"!", APP_CAPTION, MB_OK | MB_ICONERROR | MB_SETFOREGROUND);
	return 0;
}