Option Explicit On
Option Strict On
Public Class Form1
Private Declare Function QueryPerformanceCounter Lib "Kernel32" (ByRef X As Long) As Integer
Private Declare Function QueryPerformanceFrequency Lib "Kernel32" (ByRef X As Long) As Integer
Private Structure Sprite_Type
Public X As Integer
Public Y As Integer
Public Width As Integer
Public Height As Integer
Public Texture As Image
Public Texture_Graphics As Graphics
End Structure
'Key Functions
Private Const P1_UP As Keys = Keys.Up
Private Const P1_DOWN As Keys = Keys.Down
Private Const P1_LEFT As Keys = Keys.Left
Private Const P1_RIGHT As Keys = Keys.Right
'Key Flags (MUST BE IN BINARY FORMAT THE MORE KEYS YOU USE: ex. 1, 2, 4, 8, 16, 32, 64, 128, etc.
Private Const P1_UP_FLAG As Integer = 1
Private Const P1_DOWN_FLAG As Integer = 2
Private Const P1_LEFT_FLAG As Integer = 4
Private Const P1_RIGHT_FLAG As Integer = 8
Private Ticks_Per_Second As Long
Private Start_Time As Long
Private Milliseconds As Integer
Private Get_Frames_Per_Second As Integer
Private Frame_Count As Integer
Private Keystate As Integer
Private Running As Boolean
Dim Backbuffer_Graphics As Graphics
Private Sprite As Sprite_Type
Private Function Hi_Res_Timer_Initialize() As Boolean
If QueryPerformanceFrequency(Ticks_Per_Second) = 0 Then
Hi_Res_Timer_Initialize = False
Else
QueryPerformanceCounter(Start_Time)
Hi_Res_Timer_Initialize = True
End If
End Function
Private Function Get_Elapsed_Time() As Single
Dim Last_Time As Long
Dim Current_Time As Long
QueryPerformanceCounter(Current_Time)
Get_Elapsed_Time = Convert.ToSingle((Current_Time - Last_Time) / Ticks_Per_Second)
QueryPerformanceCounter(Last_Time)
End Function
Private Function Get_Elapsed_Time_Per_Frame() As Single
Static Last_Time As Long
Static Current_Time As Long
QueryPerformanceCounter(Current_Time)
Get_Elapsed_Time_Per_Frame = CSng((Current_Time - Last_Time) / Ticks_Per_Second)
QueryPerformanceCounter(Last_Time)
End Function
Private Sub Lock_Framerate(ByVal Target_FPS As Long)
Static Last_Time As Long
Dim Current_Time As Long
Dim FPS As Single
Do
QueryPerformanceCounter(Current_Time)
FPS = Convert.ToSingle(Ticks_Per_Second / (Current_Time - Last_Time))
Loop While (FPS > Target_FPS)
QueryPerformanceCounter(Last_Time)
End Sub
Private Function Get_FPS() As String
Frame_Count = Frame_Count + 1
If Get_Elapsed_Time() - Milliseconds >= 1 Then
Get_Frames_Per_Second = Frame_Count
Frame_Count = 0
Milliseconds = Convert.ToInt32(Get_Elapsed_Time)
End If
Get_FPS = "Frames Per Second: " & Get_Frames_Per_Second
End Function
Private Function Check_Key(ByVal Keyflag As Integer) As Integer
Check_Key = Keystate And Keyflag
End Function
Private Sub Keyboard_Control()
If Check_Key(P1_LEFT_FLAG) > 0 Then Sprite.X -= 5
If Check_Key(P1_RIGHT_FLAG) > 0 Then Sprite.X += 5
End Sub
Private Sub Load_Textures()
Sprite.Texture_Graphics = Me.CreateGraphics()
Sprite.Texture = Image.FromFile(Application.StartupPath() & "\Link.png")
End Sub
Private Sub Unload_Textures()
Sprite.Texture_Graphics.Dispose()
End Sub
Private Sub Setup_Sprites()
With Sprite
.Width = 54
.Height = 69
.X = CInt(Me.Width / 2) - CInt(Sprite.Width / 2)
.Y = 490
End With
End Sub
Private Sub Clear_Screen()
Backbuffer_Graphics.Clear(Color.FromArgb(255, 0, 0, 0))
End Sub
Private Sub Render()
Clear_Screen()
Sprite.Texture_Graphics.DrawImage(Sprite.Texture, New RectangleF(Sprite.X, Sprite.Y, Sprite.Width, Sprite.Height))
End Sub
Private Sub Game_Loop()
Do While Running = True
Keyboard_Control()
Render()
Lock_Framerate(60)
Me.Text = Get_FPS()
Application.DoEvents()
Loop
End Sub
Private Sub Main()
With Me
.Show()
.Focus()
.DoubleBuffered = True
.KeyPreview = True
End With
Backbuffer_Graphics = Me.CreateGraphics()
Load_Textures()
Setup_Sprites()
Hi_Res_Timer_Initialize()
Running = True
Game_Loop()
End Sub
Private Sub Shutdown()
Unload_Textures()
Running = False
Application.Exit()
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case P1_UP
Keystate = Keystate Or P1_UP_FLAG
Case P1_DOWN
Keystate = Keystate Or P1_DOWN_FLAG
Case P1_LEFT
Keystate = Keystate Or P1_LEFT_FLAG
Case P1_RIGHT
Keystate = Keystate Or P1_RIGHT_FLAG
End Select
End Sub
Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
Select Case e.KeyCode
Case P1_UP
Keystate = Keystate And (Not P1_UP_FLAG)
Case P1_DOWN
Keystate = Keystate And (Not P1_DOWN_FLAG)
Case P1_LEFT
Keystate = Keystate And (Not P1_LEFT_FLAG)
Case P1_RIGHT
Keystate = Keystate And (Not P1_RIGHT_FLAG)
End Select
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Main()
End Sub
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.FormClosing
Shutdown()
End Sub
End Class