I would like to create an OpenGL project with SDL2. I usually start my OpenGL projects by including GL/glew.h
first. Do I have to include SDL_opengl.h
afterwards? It also contains the OpenGL API headers, so It seems to me that it is not necessary to include both.
SDL
can be told to render using OpenGL. But you can use SDL
for other things and do OpenGL stuff on your own.
In the second case you need a way to define and load GL's API function pointers from the driver. GLEW
is such a function loader.
In the first case, using SDL rendering, SDL
will #include all OGL headers needed by #include SDL_opengl.h
. For example, it's included from SDL_render_gl.c
Getting the OGL function pointers twice, including both glew.h
and SDL_opengl.h
is not the way to go. If you include SDL_opengl.h
before glew.h
likely you get a compiler assertion failure.
If you first include glew.h
perhaps the compiler allows it, I haven't tested it and nothing in SDL headers seems to check this case.
But the real point is that you get nothing using GLEW
with SDL
(*) and this is prune to fail.
(*) It's true that you may need some "new" feature that is contained in last version of glew but not in SDL. In this case, avoid all OGL drawing feature provided by SDL.
EDIT
@HolyBlackCat's comment about SDL supporting only GL 1.x is not completely true. But this requires more explanation.
SDL provides its own API. To draw a line you call SDL_RenderDrawLine, which internally and depending on the renderer used calls a gl-command or another command for another API (OS, DirectX, etc). By gl-command
I mean a command as OpenGL specs define it (glDrawArrays, glBindVertexBuffer, etc).
All gl-whatever must not only be defined, but for OGL>1.1 the gl-functions must also be loaded from the driver. SDL loads the gl-functions it uses, admittedly most of GL1.1 and a bunch of GL2.1.
I've found this SDL+OGL3 example which seems to work with no extern loader (like glew), only SDL. I've not tested it, but I can't find in the SDL source where some OGL functions used in this example are loaded.
What I do can see in SDL_opengl_glext.h (included from SDL_opengl.h
) is an almost-copy of the headers provided by Khronos, which do define the prototipes with recent version (4.4?). These headers are also part of glew
files; glew updates them as soon as possible.
And then we arise to the matter of this question:
I'm in case 4., Which headers do I include?
First of all, remember you need a "gl-functions loader". Because SDL doesn't load all OGL functions (but just a bunch), you need another loader, like GLEW.
Then if you are going to use GLEW anyhow, just include glew.h
instead of SDL_opengl.h
. Using both has no profit, and you may meet some error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With