在Mac OSX下的SDL中不显示图像和文本



我必须在XCode 4.3和SDL 1.2.15下编译,捆绑和加载资源

我知道资源正在正确加载,因为文件句柄不是空的,没有抛出错误。

我成功加载png和ttf文件,获取和裁剪表面,并对它们进行blit。

但是当我翻转时,我唯一能看到的就是我用SDL_Draw

画的线

我将放一些代码,因为我试图保持一个引擎英语的结构,所以代码是所有的东西,但在一起。

初始化:

void CEngine::Init() {
    // Register SDL_Quit to be called at exit; makes sure things are cleaned up when we quit.
    atexit( SDL_Quit );
    // Initialize SDL's subsystems - in this case, only video.
    if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) {
        fprintf( stderr, "Unable to init SDL: %sn", SDL_GetError() );
        exit( 1 );
    }
    // Attempt to create a window with the specified height and width.
    SetSize( m_iWidth, m_iHeight );
    // If we fail, return error.
    if ( m_pScreen == NULL ) {
        fprintf( stderr, "Unable to set up video: %sn", SDL_GetError() );
        exit( 1 );
    }
    AdditionalInit();
}

void CTileEngine::AdditionalInit() {
    SetTitle( "TileEngine - Loading..." );
    PrintDebug("Initializing SDL_Image");
    int flags = IMG_INIT_PNG;
    int initted = IMG_Init( flags );
    if( ( initted & flags ) != flags ) {
        PrintDebug("IMG_Init: Failed to init required image support!");
        PrintDebug(IMG_GetError());
        // handle error
    }
    PrintDebug("Initializing SDL_TTF");
    if( TTF_Init() == -1 ) {
        PrintDebug("TTF_Init: Failed to init required ttf support!");
        PrintDebug(TTF_GetError());
    }
    PrintDebug("Loading fonts");
    font = TTF_OpenFont( OSXFileManager::GetResourcePath("Roboto-Regular.ttf"), 28 );
    if( !font ) {
        PrintDebug("Error loading fonts");
        PrintDebug(TTF_GetError());
    }
    g_pGame = new CGame;
    LoadGame( OSXFileManager::GetResourcePath( "test", "tmx") );
    SetTitle( "TileEngine" );
    PrintDebug("Finished AditionalInit()");
} 

主绘制方法

void CEngine::DoRender(){
    ++m_iFPSCounter;
    if ( m_iFPSTickCounter >= 1000 ) {
        m_iCurrentFPS = m_iFPSCounter;
        m_iFPSCounter = 0;
        m_iFPSTickCounter = 0;
    }
    SDL_FillRect( m_pScreen, 0, SDL_MapRGB( m_pScreen->format, 0, 0, 0 ) );

    // Lock surface if needed
    if ( SDL_MUSTLOCK( m_pScreen ) ){
        if ( SDL_LockSurface( m_pScreen ) < 0 ){
            return;
        }
    }
    Render( GetSurface() );
    // Render FPS
    SDL_Color fpsColor = { 255, 255, 255 };
    string fpsMessage = "FPS: ";
    fpsMessage.append( SSTR(m_iCurrentFPS) );
    SDL_Surface* fps = TTF_RenderText_Solid(font, fpsMessage.c_str(), fpsColor);
    if( fps ) {
        SDL_Rect destRect;
        destRect.x = pDestSurface->w - fps->w;
        destRect.y = pDestSurface->h - fps->h;
        destRect.w = fps->w;
        destRect.h = fps->h;
        SDL_BlitSurface(fps, &fps->clip_rect, pDestSurface, &destRect);
        SDL_FreeSurface(fps);
    }
    // Unlock if needed
    if ( SDL_MUSTLOCK( m_pScreen ) )
        SDL_UnlockSurface( m_pScreen );
    // Tell SDL to update the whole gScreen
    SDL_Flip( m_pScreen );
}

图片文件加载

bool CEntity::VLoadImageFromFile( const string& sFile) {
    if ( m_pSurface != 0 ){
        SDL_FreeSurface( m_pSurface );
    }
    string nFile = string(OSXFileManager::APPNAME) + OSXFileManager::RESOURCEDIR + sFile;
    SDL_Surface *pTempSurface;
    pTempSurface = IMG_Load( nFile.c_str() );
    m_sImage = sFile;
    if ( pTempSurface == 0 ){
        char czError[256];
        sprintf( czError, "Image '%s' could not be opened. Reason: %s", nFile.c_str(), IMG_GetError() );
        fprintf( stderr, "nERROR: %s", czError );
        return false;
    } else {
        pTempSurface = SDL_DisplayFormatAlpha(pTempSurface);
    }

    m_pSurface  = pTempSurface;
    return true;
}

实体绘制方法

void CEntity::VRender( SDL_Surface *pDestSurface ) {
    if ( ( m_pSurface == 0 ) || ( m_bVisible == false) || ( m_iAlpha == 0 ) ){
    // If the surface is invalid or it's 100% transparent.
        return;
    }
    SDL_Rect SDestRect;
    SDestRect.x = m_iPosX;
    SDestRect.y = m_iPosY;
    SDestRect.w = m_pSurface->w;
    SDestRect.h = m_pSurface->h;
    if ( m_iAlpha != 255 )
        SDL_SetAlpha( m_pSurface, SDL_SRCALPHA, m_iAlpha );
    SDL_BlitSurface( m_pSurface, &m_pSurface->clip_rect, pDestSurface, &SDestRect );
}

我已经检查和调试了一百万次,我不知道哪里出错了。正如我之前所说,文件加载似乎是OK的。

但这部分

void CTile::RenderGrid( SDL_Surface* pDestSurface ) {
    Uint32 m_GridColor = SDL_MapRGB( pDestSurface->format, 0xFF, 0xFF, 0xFF );
    Draw_Rect(pDestSurface, GetPosX(), GetPosY(), GetWidth(), GetHeight(), m_GridColor);
}

很有魅力。

我知道发生了什么事。事实证明,从SDL版本1.1.18开始,SDL_Lock调用是递归的,因此每个锁必须配对一个解锁。上次我使用SDL时没有发生这种情况,所以我没有意识到这一点。简单地匹配锁和锁就可以了。

最新更新