Transparent Sprites

Loffen

Regular
Joined
Mar 21, 2004
Messages
51
Location
Akershus, Norway
I am trying to create a 'player' for my tilegame. The sprite function to load the image and create the vertexbuffer looks like this:

Code:
    Public Sub Load(ByVal ImageName As String, ByVal TopLeft As Point, ByVal Width As Integer, ByVal Height As Integer, ByVal ColorKey As Integer)
        Dim Vertices() As CustomVertex.PositionTextured

         Try
            SpriteImage = TextureLoader.FromFile(pDevice, ImageName, Width, Height, D3DX.Default, 0, Format.Unknown, Pool.Default, Filter.Point, Filter.Point, ColorKey)
        Catch ex As Exception
            MessageBox.Show("Could not load image: " & ImageName & vbCrLf & ex.StackTrace, "Critical Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End
        End Try

        SpriteBuffer = New VertexBuffer(GetType(CustomVertex.PositionTextured), 4, pDevice, Usage.WriteOnly, CustomVertex.PositionTextured.Format, Pool.Default)
        Vertices = SpriteBuffer.Lock(0, 0)

        Vertices(0) = New CustomVertex.PositionTextured(TopLeft.X, TopLeft.Y, 1, 0, 0)
        Vertices(1) = New CustomVertex.PositionTextured(TopLeft.X + Width, TopLeft.Y, 1, 1, 0)
        Vertices(2) = New CustomVertex.PositionTextured(TopLeft.X, TopLeft.Y + Height, 1, 0, 1)
        Vertices(3) = New CustomVertex.PositionTextured(TopLeft.X + Width, TopLeft.Y + Height, 1, 1, 1)

        SpriteBuffer.Unlock()
    End Sub

And the function that draws the sprite like this:

Code:
    Public Sub Render()
        pDevice.RenderState.AlphaBlendEnable = True
        pDevice.VertexFormat = CustomVertex.PositionTextured.Format
        pDevice.SetStreamSource(0, SpriteBuffer, 0)
        pDevice.SetTexture(0, SpriteImage)
        pDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2)
        pDevice.RenderState.AlphaBlendEnable = False
    End Sub

And during Device Init:

Code:
        Device.RenderState.Lighting = False
        Device.RenderState.ZBufferEnable = False
        Device.RenderState.CullMode = Cull.None

        Device.RenderState.AlphaSourceBlend = Blend.SourceAlpha
        Device.RenderState.AlphaDestinationBlend = Blend.InvSourceAlpha
 
Thsi si what I do:

Dim ColorKeyVal As Color = Color.FromArgb(255, 0, 255, 0) 'LimeGreen

SpriteImage = TextureLoader.FromFile(dxDevice, ImageName, _32, 32, D3DX.Default, 0, Format.Unknown, Pool.Default, Filter.Point, Filter.Point, ColorKeyVal.ToArgb)

I think you'd have to use the ARGB values.

-The Pentium Guy

 
Well, figured out, now posting the answear :p..

When you init the device do not use:

Code:
        dxDevice.RenderState.[COLOR=Red]Alpha[/COLOR]SourceBlend = Blend.SourceAlpha
        dxDevice.RenderState.[COLOR=Red]Alpha[/COLOR]DestinationBlend = Blend.InvSourceAlpha

But:

Code:
        dxDevice.RenderState.SourceBlend = Blend.SourceAlpha
        dxDevice.RenderState.DestinationBlend = Blend.InvSourceAlpha
 
Back
Top