Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
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". –  Nicol Bolas Nov 12 '12 at 11:09
add comment

1 Answer 1

up vote 1 down vote accepted

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();
share|improve this answer
    
This doesn;t work because I don't have zoom implemented on the camera. Also, wouldn't zooming back in make it super pixelly? –  redcodefinal Nov 11 '12 at 21:07
    
@redcodefinal, For some reason I assumed that you were using 3D. if you are using 2D it is even easier. See my edit. –  Ken Nov 12 '12 at 11:27
    
I am using 2D. I've never used render targets before so I will give them a research. –  redcodefinal Nov 18 '12 at 19:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.