Guest User

Untitled

a guest
Oct 24th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. ;{- Alle Control-Thread-Procedures
  3. Global Control_Flag.l, Control_Handle.l, Control_Hz.l, Control_Callback.l
  4. Enumeration 1
  5.     #Control_Normal
  6.     #Control_Quit
  7.     #Control_Pause
  8.     #Control_ChangeHz
  9.    
  10.     #Control_PtrIsNull
  11.     #Control_CBIsFalse
  12.     #Control_FlagError
  13. EndEnumeration
  14.  
  15. ;Control-Thread
  16. Procedure Control(Callback.l)
  17.     Protected time_Delay.l, time_End.l
  18.     Control_Hz = 100
  19.     Control_Callback = Callback
  20.    
  21.     time_Delay = 1000 / Control_Hz
  22.     time_End = TimeGetTime_() + time_Delay
  23.     Repeat
  24.         Select Control_Flag
  25.                
  26.             Case #Control_Quit      ;Procedure beenden
  27.                 Break
  28.                
  29.             Case #Control_ChangeHz  ;Refreshrate ändern
  30.                 time_Delay = 1000 / Control_Hz
  31.                 Control_Flag = #Control_Normal
  32.                
  33.             Case #Control_Pause     ;Alle Eingaben ignorieren
  34.                 While Control_Flag = #Control_Pause
  35.                     ExamineKeyboard()
  36.                     ExamineMouse()
  37.                     Delay(1)
  38.                 Wend
  39.                
  40.             Case #Control_Normal    ;Eingabe verarbeiten
  41.                 ExamineKeyboard()
  42.                 ExamineMouse()
  43.                 If Control_Callback
  44.                     If CallFunctionFast(Control_Callback) = 0
  45.                         Control_Flag = #Control_CBIsFalse
  46.                         Break
  47.                     EndIf
  48.                 Else
  49.                     Control_Flag = #Control_PtrIsNull
  50.                     Break
  51.                 EndIf
  52.                
  53.             Default                 ;Fehler
  54.                 Control_Flag = #Control_FlagError
  55.                 Break
  56.         EndSelect
  57.        
  58.         ;Pause
  59.         While TimeGetTime_() < time_End : Delay(1) : Wend
  60.         time_End = TimeGetTime_() + time_Delay
  61.        
  62.     ForEver
  63.     Control_Handle = 0
  64.     Control_Hz = 0
  65.     Control_Callback = 0
  66. EndProcedure
  67.  
  68. Procedure Control_Start(*Callback)
  69.     If Control_Handle
  70.         Control_Flag = #Control_Quit
  71.         WaitThread(Control_Handle)
  72.     EndIf
  73.     Control_Flag = #Control_Normal
  74.     Control_Handle.l = CreateThread(@Control(), *Callback)
  75.     ThreadPriority(Control_Handle, 17)    ;Priorität um eins höher als das Hauptprogramm
  76.     ProcedureReturn Control_Handle
  77. EndProcedure
  78.  
  79. Procedure Control_Pause()             ;Alle Eingabe werden ignoiert
  80.     Control_Flag = #Control_Pause
  81. EndProcedure
  82. Procedure Control_Resume()            ;Rücksetzen der Pause
  83.     Control_Flag = #Control_Normal
  84. EndProcedure
  85. Procedure Control_Quit()              ;Control-Thread beenden
  86.     Control_Flag = #Control_Quit
  87. EndProcedure
  88. Procedure Control_GetHz()             ;Aktualisierungsrate des Threads auslesen
  89.     ProcedureReturn Control_Hz
  90. EndProcedure
  91. Procedure Control_SetHz(Hertz.l)      ;Aktualisierungsrate des Threads ändern
  92.     Control_Hz = Hertz.l
  93.     Control_Flag = #Control_ChangeHz
  94. EndProcedure
  95. Procedure Control_ChangeCB(*Callback) ;Control-Callback ändern
  96.     Control_Callback = *Callback
  97. EndProcedure
  98.  
  99. ; Mögliche Rückgabewerte:
  100. ; - #Control_Normal     : Thread läuft normal
  101. ; - #Control_Quit       : Thread wurde normal beendet
  102. ; - #Control_Pause      : Thread ist pausiert
  103. ;  
  104. ; - #Control_PtrIsNull  : Fehler! Aufzurufender Callback-Pointer ist Null -> Beendet
  105. ; - #Control_CBIsFalse  : Fehler! Rückgabewert der Callback-Procedure ist Null -> Beendet
  106. ; - #Control_FlagError  : Fehler! Falsches Flag gesendet -> Beendet
  107. Procedure Control_GetStatus()
  108.     ProcedureReturn Control_Flag
  109. EndProcedure
  110. ;}
  111.  
  112. ;{- Init
  113. If InitSprite() = 0
  114.     MessageRequester("Error", "InitSprite()-Fehler.")
  115.     End
  116. EndIf
  117.  
  118. If InitKeyboard() = 0
  119.     MessageRequester("Error", "InitKeyboard()-Fehler.")
  120.     End
  121. EndIf
  122.  
  123. If InitMouse() = 0
  124.     MessageRequester("Error", "InitMouse()-Fehler.")
  125.     End
  126. EndIf
  127. ;}
  128.  
  129. ;- Game
  130.  
  131. Global Game_Quit.l
  132. Global PlayerX.f, PlayerY.f
  133. Global ScreenWidth.l, ScreenHeight.l
  134.  
  135. ;Aufzurufender Callback mit Eingabenverarbeitung
  136. Procedure Controls()
  137.     Protected ControlHz.l
  138.    
  139.     If KeyboardReleased(#PB_Key_Escape)
  140.         Game_Quit = #True
  141.     EndIf
  142.     If KeyboardPushed(#PB_Key_Left) And PlayerX > 0
  143.         PlayerX - 1
  144.     EndIf
  145.     If KeyboardPushed(#PB_Key_Right) And PlayerX < ScreenWidth
  146.         PlayerX + 1
  147.     EndIf
  148.    
  149.     If KeyboardPushed(#PB_Key_Up) And PlayerY > 0
  150.         PlayerY - 1
  151.     EndIf
  152.     If KeyboardPushed(#PB_Key_Down) And PlayerY < ScreenHeight
  153.         PlayerY + 1
  154.     EndIf
  155.    
  156.     ControlHz = Control_GetHz()
  157.     If KeyboardReleased(#PB_Key_Add)
  158.         Control_SetHz(ControlHz + 10)
  159.     EndIf
  160.     If KeyboardReleased(#PB_Key_Subtract) And ControlHz > 10
  161.         Control_SetHz(ControlHz - 10)
  162.     EndIf
  163.    
  164.     ProcedureReturn #True
  165. EndProcedure
  166.  
  167. ScreenHeight = 768
  168. ScreenWidth = 1024
  169.  
  170. If OpenScreen(ScreenWidth, ScreenHeight, 32, "Control-Thread Test")
  171.     Control_Start(@Controls())
  172.     PlayerX = ScreenWidth / 2
  173.     PlayerY = ScreenHeight / 2
  174.    
  175.     ;Mainloop
  176.     FPS_Time.l = TimeGetTime_()
  177.     FPS_Counter.l = 0
  178.     FPS_Freq.f = 0
  179.     Repeat
  180.         FPS_Counter + 1
  181.         If FPS_Time + 1000 < TimeGetTime_()
  182.             FPS_Freq = FPS_Counter
  183.             FPS_Counter = 0
  184.             FPS_Time = TimeGetTime_()
  185.         EndIf
  186.        
  187.         ClearScreen(0, 0, 0)
  188.         StartDrawing(ScreenOutput())
  189.         DrawingMode(0)
  190.         Box(PlayerX - 10, PlayerY - 10, 20, 20, $FF0000)
  191.        
  192.         DrawingMode(4)
  193.         Box(0, 0, ScreenWidth, ScreenHeight, $FFFFFF)
  194.        
  195.         FrontColor(255, 0, 0)
  196.         DrawingMode(1)
  197.         Locate(5, 5)
  198.         DrawText("Keyboard Refreshrate: " + Str(Control_GetHz()) + " Hz")
  199.         Locate(5, 21)
  200.         DrawText("Screen Refreshrate: " + Str(FPS_Freq) + " Hz")
  201.         StopDrawing()
  202.         FlipBuffers()
  203.     Until Game_Quit
  204.    
  205.     Control_Quit()
  206. EndIf
Add Comment
Please, Sign In to add comment