3 Replies - 591 Views - Last Post: 15 December 2013 - 10:29 PM Rate Topic: -----

#1 dday9  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 86
  • Joined: 17-April 13

[Vb.net] Gameloop causes stackoverflow

Posted 15 December 2013 - 07:25 PM

I already have a working example of a game loop, but I wanted to create a different styled one. So this is what I currently have:

Option Strict On
Option Explicit On
Public Class Form1
    'Set a global FramesPerSecond variable
    'As well as the threading loop so that we can abort it whenever
    Private fps As Double = 0
    Private loop_thread As Threading.Thread
    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Startup a new thread
        loop_thread = New Threading.Thread(AddressOf GameLoop)
    End Sub

    Private Sub GameLoop()
        'Do until we abort the thread
        Do Until True = False
            'The "prior" time will be now
            Dim prior As DateTime = DateTime.Now

            'This is where you'd update anything
            Console.WriteLine(fps.ToString)

            'Lock the frames per second at 60
            Call LockFps(prior, 60)
        Loop
    End Sub

    Private Sub LockFps(ByVal PriorTime As DateTime, ByVal TargetFps As Double)
        'Get the current time this method was called
        Dim CurrentTime As DateTime = DateTime.Now

        'The actual frames per second will be the current time we just got
        'minus the prior time before we did any updating
        Dim ActualFps As Double = CurrentTime.Subtract(PriorTime).TotalMilliseconds

        'If the target frames per second(divided by 1000 because we're working with milliseconds)
        'Is less than the actual frames per second, then don't exit the sub yet
        If 1000 / TargetFps > ActualFps Then
            Call LockFps(CurrentTime, TargetFps)
        Else
            'If the actual frames per second is greater or equal to the target, then set the variable
            fps = ActualFps * 1000
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Go! Go! Go!
        loop_thread.Start()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Abort! Abort! Abort!
        loop_thread.Abort()
    End Sub
End Class



However, after a few cycles I get a stackoverflow error on the line where I get CurrentTime in the LockFps sub. Any suggestions on how to fix it?

Is This A Good Question/Topic? 0
  • +

Replies To: [Vb.net] Gameloop causes stackoverflow

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 9579
  • View blog
  • Posts: 36,298
  • Joined: 12-June 08

Re: [Vb.net] Gameloop causes stackoverflow

Posted 15 December 2013 - 07:28 PM

try a try catch and see what the full error says.
Was This Post Helpful? 0
  • +
  • -

#3 dday9  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 86
  • Joined: 17-April 13

Re: [Vb.net] Gameloop causes stackoverflow

Posted 15 December 2013 - 08:47 PM

The catch never get's caught because the stackoverflow exception happens every time, no matter if I wrap the Try/Catch around: 1) Where I call the sub 2)Where I get CurrentTime 3)At the beginning of the LockFps sub. However, I did copy the exceptions details:

Quote

An unhandled exception of type 'System.StackOverflowException' occurred in mscorlib.dll

Was This Post Helpful? 0
  • +
  • -

#4 Rhino1111  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 99
  • View blog
  • Posts: 231
  • Joined: 28-August 13

Re: [Vb.net] Gameloop causes stackoverflow

Posted 15 December 2013 - 10:29 PM

Might be obvious, but the problem is very likely from your LockFPS(...) method, since it makes recursive calls during a certain condition. Nothing else from a glance looks like it would cause stack overflows.

EDIT: Missed the bottom text on your post where you stated this. Your recursion is going to deep in that method. The condition for the recursive call it being met too many times consecutively and it causes the stack overflows.

This post has been edited by Rhino1111: 15 December 2013 - 10:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1