I am using SharpDX(Direct3D9 to be exact) to render textures onto the screen. It's just a simple 2D game, so there's not a lot to it. I will show a few segments of my code and I will also list the things that I have tried and that have failed.
Public Sub InitD3D(ByVal form As Form, ByVal windowed As Boolean)
direct3D = New Direct3D()
Dim deviceSettings As PresentParameters
deviceSettings.BackBufferCount = 1
deviceSettings.BackBufferWidth = form.ClientSize.Width
deviceSettings.BackBufferHeight = form.ClientSize.Height
deviceSettings.DeviceWindowHandle = form.Handle
deviceSettings.Windowed = windowed
deviceSettings.PresentationInterval = PresentInterval.Immediate
deviceSettings.SwapEffect = SwapEffect.Discard
device = New Device(direct3D, 0, DeviceType.Hardware, form.Handle, CreateFlags.HardwareVertexProcessing, deviceSettings)
device.SetTransform(TransformState.Projection, Matrix.OrthoOffCenterLH(0.0!, Convert.ToSingle(form.ClientSize.Width), 0.0!, Convert.ToSingle(form.ClientSize.Height), 0.0!, 1.0!))
device.SetTransform(TransformState.View, Matrix.Identity)
device.SetTransform(TransformState.World, Matrix.Identity)
device.SetTransform(TransformState.Texture1, Matrix.Identity)
device.Viewport = New Viewport(0, 0, form.ClientSize.Width, form.ClientSize.Height)
device.SetRenderState(RenderState.Lighting, False)
additionalSwapChains = New Dictionary(Of String, SwapChain)()
End Sub
Public Sub LoadTexture(ByVal fileName As String, ByRef texture As Texture)
Try
texture = texture.FromFile(device, fileName, Usage.None, Pool.Managed)
Catch ex As Exception
Throw New Exception("Failed to load texture from " + fileName)
End Try
End Sub
Public Sub Draw(ByVal texture As Texture, ByVal dx As Integer, ByVal dy As Integer, ByVal dw As Integer, ByVal dh As Integer, Optional ByVal sx As Integer = 0, Optional ByVal sy As Integer = 0)
device.VertexFormat = VertexFormat.Position Or VertexFormat.Texture1
device.SetTexture(0, texture)
Dim left As Single = Convert.ToSingle(dx) - 0.5!
Dim right As Single = Convert.ToSingle(dx + dw) - 0.5!
Dim bottom As Single = Convert.ToSingle(dy) - 0.5!
Dim top As Single = Convert.ToSingle(dy + dh) - 0.5!
Dim vertices(4) As CustomTextureVertex
vertices(0).position = New Vector3(left, top, 0.0!)
vertices(0).texPosition = New Vector2(0.0!, 0.0!)
vertices(1).position = New Vector3(right, top, 0.0!)
vertices(1).texPosition = New Vector2(1.0!, 0.0!)
vertices(2).position = New Vector3(left, bottom, 0.0!)
vertices(2).texPosition = New Vector2(0.0!, 1.0!)
vertices(3).position = New Vector3(right, bottom, 0.0!)
vertices(3).texPosition = New Vector2(1.0!, 1.0!)
device.DrawUserPrimitives(PrimitiveType.TriangleStrip, 2, vertices)
End Sub
Things I have tried:
Adjusting offset by 0.5f, didn't work
The dw & dh is the actual height, not the Powers of Two width/height
The position & texPosition are perfectly fine
Setting minFilter,magFilter,mipFilter,addressU,addressV to different values and nothing(I also placed it in InitD3D and Draw functions in case it mattered)
The last thing I tried could also be my problem because I did not go through and try every possibility, I have just used settings from other help topics.
Thanks for any help, it's greatly appreciated.