I was recently experimenting with the rendering of 2D sprites using David Weller's 2D Sprite sample, from his blog entry a while ago:
http://www.inkblog.com/CommentView,guid,93ecebcb-7ca6-43ab-97ae-6c09bfeb8c95.aspx
Source Code: http://www.inkblog.com/ct.ashx id=93ecebcb-7ca6-43ab-97ae-6c09bfeb8c95&url=http%3a%2f%2fwww.inkblog.com%2fcontent%2fbinary%2fcsd3dxsprite.zip
Within this code, there's a call to TextureLoader.FromFile which loads the Sprite texture:
donutTexture = TextureLoader.FromFile(device, @"..\..\donuts.bmp", 320, 384, 1, 0,Format.A8R8G8B8, Pool.Managed, Filter.Point, Filter.Point, (unchecked((int)0xff000000))); |
This works, but I didn't want to have to specify the size of every sprite bitmap I load (I'd prefer it just use the image size), but if I replace this code with the more generic overload:
donutTexture = TextureLoader.FromFile(device, @"..\..\donut.bmp"); |
The Sprite is drawn with bounds that seem to be completely off. Does anyone (psst... David Weller) know what is going on behind the scenes that makes these calls have different results Thanks,
Mike Vargas

Sprite behavior changes when TextureLoader.FromFile is called with different parameters
MicSup
Well, i'm not David Weller, but hopefully I'll have a good enough answer for you anyway.
Anyway, the 'issue' here is the size of the original texture. Particularly the fact the size isn't a multiple of a power of two. When using the 'simple' overload, the method will load the texture, but if it notices the size of the texture isn't a power of two, it will 'scale' the texture larger until it *is* a power of two. In this case, the 320x384 texture (which has neither width nor height a power of two) is 'up scaled' to a size of 512x512 (the next largest power of two). With the new size, the rectangles he uses for each of the sprites is (for obvious reasons) no longer accurate.
So, the short answer is: "If you want to use that more 'generic' overload, make sure your textures have sizes that are powers of two."
I also have a blog entry that discusses this somewhat from a while back:
http://blogs.msdn.com/tmiller/archive/2004/07/19/187096.aspx
Ignacio Danta
Thank you very much for this. I can't help but echo the sentiments of the first commenter in your blog entry; the MSDN documents don't explain any of this at all:
(from the blog entry)
It's hard to know what each function does Tom, when the documentation isn't very substantial and neither is the comments. The comment for the said method is "Creates a texture from a file", there is no mention of it performing power of 2 scaling or about it creating a mipmap chain. Even in the MSDN help it doesn't explicitly state that the method creates a mipmap chain or does any power of 2 conversion, it hints at it by making suggests about speeding up loading times. I'm guessing that users of C++/DirectX are familiar with such quirks and the C++ documentation already explains this, but MDX needs to catch up as people don't want to rely on C++ docs when writing code for C#. Not to go on, but your book also doesn't mention these quirks Tom.