为什么SDL2和SDL_RenderCopy在所有像素的双循环内得到不良性能?

Mar*_*rie 4 c++ performance game-engine raycasting sdl-2

我正在使用SDL2编写一个光线投射游戏.在绘制地板时,我需要以像素方式调用SDL_RenderCopy.这导致了瓶颈,使帧率降至10 fps以下.我正在寻找性能提升,但似乎无法找到一些.

以下是性能下降的概述:

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);
    }
 }
Run Code Online (Sandbox Code Playgroud)

Zam*_*lad 6

你可能应该使用纹理流.基本上你会创建一个SDL_Texture类型SDL_TEXTUREACCESS_STREAMING然后每个框架你'锁定'纹理,更新你需要的像素,然后再次"解锁"纹理.然后在单个SDL_RenderCopy调用中呈现纹理.

除了调用SDL_RenderCopy480,000次帧之外总是会杀死你的帧率.