PDA

View Full Version : Basic SDL + OpenGL segfault problem.



LoopDeLoop
12-28-2011, 05:01 PM
Hello everyone. I have what seems to me (a beginner) a very strange problem with OpenGL. This is possibly related to SDL or maybe even something else.

I am writing a class for doing pretty basic 2D drawing with hardware acceleration for a game idea. I had something similar working on another system and rather than rebuild it on my new laptop I decided to write again from ground up. The fact I had no probs on the other machine is driving me nuts.
Initially I thought it was my usage of glBindTexture() but I have since found that if I try to access ANY variable outside the scope of the drawing methods I get a segmentation fault.

I am using xubuntu 11.10 and SDL 1.2

2.1 Mesa 7.11
Tungsten Graphics, Inc
Mesa DRI Intel(R) Ironlake Mobile x86/MMX/SSE2

I initialized SDL with


SDL_Init (SDL_INIT_EVERYTHING)
display = SDL_SetVideoMode(video_width,video_height,32,SDL_O PENGL )

then I have an init() function in my OpenGL class like this,


bool init(int video_width, int video_height)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glDisable(GL_DEPTH_TEST);
glLoadIdentity();
glOrtho(0, video_width, video_height, 0, -1, 1);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
if(glGetError() != GL_NO_ERROR)
return false;

return true;
}


This works fine with the cout commented out


void rect_fill(float x1, float y1, float x2, float y2, float r, float g, float b)
{
//std::cout << bl_bitmaps[0]->GL_id << "\n";
glColor4f(r,g,b,1.0f);
glBegin(GL_QUADS);
glVertex2f(x1,y1);
glVertex2f(x2,y1);
glVertex2f(x2,y2);
glVertex2f(x1,y2);
glEnd();
}

This segfaults


void rect_fill(float x1, float y1, float x2, float y2, float r, float g, float b)
{
std::cout << bl_bitmaps[0]->GL_id << "\n";
glColor4f(r,g,b,1.0f);
glBegin(GL_QUADS);
glVertex2f(x1,y1);
glVertex2f(x2,y1);
glVertex2f(x2,y2);
glVertex2f(x1,y2);
glEnd();
}

It doesn't matter what that cout is accessing, I also tested with a simple int within the same class and it segfaults on that too.Can anyone shed a little light on what may be happening here? I would be very grateful.

carsten neumann
12-29-2011, 08:13 AM
Very hard to say from just the code you posted, some ideas:

- do you actually create an object of "your opengl class"?



MyOpenGL *ogl;
ogl->rect_fill(/* ... */);


the above may compile, and even though ogl is not initialized may work if you don't access any members of the class (because then "this" is unused and nothing notices that it is invalid).
It should of course be:



MyOpenGL *ogl = new MyOpenGL;
ogl->rect_fill(/* ... */);


- run this under a debugger to find the exact spot where it crashes, what it attempts to access, and what the values of the involved variables are.

LoopDeLoop
01-08-2012, 04:20 PM
Thanks for the quick response carsten and appologies for my delay. Down to new year, familiy visits etc, anyway you were spot on.
I had somehow managed to attempt to use 2 ogl* objects and only created one, which I never even used. Doh! Looked through it again and again and I just couldn't see the problem. I knew it would be something embarrassingly stupid but I never saw it until you pointed it out so thanks very much. :)