Sign in to follow this  
Believe82

Avoid Drawing Offscreen Sprites / Textures in 2D XNA

Recommended Posts

Hi,

I'm pretty new to XNA and am making a 2D RPG and I have a quick question regarding drawing in XNA.

Currently, I have a large background texture, so I would rather not re-draw the entire background every frame. I have multiple sprites I update and process and draw. I understand the GPU culls offscreen pixels, however to achieve even higher optimization for sprites I check if the sprites texture bound is inside my camera view (using the view transform matrix) bounding frustum. If the sprite is not inside the view bounding frustum I do not even make a call to draw the sprite.

However for the large background, I check if the background is currently in view, if it is I use source atlasing to determine the top left coordinate of the background I will be using, and only draw that section of the background (size of the screen). 

I feel like my way of avoiding drawing the background is pretty hack because I have to constantly calculate the new origin position within my background, and see if I will be drawing the entire section of the background or a piece of it ( for edge cases of the background).

Is there a better way of avoiding drawing the large texture every frame besides the one I gave? Do these optimizations even matter in XNA?

Thanks for any help!

Share this post


Link to post
Share on other sites

Is there a better way of avoiding drawing the large texture every frame besides the one I gave? Do these optimizations even matter in XNA?

 

This is the question I personally would ask before doing this type of optimization.  Depending on the hardware targeted, and what calculations are being done CPU side, I could even believe it would be slower.

 

The best way to know for sure is to profile, even a quick & dirty "stress test" with FRAPS running (if on PC) in the background could tell you if the easier solution (just let the GPU cull pixels) is fast enough.

 

I often found that iterating over sprites or other data structures CPU side (for large collections) is usually the bottelneck; pushing everything to the GPU (properly batched) is pretty fast, on modern PC hardware.  However, culling sprites can be important if you are doing heavy calcs on a large set, e.g. physics or collision detection.

Share this post


Link to post
Share on other sites

Thanks for the reply!

I did an FPS test, I saw no difference in FPS.

I guess in the future after attempting optimizations I'll do self-tests to determine if there was an improvement or not. Honestly I was just hoping there was some standard procedure for not constantly - drawing large texture backgrounds every frame in XNA. So... is there?

Share this post


Link to post
Share on other sites

Have you considered instead of doing a large texture but drawing a grid of sprites? There are tons of resources you can find on drawing a tilemap.

 

int[,] tilemap = [
    [0,0,0],
    [1,0,0],

    [1,0,1]
];
 
// Loop through it
for (int y = 0; y < tilemap.Length; y++) {
    for (int x = 0; x < tilemap[y].Length; x++) {
        // Check if tiles is in range of viewport and render them
    }
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this