SDL2 使用高版本opengl
Posted qianbo_insist
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SDL2 使用高版本opengl相关的知识,希望对你有一定的参考价值。
sdl2
sdl本身是一个非常好的画图工具,无论二维和三维他都给我们创立了一个良好的基础,本身是可以使用directx和opengl来绘制的,如果要保证跨平台,暂时是可以使用opengl来做。
下面是class Window的示例代码,创建窗口时我们可以使用OPENGL标识
uint32_t flags = SDL_WINDOW_OPENGL;
m_window = SDL_CreateWindow("Engine!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, mode.w, mode.h, flags);
剩下一定要使用SDL_GL_CONTEXT_PROFILE_MASK, 这样才能设置版本
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
然后就可以在sdl opengl里面设置版本了
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
还有就是我们事先使用了glew,这样可以使用更多的opengl函数,也可以使用glad,这两者一致,选一个就可以,在运行过程中创建窗口和context完毕以后,就可以使用opengl函数glGetString 来打印使用opengl的函数
log_info("the opengl version get:%s",glGetString(GL_VERSION));
ke
可以看到我们使用了opengl version 4.5.0, NVIDIA 516.94 是所用驱动版本,下面是较为完整的代码演示:
Window::Window(void)
m_quit = false;
if (SDL_Init(SDL_INIT_EVERYTHING & ~(SDL_INIT_TIMER | SDL_INIT_HAPTIC)) != 0)
log_err("SDL_Init error: %s", SDL_GetError());
SDL_DisplayMode mode;
SDL_GetCurrentDisplayMode(0, &mode);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, BITS_PER_CHANNEL);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, BITS_PER_CHANNEL);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, BITS_PER_CHANNEL);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, BITS_PER_CHANNEL);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, BITS_PER_CHANNEL * 4);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, BITS_PER_CHANNEL * 2);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
#if defined(GLES3)
log_info("Using GLES 3");
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#elif defined(GLES2)
log_info("Using GLES 2");
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#elif defined(EMSCRIPTEN)
log_info("Using GLES 2");
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
#else
log_info("Using GL 4");
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
#endif
m_fullscreen = false;
uint32_t flags = SDL_WINDOW_OPENGL;
if (m_fullscreen)
flags |= SDL_WINDOW_FULLSCREEN;
m_window = SDL_CreateWindow("Engine!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, mode.w, mode.h, flags);
if (m_window == nullptr)
log_err("SDL_CreateWindow error: %s", SDL_GetError());
m_glContext = SDL_GL_CreateContext(m_window);
if (m_glContext == nullptr)
log_err("SDL_GL_CreateContext error: %s", SDL_GetError());
log_info("the opengl version get:%s",glGetString(GL_VERSION));
SDL_GL_SetSwapInterval(0);
int display_w, display_h;
SDL_GL_GetDrawableSize(m_window, &display_w, &display_h);
this->m_width = display_w;
this->m_height = display_h;
log_info("Window init to: %i x %i", this->m_width, this->m_height);
以上是关于SDL2 使用高版本opengl的主要内容,如果未能解决你的问题,请参考以下文章
高 CPU 使用率 OpenGL Ver 3.1 + SDL2