Reputation: 233
so I've been trying to understand SDL interface and attach my OpenGL and freeglut test program to it - cleaning it from glut references. Most problematic part became when I tried to attach shaders - everything was compiling, but I keep getting Segmentation fault error. The cleaned version of program is:
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <GL/glu.h>
#include <iostream>
using namespace std;
int main(){
SDL_Window *window;
SDL_GLContext *context;
GLuint vao;
SDL_Init( SDL_INIT_VIDEO );
SDL_CreateWindow("cookie", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
SDL_GL_CreateContext(window);
SDL_GL_SetSwapInterval(1);
cout<<"Hi! It's my first instance"<<endl;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
cout<<"And it's my second - i can't reach here"<<endl;
}
I am working on Ubuntu 14.04, Radeon 8somethingsomething card (if it does really matter). The command i use to compile is:
g++ main.cpp -lGL -lGLEW -lSDL2
PS. Sorry for being iostream newbie, I started programming from c++.
Upvotes: 0
Views: 214
Reputation: 162164
You never assign a value to window
. When SDL dereferences the pointer passed to SDL_GL_CreateContext
it crashes, because there's no valid pointer value in window
.
int main(int argc, char *argv[])
{
SDL_Init( SDL_INIT_VIDEO );
SDL_Window * const window = SDL_CreateWindow(
"cookie",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
512, 512,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if( !window ) {
cerr << "creating window failed" << endl;
return 1;
}
SDL_GLContext * const context = SDL_GL_CreateContext(window);
if( !context ) {
cerr << "creating OpenGL context failed" << endl;
return 1;
}
/* ... */
what @keltar wrote here: https://stackoverflow.com/a/26645107/524368
Upvotes: 4
Reputation: 18399
glGenVertexArrays
is extension function. You've included glew.h
, which handles extensions - but you haven't initialised it. glewInit
have to be called after creating GL context (and check its return value).
Also a bit of advice - segmentation fault isn't some weird magic you should be scared of and google what the hell happened. It is very well defined. If you'll run your program in debugger, you'll see where exactly it crashed and why (because glGenVertexArrays
function is called, but value of this function pointer is NULL). If you using native language like C, you'll see that a lot, so some time spent on learning how to use debugger (if you don't know that already) will save you (and others, mind you) a lot of time later.
Upvotes: 3