Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 574
Adam Hunyadi Avatar asked Oct 28 '25 05:10

Adam Hunyadi


1 Answers

You need to create a GL context. Try

auto ctx = SDL_GL_CreateContext(window);

before glewInit() call.

like image 157
freakish Avatar answered Oct 30 '25 22:10

freakish



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!