sdl/opengl segfaults on first opengl call (macbook pro/macports)
I've used SDL and glut (separately) without much problem in the past, and now I'd like to use SDL/opengl for a project. I get a segfault when I call the very first openGl function, whichever openGl function I try.
I've tried running like 5 different example programs I've found around the net. I tried this SDLgears example from the sdl website with only a very small change to the source (SDL_GetKeyState --> SDL_GetKeyboardState) and it also failed. This makes me think there could be something wrong with my libraries?
Here are my relevant macports libraries:
libsdl-devel @1.3.0-5552_0 (active)
libsdl_mixer @1.2.11_3 (active)
mesa @7.8.2_2 (active)
though I'm unclean on whether -lGL is mesa or a system library.
All the examples I've tried pretty much boil down to this:
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
void
warn_if_fail(int sdl_ret)
{
if (sdl_ret < 0)
printf("opengl error: %s\n", SDL_GetError());
}
int main(int argc __attribute__((unused)), char* args[] __attribute__((unused)))
{
// init sdl video
SDL_Init(SDL_INIT_VIDEO);
//print video info
const SDL_VideoInfo* info = SDL_GetVideoInfo();
printf("video card memory: %d\n", info->video_mem);
printf("current_w: %d, current_hh: %d\n", info->current_w, info->current_h);
printf("bpp: %d\n", info->vfmt->BitsPerPixel);
printf("hardware_available: %d\n", info->hw_available);
printf("blit_hw: %d\n", info->blit_hw );
// set SDL_GL attributes
warn_if_fail( SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) );
warn_if_fail( SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) );
warn_if_fail开发者_如何学JAVA( SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) );
warn_if_fail( SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8) );
warn_if_fail( SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32) );
warn_if_fail( SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) );
warn_if_fail( SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 32) );
// set video mode
SDL_Surface* screen;
if ( (screen = SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL) ) == NULL)
printf("video error: %s\n", SDL_GetError());
printf("\nSDL_SetVideoMode success!\n");
// print some attributes to make sure they were set correctly
int red, green, blue, doublebuf;
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red);
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green);
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &blue);
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doublebuf);
printf("red size, green size, blue size: <%d, %d, %d>\n", red, green, blue);
printf("double buffered? %s\n\n", doublebuf == 1 ? "yes" : "no");
// init openGl stuff
printf("about to segfault\n");
glViewport(0,0,640,480);
printf("won't reach this message\n");
// this also would have segfaulted
// All openGl functions I've tried have segfaulted
glClearColor(0, 0, 0, 0);
SDL_Quit();
return 0;
}
Makefile:
PROJ = sdl_opengl_test
CXX=g++
Q = @
CPP_SRC = sdl_opengl_test.cpp
OBJ = $(CPP_SRC:.cpp=.o)
WARNINGFLAGS = -Wall -Wextra -Wshadow -Werror
INCLUDES = -I/opt/local/include
CPPFLAGS = $(WARNINGFLAGS) $(INCLUDES) -D_THREAD_SAFE
LDFLAGS = -L/opt/local/lib -lSDL -lGL -lGLU
$(PROJ): $(OBJ)
@echo LD $@
$(Q)$(CXX) $(OBJ) $(LDFLAGS) -o $@
%.o : %.cpp
@echo CXX $@
$(Q)$(CXX) $(CPPFLAGS) -c $< -o $@
clean:
rm -f $(PROJ)
rm -f $(OBJ)
program output:
greg@pimptop ~/space/blaah (git)-[master] % ./sdl_opengl_test
video card memory: 0
current_w: 1680, current_hh: 1050
bpp: 32
hardware_available: 0
blit_hw: 0
SDL_SetVideoMode success!
red size, green size, blue size: <8, 8, 8>
double buffered? yes
about to segfault
[1] 37420 segmentation fault ./sdl_opengl_test
gdb output:
(gdb) run
Starting program: /Users/greg/space/blaah/sdl_opengl_test
Reading symbols for shared libraries .++++++............................. done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
video card memory: 0
current_w: 1680, current_hh: 1050
bpp: 32
hardware_available: 0
blit_hw: 0
Reading symbols for shared libraries . done
Reading symbols for shared libraries .. done
Reading symbols for shared libraries . done
Reading symbols for shared libraries . done
SDL_SetVideoMode success!
red size, green size, blue size: <8, 8, 8>
double buffered? yes
about to segfault
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x0000000000000000 in ?? ()
(gdb) bt
#0 0x0000000000000000 in ?? ()
#1 0x0000000100000d55 in main (argc=1, args=0x7fff5fbff378) at sdl_opengl_test.cpp:50
Thank you
99 out of 100 times if your code is segfaulting on the very first OpenGL call, it's because your OpenGL context isn't being set up properly.
This seems to be a good tutorial to start with for OpenGL/SDL on mac. However, it's for code blocks and it looks like you are working with gcc (I think..?) and a shell rather than a graphical IDE. You could try copy pasting the exact code from a SDL/OpenGL tutorial and seeing if it works. If it doesn't, you know it's something to do with your headers/libraries.
What I would strongly suggest doing is taking all of the unnecessary SDL calls out and running the most basic program you can possibly come up with. However, it looks as though you've pretty much already done this.
On the topic of headers/libraries as I mentioned above, I find this to be the most likely suspect. I believe the latest stable version of SDL is 1.2, and I notice that you are using 1.3. And you're using a 1.3 lib with a 1.2 lib? Try replacing those three libs you mentioned with the latest stable versions.
Good luck! Definitely comment if nothing I've said has helped and you're still running into issues.
This issue helped me: https://github.com/libsdl-org/SDL/issues/4428
Use the SDL_CreateWindow
api instead of SDL_CreateWindowAndRenderer
in order to make an OpenGL context on mac.
精彩评论