I'm searching for a very fast way to render a tiled map with three layers with SDL2. I'm using SDL_RenderCopy but it's very slow...
Ok, I've found what I needed, so I'll explain it here.
I've actually four layers, and I used to render them in a simple for loop. In fact, the for loop isn't a good way to render tiled maps.
The best way is to render each layer into a big texture, before the main rendering loop, and then render each big texture to screen. The for loop takes a lot of time to process, however, rendering a big texture is very fast.
Take a look at the following code, considering that "bigTexture" is a layer, and "width" and "height" the size of that layer.
Uint32 pixelFormat;
SDL_QueryTexture(tileset, &pixelFormat, NULL, NULL, NULL);
SDL_Texture *bigTexture = SDL_CreateTexture(renderer, pixelFormat, SDL_TEXTUREACCESS_TARGET, width, height);
SDL_SetRenderTarget(renderer, bigTexture);
// Put your for loop here
It's done, we loaded our layer into a big texture. Let's see how to render it.
SDL_SetRenderTarget(renderer, NULL);
// Create a SDL_Rect which defines the big texture's part to display, ie the big texture's part visible in the window.
// Display the big texture with a simple SDL_RenderCopy
That's all. You're now able to render tiled maps in a very fast way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With