# Begin and End calls with ID3DXSprite

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to use ID3DXSprite to do 2d rendering for both my main scene as well as various screen overlays such as user interfaces etc. At the moment every 2d element in my scene has a weight attached to it between 0.0 and 1.0 so that I can sort them accordingly. I would like to keep this functionality the way it is, but I need some way to ensure that all screen overlays are rendered AFTER the rest of the scene has completely rendered. Seeing as D3DXSprite renders things once I call End(), in my case based on the weight of the elements, one of the only solutions I can think of is to reserve a certain portion of the weights (something like 0.9 -> 1.0) for screen overlays and such, and restrict entities to the other 90% of the weights. The other option I was thinking of was separating the drawing of screen overlays into their own series of Begin() and End() calls to the sprite device. So I would be calling Begin() and End() two times per frame. I've heard that using these functions excessively can really drop performance, so I'm wondering if doing it twice is something I should be steering away from or if maybe someone's got a better idea.

Edit: It seems I got the answer to my question over in the General Programming forum. For anyone interested it appears to be fine as long as it's in moderation.

[Edited by - ChugginWindex on September 30, 2010 2:04:43 PM]

##### Share on other sites
You do not need more than one Begin/End pair.

Try separating the usage of 2D objects into different categories. Using 2D objects in one container is an attractive choice when using runtime polymorphism, but it then becomes possible for an overlay to not REALLY be an overlay, since anything can go above it.

If you want overlays to be rendered after everything else, then render them after everything else. Treat backgrounds as backgrounds and overlays as overlays.

background.draw();// Draw (sorted) stuffoverlay.draw();

[Edited by - zyrolasting on September 30, 2010 2:05:10 PM]

##### Share on other sites
Right, but the problem I'm having is that with D3DXSprite, I set it up in such a way that it automatically sorts my textures by depth and then by texture itself. Because I figure DirectX is going to have a faster sorting method than I do myself, and I don't want to commit myself to finding out otherwise, I just want stuff to draw at the moment in the correct order. But because of this automatic sorting, it doesn't matter what order _ I _ tell the device to draw my objects, it's going to make up its own mind what order they are drawn in according to the sorting algorithm once I call End().

Unless I'm missing something entirely about how the D3DXSprite interface operates, which is absolutely possible.

EDIT: Looking at your suggestion again. To use what you've mentioned as well as circumvent the problem I am having, wouldn't it be sufficient for those separate sections (e.g. Backgrounds, Overlays) to implement their draws as exclusive pairs of Begin()/End() calls? So all Backgrounds are drawn within one Begin()/End() call, etc. To ensure that there's some control over their order I mean.

##### Share on other sites
Ah, ok. I see what you are getting at now. I saw something completely different at first.

In those circumstances, then yes, you would need to tell ID3DXSprite to Begin() again using different device states if you were not sure how to make it sort to your liking. If you wanted to keep that to a minimum, you would have to make do by configuring the D3D device yourself and using your own sorting methods.

However, wouldn't the D3DXSPRITE_SORT_DEPTH_BACKTOFRONT flag use the Z component of the position vector passed to Draw()?

##### Share on other sites
Yup, and that's how I'm doing it at the moment. It's just that I don't have a complete understanding of how I'm going to structure whatever delegates weights appropriately (no editor written, nothing so far). So I want to have some way to ensure that if an entity is given a maximum weight of 1.0f, that any overlays STILL get drawn after that entity and thus on top of it, which is why I want to keep the two types completely unassociated during drawing.

##### Share on other sites

This topic is 2632 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628707
• Total Posts
2984310

• 23
• 10
• 9
• 13
• 13