0

I have to add a second viewport (= like splitscreen) to allow the player to see an event somewhere else on the current level.

Is there any possibility to draw the event area without redrawing every things already drew ?

[EDIT] RenderTarget2D is the key. Thx User1459910 for all.

It almost worked.

New questions :

  1. I've searched for a while and still don't find a tutorial about "Xna 2D camera with source and destination rectangle" if you have a link, i'd like to see it, please ♥</li>
  2. Currently, the drawing code looks like this :

    protected override void Draw(GameTime gameTime)
    {
    
       /*
       ...
       here is the code to "draw" in the renderTarget2D renderAllScene object
       ...
       */
    
       //Let's draw into the 2 viewports
       spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, camera1.transform);
       spriteBatch.Draw(renderAllScene, viewport1.Bounds, Color.White);
       spriteBatch.End();
       if (EventIsRunning)
       {
          spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, null, null, null, camera2.transform);
          spriteBatch.Draw(renderAllScene, viewport2.Bounds, Color.White);
          spriteBatch.End();
       }
    

*The Viewport 1 is great. The camera follows the character but after moving the camera for a short distance, the map is cutted at 1280pixels i think. so it drew only 1280pixels of all the map. I don't know why. Maybe because i failed when i created renderAllScene = new RenderTarget2D. :x

        renderAllScene = new RenderTarget2D(GraphicsDevice, GraphicsDevice.PresentationParameters.BackBufferWidth, GraphicsDevice.PresentationParameters.BackBufferHeight);

*For the Viewport 2 : I need the source rectangle. I'll try it tomorrow.

4

1 回答 1

1

I'll assume you are making a 2D game with NOTHING 3D at all.

Here is what you could do:

You need to render the whole map, and all game objects that appear on it, on a Texture. If you don't know how to render to a Texture, here is the procedure:

  1. Create a RenderTarget2D object
  2. On the Draw function, before you render anything, you must call the graphicsDevice.SetRenderTarget() method, and set the RenderTarget2D you created.
  3. After you are done rendering, call graphicsDevice.SetRenderTarget(null) to reset the render target to the default one. You must do it or you'll have problems!
  4. To render the RenderTarget2D, simply use SpriteBatch.Draw((Texture2D)renderTarget2D, position, color), being "renderTarget2D" of course the name of the RenderTarget2D you created.

Then, you use two 2D Cameras. One will display where the hero is, and the other one will display the event area.

A 2D camera is basically a trick with Source and Destination rectangles. The trick is to use a Source Rectangle to define the area that displays the hero and the area around it and use the main Viewport as the Destination Rectangle, and use another Source Rectangle to define the event area and another Destination Rectangle as the second Viewport.

If you have doubts, google about "XNA 2D Camera", and research about Source and Destination rectangles on the MSDN's article for SpriteBatch.Draw().

于 2013-01-28T23:30:05.870 回答