SDL Video Init在Mac OS X 10.8上导致异常

时间:2012-09-28 14:14:28

标签: c++ macos sdl osx-mountain-lion

我刚刚将我的C ++游戏移植到OS X并且第一次运行时我在尝试调用SDL_SetVideoMode时遇到以下异常。

2012-09-28 15:01:05.437 SCRAsteroids [28595:707] *由于未捕获的异常终止应用程序' NSInternalInconsistencyException',原因:'错误(1000)创建CGSWindow在第259行' * 第一次抛出调用堆栈: (     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:backing:defer:] + 1763     6 AppKit 0x00007fff8a22692f - [NSWindow _initContent:styleMask:backing:defer:contentView:] + 1568     7 AppKit 0x00007fff8a2262ff - [NSWindow initWithContentRect:styleMask:backing:defer:] + 45     8 libSDL-1.2.0.dylib 0x0000000107c228f6 - [SDL_QuartzWindow initWithContentRect:styleMask:backing: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 ) libc ++ abi.dylib:terminate调用抛出异常 中止陷阱:6

我的初始化代码如下:

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);

它进入最后一次SDL调用并抛出上面的异常。我已经在全屏和可调整大小的窗口模式下尝试过它,同样的事情。

我在命令行上构建我的app旧学校,而不是使用Xcode。

3 个答案:

答案 0 :(得分:6)

SDL_main再次成为罪魁祸首。我的C ++ main例程位于不包含SDL.h的文件中,因此未将其重新定义为SDL_main。包含SDL的代码在可重用的静态库中,而不是您看到的主要例程。我手动将我的函数名称更改为SDL_main,这意味着SDL提供了必要的主程序。我不喜欢这样做,但就目前而言,在Mac的SDL 1.2.15上,这是必要的。

在Windows上,相同的新代码会导致链接器冲突。这是一个新问题。

答案 1 :(得分:2)

在可可中调用显卡有问题。因此,在调用SDL_Setvideomode

之前,您需要将其初始化

添加以下方法,并在main方法中首先调用它

#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 :(得分:0)

相同的问题,但通过链接libSDLmain(以及libSDL)解决。这又需要两个框架:Foundation和Cocoa。

我没有重命名主要功能。