Jump to content

  • Log In with Google      Sign In   
  • Create Account


Avoid Drawing Offscreen Sprites / Textures in 2D XNA


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 Believe82   Members   -  Reputation: 118

Like
0Likes
Like

Posted 18 December 2013 - 07:38 AM

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!



Sponsor:

#2 laztrezort   Members   -  Reputation: 954

Like
0Likes
Like

Posted 18 December 2013 - 08:35 AM

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.



#3 Believe82   Members   -  Reputation: 118

Like
0Likes
Like

Posted 18 December 2013 - 09:56 AM

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?



#4 wicked357   Members   -  Reputation: 1144

Like
0Likes
Like

Posted 18 December 2013 - 10:18 AM

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
    }
}



#5 wicked357   Members   -  Reputation: 1144

Like
2Likes
Like

Posted 18 December 2013 - 10:32 AM

In fact here is a link to an old tutorial series using XNA done by Nick Gravelyn. It is really in depth and great resource for learning.

 

http://www.youtube.com/playlist?list=PLABC34F481106A2F7



#6 Believe82   Members   -  Reputation: 118

Like
0Likes
Like

Posted 18 December 2013 - 11:13 AM

Yea a tilemap is a great idea! Thanks.

Edit* Also thanks a lot for those video tutorials, they seem awesome!


Edited by Believe82, 18 December 2013 - 11:14 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS