Skip to content

Instantly share code, notes, and snippets.

@AnthonyDGreen AnthonyDGreen/GameLoop.vb Secret
Created May 2, 2019

What would you like to do?
A primitive GDI+ based game/animation-loop base class
Public Class GameLoop
Inherits Form
Private Timer As New Stopwatch()
'Private OtherTimer As New Threading.Timer(Sub() Invalidate(ClientRectangle), Nothing, 1000, 15)
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim elapsed = Timer.ElapsedMilliseconds
If elapsed >= 16 Then
Execute(e.Graphics, elapsed / 1000)
ElseIf Not Timer.IsRunning Then
Execute(e.Graphics, elapsed / 1000)
End If
End Sub
Private Const HorizontalMargin = 20
Private Const VerticalMargin = 20
Public ReadOnly Property LeftEdge As Integer = HorizontalMargin
Public ReadOnly Property TopEdge As Integer = VerticalMargin
Public ReadOnly Property RightEdge As Integer = ClientSize.Width - HorizontalMargin
Public ReadOnly Property BottomEdge As Integer = ClientSize.Height - VerticalMargin
Public Overridable Sub Execute(buffer As Graphics, elapsedSeconds As Single)
End Sub
Sub New()
' This call is required by the designer.
' Add any initialization after the InitializeComponent() call.
Me.SetStyle(ControlStyles.Opaque Or ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint, True)
Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, False)
End Sub
Protected Overrides Sub OnPaintBackground(e As PaintEventArgs)
End Sub
Protected Overrides Sub OnResize(e As EventArgs)
_RightEdge = ClientSize.Width - HorizontalMargin
_BottomEdge = ClientSize.Height - VerticalMargin
End Sub
Private Sub InitializeComponent()
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(600, 400)
Me.Name = Me.GetType().Name
Me.Text = Name
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.