1
\$\begingroup\$

I asked a question here: C# XNA Make rendered screen a texture2d

But, I ended up not getting the exact result I was looking for since I didn't ask the question right.

In a game I am writing, I render an extremely large city out of objects, this can cause lag when moving the camera to view things that are off screen. I need a way to render then ENTIRE city, even the stuff that is off screen, and make it into a Texture2D.

The answer I chose for the last one didn't work entirely right because it only gets what is on screen, not what is off.

\$\endgroup\$
1
  • \$\begingroup\$ This is a classic XY problem. You're having trouble with framerate hitches, but instead of asking for how to solve the framerate problem, you think you have a solution. The problem is that your solution is wrong. Rendering everything to a giant texture and blitting parts of it to the screen is not an effective way to solve this issue. Certainly not for a landscape that would reasonably be called "extremely large". \$\endgroup\$ Commented Nov 12, 2012 at 11:09

1 Answer 1

1
\$\begingroup\$

You can still use this approach.

For 3D Just move the camera much further away so that the entire city is in the view volume. [or make the view volume wider/higher if using orthographic projection]

You will have to make the size of the render target much bigger to accommodate the required resolution, but you will not be able to make a texture more than 4,096 pixels in either dimension.

If 4,096 sized textures is not enough, than you could look at making a number of textures

[EDIT]

For 2D just render the scene on a render target much larger than the screen size e.g 4096 pixels across, making sure that you render things with the same scale (i.e. if a building is 10 pixels across on screen, it should be 10 pixels across on the render target). This way you will get a much larger area of your city drawn. You can then draw subsections of this texture on the screen.

int vpw = graphics.GraphicsDevice.Viewport.Width;
int vph = graphics.GraphicsDevice.Viewport.Height;

spriteBatch.Begin();

//camera.Position is the point in  the world you want at the centre of the screen
//cityRect is the sub-area of the large background you want to display
Rectangle cityRect=new Rectangle(
                (int)(camera.Position.X-vpw/2), 
                (int)(camera.Position.Y-vph/2),
                vpw, 
                vph);

//cityTexture is the large pre-rendered texture
spriteBatch.Draw(cityTexture, Vector2.Zero, 
            cityRect, Color.White);
spriteBatch.End();
\$\endgroup\$
3
  • \$\begingroup\$ This doesn;t work because I don't have zoom implemented on the camera. Also, wouldn't zooming back in make it super pixelly? \$\endgroup\$ Commented Nov 11, 2012 at 21:07
  • \$\begingroup\$ @redcodefinal, For some reason I assumed that you were using 3D. if you are using 2D it is even easier. See my edit. \$\endgroup\$
    – Ken
    Commented Nov 12, 2012 at 11:27
  • \$\begingroup\$ I am using 2D. I've never used render targets before so I will give them a research. \$\endgroup\$ Commented Nov 18, 2012 at 19:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .