/* **********************************************************************************************************************
   Included Header Files
   **********************************************************************************************************************/

#include "VDError.h"



/* **********************************************************************************************************************
   VDError
   -------
   Displays an error message.
   #param  message  The error message as described by the engine.
   #param  file     The file in which the error occured.
   #param  line     The line number where the error occured.
   **********************************************************************************************************************/

void VDError(LPCTSTR message, LPCTSTR file, int line) {
    char errorMessage[5000];
    LPTSTR buffer;

    // get the error message from GetLastError()
    (void) FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &buffer, 0, NULL);

    // set up the full error message
    (void) sprintf_s(errorMessage, "--VDEngine Description--\n%s\n\n--Microsoft® Description--\n%s\n\nFILE: %s\nLINE: %d\n", message, buffer, file, line - 2);

    // print the error message
    (void) MessageBox(NULL, errorMessage, "ERROR", MB_TOPMOST | MB_OK | MB_ICONERROR);

    exit(EXIT_FAILURE);
};



/* **********************************************************************************************************************
   VDDirectXError
   --------------
   Displays advanced error information.
   #param  message      The error message as described by the engine.
   #param  returnValue  The return value given by a DirectX function call.
   #param  file         The file in which the error occured.
   #param  line         The line number where the error occured.
   **********************************************************************************************************************/

void VDDirectXError(LPCTSTR message, HRESULT returnValue, LPCTSTR file, int line) {
    char errorMessage[5000];
    LPTSTR buffer;

    // get the error message from GetLastError()
    (void) FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &buffer, 0, NULL);

    // set up the full error message
    (void) sprintf_s(errorMessage, "--VDEngine Description--\n%s\n\n--DirectX Description--\n\"%s\": %s\n\n--Microsoft® Description--\n%s\nFILE: %s\nLINE: %d\n", message, DXGetErrorString(returnValue), DXGetErrorDescription(returnValue), buffer, file, line - 2);

    // print the error message
    (void) MessageBox(NULL, errorMessage, "ERROR", MB_TOPMOST | MB_OK | MB_ICONERROR);

    exit(EXIT_FAILURE);
};