2012-01-25 58 views
1

此代碼適用於GNU/Linux,但我無法找到如何使其在Mac OS X Lion上運行。OpenGL測試代碼在Mac OS X上不起作用

這是一個測試代碼。沒什麼太難理解的,只有一個等待退出事件停止的空窗口。如果我評論glClear調用,一切正常......事實上,glClear或任何gl調用使它崩潰在一個很好的分段錯誤。

下面是代碼:

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

我不確定,但是如果使用SDL進行窗口和上下文創建,IIRC還需要添加OpenGL框架。 – datenwolf

+0

嗨datenwolf。你能否爲這個問題創建一個答案,然後我將能夠設定好它。感謝您的幫助,OpenGL框架丟失了。現在一切正常。謝謝。 – lvictorino

回答

2

我不是當然,但如果使用SDL進行窗口和上下文創建,IIRC還需要添加OpenGL框架。

+0

再次感謝!它在這方面效果更好:g ++ -g -I/opt/local/include -I/usr/X11R6/include -L/opt/local/lib -lSDLmain -lSDL -Wl,-framework,Cocoa -framework OpenGL main的.cpp – lvictorino