SDL背景blitting减慢游戏

时间:2013-06-26 21:12:37

标签: c++ background sdl frame-rate blit

我已经解决了有关这方面的其他问题,似乎无法得到我正在寻找的答案。我目前的游戏以60 fps的速度运行,在调试模式下占用9.5 ms /帧(在释放模式下为3.8 ms /帧)。这是玩家,敌人和地图都显示在屏幕上,这似乎根本不会导致fps下降,但是当我blit我的1024x768 BG图像时它下降到27-30 FPS现在呈现每帧在33毫秒/帧。继续游戏循环---

while(!quit)
{
    if(SDL_PollEvent(&event))
    {
        quit = Sys->isQuitGame(event);
    }

    fps->timerReset();
    blit_surface(bg,Sys->screen,0,0);//HERE'S THE PROBLEM!
    mapItem.RedrawingItems(Sys->screen, Sys->map);
    movement = input->Keyboard(key, Sys->screen, player);
    enemies.moveAllEnemies(Sys->map, player );
    player->move(movement, Sys->map);
    player->DrawPlayer(Sys->screen);
    enemies.draw(Sys->screen);

    SDL_Flip(Sys->screen);
    SDL_FillRect(Sys->screen, NULL, SDL_MapRGB( Sys->screen->format, 0xFF, 0xFF, 0xFF ));

    renderTime = calculatingFps->get_ticks();
    frame++;
    if(calculatingFps->get_ticks() > 1000) 
    {
        renderTime -= totalDelay;
        totalDelay = 0;
        stringstream Afps;
        Afps <<"fps: " <<frame/ (calculatingFps->get_ticks() / 1000.f)<<
                " ---- RenderTimePerFrame: "<<(float)renderTime / (float)frame<<"/ms";
        SDL_WM_SetCaption( Afps.str().c_str(), NULL);
        Game::FPS = frame/ (calculatingFps->get_ticks() / 1000.f);
        calculatingFps->timerReset();
        frame = 0;
        renderTime = 0;
    }
    if(frameRenderedTooFast(fps))
    {
        totalDelay += (remainingTime(fps));
        SDL_Delay(remainingTime(fps));
    }
}

所以那个img的一个blit设法将fps降低了一半,我一定错过了什么?顺便说一下,我不认为它是SDL_DisplayFormat,我已经有了那个代码,继承了处理img loading的代码snippit ---

SDL_Surface* altSDL::load_image(std::string filename)  
{
    SDL_Surface* loadedImage = NULL;

    SDL_Surface* optimizedImage = NULL;

    loadedImage = IMG_Load( filename.c_str() );

    if( loadedImage != NULL )
    {
        optimizedImage = SDL_DisplayFormat( loadedImage );        
        SDL_FreeSurface( loadedImage );
    }
    else
    {
        Failure* fail;
        fail = Failure::getInstance();
        fail->failLog(filename);
    }

    if( optimizedImage != NULL )
    {
        Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xFF, 0, 0xFF );
        SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
    }

    return optimizedImage;
}

我想一次或只是偶尔一次但是每一帧都有角色动作,所以等待动作然后再重写是没用的。我是否必须在单独的表面上使用setVideoMode作为背景或什么?任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

问题可能出在SDL_SetColorKey中。设置colorkey时,将优化图像的标记提供给SDL_MapRGB。而是给SDL_MapRGB显示表面的标志(显然是Sys->屏幕)。 (尽管有标志,设置颜色键可能会减慢blitting过程。)

    Uint32 colorkey = SDL_MapRGB( video_surface->format, 0xFF, 0, 0xFF );
    SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );

看起来屏幕上的第一个绘图是背景表面,所以我不明白为什么你甚至需要设置SDL_SetColorKey。只需删除它。同样,如果背景曲面覆盖整个屏幕,则调用SDL_FillRect(Sys->screen,NULL,SDL_MapRGB(Sys->screen->format, 0xFF, 0xFF, 0xFF ))也是多余的。