C++ 使用SDL2指定OpenGL版本

C++ 使用SDL2指定OpenGL版本,c++,macos,opengl,sdl,C++,Macos,Opengl,Sdl,我正在使用Macbook air和OS X 10.9.2我的图形卡是(英特尔高清图形4000 1024 MB) 苹果表示,MacBookAir的规格和我的一样,支持Opengl 4.1,您可以在这里阅读->。 但是,当我使用SDL2并强制系统使用opengl 3.2时,以及当我通过glGetString()查询opengl版本时,我得到以下行: 2.1 INTEL-8.24.11 我的代码: #include <iostream> #include <SDL2/SDL.h&g

我正在使用Macbook air和OS X 10.9.2我的图形卡是(英特尔高清图形4000 1024 MB) 苹果表示,MacBookAir的规格和我的一样,支持Opengl 4.1,您可以在这里阅读->。 但是,当我使用SDL2并强制系统使用opengl 3.2时,以及当我通过glGetString()查询opengl版本时,我得到以下行:

2.1 INTEL-8.24.11
我的代码:

#include <iostream>
#include <SDL2/SDL.h>
#include <opengL/gl3.h>

using namespace std;


void sdldie(const char *msg)
{
printf("%s: %s\n", msg, SDL_GetError());
SDL_Quit();
exit(1);
}



void checkSDLError(int line = -1)
{
#ifndef NDEBUG
const char *error = SDL_GetError();
if (*error != '\0')
{
    printf("SDL Error: %s\n", error);
    if (line != -1)
        printf(" + line: %i\n", line);
    SDL_ClearError();
}
#endif
}


int main(int argc, const char * argv[])
{


SDL_Window *mainwindow; /* Our window handle */
SDL_GLContext maincontext;

if (SDL_Init(SDL_INIT_VIDEO) < 0) /* Initialize SDL's Video subsystem */
    sdldie("Unable to initialize SDL");

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);

mainwindow = SDL_CreateWindow("Hello", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                              512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (!mainwindow) /* Die if creation failed */
    sdldie("Unable to create window");

checkSDLError(__LINE__);

/* Create our opengl context and attach it to our window */
maincontext = SDL_GL_CreateContext(mainwindow);
checkSDLError(__LINE__);

cout << glGetString(GL_VERSION);

/* This makes our buffer swap syncronized with the monitor's vertical refresh */
SDL_GL_SetSwapInterval(1);

/* Clear our buffer with a red background */
glClearColor ( 1.0, 0.0, 0.0, 1.0 );
glClear ( GL_COLOR_BUFFER_BIT );
/* Swap our back buffer to the front */
SDL_GL_SwapWindow(mainwindow);
/* Wait 2 seconds */
SDL_Delay(2000);

/* Same as above, but green */
glClearColor ( 0.0, 1.0, 0.0, 1.0 );
glClear ( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapWindow(mainwindow);
SDL_Delay(2000);

/* Same as above, but blue */
glClearColor ( 0.0, 0.0, 1.0, 1.0 );
glClear ( GL_COLOR_BUFFER_BIT );
SDL_GL_SwapWindow(mainwindow);
SDL_Delay(2000);

/* Delete our opengl context, destroy our window, and shutdown SDL */
SDL_GL_DeleteContext(maincontext);
SDL_DestroyWindow(mainwindow);
SDL_Quit();



return 0;
}
#包括
#包括
#包括
使用名称空间std;
无效sdldie(常量字符*消息)
{
printf(“%s:%s\n”,msg,SDL_GetError());
SDL_退出();
出口(1);
}
无效检查错误(整数行=-1)
{
#ifndef NDEBUG
const char*error=SDL_GetError();
如果(*错误!='\0')
{
printf(“SDL错误:%s\n”,错误);
如果(行!=-1)
printf(“+行:%i\n”,行);
SDL_ClearError();
}
#恩迪夫
}
int main(int argc,const char*argv[]
{
SDL_窗口*主窗口;/*我们的窗口句柄*/
SDL_GLContext main context;
如果(SDL_Init(SDL_Init_VIDEO)<0)/*初始化SDL的视频子系统*/
sdldie(“无法初始化SDL”);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,2);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,16);
mainwindow=SDL_CreateWindow(“你好”,SDL_WINDOWPOS_居中,SDL_WINDOWPOS_居中,
512,512,SDL_窗口| OPENGL |显示SDL_窗口|);
如果(!mainwindow)/*在创建失败时死亡*/
sdldie(“无法创建窗口”);
检查错误(行);
/*创建opengl上下文并将其附加到窗口*/
maincontext=SDL\U GL\U CreateContext(主窗口);
检查错误(行);

cout在OSX上,您需要为后2.1版本请求一个核心上下文

尝试将此添加到属性:

SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );

顺便说一下,虽然我指定了3.2版,但它显示的是4.1 INTEL-8.24.11,这是否意味着我可以使用opengl 4.1?是的。虽然在其他系统上,它可能只提供3.2。因此,如果您确实要使用4.1,请确保请求4.1。