Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to render a tiled map with SDL2 [closed]

Tags:

c++

sdl-2

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...

like image 539
gnidmoo Avatar asked Mar 02 '14 19:03

gnidmoo


1 Answers

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.

like image 62
gnidmoo Avatar answered Oct 19 '22 21:10

gnidmoo