c++ - Mac OSX 下的 SDL 中不显示图像和文本

标签 c++ macos 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: %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);
}

就像一个魅力。

最佳答案

我发现发生了什么事。结果是,from SDL version 1.1.18 SDL_Lock 调用是递归,因此每个锁都必须配对一个解锁。上次我使用 SDL 时并没有发生这种情况,所以我没有意识到这一点。只需匹配锁和解锁就可以完成这项工作。

关于c++ - Mac OSX 下的 SDL 中不显示图像和文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17134700/

相关文章:

c++ - 显示文本到窗口 C++

java - 三角形不在 OSX 上的 OpenGL 2.1 中绘制

macos - 正确地将对象移至垃圾箱

c++ - 'SDL_main' : must return a value

c++ - vtkUnstructuredGrid->GetPoint() 的线程安全只读替代方案

C# Task.Run() 与 C++ std::async()

c++ - 如何避免丢失自动生成的初始化列表构造函数?

macos - 如何将地址字符串转换为地址字典

c++ - 如何正确使用 SDL_BlitSurface() 和 SDL_CreateRGBSurface()?

cocoa - Cocoa 可以嵌入 SDL 窗口吗?