#pragma once /* ------------------------------------------------------------------ Derived from the KException class to handle DirectX exceptions. Usage: e_dx.Test(hr, __FILE__, __LINE__, "DirectDrawCreateEx"); where e_dx is the dummy instance of the exception object and hr is the return value of the API call. ----------------------------------------------------------------*/ #define _CRT_SECURE_NO_DEPRECATE #define STRICT #define WIN32_LEAN_AND_MEAN #include #include #include #include #include using namespace std; // #include // #pragma comment(lib, "dxerr.lib") #include #pragma comment(lib, "dxerr.lib") #include #define DX_LOG_FILE_NAME "dx_err_log.txt" namespace KException { class KDxException : public KExceptionAbstract { protected: public: KDxException(); KDxException(const KDxException&); ~KDxException(); template void SetErrCode(int); template void SetErrString(HRESULT); template void SetErrString(string); template void SetErrDescription(HRESULT); void ConstructErrorMsg(void); void DisplayErrorMsg(void); void WriteErrorMsg(void); template void Test(T test_val, const string file_name, const int line_num, const string func_name); }; template void KDxException:: SetErrCode(int code) { err_code = code; } // Call the Dx API to get the error string. template void KDxException:: SetErrString(HRESULT hr) { //call the DX API and format the returned //string to fit into a C++ string. TCHAR * temp = (TCHAR *)DXGetErrorString(hr); size_t temp_len; StringCchLength(temp, 1000, &temp_len); string temp_str(&temp[0], &temp[temp_len]); err_string = temp_str; } template void KDxException:: SetErrString(string s) { err_string = s; } // Another Dx API call to get the error description. template void KDxException:: SetErrDescription(HRESULT hr) { // Same procedure as SetErrString. Format the // string to a c++ string. TCHAR * temp = (TCHAR *)DXGetErrorDescription(hr); size_t temp_len; StringCchLength(temp, 1000, &temp_len); string temp_str(&temp[0], &temp[temp_len]); err_description = temp_str; } template void KDxException:: Test(T test_val, const string file_name, const int line_num, const string func_name) { // Tests the function's return value passed to it, // populates the member data and throws an exception. if(FAILED(test_val)) { KDxException ex; ex.SetErrString(test_val); ex.SetErrDescription(test_val); ex.SetErrFileName(file_name); ex.SetErrLine(line_num); ex.SetErrFuncName(func_name); throw ex; } } } // namespce KException.