XNA Creators Club Online
Page 1 of 1 (8 items)
Sort Posts: Previous Next

Spritesheets - How are they implemented?

Last post 10/9/2007 2:15 PM by The Thing. 7 replies.
  • 10/5/2007 5:28 PM

    Spritesheets - How are they implemented?

    Hi folks,

    In relation to an earlier post of mine I am attempting to refine various aspects of my demo and one of the things I'd like to do is use a spritesheet to store several different sprites - perhaps three or four - in a single image file. I would rather this approach than loading those same three or four sprites individually even though this latter approach would be the easiest for me to achieve in my limited experience of XNA. My basic plan is as follows:

    1 - Load the spritesheet in my main class
    2 - Extract from the spritesheet a selected sprite and store it in another Texture2D object for passing to my sprite set-up class.

    I'd be grateful if someone could post a short piece of code to help me out because the examples I've seen so far are a little complicated and I don't understand them and they appear to use different approaches which has added to my confusion.

    Thanks for reading my post.
    Man Is The Warmest Place To Hide
  • 10/5/2007 5:38 PM In reply to

    Re: Spritesheets - How are they implemented?

    Extracting sprites from sprite sheets is the wrong way to go. The way that DirectX batches up drawing commands means that you need to minimize these. If you draw 200 sprites each with a differentTexture2D you will cause 200 draw commands to Directx and performance will suffer.

    If you draw 200 sprites from 5 sprite sheets then SpriteBatch will batch this into at most 5 draw calls and your graphics care will barley notice it. If you can put all your sprites in one texture even better.

    So you need to pass the original Texture2D AND a Rectangle to your sprite setup - then when you draw it just use an overload that allows you to choose which part of a texture to Draw. The rectangle describes where in the texture each sprite goes.

    Make sure you don't do a SpriteBatch.Begin/End for each sprite - that is also just as bad.

     

    Playtest Kissy Poo - a game for 4 year olds on Xbox and windows
    The ZBuffer
    News and information for XNA
      Follow The Zman on twitter, Email me
        Please read the forum FAQs - Bug/Feature reporting
          Don't forget to mark good answers and good playtest feedback when you see it!!!
  • 10/5/2007 5:49 PM In reply to

    Re: Spritesheets - How are they implemented?

    Thank you ZMan, I am working on it now :)
    Man Is The Warmest Place To Hide
  • 10/6/2007 12:51 AM In reply to

    Re: Spritesheets - How are they implemented?

    Here's an example... in VB :P

    This is a snippet from my sprite class.  It holds a pointer to it's sheet (Me.Sheet) as well as the rectangle that defines where on the sheet the texture comes from (Me.objSheetRectangle)

    XNALib is just a static class of tools in my engine where SpriteBatch lives, among other things.

    With Me

    'If I have a rectangle definition for my sheet

    If .objSheetRectangle <> Rectangle.Empty Then

    'Begin the sprite batch (implements my transform matrix)

    XNALib.SpriteDrawer.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None, .DrawMatrix)

    'Draw from the sheet to the target (matrix does all the positioning work)

    XNALib.SpriteDrawer.Draw(.Sheet, Vector2.Zero, _
    .objSheetRectangle, New Color(.Tint.R, .Tint.G, .Tint.B, .bytParentAdjustedAlpha))

    'End the sprite batch

    XNALib.SpriteDrawer.End()

    End If

    End With


  • 10/7/2007 12:12 PM In reply to

    Re: Spritesheets - How are they implemented?

    Go to this page. Read the tutorial (if you want to be more flexible with your animations) but really look down to where a replyer called "Jay" says something about his animation Framework. http://www.xna3way.com/blog/?p=19

    I used the base of his entry with the tutorial and added some other things to make a simple a very easy way to make sprite sheets work with more effects.
    Independent Game Developer - Blog
  • 10/8/2007 6:06 PM In reply to

    Re: Spritesheets - How are they implemented?

    Thanks for the replies emachine and The Zedox. I got it working the way The ZMan suggested shortly after he posted his reply.

    Can anyone tell me if I am correct in thinking that the width and height of a sprite should ideally be some multiple of 2 - so instead of a sprite being 3 X 5 it should be 4 X 6, or 2 x 4, etc, etc. I'm not 100% sure but I think I saw mention of this on these forums a while back. I don't know why that'd be an issue, but I'm curious.

    Thanks.  
    Man Is The Warmest Place To Hide
  • 10/8/2007 11:14 PM In reply to

    Re: Spritesheets - How are they implemented?

    Yea they should be.  I can't explain the technical side of it (it has something to do with the black magic that goes on inside the video card) but the image file that you load (the sheet) should have a width and height that are both a power of 2. 

    On my nVidia based computer at work, if I use a 1024x768 sheet (768 is an even number, but it's not a power of 2) the image quality is very bad (but it works). 

    On ATI based laptop running the same code, I actually get an error because the card simply does not support loading non-power of 2 images.  So in my experience not using power of 2 can do anything from lowering quality to breaking the program.

    Once a power of 2 sheet is loaded into the card, I believe the rectangles you use to make sprites from the sheet can be any size you want.  From my 1024x1024 sheet I can use any arbitrary size... 91x47 or 15x4 or whatever, doesn't seem to matter once the sheet is loaded.
  • 10/9/2007 2:15 PM In reply to

    Re: Spritesheets - How are they implemented?

    Thanks emachine,

    There is a not so subtle difference between what I said about multiples of 2 and your reply of powers of 2

    Me (The Thing - multiples) = 2, 4, 6, 8, 10, 12, 14, etc, etc.

    You (emachine - powers) =  2, 4, 8, 16, 32, 64, 128, 256, etc, etc.  //This is the correct way.

    I was curious as to the file size of a 512 x 512 texture so I just created a 24 bit *.png in Paint Shop Pro, saved it to the desktop and I see that it's only 953 bytes in size, but occupies 4,096 bytes on disk.

    Anyway, just to recap for any other newbies who might be reading this - create your textures / sprite sheets in powers of two,
    and after they're loaded into your game you can extract a sprite of any size by specifying a rectangle which will define the sprite's width and height.    

          
     


    Man Is The Warmest Place To Hide
Page 1 of 1 (8 items) Previous Next