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

前端 未结 2 2046
温柔的废话
温柔的废话 2020-12-18 04:54

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

相关标签:
2条回答
  • 2020-12-18 05:46

    You are calling SDL_RenderCopy() in each frame so 600 * 800 = 480 000 times! It is normal for performance to drop.

    0 讨论(0)
  • 2020-12-18 05:49

    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.

    • LazyFoo Example - http://lazyfoo.net/tutorials/SDL/42_texture_streaming/index.php
    • Exploring Galaxy - http://slouken.blogspot.co.uk/2011/02/streaming-textures-with-sdl-13.html

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

    0 讨论(0)
提交回复
热议问题