Question About Sprite Rendering

Oct 6, 2014 at 3:58 PM
Hi guys Can any one explain me how to render my sprites with transparency ? I'm new to DIrectXTK and I was using DIrectDraw where you simply set the color key, But how do I achieve this with sprite batch ? maybe you have any links to tutorials or forums that gonna help me get my head around it. thanks
Coordinator
Oct 6, 2014 at 7:12 PM
Edited Oct 6, 2014 at 7:16 PM
Color-keying is an old-school technology, so the modern API doesn't support it with specific APIs.

You can convert color-keyed images to images with an alpha channel, and then you use the standard alpha-blending techniques to do the blending/transparency at runtime. I have a work-item to include functionality like this in DirectXTex, but it's also a fairly easy operation to do yourself.

Ideally you do this conversion at build time and not at load time (i.e. only ship textures with alpha and not with color-keying).

DirectX Tool Kit uses premultiplied-alpha by default, but you can use 'straight-alpha' as well. See SpriteBatch for some details on this.

You can also do color-keying with a custom pixel shader, but there's not a lot of value in doing this way unless you have to deal with color-keyed images coming from a user or other source you can't convert ahead of time.
Oct 6, 2014 at 8:33 PM
It all sounds like magic for me as I never used alpha blending before, I don't even know where to start. But Let's say I wanna use premultiplied alpha Can you tell me where to start ? What I need to know first to use alpha blending with DIrectXTK ?
Coordinator
Oct 6, 2014 at 10:02 PM
Coordinator
Oct 6, 2014 at 11:29 PM
Note that for simple sprite drawing usages you don't typically need to think so hard about these nuances. Create your artwork in a format that includes an alpha (transparency) channel such as .png, and it'll pretty much Just Work (tm) using DirectXTK.
Oct 7, 2014 at 4:11 PM
ShawnHargreaves wrote:
Note that for simple sprite drawing usages you don't typically need to think so hard about these nuances. Create your artwork in a format that includes an alpha (transparency) channel such as .png, and it'll pretty much Just Work (tm) using DirectXTK.
What about the .dds files ?? And what you mean by "pretty much just work" ? sorry for silly questions but as I said before I have never use alpha before I know theory and what it does but I never implement it before. I got my book today "Beginning DirectX11 programming" so that gonna help a bit. for now from that book I know I need pointer to ID3D11BlendState, blendDescr struct , blendFactor array, than I need to create blend state and bind it but how do I use that with DirectXTK ? Looks like it gonna be a long week
Coordinator
Oct 7, 2014 at 5:29 PM
Edited Oct 7, 2014 at 5:33 PM
You create your artwork with .png and can initially load them with WICTextureLoader functions. You can later optimize the memory and loadtime performance by converting the .png to a .dds using the Visual Studio content pipeline or using texconv from DirectXTex, and switch to DDSTextureLoader. As Shawn notes, you don't have to get all complicated at first.

The CommonStates helper does exactly what you are asking: quickly and easily creates D3D11BlendState objects.

Take a look at the documentation on the CodePlex wiki here as we do cover at least the basics of usage.

Check out the DirectXTKSimpleSample for your platform as well to get some 'live' usage.

Remember that the DirectX Tool Kit is just a loose collection of 'helpers' to make it easier to code against DirectX 11. You can start with a simple program that uses lots of DirectX Tool Kit functions to get going, step into each with a debugger to learn what it is doing, and then either replace it with your own or customize it to your needs piecemeal. You can get a very long way with for a 2D game with just CommonStates, SpriteBatch, and SpriteFont along with some simple use of GamePad and the Audio library.
Coordinator
Oct 7, 2014 at 7:15 PM
Note that SpriteBatch sets standard sprite drawing states by default, so you don't need to touch CommonStates or ID3D11BlendState at all unless you want something more specialized.

Just make your image files including an alpha channel, load them with WICTextureLoader or DDSTextureLoader, then draw them using SpriteBatch.
Marked as answer by walbourn on 10/7/2014 at 3:57 PM
Oct 7, 2014 at 9:40 PM
ShawnHargreaves wrote:
Note that SpriteBatch sets standard sprite drawing states by default, so you don't need to touch CommonStates or ID3D11BlendState at all unless you want something more specialized.

Just make your image files including an alpha channel, load them with WICTextureLoader or DDSTextureLoader, then draw them using SpriteBatch.
Damn!! it's so simple. you guys wrote good peace of code. I have the transparency by adding alpha channel to my resources as you said, but I'll be back!! With more questions
Oct 25, 2014 at 6:57 PM
Hi all can any one tell me if there is any difference in rendering all my game sprites between one Begin() - End(), than using Begin() - End() for each sprite ?
Coordinator
Oct 26, 2014 at 1:07 AM
Edited Oct 26, 2014 at 9:55 PM
It depends on your sorting mode and the number of sprites you are drawing but generally you want to draw more than one per Begin/End pair so the SpriteBatch class can queue them up and submit them in a more efficient way. If using SpriteSortMode_BackToFront or SpriteSortMode_FrontToBack, it can only sort the sprites submitted in that 'batch'.

You should read ShawnHar's blog post on SpriteBatch sorting. It was originally written for XNA Game Studio, but it's essentially the same thing:

SpriteBatch and SpriteSortMode
SpriteBatch sorting part 2
Return of the SpriteBatch: sorting part 3
SpriteSortMode.Immediate in XNA Game Studio 4.0
Marked as answer by walbourn on 10/25/2014 at 5:11 PM
Oct 26, 2014 at 6:28 PM
thanks !!