#include "stdafx.h"
#include "game.h"

#ifdef _DEBUG
#define DLL_PATH "..\\..\\release\\DGLE2.dll"
#else
#define DLL_PATH "DGLE2.dll"
#endif

using namespace DGLE2;

ENG_DYNAMIC_FUNC

void UnexpectedHandler()
{
	MessageBoxA(NULL, "Unexpected exception encountered.", "Error", MB_ICONERROR);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	set_unexpected(UnexpectedHandler);
	int result = EXIT_SUCCESS;
	IEngineCore *pEngineCore;
	if (GetEngine(DLL_PATH, pEngineCore))
	{
		try
		{
			CGame::CGame(*pEngineCore)();
		}
		catch (const CException &error)
		{
			result = EXIT_FAILURE;
			error();
		}
		catch (const std::exception &error)
		{
			result = EXIT_FAILURE;
			MessageBoxA(NULL, error.what(), "Error", MB_ICONERROR);
		}

		if (!FreeEngine())
		{
			result = EXIT_FAILURE;
			MessageBoxA(NULL, "Couldn't free engine.", "Error", MB_ICONERROR);
		}
	}
	else
	{
		result = EXIT_FAILURE;
		MessageBoxA(NULL, "Couldn't load \""DLL_PATH"\"!", "Error", MB_ICONERROR);
	}
	return result;
}