0

我必须在 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: %s\n", 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: %s\n", 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 );
}

我已经检查和调试了数百万次,但我不明白这里出了什么问题。正如我之前所说,文件加载似乎没问题。

但是这部分

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

奇迹般有效。

4

1 回答 1

2

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

于 2013-06-16T18:29:00.260 回答