【问题标题】:SDL : blitting time optimizationSDL:blitting 时间优化
【发布时间】:2011-12-29 10:07:35
【问题描述】:

我在窗口模式下使用 SDL。

给定一个大精灵,比如 1024 x 640,将整个图像blit 成几个小图块,比如32 x 32 px,比一次blit 整个图像更快吗?

我必须在窗口模式下以 30 FPS(至少)在屏幕上(1024 * 640)blit 背景。我认为窗口化时不能使用双缓冲和硬件表面......现在,我一次将整个精灵blit,但是对于简单的背景blitting,CPU消耗似乎非常高。

【问题讨论】:

  • 别忘了convert your surfaces to the display format,否则 SDL 将为每个 blit 进行幕后转换。
  • 我做到了。但是,blit 仍然是 CPU 贪婪的 ...
  • @ArnaudG 我知道这已经很老了,但是您如何计算 CPU 消耗?你的帧率是多少?

标签: image graphics sdl tiles blit


【解决方案1】:

首先请确保图片为“PNG”等小文件类型格式。其次,在对图像进行 blitting 时,请确保您没有将透明度颜色添加到背景图像中,否则它是没有意义的,并且会占用大量的 cpu 周期。这是我很久以前作为图像加载包装器编写的代码示例

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

在此代码中,您将能够将所有图像传递到其中以非常无缝地进行优化,但在背景/大图像的情况下,您需要确保将最后一段代码更改为更多内容像这样的行:

//Added some Sudo code

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

isBigImage是我添加的sudo代码,是传入的参数;在某些情况下。这将使您的所有图像都得到优化,更小(应确保它们是 png 文件),并将使其透明度仅添加到小图像文件中,因此您的背景不会降低 fps

【讨论】:

    猜你喜欢
    • 2015-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 2013-03-10
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多