Вопрос

Im making a 2d game in XNA. I started off using drawable game components and was quite happy. Every sprite is derived from drawable-game-component and each one has its own begin/end statement (even each tile making up a wall). Ive since realised after reading around a bit that this is a bad way to do it for performance.

Now I'm wondering what is a good way to do it... Lets say for each room in my game I do...

_spritebatch.Begin(SpriteSortMode.Deferred,
                       BlendState.Opaque,
                     SamplerState.LinearClamp,
                DepthStencilState.Default,
                  RasterizerState.CullNone,
                                  null,
                         _camera.Transform);

for (each block in Blocklist)
{
    _spritebatch.Draw(block._txture,
                      block._position + block._centerVect,
                      block.txtrect, block._color,
               (float)block.txtrot,
                      block._centerVect, 
                          1.0f,
              SpriteEffects.None,
                            0f);
}
_spritebatch.End();

the reason I'm asking is I'll have to make loads of the fields in each block public so the Room class can get at them to do its _spritebatch.Draw eg. block._position

I'm assuming this wont really matter and performance will improve but is there any nicer architecture anyone can recommend to do this?

Also since that code is up there, can anyone comment on the SpriteBatch.Begin paramaters I'm using? Are they the fastest ones? All I need in there is the transform

Это было полезно?

Решение

the reason I'm asking is I'll have to make loads of the fields in each block public so the Room class can get at them to do its _spritebatch.Draw eg. block._position

Assuming you have many rooms and in each room many blocks, you can give each room and each block a public or protected Draw() method, then all the fields of the block do not need to be public.

class Block
{
  ... // private fields about Block
  ...

  Public void Draw(GameTime gt, SpriteBatch sb)
  {
     sb.Draw(private block fields here);
  }
}

Then for the Rooms

class Room
{
  ... // private fields about room
  ...
  List<Block> blocks;

  Public void Draw(GameTime gt, SpriteBatch sb)
  {
     sb.Draw(private room fields here);
     foreach(Block b in blocks)
         b.Draw(gt, sb)
  }
}

Then in the main game class

//fields section
List<Room> rooms;


    // in the Draw

    spriteBatch.Begin();
      foreach(Room r in rooms)
          r.Draw(gt, spriteBatch);
    spriteBatch.End();

In this scenario, there is a single Begin/End block for the whole frame.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top