c++ - 用函数替换宏

标签 c++ c++98

我有一段宏用来检查与 opengl 相关的错误

#if (GL_ERROR_CHECK == On)
#define GL_CHECK(x)                                                                                                    \
    x;                                                                                                                 \
    {                                                                                                                  \
        GLenum glError = glGetError();                                                                                 \
        if(glError != GL_NO_ERROR)                                                                                     \
        {                                                                                                              \
            std::cout << "GL Error: " << glError << " at " << __FILE__ << ", " << __LINE__ << std::endl;               \
        }                                                                                                              \
    }
#else
#define GL_CHECK(x)  x;
#endif

并这样使用它

GL_CHECK(glGenFramebuffers(1, (GLuint*)&m_u32FboID));
GL_CHECK(glBindFramebuffer(GL_FRAMEBUFFER, m_u32FboID));

我想知道是否有任何方法可以用适当的 c++ 函数替换此宏?

最佳答案

如果您使用的是 OpenGL 4.3 及更高版本,则可以改用调试回调,这样您就不必将每个 GL 函数都包装在宏中:Check it out here

启用一切:

glDebugMessageCallback(some_callback_function, nullptr);
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, nullptr, true);

例如,这个函数:

void gl_debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity,
                       GLsizei length, const GLchar* message, const void* userParam)
{
    std::cerr << "GL Debug Message: " << message << '\n';
}

请注意,这有时会输出非错误的消息,因此您可以考虑打开严重性,或者对使用 ...MessageControl 函数启用的内容进行更严格的限制。

关于c++ - 用函数替换宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54685076/

相关文章:

C++值初始化

c++ - 以最紧凑的方式调用默认(也称为无参数)构造函数

c++ - 跨模块共享内存对象

c++ - 'flushing the stream' 是什么意思?

c++ - 删除字符直到下一个 '\n'

c++ - 为什么 stoi 比没有 -O3 的 stringstream 慢得多?

c++ - 为什么 C+ +'s "using namespace"以这种方式工作?

c++ - 编译带时间符号的移位运算符

c++ - 在 OSX 终端 : Undefined symbols for architecture x86_64 中编译 C++

c++ - 如何将 QString 转换为特定格式的 QDate?