sdl/opengl在第一个opengl调用上段错误(MacBook Pro/MacPorts)

5
我之前使用过SDL和glut,没有遇到太多问题,现在我想在一个项目中使用SDL/opengl。但是,当我调用第一个openGl函数时,无论我尝试哪个openGl函数,都会出现段错误。
我尝试了网上找到的5个不同的示例程序。我尝试了来自sdl网站的this SDLgears示例,只对源代码进行了很小的更改(从SDL_GetKeyState--> SDL_GetKeyboardState),但也失败了。这让我认为我的库可能有问题。
以下是我相关的macports库:
libsdl-devel @1.3.0-5552_0 (active)
libsdl_mixer @1.2.11_3 (active)
mesa @7.8.2_2 (active)

虽然我不确定-lGL是Mesa还是系统库。

我尝试的所有示例基本上都可以归结为以下内容:

#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(  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)

程序输出:
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) 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

谢谢


1
你使用SDL 1.3的原因是什么?它现在非常不稳定。 - TheBuzzSaw
SDL 1.3 是你安装 MacPorts libsdl-devel 时得到的。今天我卸载了它,并安装了 libsdl +devel,这是 1.2 版本,但这并没有解决我的问题。 - ghorn
2个回答

2
99次中有99次,如果你的代码在第一个OpenGL调用时出现段错误,那么很可能是你的OpenGL上下文没有正确设置。这篇教程似乎是Mac上开始学习OpenGL/SDL的好材料。但是,它是针对Code Blocks的,而你好像是使用gcc(我想是吧?)和Shell而不是图形化IDE。你可以尝试从SDL/OpenGL教程中复制粘贴完全相同的代码,看看是否可以正常工作。如果不行,就说明与你的头文件/库有关。
强烈建议你把所有不必要的SDL调用都去掉,运行你能想到的最基本的程序。然而,看起来你已经几乎做到了这一点。
至于我上面提到的头文件/库问题,我认为这是最可能的问题。我相信最新稳定版的SDL是1.2,而我注意到你正在使用1.3版本。你正在使用1.3库和1.2库?尝试用最新的稳定版本替换你提到的三个库。
祝你好运!如果我说的没有帮助到你,你仍然遇到问题,请务必留言。

哈!我没有注意到他试图在1.3库旁边使用1.2库。我只看到了1.3的SDL。 - TheBuzzSaw
如果你没有指出来,我可能不会注意到。这是团队的努力 ;) - Andrew Rasmussen

0

1
不仅在Mac上如此。如果你使用GL,拥有SDL_Renderer是个坏主意。 - HolyBlackCat
@HolyBlackCat同意,以前在我的代码中能够正常工作,现在不再能够正常工作。 - Justin Meiners

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接