7

我刚刚将我的 C++ 游戏移植到 OS X,第一次运行时,我在尝试调用SDL_SetVideoMode.

2012-09-28 15:01:05.437 SCRAsteroids [28595:707] * 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“错误(1000)在第 259 行创建 CGSWindow”*第一掷调用堆栈:(0的CoreFoundation 0x00007fff8b53b716 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff90e30470 objc_exception_throw + 43 2的CoreFoundation 0x00007fff8b53b4ec + [NSException提高:格式:] + 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 0x00007fff8a2226styleMask: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 + 28.37 libSDL-101. .dylib 0x0000000107c17af5 SDL_SetVideoMode + 917 11 SCRAsteroids 0x0000000107be60fb _ZN11SDLGraphics4initEP6IWorldii + 291 ) libc++abi.dylib:终止调用抛出异常中止陷阱:6dylib 0x0000000107c17af5 SDL_SetVideoMode + 917 11 SCRAsteroids 0x0000000107be60fb _ZN11SDLGraphics4initEP6IWorldii + 291 ) libc++abi.dylib:终止调用抛出异常中止陷阱:6dylib 0x0000000107c17af5 SDL_SetVideoMode + 917 11 SCRAsteroids 0x0000000107be60fb _ZN11SDLGraphics4initEP6IWorldii + 291 ) libc++abi.dylib:终止调用抛出异常中止陷阱: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 调用并抛出上面的异常。我在全屏和可调整大小的窗口模式下都试过了,同样的事情。

我在命令行上构建我的应用程序老派,而不是使用 Xcode。

4

3 回答 3

6

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

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

于 2012-09-30T21:34:40.727 回答
2

在 cocoa 中调用视频卡有问题。所以你需要在调用 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();
}

`

于 2013-07-25T17:50:34.277 回答
0

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

我没有重命名主要功能。

于 2012-10-08T10:20:36.277 回答