2012-09-28 12 views
7

Acabo de portar mi juego C++ a OS X y la primera vez que lo ejecuté recibí la siguiente excepción al intentar llamar al SDL_SetVideoMode.SDL Video Init causa una excepción en Mac OS X 10.8

28/09/2012 15: 01: 05,437 SCRAsteroids [28595: 707] * Terminación de aplicación debido a excepción no detectada 'NSInternalInconsistencyException', razón: 'Error (1000) la creación de CGSWindow en la línea 259' * primer tiro pila de llamadas: ( 0 CoreFoundation 0x00007fff8b53b716 __exceptionPreprocess + 198 1 libobjc.A.dylib 0x00007fff90e30470 objc_exception_throw + 43 2 CoreFoundation 0x00007fff8b53b4ec + [NSException aumento: formato:] + 204 3 AppKit 0x00007fff8a26a579 _NSCreateWindowWithOpaqueShape2 + 655 4 AppKit 0x00007fff8a268d70 - [ NSWindow _commonAwake] + 2002 5 AppKit 0x00007fff8a2277e2 - [NSWindow _commonInitFrame: styleMask: respaldo: Defer:] + 1,763 6 AppKit 0x00007fff8a22692f - [NSWindow _initContent: styleMask: respaldo: Defer: contentView:] + 1,568 7 AppKit 0x00007fff8a2262ff - [NSWindow initWithContentRect: styleMask: respaldo : Defer:] + 45 8 libSDL-1.2.0.dylib 0x0000000107c228f6 - [SDL_QuartzWindow initWithContentRect: styleMask: respaldo: 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: terminar llamado lanzar una excepción trampa Abort: 6

Mi código de inicio se ve así:

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

Se pone en esa última llamada SDL y lanza la excepción anteriormente. Lo he intentado tanto en pantalla completa como en modo de ventana de tamaño variable, lo mismo.

Construyo mi aplicación de la vieja escuela, en la línea de comandos, en lugar de usar Xcode.

Respuesta

6

SDL_main fue una vez más el culpable. Mi rutina C++ main estaba en un archivo que no incluye SDL.h, por lo que no estaba siendo redefinido a SDL_main. El código que incluye SDL está en su lugar en una biblioteca estática reutilizable, sin rutina principal que vea. Cambié manualmente el nombre de mi función a SDL_main y esto significa que SDL proporciona la rutina principal esencial. No me gusta hacer esto, pero por el momento, en SDL 1.2.15 para Mac, es necesario.

En Windows, el mismo nuevo código provoca conflictos de vinculador. Ese es un problema nuevo.

0

Mismo problema, pero resuelto al vincular libSDLmain (así como libSDL). Esto a su vez requiere dos marcos: Fundación y Cocoa.

No cambié el nombre de la función principal.

+1

¿Tiene su archivo principal incluyen SDL.h? Si es así, su main() se renombra para usted. – ScrollerBlaster

2

Hay problemas para llamar a la tarjeta de video en cacao.Así que hay que inicializar antes de llamar SDL_Setvideomode

Añadir el siguiente método, y lo llaman por primera vez en su principal método

#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(); 
} 

`