使用glew和SDL2

时间:2017-07-11 11:26:22

标签: c++ opengl sdl-2 glew

当我想要使用SDL2和glew时,glewInit()会出现以下错误:

  

缺少GL版

重现错误的示例:

#include <iostream>

#define NO_SDL_GLEXT
#include <GL/glew.h>
#include "SDL2/SDL.h"
#include "SDL/SDL_opengl.h"

int main(int argc, char **argv)
{
    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        std::cout << "SDL could not initialize! SDL Error: " << SDL_GetError() << std::endl;
        return -1;
    }
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); // Does nothing
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); // Does nothing
    SDL_Window *window = SDL_CreateWindow("Example for SO", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_OPENGL);
    if(window == nullptr)
    {
        std::cout << "Window could not be created! SDL Error: " << SDL_GetError() << std::endl;
        return -1;
    }
    glewExperimental = GL_TRUE;
    auto init_res = glewInit();
    if(init_res != GLEW_OK)
    {
        std::cout << glewGetErrorString(glewInit()) << std::endl;
    }
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

编译:

c++ (Debian 6.3.0-11) 6.3.0 (gcc)
flags: -std=c++17
libraries:
-L/usr/local/lib -lSDL2 -Wl,-rpath=/usr/local/lib -lGLEW

1 个答案:

答案 0 :(得分:7)

您需要创建GL上下文。尝试

auto ctx = SDL_GL_CreateContext(window);
glewInit()致电之前