一次性繪製所有 DrawableGameComponents (Drawing all DrawableGameComponents in a single batch)


問題描述

一次性繪製所有 DrawableGameComponents (Drawing all DrawableGameComponents in a single batch)

This is an XNA question...

I have a large number of objects that are DrawableGameComponents in my game and they all share the same SpriteBatch. However, I have to call SpriteBatch.Begin() and .End() everytime to draw each component. I want to draw everything in a single batch for better performance but I don't know where .Begin() and .End() can go since the .Draw() is being automatically called (as they are DrawableGameComponents)

Does anyone know what I can do to make them all be drawn between a single .Begin() and .End() call whilst keeping them as DrawableGameComponents?

Edit:

Alright from this website http://www.progware.org/Blog/post/XNA‑Game‑Development‑(Decentralize).aspx I figured that it's called at the main game loop's base.Draw() call... so I can just wrap that with the Begin and End methods

protected override void Draw(GameTime gameTime) {
    if (Cell.Cell.CellSpriteBatch != null) {
        Cell.Cell.CellSpriteBatch.Begin(
            SpriteSortMode.BackToFront, BlendState.AlphaBlend, 
            SamplerState.AnisotropicClamp, null, null, null,
            gamePlay.Camera.GetTransformation()
        );
    }

    base.Draw(gameTime);

    if (Cell.Cell.CellSpriteBatch != null) {
        Cell.Cell.CellSpriteBatch.End();
    }
}

and it works now but doing it this way seems to mean that everything else that I want drawn in a different SpriteBatch will always be drawn below this (as they are drawn first) because the .End() of this SpriteBatch is always going to be called last.

Any solution to this?

‑‑‑‑‑

參考解法

方法 1:

Firstly, unless you already know that you are batch limited its probably not worth doing this. If every single .Draw call is pulling from the same texture then you might see some speed up. If they are different textures then switching to a single Begin/End will have no difference. SpriteBatch will break up your draw calls anyway.

As you found doing this makes your code pretty confusing too ‑ you have to keep track between components.

The only solution to your current problem is to make the SpriteBatch Immediate so that it draws as you call the draw calls instead of drawing at the End call. However this will kill your perf as no batching is done at all.

Personally I would stick to a Begin/End per component until you hit a perf problem. Assuming you don't have 5000 components that is in which case you probably need to architect things a little differently.

(by DoisThe ZMan)

參考文件

  1. Drawing all DrawableGameComponents in a single batch (CC BY‑SA 3.0/4.0)

#draw #xna #drawablegamecomponent #sprite #C#






相關問題

使用Java繪製維恩圖 (Draw Venn diagram using Java)

android中的ontouch:獲取X和Y坐標並在該點上繪製圓 (ontouch in android: getting X and Y coordinates and drawing circle on that point)

如何在android上將谷歌地圖圖層作為片段佈局移動 (how to move google map layers as fragment layout on android)

onTouch() 無法繪製畫布 (onTouch() can't draw canvas)

佈局僅在屏幕關閉或離開應用程序並返回後顯示兒童 (Layout only shows children after screen turn off or leaving the app and coming back)

將形狀對象的 ArrayList 繪製到框架 (Paint ArrayList of Shape Objects to Frame)

多線程圖形 (multithreading Graphics)

一次性繪製所有 DrawableGameComponents (Drawing all DrawableGameComponents in a single batch)

html5畫布繪製帶有額外數據的圓形變量 (html5 canvas draw circle variables with extra data)

iphone初學者問題:畫一個矩形。我究竟做錯了什麼? (Beginner iphone question: drawing a rectangle. What am I doing wrong?)

連接 4 Android Studio 繪製空格 (Connect 4 Android Studio Draw empty spaces)

矩形不動 (Rectangle is not moving)







留言討論