Results 1 to 2 of 2

Thread: Controlling refresh rate

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2012
    Posts
    1

    Controlling refresh rate

    Hello!
    I'm a 18 year old student from Sweden. I'm fairly new to programming and just started ~6 months ago with Small Basic and then moved on to Visual Basic. I chose "making a fairly decent computer game" as a project in school. I have done a 2D game in the Windows Form Application. It works but I am interested in optimizing it. My question is: how do I control the screen refresh rate for the form?
    I would like to set it to 0 Hz and then make it go back to the previous level or something along those lines.

    An answer would be much appreciated!

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Controlling refresh rate

    Take a look at my managed game loop. It locks the framerate at 60 frames per second or whatever you so choose:

    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
    4. Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
    5.  
    6. Private Ticks_Per_Second As Currency
    7. Private Start_Time As Currency
    8. Private Milliseconds As Long
    9. Private Last_Time As Long
    10.  
    11. Private Get_Frames_Per_Second As Long
    12. Private Frame_Count As Long
    13.  
    14. Private Running As Boolean
    15.  
    16. Public Function Hi_Res_Timer_Initialize() As Boolean
    17.  
    18.     If QueryPerformanceFrequency(Ticks_Per_Second) = 0 Then
    19.         Hi_Res_Timer_Initialize = False
    20.     Else
    21.         QueryPerformanceCounter Start_Time
    22.         Hi_Res_Timer_Initialize = True
    23.     End If
    24.  
    25. End Function
    26.  
    27. Public Function Get_Elapsed_Time() As Single
    28.    
    29.     Dim Last_Time As Currency
    30.     Dim Current_Time As Currency
    31.    
    32.     QueryPerformanceCounter Current_Time
    33.     Get_Elapsed_Time = (Current_Time - Last_Time) / Ticks_Per_Second
    34.     QueryPerformanceCounter Last_Time
    35.    
    36. End Function
    37.  
    38. Private Sub Lock_Framerate(Target_FPS As Long)
    39.  
    40.     Static Last_Time As Currency
    41.     Dim Current_Time As Currency
    42.     Dim FPS As Single
    43.    
    44.     Do
    45.         QueryPerformanceCounter Current_Time
    46.         FPS = Ticks_Per_Second / (Current_Time - Last_Time)
    47.     Loop While (FPS > Target_FPS)
    48.    
    49.     QueryPerformanceCounter Last_Time
    50.  
    51. End Sub
    52.  
    53. Private Function Get_FPS() As String
    54.  
    55.     Frame_Count = Frame_Count + 1
    56.        
    57.     If Get_Elapsed_Time - Milliseconds >= 1 Then
    58.         Get_Frames_Per_Second = Frame_Count
    59.         Frame_Count = 0
    60.         Milliseconds = Get_Elapsed_Time
    61.     End If
    62.    
    63.     Get_FPS = "FPS: " & Get_Frames_Per_Second
    64.  
    65. End Function
    66.  
    67. Private Sub Shutdown()
    68.  
    69.     Running = False
    70.     Unload Me
    71.  
    72. End Sub
    73.  
    74. Private Sub Main()
    75.    
    76.     With Me
    77.         .Show
    78.         .ScaleMode = 3
    79.         .AutoRedraw = True
    80.         .BackColor = RGB(0, 0, 0)
    81.     End With
    82.  
    83.     Running = True
    84.     Hi_Res_Timer_Initialize
    85.     Milliseconds = Get_Elapsed_Time
    86.     Main_Loop
    87.  
    88. End Sub
    89.  
    90. Private Sub Main_Loop()
    91.  
    92.     Do While Running = True
    93.        
    94.         '//Timer Code Goes Here
    95.         Me.Caption = Get_FPS
    96.         DoEvents
    97.         Lock_Framerate 60
    98.        
    99.     Loop
    100.  
    101. End Sub
    102.  
    103. Private Sub Form_Load()
    104.    
    105.     Main
    106.  
    107. End Sub
    108.  
    109. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
    110.  
    111.     If KeyCode = vbKeyEscape Then
    112.         Shutdown
    113.     End If
    114.  
    115. End Sub
    116.  
    117. Private Sub Form_Unload(Cancel As Integer)
    118.  
    119.     Shutdown
    120.  
    121. End Sub

    Also if you ever want to, take a look at my Massive DirectX Tutorial in my signature if you wanna make your games a little more professional. I also am currently redoing the entire tutorial by improving the ones I made by a lot and slapping on a couple hundred other tutorials as well in both 2D and 3D and DirectDraw in DirectX7 - DirectX11 in VB6, VB.Net 2008, VB.Net 2010, C# 2008, C# 2010, C++ 6.0, C++ 2008, and C++ 2010. I got a website going but its still under construction along with my tutorials. Its gonna cover every aspect of DirectX nearly and have advanced topics that are difficult to find on the net.

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