kicad/common/gal/opengl/utils.cpp

115 lines
3.5 KiB
C++
Raw Normal View History

2016-05-02 13:56:14 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2016-2017 CERN
2016-05-02 13:56:14 +00:00
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <confirm.h> // DisplayError
2016-05-02 13:56:14 +00:00
2016-05-02 14:12:14 +00:00
#include <GL/glew.h>
2017-01-13 16:15:32 +00:00
#include <stdexcept>
2016-05-02 13:56:14 +00:00
int checkGlError( const std::string& aInfo, bool aThrow )
2016-05-02 13:56:14 +00:00
{
int result = glGetError();
wxString errorMsg;
2016-05-02 13:56:14 +00:00
switch( result )
{
case GL_NO_ERROR:
// all good
break;
case GL_INVALID_ENUM:
errorMsg = wxString::Format( "Error: %s: invalid enum", aInfo );
2016-05-02 13:56:14 +00:00
break;
case GL_INVALID_VALUE:
errorMsg = wxString::Format( "Error: %s: invalid value", aInfo );
2016-05-02 13:56:14 +00:00
break;
case GL_INVALID_OPERATION:
errorMsg = wxString::Format( "Error: %s: invalid operation", aInfo );
2016-05-02 13:56:14 +00:00
break;
case GL_INVALID_FRAMEBUFFER_OPERATION:
errorMsg = wxString::Format( "Error: %s: invalid framebuffer operation", aInfo );
2016-05-02 13:56:14 +00:00
break;
case GL_OUT_OF_MEMORY:
errorMsg = wxString::Format( "Error: %s: out of memory", aInfo );
2016-05-02 13:56:14 +00:00
break;
case GL_STACK_UNDERFLOW:
errorMsg = wxString::Format( "Error: %s: stack underflow", aInfo );
2016-05-02 13:56:14 +00:00
break;
case GL_STACK_OVERFLOW:
errorMsg = wxString::Format( "Error: %s: stack overflow", aInfo );
2016-05-02 13:56:14 +00:00
break;
default:
errorMsg = wxString::Format( "Error: %s: unknown error", aInfo );
2016-05-02 13:56:14 +00:00
break;
}
if( result != GL_NO_ERROR )
{
if( aThrow )
throw std::runtime_error( (const char*) errorMsg.char_str() );
else
DisplayErrorMessage( nullptr, "OpenGL error occurred", errorMsg );
}
2016-05-02 13:56:14 +00:00
return result;
}
2017-08-04 08:07:39 +00:00
// debugMsgCallback is a callback function for glDebugMessageCallback.
// It must have the right type ( GLAPIENTRY )
static void GLAPIENTRY debugMsgCallback( GLenum aSource, GLenum aType, GLuint aId,
GLenum aSeverity, GLsizei aLength, const GLchar* aMessage, const void* aUserParam )
2017-08-04 08:07:39 +00:00
{
switch( aSeverity )
{
case GL_DEBUG_SEVERITY_HIGH: printf( "OpenGL ERROR: " ); break;
case GL_DEBUG_SEVERITY_MEDIUM: printf( "OpenGL WARNING: " ); break;
case GL_DEBUG_SEVERITY_LOW: printf( "OpenGL INFO: " ); break;
case GL_DEBUG_SEVERITY_NOTIFICATION: return;
}
printf( "%s\n", aMessage );
2017-08-04 08:07:39 +00:00
}
void enableGlDebug( bool aEnable )
{
if( aEnable )
{
glEnable( GL_DEBUG_OUTPUT );
glDebugMessageCallback( (GLDEBUGPROC) debugMsgCallback, nullptr );
2017-08-04 08:07:39 +00:00
}
else
{
glDisable( GL_DEBUG_OUTPUT );
}
}