Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

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

I am programming a raycasting game using SDL2. When drawing the floor, I need to call SDL_RenderCopy pixelwise. This leads to a bottleneck which drops the framerate below 10 fps. I am looking for performance boosts but can't seem to find some.

Here's a rough overview of the performance drop:

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);
    }
 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should probably be using texture streaming for this. Basically you will create an SDL_Texture of type SDL_TEXTUREACCESS_STREAMING and then each frame you 'lock' the texture, update the pixels that you require then 'unlock' the texture again. The texture is then rendered in a single SDL_RenderCopy call.

Other than that calling SDL_RenderCopy 480,000 times a frame is always going to kill your framerate.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...