2013-10-15 37 views
1

我使用SDL 2,我有8×8像素塊的二維數組:如何優化SDL 2中許多紋理的渲染?

SDL_RenderClear(renderer); 
    SDL_SetRenderDrawColor(renderer, 0xdf, 0xef, 0xff, 0xff); 
    for (s32 y = 0, Y = 0; y < worldSize.y; y++, Y += blockSize) 
     for (s32 x = 0, X = 0; x < worldSize.x; x++, X += blockSize) 
      if (block[x][y].GetMaterial()) 
      { 
       if (block[x][y].IsSolid()) 
       { 
        SDL_Rect src = 
        { 
         block[x][y].GetTileVariant() * blockSize, 
         block[x][y].GetTile() * blockSize, 
         blockSize, 
         blockSize 
        }; 
        SDL_Rect dst = 
        { 
         X + (s32)posX, 
         Y + (s32)posY, 
         blockSize, 
         blockSize 
        }; 
        SDL_RenderCopy(renderer, materials[block[x][y].GetMaterial()]->texture, &src, &dst); 
       } 
      } 
    SDL_RenderPresent(renderer); 

所以在這裏我以隨機順序使用一些紋理,如:1,2,5,2,2, 3,1等。如果它填滿了所有屏幕(1920x1080),它就會延遲。不是那麼多,但無論如何大聲。所以我的問題是:如何優化它?我應該使用什麼?我需要一些代碼示例。最後,我應該自己寫一些關於openGL的東西,還是應該怎麼做?

+1

最終像https://bugzilla.libsdl.org/show_bug.cgi?id=1734將降落。在此期間,您可以在我的叉子上編譯:https://bitbucket.org/gabomdq/sdl-1.3-experimental/commits/branch/RenderGeometry – gabomdq

回答

1

嘗試按材質/紋理對SDL_RenderCopy()調用進行分組,並希望OpenGL驅動程序快速路徑冗餘glBindTexture() s。

和/或將您的所有瓷磚分組成一個大紋理圖集。

或者,如果你的瓷磚實際上並沒有移動/改變幀到幀blit them into one large texture並渲染。

長期問題,mailing list上的bug SamSDL_RenderCopyBatch()類似,與SDL_RenderDrawRects()類似。

+0

一個想法與Terraria的塊有關。我有巨大的二維數組,但我只渲染掉。 – Necronomicron

-2

我和我的朋友在沒有OpenGL的情況下解決了它。下面是一個例子(源代碼太大而不必要在這裏,我將只介紹的主要思想):

int pitch, *pixels; 
SDL_Texture *texture; 
... 
if (!SDL_LockTexture(texture, 0, (void **)&pixels, &pitch)) 
{ 
    for (/*Conditions*/) 
     memcpy(/*Params*/); 
    SDL_UnlockTexture(texture); 
} 
SDL_RenderCopy(renderer, texture, 0, 0); 
+0

請注意,這並不能解決問題中的問題; LockTexture/UnlockTexture打開要由CPU寫入的紋理。除非你在程序上生成紋理,否則這可能不是你想要的。 – Cubic