使用SDL2渲染平铺地图的最快方法

gni*_*moo 6 c++ sdl-2

我正在寻找一种非常快速的方法来渲染带有SDL2三层的平铺地图.我正在使用SDL_RenderCopy,但它很慢......

gni*_*moo 17

好的,我找到了我需要的东西,所以我会在这里解释一下.

我实际上有四层,我曾经在一个简单的for循环中渲染它们.实际上,for循环不是渲染平铺地图的好方法.

最好的方法是在主渲染循环之前将每个图层渲染为大纹理,然后将每个大纹理渲染到屏幕.for循环需要花费大量时间来处理,然而,渲染大纹理非常快.

考虑到"bigTexture"是一个图层,"width"和"height"是该图层的大小,请查看以下代码.

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

它完成后,我们将图层加载到一个大的纹理中.让我们看看如何呈现它.

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

就这样.您现在能够以非常快的速度渲染平铺地图.