/*
 =======================================================================================
  Name        : OGL_Errors.c
  Author      : Bart     
  Description :
 =======================================================================================
 */

/*
 =======================================================================================
    Include 
 =======================================================================================
 */

#include <windows.h>
#include <stdio.h>
#include <gl/gl.h>

#include "OGL_Options.h"
#include "OGL_Errors.h"
#include "OGL_Thread.h"


/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#ifdef __cplusplus
extern "C" 
{
#endif
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/


/***** Errors Managment (ERROR_TEST) ******/

#if defined (ERROR_TEST)

void fn_vOutputLog(char* ErrorString)
{
	static FILE *g_fErrorFile;
	static BOOL g_bFirstTime = TRUE;

	if (g_bFirstTime) {
		g_bFirstTime = FALSE;
		g_fErrorFile = fopen( "errors.log", "wt" );
	}
	if (g_fErrorFile != NULL) {
		fprintf(g_fErrorFile, ErrorString);
		fflush(g_fErrorFile);
	}
}

void fn_vOglError(char* oglFunc)
{
	GLenum oglError;
	GLenum oglPrevError;
	char oglErrorString[255];
	int oglErrorCount;

	oglError = glGetError();
	while ( oglError != GL_NO_ERROR )
	{
		switch (oglError) {
			case GL_INVALID_ENUM : sprintf(oglErrorString, "OpenGLError: INVALID_ENUM in %s ", oglFunc); break;
			case GL_INVALID_VALUE : sprintf(oglErrorString, "OpenGLError: INVALID_VALUE in %s ", oglFunc); break;
			case GL_INVALID_OPERATION : sprintf(oglErrorString, "OpenGLError: INVALID_OPERATION in %s ", oglFunc); break;
			case GL_STACK_OVERFLOW : sprintf(oglErrorString, "OpenGLError: STACK_OVERFLOW in %s ", oglFunc); break;
			case GL_STACK_UNDERFLOW : sprintf(oglErrorString, "OpenGLError: STACK_UNDERFLOW in %s ", oglFunc); break;
			case GL_OUT_OF_MEMORY : sprintf(oglErrorString, "OpenGLError: OUT_OF_MEMORY in %s ", oglFunc); break;
			default : sprintf(oglErrorString, "OpenGLError: !!UNKNOWN!! in %s ", oglFunc); break;
		}
		M_OUTPUT(oglErrorString);

		oglErrorCount = 20;
		oglPrevError = glGetError();
		while ( (oglErrorCount>0) && ( oglPrevError == oglError ) )
		{
			M_OUTPUT(".");
			oglErrorCount--;
			oglPrevError = glGetError();
		}
		if ( (oglErrorCount == 0) && ( oglPrevError == oglError ) ) {
			M_OUTPUT("\n\tError on glGetError(); ! ! !");
			oglError = GL_NO_ERROR; /* to skip  */ 
		}
		else
		{
		 	oglError = oglPrevError; /* error changed */ 
		}

		M_OUTPUT("\n");
	}
}

void fn_vError()
{
	LPVOID lpMsgBuf;
	DWORD dwError;

	dwError = GetLastError();

	FormatMessage( 
		FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,    NULL,
		dwError/*GetLastError()*/,
		MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		(LPTSTR) &lpMsgBuf,
		0,
		NULL
	);
	//MessageBox( NULL, lpMsgBuf, "GetLastError", MB_OK|MB_ICONINFORMATION );
	M_OUTPUT(lpMsgBuf);
	LocalFree( lpMsgBuf );
}

#endif //defined (ERROR_TEST)



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#ifdef __cplusplus
} //extern "C"
#endif
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
