# MDX Sprite.Draw with SrcRect error

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

## Recommended Posts

Hi, I'm currently trying to convert a 2D game from DirectDraw to Direct3D (that i had just converted from GDI to directdraw :(). I'm using VB.Net and the newest version of the DX9 SDK. I'm trying to make Sprite.Draw work, but it crashes sometimes when i send a sourceRectangle with X or Y different from 0. The first thing that i verified is that X+Width of sourceRectangle doesn't exceed the image width. I verified the same thing with the height. The first DrawSprite() that crashed had SrcRect(X = 288, Y=96, Width = 48, Height = 48) Texture(Width = 512, Height = 512) heres the code that i am using to draw.
    Public Sub DrawSprite(ByVal pDestRect As Rectangle, ByRef pTex As Texture, ByVal pSrcRect As Rectangle)
m_Sprite.Begin()

'If i remove this, i'll Crash !
If pSrcRect.X > 0 Then pSrcRect.X = 0
If pSrcRect.Y > 0 Then pSrcRect.Y = 0

m_Sprite.Draw(pTex, pSrcRect, New Microsoft.DirectX.Vector2(1.0F, 1.0F), New Microsoft.DirectX.Vector2(0, 0), 0.0F, New Microsoft.DirectX.Vector2(pDestRect.X, pDestRect.Y), Color.White)

m_Sprite.End()
End Sub


What's funny about this is that with the patch i applied to set the X and Y to 0, it doesn't crash ! The error that i get from DX is D3D_INVALIDCALL on m_Sprite.Draw I am clueless about what might cause the crash and after 2 days of searching google, and finding out that most of the good google finds came from this forum, i decided to ask my question here !

##### Share on other sites
Wow !

i have finally resolved the mystery !

When using a VB.Net Rectangle as a source, i must set the rectangle's width to it's Right, and it's Height to it's Bottom !

Big thanks to MJP for his C++ sprite sample posted in another post which gave me the idea to try this solution !

Here's the working code, if it can help someone in the future !

    Public Sub DrawSprite(ByVal pDestRect As Rectangle, ByRef pTex As Texture, ByVal pSrcRect As Rectangle)        m_Sprite.Begin()        'Build an adjusted rectangle where the width is set to the right        'and the height set to bottom ! this is because DX expects a rectangle        'width a right and a bottom, NOT a width and height !        Dim adjustedSrcRect As New Rectangle(pSrcRect.X, _                                             pSrcRect.Y, _                                             pSrcRect.Right, _                                             pSrcRect.Bottom)             m_Sprite.Draw(pTex, adjustedSrcRect, New Microsoft.DirectX.Vector2(1.0F, 1.0F), New Microsoft.DirectX.Vector2(0, 0), 0.0F, New Microsoft.DirectX.Vector2(pDestRect.X, pDestRect.Y), Color.White)             m_Sprite.End()    End Sub

##### Share on other sites
Just so you know, but you should only being calling Begin/End once per frame in order to batch your sprites, all the actual drawing is done in End. Calling them all the time will create a very slow program, and it defeats the purpose of using the sprite class.

##### Share on other sites
thanks for the tip !

it's probably going to improve performance, as i m drawing about 500 sprites per frame !

• 15
• 9
• 13
• 41
• 15