Direct3D.Sprite Questions.

Started by
7 comments, last by Sei 20 years ago
Hello, I’m creating a 2D game, using MDX, but this should apply to an ID3DXSprite. I load an image (a PNG right now), that is 256x256 in size, containing 16 cells each 64x64 in size. I can get the sprite on the screen, using Direct3D.Sprite, and am able to animate it too. But, I would like to be able to mirror the cell being drawn. I tried using a negative scale, but that has the same effect as scaling to 0. Is there any way to flip a sprite? Second question is, about colour keying in textures and/or sprites. I know you can set the color key when loading the texture. I was wondering if there was a way to add my own, at runtime, not load time. For example, set color x, to color y. Thanks a lot.
Advertisement
I believe that if you specify the source rect as eg. (32,32,0,0) it will flip it. In this case both horizontally and vertically, but you can do one of those as well.

Color keying is supported, if you load your texture with D3DXCreateTextureFromFileEx, it takes more parameters than D3DXCreateTextureFromFile. In Managed DirectX it would be TextureLoader.FromFile with a different set of parameters (it''s overloaded).

But color keying is just updating the alpha part of a texture (a 32 bit texture has RGBA components), it sets the alpha of the pixel where the colorkey matches and resets it on all the others. Actually drawing the sprite doesn''t use the colorkey, it uses the alpha channel. If you want to manipulate this, you can probably update the alpha channel of the texture.

Would sprite rotation be acceptable? (ie; rotate the sprite 180 degrees). I made a generic sprite rotation method which can be found here;
http://www.danpeverill.com/samples/2D_Sprite_Rotation_In_D3D.asp

Keep in mind that if you rotate one sprite, you must rotate all sprites that use the same Sprite class. That doesn't mean you have to actually rotate them though, you can specify 0 degrees as the rotation (no rotation at all).

[edited by - wyrd on March 24, 2004 12:17:35 PM]
Well, I want to mirror the sprite because, it is of a character running left or right. A rotation would make him running doing a handstand.

Prior to the mdx summer update, the source Rectangle was built like
new Rectangle(x,y,x+width,y+height) 

This was an odd way of doing things...
In the MDX summer update, they fixed that, so it''s now
new Rectangle(x,y,width,height) 

Which is how a rectangle is supposed to be, except now, you can''t just switch the x, y with the height, width now.

I assumed there would be an easy way to mirror a sprite somehow, as it seems like an important concept in sprite animation. Worst case is load two textures, one for running right, and one for left, but that seems wasteful.
But you can specify -width to mirror it, right ?
Unfortunately no.

In my cells, each are 64x64.

So
at cell one, (x,y) = (0,0) with dimensions of 64x64
at cell two, (x,y) = (64,64) with dimensions of 64x64

if we make width negative,
cell one would draw the 0,0 to -64,64, not showing anything
cell two would draw the (64,0) to (0,64), showing what should be cell one.

So whenever x = 0, it can''t draw a cell.

This is how it is working for me.

Maybe my parameters are wrong...I''ve been specifying SpriteFlags.None. Should I try something else?

I don't really know anything about MDX or ID3DXSprite, but I just read the following here, which seemed related:

quote:
Reflection is identical to scaling with a negative number. Another way to reflect sprites is to reverse the texture coordinates (tu, tv). To reflect about the

1. x-axis, make tu = (1 - tu)
2. y-axis, make tv = (1 - tv)

For example, reflecting about the x-axis looks like this
Sprite (x, y) 	Normal (tu, tv) Reflect (1 - tu, tv)(left, top) 	     (0, 0) 	     (1, 0)(right, top) 	     (1, 0) 	     (0, 0)(right, bottom)      (1, 1) 	     (0, 1)(left, bottom) 	     (0, 1) 	     (1, 1)   


EDIT 2: article also says this:
quote: 3. Turn off back face culling so that we can see the back of primitives. This lets us reflect our sprite (to change the direction that it is facing). With culling on (the default), a reflected sprite is not drawn.


EDIT 1: forgot link
Tadd
- WarbleWare

[edited by - reana1 on March 24, 2004 9:50:37 PM]
Tadd- WarbleWare
Yes, you can rotate and flip the sprite object. This is the code I''m using in my tile based map program to do it.

I converted it from the DirectX8 tutorial at ( www.gamasutra.com/features/20010629/geczy_01.htm )

The Transform and Draw at the bottom are Sprite.Transform and Sprite.Draw since I inherited the Sprite class.

This routine enables scaling, rotating and flipping of the sprite.

    Public Sub BltSpriteEx(ByVal pDestRect As Rectangle, ByVal pSrcTexture As Texture, ByVal pSrcRect As Rectangle, ByVal dwFlags As mmdMirrorEnum, _        ByVal modulate As Integer, ByVal rotation As mmdRotateType)        Dim scaling As New Vector2(0, 0)        Dim rcenter As New Vector2(0, 0)        Dim trans As New Vector2(0, 0)        If Not IsNothing(pDestRect) Then            ''// destination translation            trans.X = pDestRect.Left            trans.Y = pDestRect.Top        End If        ''// rotate from the center of the scaled texture        rcenter.X = (pDestRect.Right - pDestRect.Left) / 2        rcenter.Y = (pDestRect.Bottom - pDestRect.Top) / 2        If Not IsNothing(pDestRect) And Not IsNothing(pSrcRect) Then            ''// set the scale            scaling.X = (pDestRect.Right - pDestRect.Left) / (pSrcRect.Right - pSrcRect.Left)            scaling.Y = (pDestRect.Bottom - pDestRect.Top) / (pSrcRect.Bottom - pSrcRect.Top)        End If        If Not IsNothing(pSrcRect) And dwFlags <> mmdMirrorEnum.mmdMirrorNone Then            If (dwFlags And mmdMirrorEnum.mmdMirrorLeft) = mmdMirrorEnum.mmdMirrorLeft Then                ''// flipping x                rcenter.X = -rcenter.X                scaling.X = -scaling.X                trans.X += pDestRect.Right - pDestRect.Left            End If            If (dwFlags And mmdMirrorEnum.mmdMirrorDown) = mmdMirrorEnum.mmdMirrorDown Then                ''// flipping y                rcenter.Y = -rcenter.Y                scaling.Y = -scaling.Y                trans.Y += pDestRect.Bottom - pDestRect.Top            End If        End If        ''// set the transform matrix        Transform = Matrix.Transformation2D(Nothing, Nothing, scaling, rcenter, rotation.Radians, trans)        ''// draw the sprite        Draw(pSrcTexture, pSrcRect, Nothing, Nothing, modulate)    End Sub
Thanks Kracs.

From your example, I was able to understand what was needed to flip.

Thanks a lot!

This topic is closed to new replies.

Advertisement