Results 1 to 36 of 36

Thread: vb2010: why i get the 'NullReferenceException'?

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    vb2010: why i get the 'NullReferenceException'?

    see these code:
    Code:
    Public Class Form1
        Dim bitImage As New Bitmap(100, 100)
        Dim grBitImage As Graphics = Graphics.FromImage(bitImage)
        Dim blnLoop As Boolean = True
        Dim FPS As Integer
        Dim FrameCount As Integer
        Dim s As Graphics = Me.CreateGraphics
    
        Private Sub Form1_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
            Timer1.Enabled = False
            blnLoop = False
            s.Dispose()
            grBitImage.Dispose()
            bitImage.Dispose()
        End Sub
    
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            grBitImage.DrawLine(Pens.Black, 0, 0, 50, 0)
            Me.Show()
    
            Do While (blnLoop = True)
                FrameCount = FrameCount + 1
                Me.Text = "FPS: " & CStr(FPS)
                s.DrawImage(bitImage, 0, 0)
                Application.DoEvents()
            Loop
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            FPS = FrameCount
            FrameCount = 0
            Me.Text = "FPS: " & CStr(FPS)
        End Sub
    
    End Class
    if i close the form, i will get the 'NullReferenceException' error message.. why?
    isn't the best clear everything and terminate the loop and timer on 'FormClosed' event?
    ok... i'm new on VB2010.. but i don't understand these error
    if i use a button, instead the 'FormClosed' event, the error don't happens.... so my mistake is by using the 'FormClosed' event?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    Which line does the exception occur on?

    Then again there are a lot of possible issues with that code... DoEvents is normally best avoided, and don't your form load run an infinite loop like that is also unusual.

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by PlausiblyDamp View Post
    Which line does the exception occur on?

    Then again there are a lot of possible issues with that code... DoEvents is normally best avoided, and don't your form load run an infinite loop like that is also unusual.
    1 - the line isn't showed on error but seems that everything is destroyed, even variables, on close's events... but not the loop
    2 - i use the 'DoEvents' for don't 'freeze' the program.. realy...
    3 - i can use a button for activate the infinite loop.
    ok... maybe i'm doing some wrong steps.. but i'm trying learning it and using a simples samples
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    Lively Member VisualBrian's Avatar
    Join Date
    Nov 2019
    Location
    North America
    Posts
    69

    Re: vb2010: why i get the 'NullReferenceException'?

    Not sure, but it seems to me that your FormClosedEvent should be a FormClosingEvent.
    I may not know anything, but I know it well!

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,028

    Re: vb2010: why i get the 'NullReferenceException'?

    That loop is causing trouble, and DoEvents is part of it. I see what you are trying to do, and I've done similar things, but you have to be able to terminate the loop. You're trying to do that with the Boolean, and it seems like it should work, so I suspect it is DoEvents that is causing the trouble.

    DoEvents is a problematic method, as it pauses current execution to allow ALL pending messages to be processed. What messages are pending? That's hard to say. I think the closing event would be such a message, but perhaps not exactly. Presumably, you do something that causes the close event. Perhaps a button click. When DoEvents is encountered, that button click would just be sitting there in the message queue. Other things might also be sitting there. The button then closes the form (I'm making assumptions here, as I don't know that there even IS a button, let alone what it does), which then leads to the closing event, and possibly quite a bit else. One of those things is probably the cause.

    I note that you are running your game loop in Load. Load isn't even the final event for loading a form (though it might have been in VB6, I forget). The form hasn't shown by the time Load is run, so you are interrupting the loading sequence. Without the Me.Show, the form would never be visible, which you probably figured out. Moving the loop to the FormShown event (or whatever it is called), might improve the situation, but it still isn't great.

    What would be better would be to add an Initialize method to the form and put the loop in there. When the form loads, you can then call Initialize as a next step after showing the form. That might solve the problem, since it would move the game loop outside of loading/showing the form. If that's the startup object, then it could be a bit more difficult, though solvable.

    Better still would be to shift to using a proper game loop and getting rid of DoEvents entirely. That may not work well with Windows Forms, though. I certainly haven't tried it.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by Shaggy Hiker View Post
    That loop is causing trouble, and DoEvents is part of it. I see what you are trying to do, and I've done similar things, but you have to be able to terminate the loop. You're trying to do that with the Boolean, and it seems like it should work, so I suspect it is DoEvents that is causing the trouble.

    DoEvents is a problematic method, as it pauses current execution to allow ALL pending messages to be processed. What messages are pending? That's hard to say. I think the closing event would be such a message, but perhaps not exactly. Presumably, you do something that causes the close event. Perhaps a button click. When DoEvents is encountered, that button click would just be sitting there in the message queue. Other things might also be sitting there. The button then closes the form (I'm making assumptions here, as I don't know that there even IS a button, let alone what it does), which then leads to the closing event, and possibly quite a bit else. One of those things is probably the cause.

    I note that you are running your game loop in Load. Load isn't even the final event for loading a form (though it might have been in VB6, I forget). The form hasn't shown by the time Load is run, so you are interrupting the loading sequence. Without the Me.Show, the form would never be visible, which you probably figured out. Moving the loop to the FormShown event (or whatever it is called), might improve the situation, but it still isn't great.

    What would be better would be to add an Initialize method to the form and put the loop in there. When the form loads, you can then call Initialize as a next step after showing the form. That might solve the problem, since it would move the game loop outside of loading/showing the form. If that's the startup object, then it could be a bit more difficult, though solvable.

    Better still would be to shift to using a proper game loop and getting rid of DoEvents entirely. That may not work well with Windows Forms, though. I certainly haven't tried it.
    so what it the best why for do the loop?
    - if i don't use DoEvents, the program freezes;
    - i can add the loop on button click... but the problem remains for close it....
    i accept corrections for create a best graphic\game loop..

    VisualBrian: i tried both
    VB6 2D Sprite control

    To live is difficult, but we do it.

  7. #7
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by joaquim View Post
    so what it the best why for do the loop?
    - if i don't use DoEvents, the program freezes;
    - i can add the loop on button click... but the problem remains for close it....
    i accept corrections for create a best graphic\game loop..

    VisualBrian: i tried both
    If you want the screen to update then you should be doing your drawing code in the Paint event, you shouldn't normally be creating your own graphics in the way you are doing. If you aren't doing a busy loop, then you won't need DoEvents to allow the screen to update.

    In all honesty if you are trying to write a game in dotnet you would either need to spend more time understanding the winforms processing model, or just use something like MonoGame - that way you get to avoid a lot of the winforms stuff that is not really designed with a game in mind.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,028

    Re: vb2010: why i get the 'NullReferenceException'?

    The typical way that a game loop works is a perpetual loop, but it is not in a Windows form. A Windows Forms application has a whole bunch of stuff, such as the message loop, for dealing with user input, drawing the screen, and so forth. Serious game loops do those things themselves. Semi-serious game loops might not be loops at all. Which you choose isn't all that obvious. For example, I have an application that would count as a semi-serious game loop. There is a panel in the center of the screen, which is where everything that you would call game related is drawn. Around that are a series of other controls which are just Windows Forms controls (mostly pictureboxes, but also a toolbar and a menu bar).

    Drawing the panel is the difficult part. Originally, I used GDI, but I was drawing up to 100s of items. I couldn't draw them all in a reasonable amount of time with GDI. At first, I tried caching pre-drawn images, only a few of which would change at any given time. That worked very smoothly, so long as those were the only things I needed to draw. The Paint event of the panel was where the drawing was taking place, so I could invalidate the panel to get a re-draw. The Paint event was checking the cached images, re-drawing the few (if any) objects that needed drawing, then rendering them all to the screen. It was quick...unless I had to invalidate everything in the cache, at which point it could take 15 seconds, or more, to refresh the cache and draw the screen. I thought I would almost never need to invalidate the whole cache, and then I found a case where I would quite often have to invalidate the whole cache, at which point that approach wasn't going to work.

    The problem was one that you are probably familiar with. The objects I was drawing were all rectangles of various sizes, and they were bitmaps onto which some other stuff was drawn. Those are sloooow in GDI.

    I then switched to XNA (and then to MonoGame, which is what XNA became). That allowed me to throw out the cache. All the items can be calculated and drawn onto the panel. Often this is very smooth, and if I turned on vertical syncing, it would be smoother, though slightly slower. I turned it off, which means I get some slight tearing at times, but drawing is a fraction of a second, so the tearing isn't always noticeable. All the rectangles are rendered, complete with all the items that are drawn in them, on every invalidation.

    There is no game loop, with this approach. The panel is no longer a Windows.Forms.Panel, but a custom panel that derives from Control and implements the Paint event. Unfortunately, it gets complicated from there, as everything on the panel, along with the panel itself, is similarly derived. It's basically a panel with a series of pictureboxes on it, but the panel uses XNA, as do the pictureboxes. In my case, I then make it far more complicated, as the pictureboxes aren't part of the project, but are loaded dynamically from dlls at runtime. The basic idea is simple enough, though, as it is some custom XNA controls that inherit Control at their base. Redrawing is accomplished by invalidating either the panel, or any of the pictureboxes on the panel, though usually it is simpler just to invalidate the panel.

    Not sure whether anything like that would work for you, but the MonoGame site has a lot of examples on game loops and the like. I used that site extensively for writing what I ended up with.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    i resolve 2 problems now:
    1 - instead use a .NET timer, i use the API timer(from 60FPS to 1000FPS.... an excelent performance:
    Code:
    Imports System.Runtime.InteropServices
    
    
    Public Class clsTimer
        Private timerID As Integer
        Private timerInterval As Integer
        Private timerCallback2 As TimerCallback
    
        Public Event TimerElapsed()
    
        <DllImport("winmm.dll")>
        Private Shared Function timeSetEvent(ByVal delay As UInteger, ByVal resolution As UInteger, ByVal handler As TimerCallback, ByVal user As UIntPtr, ByVal eventType As UInteger) As Integer
        End Function
    
        <DllImport("winmm.dll")>
        Private Shared Function timeKillEvent(ByVal id As Integer) As Integer
        End Function
    
        Private Delegate Sub TimerCallback(ByVal id As Integer, ByVal msg As Integer, ByVal user As UIntPtr, ByVal rsv1 As UIntPtr, ByVal rsv2 As UIntPtr)
    
        Public Sub StartTimer(ByVal interval As Integer)
            timerInterval = interval
            timerCallback2 = AddressOf TimerProc
            timerID = timeSetEvent(Convert.ToUInt32(interval), 0, timerCallback2, UIntPtr.Zero, 1)
        End Sub
    
        Public Sub StopTimer()
            If timerID <> 0 Then
                timeKillEvent(timerID)
                timerID = 0
            End If
        End Sub
    
        Private Sub TimerProc(ByVal id As Integer, ByVal msg As Integer, ByVal user As UIntPtr, ByVal rsv1 As UIntPtr, ByVal rsv2 As UIntPtr)
            RaiseEvent TimerElapsed()
        End Sub
    End Class
    heres the form1 class:
    Code:
    Public Class Form1
    
    
        Dim tmrGameLoop As New Timer
        Dim tmrFPS As New Timer
        Dim intFrameCount As Integer
        Dim intFPS As Integer
        Dim gr As Graphics = Me.CreateGraphics
        Dim PosX As Long
        Dim PosY As Long
        Dim bitImage As New Bitmap(100, 100)
    
        Private WithEvents myTimer As New clsTimer
        Private WithEvents MyFrameTimer As New clsTimer
    
        'Override the Pain() event:
        Private Shadows Sub Paint(g As Graphics)
            GameDraw(g)
        End Sub
    
        Private Sub GameLoop()
            If (Me.IsDisposed = False And Me.Disposing = False And Me.Visible = True) Then
                'call the me Paint() event:
                Me.Paint(gr)
            End If
        End Sub
    
        Private Sub MyFrameTimer_TimerElapsed() Handles MyFrameTimer.TimerElapsed
            intFPS = intFrameCount
            intFrameCount = 0
        End Sub
    
        Private Sub GameDraw(ByVal e As Graphics)
            Dim grBitImage As Graphics = Graphics.FromImage(bitImage)
            Dim s As New Font("Arial", 10)
            grBitImage.Clear(Color.White)
            grBitImage.FillEllipse(Brushes.Red, PosX, PosY, 30, 30)
            grBitImage.DrawString("FPS: " & intFPS, s, Brushes.Black, 0, 0)
            e.DrawImage(bitImage, 0, 0)
            intFrameCount += 1
            grBitImage.Dispose()
            s.Dispose()
        End Sub
    
        Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If (e.KeyCode = Keys.Up) Then
                PosY -= 1
            ElseIf (e.KeyCode = Keys.Down) Then
                PosY += 1
            ElseIf (e.KeyCode = Keys.Right) Then
                PosX += 1
            ElseIf (e.KeyCode = Keys.Left) Then
                PosX -= 1
            ElseIf (e.KeyCode = Keys.Escape) Then
                bitImage.Dispose()
                End
            End If
        End Sub
    
        Private Sub myTimer_TimerElapsed() Handles myTimer.TimerElapsed
            GameLoop()
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Show()
            myTimer.StartTimer(1)
            
            MyFrameTimer.StartTimer(1000)
        End Sub
    End Class
    the API is old, but we can get much more performance
    thanks for all to all... maybe with time, i can win more performance, because these code only do:
    1 - clear the form;
    2 - draw a fillellipse.
    i accept more corrections
    VB6 2D Sprite control

    To live is difficult, but we do it.

  10. #10

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    and now we have another GameLoop() that i can control more easy
    VB6 2D Sprite control

    To live is difficult, but we do it.

  11. #11
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by joaquim View Post
    i resolve 2 problems now:
    1 - instead use a .NET timer, i use the API timer(from 60FPS to 1000FPS.... an excelent performance:
    Code:
    Imports System.Runtime.InteropServices
    
    
    Public Class clsTimer
        Private timerID As Integer
        Private timerInterval As Integer
        Private timerCallback2 As TimerCallback
    
        Public Event TimerElapsed()
    
        <DllImport("winmm.dll")>
        Private Shared Function timeSetEvent(ByVal delay As UInteger, ByVal resolution As UInteger, ByVal handler As TimerCallback, ByVal user As UIntPtr, ByVal eventType As UInteger) As Integer
        End Function
    
        <DllImport("winmm.dll")>
        Private Shared Function timeKillEvent(ByVal id As Integer) As Integer
        End Function
    
        Private Delegate Sub TimerCallback(ByVal id As Integer, ByVal msg As Integer, ByVal user As UIntPtr, ByVal rsv1 As UIntPtr, ByVal rsv2 As UIntPtr)
    
        Public Sub StartTimer(ByVal interval As Integer)
            timerInterval = interval
            timerCallback2 = AddressOf TimerProc
            timerID = timeSetEvent(Convert.ToUInt32(interval), 0, timerCallback2, UIntPtr.Zero, 1)
        End Sub
    
        Public Sub StopTimer()
            If timerID <> 0 Then
                timeKillEvent(timerID)
                timerID = 0
            End If
        End Sub
    
        Private Sub TimerProc(ByVal id As Integer, ByVal msg As Integer, ByVal user As UIntPtr, ByVal rsv1 As UIntPtr, ByVal rsv2 As UIntPtr)
            RaiseEvent TimerElapsed()
        End Sub
    End Class
    heres the form1 class:
    Code:
    Public Class Form1
    
    
        Dim tmrGameLoop As New Timer
        Dim tmrFPS As New Timer
        Dim intFrameCount As Integer
        Dim intFPS As Integer
        Dim gr As Graphics = Me.CreateGraphics
        Dim PosX As Long
        Dim PosY As Long
        Dim bitImage As New Bitmap(100, 100)
    
        Private WithEvents myTimer As New clsTimer
        Private WithEvents MyFrameTimer As New clsTimer
    
        'Override the Pain() event:
        Private Shadows Sub Paint(g As Graphics)
            GameDraw(g)
        End Sub
    
        Private Sub GameLoop()
            If (Me.IsDisposed = False And Me.Disposing = False And Me.Visible = True) Then
                'call the me Paint() event:
                Me.Paint(gr)
            End If
        End Sub
    
        Private Sub MyFrameTimer_TimerElapsed() Handles MyFrameTimer.TimerElapsed
            intFPS = intFrameCount
            intFrameCount = 0
        End Sub
    
        Private Sub GameDraw(ByVal e As Graphics)
            Dim grBitImage As Graphics = Graphics.FromImage(bitImage)
            Dim s As New Font("Arial", 10)
            grBitImage.Clear(Color.White)
            grBitImage.FillEllipse(Brushes.Red, PosX, PosY, 30, 30)
            grBitImage.DrawString("FPS: " & intFPS, s, Brushes.Black, 0, 0)
            e.DrawImage(bitImage, 0, 0)
            intFrameCount += 1
            grBitImage.Dispose()
            s.Dispose()
        End Sub
    
        Private Sub Form1_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            If (e.KeyCode = Keys.Up) Then
                PosY -= 1
            ElseIf (e.KeyCode = Keys.Down) Then
                PosY += 1
            ElseIf (e.KeyCode = Keys.Right) Then
                PosX += 1
            ElseIf (e.KeyCode = Keys.Left) Then
                PosX -= 1
            ElseIf (e.KeyCode = Keys.Escape) Then
                bitImage.Dispose()
                End
            End If
        End Sub
    
        Private Sub myTimer_TimerElapsed() Handles myTimer.TimerElapsed
            GameLoop()
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.Show()
            myTimer.StartTimer(1)
            
            MyFrameTimer.StartTimer(1000)
        End Sub
    End Class
    the API is old, but we can get much more performance
    thanks for all to all... maybe with time, i can win more performance, because these code only do:
    1 - clear the form;
    2 - draw a fillellipse.
    i accept more corrections
    Rather than going to the api you might want to look at System.Timers.Timer instead of the timer control.

  12. #12

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    "Rather than going to the api you might want to look at System.Timers.Timer instead of the timer control."
    is more accuracy(have more performance)?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  13. #13
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by joaquim View Post
    "Rather than going to the api you might want to look at System.Timers.Timer instead of the timer control."
    is more accuracy(have more performance)?
    It is a lot closer to the system clock, so it can be more precise.

  14. #14

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    you are tell me for avoiding API timer functions.. so i did:
    Code:
    Dim WithEvents Timer1 As New System.Timers.Timer
        Dim WithEvents Timer2 As New System.Timers.Timer
    i use the timer controls and these timers.. the result is always the same: 65FPS.. only for show 1 circle and clear.
    the bitmap it's 100X50.... but even getting only 65FPS, it's bad
    i need much more accuracy
    VB6 2D Sprite control

    To live is difficult, but we do it.

  15. #15
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by joaquim View Post
    you are tell me for avoiding API timer functions.. so i did:
    Code:
    Dim WithEvents Timer1 As New System.Timers.Timer
        Dim WithEvents Timer2 As New System.Timers.Timer
    i use the timer controls and these timers.. the result is always the same: 65FPS.. only for show 1 circle and clear.
    the bitmap it's 100X50.... but even getting only 65FPS, it's bad
    i need much more accuracy
    Personally I just try to avoid the api if it is something dotnet can do itself. Looks like in this case the built in timers might not be good enough.

    Then again winforms itself isn't a good choice if you need a high framerate.

    As people have suggested before, if you are trying to create a game then you might as well avoid winforms, sooner or later you will hit limitations. DirextX or OpenGL is the way to go, OpenGL would also open up the possibility of cross platform games.

    If I get chance later I will throw together a minimal vb monogame example that would draw a simple image on screen, allow movement with a keyboard etc.

  16. #16
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,028

    Re: vb2010: why i get the 'NullReferenceException'?

    I'm curious as to why you need performance greater than 65 FPS anyways? My understanding of games is that anything over 30 FPS is usually acceptable, while the human eye generally doesn't notice anything above 60 FPS (though I think that's actually a bit low, as I can generally see flickering of a solid color at a refresh rate of 60 FPS). In any case, a frame rate faster than the refresh rate of the monitor is kind of pointless, and I thought modern screens had a refresh of 60 Hz.
    My usual boring signature: Nothing

  17. #17
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    The attached zip is a really rough example of a VB Monogame app, it allows you to move a character around a screen and for no real reason a number of footballs bounce around in the background (I just wanted to have some movement on screen really). The app is an OpenGL based game, should run on Windows, Linux, or Mac (and if you have the relevant membership even on XBox, Switch, and other consoles).

    The code is pretty rough, but is shows how the core loading of resources, the game loop, and the handling of input can be done. No need to worry about timers, GDI, GDI+, drawing to Graphics etc. Just using the core functionality provided. Depending on your PC you can happily bump up the number of on screen balls to a couple of thousand or so - looks rubbish but should still give reasonable FPS.

    When run the app is capped to 60 fps (more than enough, but that can be removed in code), exactly the point Shaggy made in he previous post.

    https://monogame.net/articles/gettin...t_windows.html goes through what would be needed to get the Monogame extension installed, once it is installed the project should build and run (you might need to force a nuget package restore first though) the project uses .Net 8 but it should probably work on 6 or 7 as well if you change the framework version.

    There should be two screenshots, one with the fps capped, one with the cap removed.

    The cursor keys move the man around and escape quits the game.

    Edit: I have updated the attachment to give a consistent speed regardless of fps
    Attached Images Attached Images   
    Attached Files Attached Files
    Last edited by PlausiblyDamp; Mar 7th, 2024 at 05:13 AM. Reason: Updated the attachment

  18. #18

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by Shaggy Hiker View Post
    I'm curious as to why you need performance greater than 65 FPS anyways? My understanding of games is that anything over 30 FPS is usually acceptable, while the human eye generally doesn't notice anything above 60 FPS (though I think that's actually a bit low, as I can generally see flickering of a solid color at a refresh rate of 60 FPS). In any case, a frame rate faster than the refresh rate of the monitor is kind of pointless, and I thought modern screens had a refresh of 60 Hz.
    simple!!! if i add more images or the bitmap is more big, it will be less than 60FPS... i only use a bitmap 100X100 and draw an ellipse and gray effect.

    PlausiblyDamp: i will now test the game.. thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

  19. #19

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    PlausiblyDamp: i can't use it on VB2010
    i get some errors
    "C:\Users\Utilizador\Downloads\MonoVB\MonoVB\MonoVB.vbproj : error : Unable to read the project file 'MonoVB.vbproj'.
    C:\Users\Utilizador\Downloads\MonoVB\MonoVB\MonoVB.vbproj(1,1): The default XML namespace of the project must be the MSBuild XML namespace. If the project is authored in the MSBuild 2003 format, please add xmlns="http://schemas.microsoft.com/developer/msbuild/2003" to the <Project> element. If the project has been authored in the old 1.0 or 1.2 format, please convert it to MSBuild 2003 format."
    why i get these errors?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  20. #20
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    Ah, the project was created in vs 2022 and uses the latest version of dotnet.

    I think you can still get older versions of Monogame that might work on 2010.

    Might also work with vs code if you install the latest dotnet sdk.
    Last edited by PlausiblyDamp; Feb 27th, 2024 at 02:58 PM.

  21. #21

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by PlausiblyDamp View Post
    Ah, the project was created in vs 2022 and uses the latest version of dotnet.

    I think you can still get older versions of Monogame that might work on 2010.

    Might also work with vs code if you install the latest dotnet sdk.
    i have ,too, the VS2022(but is more slow on my laptop)... i get these error(after install the the XNA Framework):
    "Gravidade Código Descrição Projeto Arquivo Linha Estado de Supressão
    Erro NETSDK1183 Não é possível otimizar assemblies para compilação antecipada: um pacote de tempo de execução válido não foi encontrado. Defina a propriedade PublishAot como false ou use um identificador de tempo de execução compatível ao publicar. Ao direcionar o .NET 7 ou superior, certifique-se de restaurar os pacotes com a propriedade PublishAot definida como verdadeiro. MonoVB C:\Program Files\dotnet\sdk\7.0.400\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.FrameworkReferenceResoluti on.targets 90 "
    ok.. it's a Portuguese text and an error message
    VB6 2D Sprite control

    To live is difficult, but we do it.

  22. #22
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    If you look in the .vbproj file find and remove the following line
    Code:
    <PublishAot>true</PublishAot>
    and see if that helps.

  23. #23

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by PlausiblyDamp View Post
    If you look in the .vbproj file find and remove the following line
    Code:
    <PublishAot>true</PublishAot>
    and see if that helps.
    i'm sorry, but no...
    what frame work version you used?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  24. #24
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by joaquim View Post
    i'm sorry, but no...
    what frame work version you used?
    I used 8, it should probably work if you drop it to 7 or maybe even 6

  25. #25

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by PlausiblyDamp View Post
    I used 8, it should probably work if you drop it to 7 or maybe even 6
    i did a big update on VS2022... now i'm trying recompile... it takes several time lol and continues
    VB6 2D Sprite control

    To live is difficult, but we do it.

  26. #26

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    seems like frozen on compilation
    VB6 2D Sprite control

    To live is difficult, but we do it.

  27. #27
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,028

    Re: vb2010: why i get the 'NullReferenceException'?

    8 isn't actually a framework version. That would be .NET8.

    You can certainly use MonoGame in framework. My program started in VS2010 targeting FW4.0, and is still framework, though now targeting 4.7.2 or 4.8.

    Framework and whatever came after it are fairly different animals, though.
    My usual boring signature: Nothing

  28. #28
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by joaquim View Post
    seems like frozen on compilation
    In that case I am not sure what to suggest, the first time might be a bit slow as it needs to restore the various nuget packages. Then again it still only took a minute or so on mine for the first run.

  29. #29

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    maybe because i turn on the laptop, after updates... or something.. i tried install the XNA Framework... maybe it's there.. but works
    why i only get 60FPS?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  30. #30
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,535

    Re: vb2010: why i get the 'NullReferenceException'?

    Hear me out ... if VB isn't working out, you could try godot, which uses C# for its programming (also has built-in gd-script, and also suports other languages through plugin system. The game engine is already built... you just have to supply the sprites and the code to make things move (or not if that's hte case). It may or may not be worth the juice for a change, but if you can't get the engine off the ground, it might be an alternative.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  31. #31
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by joaquim View Post
    maybe because i turn on the laptop, after updates... or something.. i tried install the XNA Framework... maybe it's there.. but works
    why i only get 60FPS?
    Because it is capped to 60fps by default. I said that in the post, if you look at the code there is a comment telling you what to change to remove the fps limit.

    In reality though you wouldn't want to do that as more than 60fps is pointless, plus it messes up the game speed.

  32. #32

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by PlausiblyDamp View Post
    Because it is capped to 60fps by default. I said that in the post, if you look at the code there is a comment telling you what to change to remove the fps limit.

    In reality though you wouldn't want to do that as more than 60fps is pointless, plus it messes up the game speed.
    Code:
     Public Sub New()
         _graphics = New GraphicsDeviceManager(Me)
         Content.RootDirectory = "Content"
    
         'set the following to false to remove fps limit
         _graphics.SynchronizeWithVerticalRetrace = False
         IsFixedTimeStep = False
     End Sub
    yes i found it.. thanks...
    so, why, i think, it's better control it(if is more slow or not)?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  33. #33
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    Why do you think you need more than 60fps? Can you tell the difference in quality between 60 and 4,000 fps?

  34. #34

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    on my head is like think: more FPS is more accurate than less FPS
    VB6 2D Sprite control

    To live is difficult, but we do it.

  35. #35
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,470

    Re: vb2010: why i get the 'NullReferenceException'?

    No point in running a game at such a high fps that nobody can actually perceive.

  36. #36

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,904

    Re: vb2010: why i get the 'NullReferenceException'?

    Quote Originally Posted by PlausiblyDamp View Post
    No point in running a game at such a high fps that nobody can actually perceive.
    thank you so much for correct me...
    i need ask: getting 60FPS on VB2010 is normal(thinking on these way.. even if is technicall(i don't know if is))?
    VB6 2D Sprite control

    To live is difficult, but we do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width