Adam Hunyadi
Adam Hunyadi

Reputation: 1952

Using glew with SDL2

When I want to use SDL2 and glew together I get the following error upon glewInit():

Missing GL version

An example that reproduces the error:

#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;
}

Compiled with:

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

Upvotes: 4

Views: 8800

Answers (1)

freakish
freakish

Reputation: 56587

You need to create a GL context. Try

auto ctx = SDL_GL_CreateContext(window);

before glewInit() call.

Upvotes: 10

Related Questions