SDL Video Init throws exception on Mac OS X 10.8

I just ported my game to C ++ in OS X, and the first time I get it, I get the following exception when trying to call SDL_SetVideoMode .

2012-09-28 15: 01: 05.437 SCRAsteroids [28595: 707] * Application terminated due to an uncaught exception 'NSInternalInconsistencyException', reason: 'Error (1000), creating CGSWindow on line 259' * First stack of throw calls: (0 CoreFoundation 0x00007fff8b53b716 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff90e30470 objc_exception_throw + 43 2 CoreFoundation 0x00007fff8b53b4ec + [NSException raise: format:] + 204 3 AppKit 0x00007fff8a26a579 _NSCreateWindowWithOpaqueShape2 + 655 4 AppKit 0x00007fff8a268d70 - [NSWindow _commonAwake] + 2002 5 AppKit 0x00007fff8a2277e2 - [NSWindow _commonInitFrame: styleMask: support: defer:] + 1763 6 AppKit 0x00007fff8a22692f - [NSWindow _initContent: styleMask: support: defer: contentView:] + 1568 7 AppKit 0x00007fff8a2262ff - [NSWindow initWithContentRect: styleMask it:] + 45 8 libSDL-1.2.0.dylib 0x0000000107c228f6 - [SDL_QuartzWindow initWithContentRect: styleMask: Support: defer:] + 294 9 libSDL-1.2.0.dylib 0x0000000107c20505 QZ_SetVideoMode + 2837 10 libSDL-1.2.0.dylib 0x0000000107c17af5 SDL_SetVideoMode + 917 11 SCRAsteroids 0x0000000107be60fb _ZN11SDLGraphics4initEP6IWorldii + 291) lib ++ abi.dylib: terminate is called throwing exception Interrupt trap: 6

My initialization code is as follows:

 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return false; const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo(); if (!videoInfo) { fprintf(stderr, "Video query failed: %s\n", SDL_GetError()); return false; } /* the flags to pass to SDL_SetVideoMode */ videoFlags = SDL_OPENGL; /* Enable OpenGL in SDL */ videoFlags |= SDL_GL_DOUBLEBUFFER; /* Enable double buffering */ videoFlags |= SDL_HWPALETTE; /* Store the palette in hardware */ /* This checks to see if surfaces can be stored in memory */ if (videoInfo->hw_available) videoFlags |= SDL_HWSURFACE; else videoFlags |= SDL_SWSURFACE; if (w == 0) { widthViewport = videoInfo->current_w; heightViewport = videoInfo->current_h; cout << "Will use full screen resolution of "; videoFlags |= SDL_FULLSCREEN; } else { cout << "Will use full user supplied resolution of "; widthViewport = w; heightViewport = h; videoFlags |= SDL_RESIZABLE; /* Enable window resizing */ } cout << widthViewport << "x" << heightViewport << "\n"; /* This checks if hardware blits can be done */ if (videoInfo->blit_hw) videoFlags |= SDL_HWACCEL; /* Sets up OpenGL double buffering */ SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); /* get a SDL surface */ surface = SDL_SetVideoMode(widthViewport, heightViewport, SCREEN_BPP, videoFlags); 

It falls into this last SDL call and throws an exception above. I tried it both in full screen mode and in resize mode, the same thing.

I am building my old school application on the command line, as opposed to using Xcode.

+7
source share
3 answers

SDL_main became the culprit again. My C ++ main program was in a file that does not include SDL.h , so it was not redefined to SDL_main . Code that includes SDL is instead used in a reusable static library, no basic procedure that you see. I manually changed the name of my function to SDL_main , and this means that SDL provides the main main procedure. I do not like to do this, but at the moment, on SDL 1.2.15 for Mac, this is necessary.

On Windows, the same new code causes linker conflicts. This is a new problem.

+6
source

There are problems with calling a graphics card in cocoa. Therefore, you need to initialize it before calling SDL_Setvideomode

Add the following method and call it first in the main method

 #include <dlfcn.h> //To make it work on mac //This must be called before playing with SDL, else it won't work on osx. void pre_init() { void* cocoa_lib; cocoa_lib = dlopen( "/System/Library/Frameworks/Cocoa.framework/Cocoa", RTLD_LAZY ); void (*nsappload)(void); nsappload = (void(*)()) dlsym( cocoa_lib, "NSApplicationLoad"); nsappload(); } 

`

+2
source

Same problem, but resolved by linking libSDLmain (as well as libSDL). This, in turn, requires two frameworks: Foundation and Cocoa.

I did not rename the main function.

0
source

All Articles