开发者

XNA 4.0 - Using spriteBatch and basicEffect lags

I am currently working on a game, in which we need to combine the use of DrawUserIndexedPrimitives and normal spriteBatch.Draw. Not combined as we use them at the same time, but we first have to draw some 2d sprites using spriteBatch, where after we disable spriteBatch to enable basicEffect and draw the primitives, and at last enable spriteBatch again. The code below shows the section of the code, where the problem is occurring.

spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(GraphicsDevice));
                levelController.Draw(spriteBatch);


                if (gameReady)
                {
                    foreach (GameObject.GameObject go in gameObjects)
                    {
                        go.Draw(spriteBatch);
                    }
                    spriteBatch.End();

                    foreach (GameObject.GameObject go in gameObjects)
                    {
                        if (go is GameObject.Enemy.Enemy)
                        {
                            GameObject.Enemy.Enemy enemy = (GameObject.Enemy.Enemy)go;

                            basicEffect = new BasicEffect(graphics.GraphicsDevice);
                            basicEffect.VertexColorEnabled = true;
                            basicEffect.CurrentTechnique.Passes[0].Apply();

                            GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, enemy.GetCollisionTriangle.Triangle, 0, 3, enemy.GetCollisionTriangle.Indices, 0, 1);
                        }
                    }
                }

                spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, cam.get_transformation(GraphicsDevice));

If the code below gets quoted out, the lag stops.

basicEffect = new BasicEffect(graphics.GraphicsDevice);
                        basicEffect.VertexColorEnabled = true;
                        basicEffect.CurrentTechnique.Passes[0].Apply();

                        GraphicsDevice.DrawUserIndexedPrimitives<VertexPositionColor>(PrimitiveType.TriangleList, enemy.GetCollisionTriangle.Triangle, 0, 3, enemy.GetCollisionTriangle.Indices, 0, 1)

Can it really be that we can't both use spriteBatch and basicEffect without the game lags a lot? it has been tested on 3 different computers, from a very开发者_StackOverflow old laptop, to a brand new gamer pc. The game is unplayable with the lag that occurs.


I think you should create the basiceffect elsewhere then your drawing routine. If I guess right you will use the same basiceffect all the time, so put it into inicializing, since "new" per every frame (edit: in a foreach for each and every object) can cost performance.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜