为什么我在所有像素的双for循环中使用SDL2和SDL_RenderCopy得到糟糕的性能

Why do I get bad performance with SDL2 and SDL_RenderCopy inside a double for loop over all pixels?

本文关键字:RenderCopy SDL2 SDL 性能 像素 循环 for 为什么      更新时间:2023-10-16

我正在使用SDL2编程一个光线投射游戏。当绘制地板时,我需要在像素上调用SDL_RenderCopy。这将导致瓶颈,使帧率下降到10fps以下。我正在寻找提高性能的方法,但似乎找不到。

下面是性能下降的粗略概述:

int main() {
  while(true) {
        for(x=0; x<800; x++) {
            for(y=0; y<600; y++) {
                SDL_Rect src = { 0, 0, 1, 1 };
                SDL_Rect dst = { x, y, 1, 1 };
                SDL_RenderCopy(ren, tx, &src, &dst); // this drops the framerate below 10
            }
        }
        SDL_RenderPresent(ren);
    }
 }

你可能应该使用纹理流。基本上,你将创建一个SDL_TEXTUREACCESS_STREAMING类型的SDL_Texture,然后每一帧你"锁定"纹理,更新你需要的像素,然后再次"解锁"纹理。然后在单个SDL_RenderCopy调用中渲染纹理。

  • LazyFoo示例http://lazyfoo.net/tutorials/SDL/42_texture_streaming/index.php
  • 探索银河系-http://slouken.blogspot.co.uk/2011/02/streaming -纹理- - sdl 13. - html

除此之外,每帧调用SDL_RenderCopy 48万次总是会杀死你的帧率

你在每一帧中调用SDL_RenderCopy(),所以600 * 800 = 48万次!性能下降是正常的