Sign in to follow this  
dagophil

[SOLVED] DirectX Fonts Error

Recommended Posts

dagophil    100
Hi! I am programming in VB.NET and I am learning DirectX programming. At the moment I want to create a simple 3D-environment with some meshes. I tried to add some text to my scene. Everything works fine, the text I added is also shown, but when I quit the application, I get the following error:
An unhandled exception of type 'System.NullReferenceException' occurred in Microsoft.DirectX.Direct3DX.dll

Additional information: Object reference not set to an instance of an object.
I found out that I get this error when I create a new DirectX Font:
myFontX = New Microsoft.DirectX.Direct3D.Font(myDevice, myFont)
When I comment out this line (and the myFontX.DrawText() line), the text isn't shown anymore and there is no error anymore when I quit the application. Using breakpoints, I found out that the error comes after the FormClosed event. Here is the code: Form1.vb:
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

Public Class Form1

    Private myDevice As Device
    Private myBox As Mesh
    Private myMaterial As Material

    Private myFont As System.Drawing.Font
    Private myFontX As Microsoft.DirectX.Direct3D.Font

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If InitD3D(myDevice, Me.Handle) Then

            myBox = Mesh.Box(myDevice, 100, 100, 100)

            Timer1.Enabled = True

            myMaterial.Ambient = Color.White
            myMaterial.Diffuse = Color.White

            myDevice.RenderState.Ambient = System.Drawing.Color.DarkGray

            myFont = New System.Drawing.Font("Arial", 100, FontStyle.Regular, GraphicsUnit.Pixel, CharacterSet.Ansi)
            myFontX = New Microsoft.DirectX.Direct3D.Font(myDevice, myFont)

        Else

            MsgBox("Could not initialize Direct3D")

        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        myDevice.BeginScene()

        myDevice.Clear(ClearFlags.Target Or ClearFlags.ZBuffer, System.Drawing.Color.Black, 1, 0)

        myDevice.Transform.View = Matrix.LookAtLH(New Vector3(0, 100, -300), New Vector3(0, 0, 0), New Vector3(0, 1, 0))
        myDevice.Transform.Projection = Matrix.PerspectiveFovLH(Math.PI / 4, Me.Width / Me.Height, 1, 10000)

        myDevice.Material = myMaterial

        myBox.DrawSubset(0)

        myFontX.DrawText(Nothing, "Hi", 0, 0, Color.White)

        myDevice.EndScene()

        myDevice.Present()

    End Sub

End Class


moduleInitD3D.vb:
Imports Microsoft.DirectX
Imports Microsoft.DirectX.Direct3D

Module moduleInitD3D
    Public Function InitD3D(ByRef myDevice As Device, ByVal lHandle As System.IntPtr) As Boolean

        ' Set up the variables we need
        Dim DispMode As DisplayMode
        Dim Params As New PresentParameters
        Dim worked As Boolean

        Try

            ' Pass the display mode to the variable
            DispMode = Manager.Adapters.Default.CurrentDisplayMode

            ' Set the window parameters
            With Params
                .Windowed = True
                .SwapEffect = SwapEffect.Discard
                .BackBufferCount = 1
                .BackBufferFormat = DispMode.Format
                .BackBufferHeight = DispMode.Height
                .BackBufferWidth = DispMode.Width
                .PresentationInterval = PresentInterval.Immediate
                .AutoDepthStencilFormat = DepthFormat.D16
                .EnableAutoDepthStencil = True
            End With

            ' Set the device variable
            myDevice = New Device(0, DeviceType.Hardware, lHandle, _
                                  CreateFlags.HardwareVertexProcessing, Params)

            ' If there was an error and myDevice could not be set, set
            ' worked to False so the application will throw an error
            worked = Not myDevice Is Nothing

            ' Set up the light
            With myDevice.Lights(0)
                .Type = LightType.Point
                .Diffuse = System.Drawing.Color.FromArgb(128, 128, 128, 128)
                .Ambient = System.Drawing.Color.FromArgb(13, 13, 13, 13)
                .Specular = System.Drawing.Color.FromArgb(100, 100, 100, 100)
                .Position = Nothing
                .Position = New Vector3(500, 500, 1000)
                .Direction = Nothing
                .Direction = New Vector3(0, 0, 1)
                .Range = 10000
                .Falloff = 1
                .Attenuation0 = 1
                .Attenuation1 = 0
                .Attenuation2 = 0
                .Enabled = True
                .Update()
            End With

            ' Set the other render parameters
            With myDevice.RenderState
                .Lighting = True
                .FillMode = FillMode.Solid
                .ZBufferEnable = True
                .AntiAliasedLineEnable = True
                .MultiSampleAntiAlias = True
                .DitherEnable = True
                .NormalizeNormals = True
                .SpecularEnable = True
                .ShadeMode = ShadeMode.Gouraud
            End With

        Catch

            ' If there was an error, set worked to False
            worked = False

        Finally

            ' Release the temporary variables after the creation
            Params = Nothing
            DispMode = Nothing

        End Try

        Return worked
    End Function
End Module


I hope you can help me :-) Greetings, dagophil [Edited by - dagophil on February 8, 2010 3:19:41 AM]

Share this post


Link to post
Share on other sites
Evil Steve    2017
(I don't know VB at all...)

What does your form closed event look like? That error indicates that your font object is null (or invalid in some other way) when it's used - probably at your DrawText call.

Are you releasing your font in your form closed handler? And if so, is the Timer1_Tick() function being called after the form is closed (Which would mean that it's trying to use an invalid object)?

Share this post


Link to post
Share on other sites
dagophil    100
Ahh, that's it!

I didn't release the font when the form is closing.
Now I added
myFontX.Dispose()
to the FormClosed event and everything works fine :-)

Thank you very much.


Greetings,

dagophil

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this