SDL/openGl combo

I’ve learned in the forums that SDL plus opengl is a more potent combination that either alone. What is a practical example of this?

Generally SDL’s rendering API is meant to be simple: 2D graphics with basic scaling and rotation. Even if it’s using OpenGL behind the scenes, we limit what you can do with it.

Using OpenGL directly is more difficult (sometimes significantly more difficult), but you can do anything that your GPU is capable of through OpenGL.

I generally tell people that SDL’s rendering API is meant for simple games and prototypes…the functionality it offers is like a really powerful Super Nintendo. Using OpenGL directly is more like a PlayStation 4.

As far as a practical example: OpenGL is too complex to explain here, but lots of articles and books explain it. The part that involves SDL looks roughly like this example code on the Wiki:

https://wiki.libsdl.org/SDL_GL_CreateContext

Note that we don’t create an SDL_Renderer here, call SDL_GL_SwapWindow() to get your drawing on the screen, and SDL_GL_DestroyContext() when we’re shutting down.

No problem. I’ve had some success with SDL. I want to experiemnt with OpenGL but my helloworld opengl keeps crashing. Could you tell me whats wrong? Its compiling fine, just crashing on run.

#include <SDL.h>
#include
#include <GL/glut.h>

using namespace std;
void displayMe(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.5, 0.0, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
glFlush();
}
int main(int argc, char** argv)
{

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(300, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello world :D");
glutDisplayFunc(displayMe);
glutMainLoop();
return 0;

}

Thank you in advance.

I don’t know why that’s do that to me in the ‘includes’

I don’t know why that’s do that to me in the ‘includes’

Indent the code by 4 spaces and it’ll treat it as preformatted text. Alternately: paste it in, highlight it, and click this button on the Discourse post editor…

…and it’ll indent it all for you.

As for why the program is crashing: I don’t know. If you can get me a stack trace, we can see where it’s crashing which might tell us why it’s crashing.

(GLUT isn’t part of SDL, it offers some of the same functionality though.)

#include <SDL.h>
#include <iostream>
#include <GL/glut.h>

If you can get me a stack trace,

Oops, stepped in a mud puddle here…I’ll have to work on this.

1 Like

Ok, I’ve got the problem cornered. The reason I thought my program was crashing after a successful compile was erroneous: my compiler was running the program anyway despite the errors pre-compile. Turns out all of the errors were generated during compile, and I think its my include files - possibly missing a few. If someone could doubl;e check for me…Its missing functions contained in the OpenGl header files I think

     18:45:45 **** Incremental Build of configuration Release for project OGlSDL2_proj ****
Info: Internal Builder is used for build
g++ "-IC:\\MinGW\\include\\SDL2" "-IC:\\OGLib\\freeglut\\include" -O3 -Wall -c -fmessage-length=0 -o "src\\OGlSDL2_proj.o" "..\\src\\OGlSDL2_proj.cpp" 
..\src\OGlSDL2_proj.cpp: In function 'void displayMe()':
..\src\OGlSDL2_proj.cpp:16:13: error: 'GL_COLOR_BUFFER_BIT' was not declared in this scope
     glClear(GL_COLOR_BUFFER_BIT);
             ^
..\src\OGlSDL2_proj.cpp:16:32: error: 'glClear' was not declared in this scope
     glClear(GL_COLOR_BUFFER_BIT);
                                ^
..\src\OGlSDL2_proj.cpp:17:13: error: 'GL_POLYGON' was not declared in this scope
     glBegin(GL_POLYGON);
             ^
..\src\OGlSDL2_proj.cpp:17:23: error: 'glBegin' was not declared in this scope
     glBegin(GL_POLYGON);
                       ^
..\src\OGlSDL2_proj.cpp:18:33: error: 'glVertex3f' was not declared in this scope
         glVertex3f(0.0, 0.0, 0.0);
                                 ^
..\src\OGlSDL2_proj.cpp:22:11: error: 'glEnd' was not declared in this scope
     glEnd();
           ^
..\src\OGlSDL2_proj.cpp:23:13: error: 'glFlush' was not declared in this scope
     glFlush();
             ^
..\src\OGlSDL2_proj.cpp: In function 'int SDL_main(int, char**)':
..\src\OGlSDL2_proj.cpp:28:25: error: 'glutInit' was not declared in this scope
     glutInit(&argc, argv);
                         ^
..\src\OGlSDL2_proj.cpp:29:25: error: 'GLUT_SINGLE' was not declared in this scope
     glutInitDisplayMode(GLUT_SINGLE);
                         ^
..\src\OGlSDL2_proj.cpp:29:36: error: 'glutInitDisplayMode' was not declared in this scope
     glutInitDisplayMode(GLUT_SINGLE);
                                    ^
..\src\OGlSDL2_proj.cpp:30:32: error: 'glutInitWindowSize' was not declared in this scope
     glutInitWindowSize(300, 300);
                                ^
..\src\OGlSDL2_proj.cpp:31:36: error: 'glutInitWindowPosition' was not declared in this scope
     glutInitWindowPosition(100, 100);
                                    ^
..\src\OGlSDL2_proj.cpp:32:36: error: 'glutCreateWindow' was not declared in this scope
   glutCreateWindow("Hello world :D");
                                    ^
..\src\OGlSDL2_proj.cpp:33:28: error: 'glutDisplayFunc' was not declared in this scope
   glutDisplayFunc(displayMe);
                            ^
..\src\OGlSDL2_proj.cpp:34:18: error: 'glutMainLoop' was not declared in this scope
     glutMainLoop();
                  ^

18:45:45 Build Finished (took 274ms)

Current OpenGl includes:

AM I missing any header files ?