7

I've been seeing people combine SDL2 and OpenGL (glfw.h or glut.h) for a while now, I've done some research and found out SDL2 runs on OpenGL. So why does people combine these two? Some people discussed how they use SDL2 for making the window and OpenGL for everything else, but that doesn't make sense since OpenGL can create windows as well.

GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Opengl Tutorials", NULL, NULL);
glfwMakeContextCurrent(window); // Cause OpenGL to work on this window

glfwSetKeyCallback(window, KeyCallback); // Callback to handle key events
glfwSetCursorPosCallback(window, MouseCallback); // Callback to handle mouse motion events
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

Some also say they use SDL2 for 2D animations, but that doesn't make sense as OpenGL can do that as well. As I said before SDL2 uses OpenGL so anything SDL2 can do, OpenGL can do, SDL2 is a wrapper of OpenGL. So why do people still add SDL2 and OpenGL together? For example these.

Is there any advantage? Is there something I'm missing? I'm so confused. Why would you combine SDL2 with OpenGL even though SDL2 is only a wrapper?

Between this is all for C/C++ so that may clear up some confusion. When I refer to OpenGL I'm referring to glut.h or/and glfw.h not OpenGL by itself since I don't know if you can do that.

vallentin
  • 125
  • 7
Nfagie Yansaneh
  • 181
  • 1
  • 1
  • 5
  • 3
    Presumably, SDL2 can't do everything that OpenGL can do, but for those things that SDL2 can do it is easier to use than plain OpenGL. – Bart van Ingen Schenau Aug 13 '17 at 08:28
  • 3
    "but that doesn't make sense since OpenGL can create windows as well." OpenGL can't create windows. Your question boils down to "Why use a library at all when you can write all code yourself" – tkausl Aug 13 '17 at 09:40
  • What do you mean OpenGL can't create Windows? I meaning about glfw.h or glut.h look for example this is some glfw.h code. Wait Ill just edit the question @tkausl – Nfagie Yansaneh Aug 13 '17 at 09:44
  • 9
    Your example code isn't OpenGL but GLFW. GLFW is just another library just like SDL2, so you could ask "Why use GLFW instead of plain OpenGL" aswell. – tkausl Aug 13 '17 at 10:04
  • But like I said above OpenGL(glfw.h or glut.h) and I'm curious why people add glfw.h and sdl2 since they can both do the same things but SDL2 is only well 2D, so the users hinder themselves, but I think @BartvanIngenSchenau Answered it, "simplicity" – Nfagie Yansaneh Aug 13 '17 at 10:11
  • 1
    See https://stackoverflow.com/questions/1236256/how-do-you-decide-whether-to-use-a-library-or-write-your-own-implementation – Doc Brown Aug 13 '17 at 11:36
  • You might get better results by finding someone on the Internet who has done this and asking *them* (You must surely know of such a person, or you wouldn't have known to ask). This community is a very tiny percentage of the computing community as a whole, and while it's possible that there's someone here who has done what you described and checks in regularly here, it's not very likely. – Robert Harvey Aug 13 '17 at 13:50
  • 4
    @NfagieYansaneh: "*But like I said above OpenGL(glfw.h or glut.h)*" No, GLFW and GLUT ***are not OpenGL***. – Nicol Bolas Aug 13 '17 at 17:38
  • I know that OFC I don't program in OpenGL I program with GLFW.h and I was wondering why people add GLFW.h ad SDL2. I think we are just going in circles now, also isn't glfw.h a wrapper for OpenGL? If so, why use glfw.h and SDL2 (is a wrapper as well) since they just wrap the same thing, but this is pointless since you think im referring to OpenGL by it self, as I said before, @BartvanIngenSchenau already answered – Nfagie Yansaneh Aug 13 '17 at 19:55
  • 3
    @NfagieYansaneh: "*isn't glfw.h a wrapper for OpenGL*" No. It initializes and manages an OpenGL-capable window. But it is not a wrapper for actually making OpenGL calls. – Nicol Bolas Aug 13 '17 at 23:46
  • +1 Oh I see, thank you. I know understand a little more how glfw.h works – Nfagie Yansaneh Aug 14 '17 at 03:49

2 Answers2

15

OpenGL only has functions to work with a graphics context, nothing else. You need at least a platform integration library to get such a context. But in reality you of course need more functionality, such as input handling. GLFW, GLUT and SDL2 are just different ways of getting such additional functionality. SDL2 is nice because it also includes audio and resource loading.

Sebastian Redl
  • 14,950
  • 7
  • 54
  • 51
  • Indeed, the [Wikipedia page for GLFW](https://en.wikipedia.org/wiki/GLFW) says, "GLFW is by design not...a threading library...able to play back sound...GLUT or SDL." Conversely, [SDLs page](https://en.wikipedia.org/wiki/Simple_DirectMedia_Layer) says, "SDL manages video, audio, input devices, CD-ROM, threads, shared object loading, networking and timers." – user1118321 Aug 13 '17 at 22:43
  • +1 Thank you, I know see the reason for why someone would combine those two. Before I was spectical for why someone would add a wrapper to something that could handle anything for openGL, now I know that SDL2 has some extras like audio, cd-rom etc. that glfw.h can't handle – Nfagie Yansaneh Aug 14 '17 at 03:52
1

I was also wondering why. but yes I use SDL2 because it has the best api for sound.keyboard and joysticks I could find for sound processing. but you can make a window in SDL2 and draw 3d but GLFW seems to work easyer. Im trying to draw 2d and 3d together with SDL2 but its not working well but i just began.