OpenGL测试代码在mac OS X上不起作用

时间:2012-01-25 20:05:17

标签: macos opengl sdl

此代码适用于GNU / Linux,但我找不到如何在Mac OS X Lion上运行它。

这是一个测试代码。没有什么太难理解,只有一个空窗口等待戒烟事件停止。如果我评论glClear调用,一切正常......实际上glClear或任何gl调用会使它在一个很好的Segmentation错误上崩溃。

以下是代码:

#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>

int main( int ac, char **av )
{

    SDL_Surface *screen;
    SDL_Init( SDL_INIT_VIDEO );

    if(SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32) < 0) { printf("opengl error: %s\n", SDL_GetError()); }
    if(SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) < 0) { printf("couldn't set double buffering: %s\n", SDL_GetError()); }

    if ( (screen = SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL | SDL_NOFRAME | SDL_DOUBLEBUF )) == NULL )
    {
        exit( EXIT_FAILURE );
    }
    SDL_WM_SetCaption( "test", NULL );

    bool loop = true;
    SDL_Event event;
    while ( loop )
    {
        glClear( GL_COLOR_BUFFER_BIT );
        SDL_PollEvent( &event );
        switch ( event.type )
        {
            case SDL_QUIT:
            loop = false;
            break;
        }
    }

    SDL_Quit();
    return 0;
}

以下是我如何编译它:

g++ -g -I/opt/local/include -I/usr/X11R6/include -L/opt/local/lib -lSDLmain -lSDL -Wl,-framework,Cocoa -L/usr/X11R6/lib -lGL main.cpp

gdb对我没那么帮助:

Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
0x0000000000000000 in ?? ()
(gdb) bt 
#0  0x0000000000000000 in ?? ()
#1  0x000000010000269d in SDL_main (ac=1, av=0x100517c90) at main.cpp:28
#2  0x0000000100002360 in -[SDLMain applicationDidFinishLaunching:] ()
#3  0x00007fff90bc2de2 in __-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke_1 ()
#4  0x00007fff8c354e0a in _CFXNotificationPost ()
#5  0x00007fff90baf097 in -[NSNotificationCenter postNotificationName:object:userInfo:] ()
#6  0x00007fff8a49faa7 in -[NSApplication _postDidFinishNotification] ()
#7  0x00007fff8a49f80d in -[NSApplication _sendFinishLaunchingNotification] ()
#8  0x00007fff8a49e4d2 in -[NSApplication(NSAppleEventHandling) _handleAEOpenEvent:] ()
#9  0x00007fff8a49e233 in -[NSApplication(NSAppleEventHandling) _handleCoreEvent:withReplyEvent:] ()
#10 0x00007fff8c39e851 in -[NSObject performSelector:withObject:withObject:] ()
#11 0x00007fff90be589b in __-[NSAppleEventManager setEventHandler:andSelector:forEventClass:andEventID:]_block_invoke_1 ()
#12 0x00007fff90be4822 in -[NSAppleEventManager dispatchRawAppleEvent:withRawReply:handlerRefCon:] ()
#13 0x00007fff90be46b0 in _NSAppleEventManagerGenericHandler ()
#14 0x00007fff8e760c25 in aeDispatchAppleEvent ()
#15 0x00007fff8e760b03 in dispatchEventAndSendReply ()
#16 0x00007fff8e7609f7 in aeProcessAppleEvent ()
#17 0x00007fff912a1b6d in AEProcessAppleEvent ()
#18 0x00007fff8a49b63d in _DPSNextEvent ()
#19 0x00007fff8a49acf5 in -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] ()
#20 0x00007fff8a49762d in -[NSApplication run] ()
#21 0x0000000100002174 in main ()

1 个答案:

答案 0 :(得分:2)

我不确定,但如果使用SDL进行窗口和上下文创建,IIRC还需要添加OpenGL框架。