November 24, 2020, 09:27:32 AM

Author Topic: [bb] 3 functions Timer_Sec Timer_mSec and Screen by Dan [ 1+ years ago ]  (Read 737 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : 3 functions Timer_Sec Timer_mSec and Screen
Author : Dan
Posted : 1+ years ago

Description : 3 functions with demo.

Edit : MiliSeconds function may not properly work on XP (as it is, it works on win8.1/64bit)

;====================================================================
; Project: MiliSeconds + Seconds Timer, Screen
; Version: 0.0
; Author: Dan
; Email: -.-
; Copyright: PD
; Description: Timer function returns seconds passed since last function reset
;              parameter to count up needs to be passed as - eg. -1
;              Can be used as countdown if parameter is higer than 0
;              it continues below 0 !
;
;              Screen = Graphic call function  
;              Opens a window in specified resolution and maximizes
;              it to the desktop resolution, usefull for small progs
;              or testing functions
;              uncomment below code for demo
;        (Decls needed for the Screen are at the end of the function)
;====================================================================


Code :
Code: BlitzBasic
  1. ;====================================================================
  2. ; Project: MiliSeconds + Seconds Timer, Screen
  3. ; Version: 0.0
  4. ; Author: Dan
  5. ; Email: -.-
  6. ; Copyright: PD
  7. ; Description: Timer function returns seconds passed since last function reset
  8. ;              parameter to count up needs to be passed as - eg. -1
  9. ;              Can be used as countdown if parameter is higer than 0
  10. ;              it continues below 0 !
  11. ;
  12. ;              Screen = Graphic call function  
  13. ;              Opens a window in specified resolution and maximizes
  14. ;              it to the desktop resolution, usefull for small progs
  15. ;              or testing functions
  16. ;              uncomment below code for demo
  17. ;        (Decls needed for the Screen are at the end of the function)
  18. ;====================================================================
  19.  
  20. ;Screen(320,250)
  21. ;
  22. ;Global Timer_s=MilliSecs() ;<---Put at the start of your program---
  23. ;Global Timer_m=MilliSecs() ;<---Put at the start of your program---
  24. ;
  25. ;Repeat
  26. ;
  27. ;Cls
  28. ;
  29. ;blink=Time_mSec(-1) Mod 80
  30. ;
  31. ;If blink>0 And blink <40
  32. ;   Color $0,$25,$ff
  33. ;   Rect 0,100,160,15,1
  34. ;Else
  35. ;  Color $ff,$0,$ff
  36. ;  Rect 0,114,160,15,1
  37. ;EndIf
  38. ;
  39. ;Color $ff,$ff,$ff
  40. ;Text 1,1,"Left Mousebutton resets Timer_Sec"
  41. ;Text 1,140,"Right Mousebutton resets Timer_mSec"
  42. ;Text 1,28,Time_Sec(25)+" = Time_Sec(25)"
  43. ;Text 1,44,Time_Sec(-1)+" = Time_Sec(-1)"
  44. ;Text 1,70,"This Program ends in 0:"+Time_Sec(50)
  45. ;Text 1,100,Time_mSec(800)+" = Time_mSec(800)"
  46. ;Text 1,114,Time_mSec(-1)+  " = Time_mSec(-1)"
  47. ;If Time_Sec(50)=<0 Then End
  48. ;
  49. ;If MouseDown(1)
  50. ;Time_Sec(0)
  51. ;EndIf
  52. ;
  53. ;If MouseDown(2)
  54. ;Time_mSec(0)
  55. ;EndIf
  56. ;
  57. ;Delay 1
  58. ;Until KeyDown (1)
  59.  
  60. Function Screen(x,y)
  61.     DeskX=api_GetSystemMetrics(0)
  62.         DeskY=api_GetSystemMetrics(1)
  63.         If x>DeskX Then x=DeskX
  64.     If x<64 Then x=64
  65.         If y>DeskY Then y=DeskY
  66.     If y<64 Then y=64
  67.     bits=api_GetDeviceCaps(api_GetDC( api_GetDesktopWindow()),12)
  68.         Graphics x,y,bits,2
  69.         Graphics x,y,bits,3
  70.         api_MoveWindow(api_GetActiveWindow(),0,0,DeskX,DeskY,True)
  71.  
  72. ; User32.decls 
  73. ;;.lib "user32.dll"
  74. ;api_GetSystemMetrics% (nIndex%) : "GetSystemMetrics"
  75. ;api_GetActiveWindow%():"GetActiveWindow"
  76. ;api_GetDC% (hwnd%) : "GetDC"
  77. ;api_GetDesktopWindow% () : "GetDesktopWindow"
  78. ;api_MoveWindow% (hwnd%, x%, y%, nWidth%, nHeight%, bRepaint%) : "MoveWindow"
  79. ;
  80. ; GDI32.decls
  81. ;.lib "gdi32.dll"
  82. ;api_GetDeviceCaps% (hdc%, nIndex%) : "GetDeviceCaps"
  83.  
  84. End Function
  85.  
  86. Function Time_Sec(x)
  87. ;Global Timer_s=MilliSecs() ;<---Put at the start of your program---
  88. ; x can be -1<,0 or >0
  89. ;- numbers returns seconds passed since last function call with 0
  90. ;0 resets the timer
  91. ;above 0 sets a countdown timer in seconds
  92.         If x>0
  93.                 Return x-Int((MilliSecs()-Timer_s)*0.001)
  94.         ElseIf x=0
  95.                 Timer_s=MilliSecs()
  96.     Else
  97.                 Return Int((MilliSecs()-Timer_s)*0.001)
  98.         EndIf
  99. End Function
  100.  
  101. Function Time_mSec(x)
  102. ;Global Timer_m=MilliSecs() ;<---Put at the start of your program---
  103. ; x can be -1<,0 or >0
  104. ;- numbers returns miliseconds passed since last function call with 0
  105. ;0 resets the timer
  106. ;above 0 sets a countdown timer in seconds
  107.        
  108.         If x>0
  109.             y1=MilliSecs()-Timer_m
  110.                 If Len(y1)>2
  111.                         y2=Left$(y1,Len(y1)-2)
  112.                         Return x-Int(y2)
  113.                 Else
  114.                         Return x
  115.                 EndIf
  116.         ElseIf x=0
  117.                 Timer_m=MilliSecs()
  118.     Else
  119.             y1=Left$(MilliSecs(),7)
  120.                 y2=Left$(Timer_m,7)
  121.                 Return Int(y1)-Int(y2)
  122.         EndIf
  123. End Function


Comments :


dna(Posted 1+ years ago)

 Great job here but I never found out how the decls processes work or how to install code for that feature.Can you point me to information that will instruct me?


Dan(Posted 1+ years ago)

 The Decls in here are only for the Screen function.timer_sec and timer_msec functions does not need it.You can, remove the whole Screen function from the code and the call from the beginning. (it acts like magnifying glass, displaying low resolution (320,250) in maximized window (here 1680,1050)The screen function gets the Desktop Resolution (Width,Height and Depth) and thenCalls blitz basic Graphic function with theese parameters:Graphic Width,Height,Desktop ColorDepth,2  Graphic Width,Height,Desktop ColorDepth,3 < makes the window resizeableThen it uses the Api call to Set the Window to Desktop Resolutionand moves it to 0,0 with api_MoveWindow(api_GetActiveWindow(),0,0,DeskX,DeskY,True)For the Userlibs open Notepad or the Text editor you are usingandpaste these lines:
Code: [Select]
; User32.decls  <------- this is the filename you can use.
;;.lib "user32.dll"
;api_GetSystemMetrics% (nIndex%) : "GetSystemMetrics"
;api_GetActiveWindow%():"GetActiveWindow"
;api_GetDC% (hwnd%) : "GetDC"
;api_GetDesktopWindow% () : "GetDesktopWindow"
;api_MoveWindow% (hwnd%, x%, y%, nWidth%, nHeight%, bRepaint%) : "MoveWindow"
Remove the Semicolons at the beginning, so that it looks like this:
Code: [Select]
.lib "user32.dll"
api_GetSystemMetrics% (nIndex%) : "GetSystemMetrics"
api_GetActiveWindow%():"GetActiveWindow"
api_GetDC% (hwnd%) : "GetDC"
api_GetDesktopWindow% () : "GetDesktopWindow"
api_MoveWindow% (hwnd%, x%, y%, nWidth%, nHeight%, bRepaint%) : "MoveWindow"
Save this as User32.decls in blizbasicuserlibs folder (eg.my blitz3d is installed in c:b3d so the userlibs folder here is C:BB3Duserlibs"Do the same for
Code: [Select]
.lib "gdi32.dll"
api_GetDeviceCaps% (hdc%, nIndex%) : "GetDeviceCaps"
save it as GDI32.declsOr if you have the Decls files in your userlibs folder, you can check if the functions are allready in thereand add it if not.(im using the api_ before the function call so i can easily see, in the code, that it is userlibs function call.)Your Lib may have the name without Api_ eg.: GetSystemMetrics% (nIndex%) : "GetSystemMetrics"Duplicate functions will crash your BlitzBasic IDE, or you wont be able to Compile the program if you are using Ideal.(blitz will report a duplicate identifier) There are few (more or less)complete Api call functions in the code archives/userlibs section <a href="../comments/editcomment6308.html?comment=8130" target="_blank"> >click< [/url]<a href="codearcs3907-2.html?code=1181" target="_blank"> gdi32 [/url]<a href="codearcs2550-2.html?code=1180" target="_blank"> kernel32 [/url]<a href="codearcsbcfe.html?code=1179" target="_blank"> User32 [/url]


Dan(Posted 1+ years ago)

 update to the screen function:Usage Screen (height,width,full)If full is set to -1, the Graphic window will be maximized (up/down scaled to desktop resolution) it will be a Borderless window without titlebar.If full is set to 0 or higher than 5, the window will be maximized to the Desktop resolution, with titlebar.If full is set between 1 and 5 the original height and width will be multiplied by the number (up to the destop resolution).
Code: [Select]
Function Screen(x,y,full=0)
;full <0 = Fulldesktop, borderless window
;full =0 = Fulldesktop
;full =1 = original x,y
;full >1-5 = x*full,y*full size
;full >5 = 0
DeskX=api_GetSystemMetrics(0)
DeskY=api_GetSystemMetrics(1)
If x>DeskX Then x=DeskX
    If x<64 Then x=64
If y>DeskY Then y=DeskY
    If y<64 Then y=64
    bits=api_GetDeviceCaps(api_GetDC( api_GetDesktopWindow()),12)
Graphics x,y,bits,6
Graphics x,y,bits,7
If full<=0 Or full>5
If full<0 Then api_SetWindowLong(api_GetActiveWindow(), -16, $10000000)
api_MoveWindow(api_GetActiveWindow(),0,0,DeskX,DeskY,True)
    EndIf
If full>1 And full<=5 Then api_MoveWindow(api_GetActiveWindow(),0,0,x*full,y*full,True)
End Function
and the missing user32.decls entry
Code: [Select]
api_MoveWindow% (hwnd%, x%, y%, nWidth%, nHeight%, bRepaint%) : "MoveWindow"



Guy Fawkes(Posted 1+ years ago)

 Here's a fix for that GOD FORSAKEN WINDOW BLINKING ><WindowCode.bb:
Code: [Select]
Screen ( 800, 600, 2 )

Global Timer_s=MilliSecs() ;<---Put at the start of your program---
Global Timer_m=MilliSecs() ;<---Put at the start of your program---

Repeat

Cls

blink=Time_mSec(-1) Mod 80

If blink>0 And blink <40
   Color $0,$25,$ff
   Rect 0,100,160,15,1
Else
  Color $ff,$0,$ff
  Rect 0,114,160,15,1
EndIf

Color $ff,$ff,$ff
Text 1,1,"Left Mousebutton resets Timer_Sec"
Text 1,140,"Right Mousebutton resets Timer_mSec"
Text 1,28,Time_Sec(25)+" = Time_Sec(25)"
Text 1,44,Time_Sec(-1)+" = Time_Sec(-1)"
Text 1,70,"This Program ends in 0:"+Time_Sec(50)
Text 1,100,Time_mSec(800)+" = Time_mSec(800)"
Text 1,114,Time_mSec(-1)+  " = Time_mSec(-1)"
If Time_Sec(50)=<0 Then End

If MouseDown(1)
Time_Sec(0)
EndIf

If MouseDown(2)
Time_mSec(0)
EndIf

Delay 1

Flip

Until KeyDown (1)

End

Function Screen(x,y,full=0)
;full <0 = Fulldesktop, borderless window
;full =0 = Fulldesktop
;full =1 = original x,y
;full >1-5 = x*full,y*full size
;full >5 = 0
DeskX=api_GetSystemMetrics(0)
DeskY=api_GetSystemMetrics(1)
If x>DeskX Then x=DeskX
    If x<64 Then x=64
If y>DeskY Then y=DeskY
    If y<64 Then y=64
    bits=api_GetDeviceCaps(api_GetDC( api_GetDesktopWindow()),12)
Graphics x,y,bits,6
Graphics x,y,bits,7
If full<=0 Or full>5
If full<0 Then api_SetWindowLong(api_GetActiveWindow(), -16, $10000000)
api_MoveWindow(api_GetActiveWindow(),0,0,DeskX,DeskY,True)
    EndIf
If full>1 And full<=5 Then api_MoveWindow(api_GetActiveWindow(),0,0,x*full,y*full,True)
End Function

Function Time_Sec(x)
;Global Timer_s=MilliSecs() ;<---Put at the start of your program---
; x can be -1<,0 or >0
;- numbers returns seconds passed since last function call with 0
;0 resets the timer
;above 0 sets a countdown timer in seconds
If x>0
Return x-Int((MilliSecs()-Timer_s)*0.001)
ElseIf x=0
Timer_s=MilliSecs()
    Else
Return Int((MilliSecs()-Timer_s)*0.001)
EndIf
End Function

Function Time_mSec(x)
;Global Timer_m=MilliSecs() ;<---Put at the start of your program---
; x can be -1<,0 or >0
;- numbers returns miliseconds passed since last function call with 0
;0 resets the timer
;above 0 sets a countdown timer in seconds

If x>0
   y1=MilliSecs()-Timer_m
If Len(y1)>2
y2=Left$(y1,Len(y1)-2)
Return x-Int(y2)
Else
Return x
EndIf
ElseIf x=0
Timer_m=MilliSecs()
    Else
   y1=Left$(MilliSecs(),7)
y2=Left$(Timer_m,7)
Return Int(y1)-Int(y2)
EndIf
End Function
~GF


Dan(Posted 1+ years ago)

 Window is blinking ? Where ? How ?if you mean the blue and pink bars, then comment or remove following lines :
Code: [Select]
blink=Time_mSec(-1) Mod 80

If blink>0 And blink <40
   Color $0,$25,$ff
   Rect 0,100,160,15,1
Else
  Color $ff,$0,$ff
  Rect 0,114,160,15,1
EndIf
p.s. God does not forsake ... its impossibile !


Guy Fawkes(Posted 1+ years ago)

 i meant the WHOLE screen. the WHOLE damn screen was blinking... uncontrollably. And don't you EVEN think of going religious on me. I have no religion.


Dan(Posted 1+ years ago)

 Sorry, i have no religion as well. I do not confuse God with Religion. When i write something about God, then it is a Reminder for Myself, not for you, you can accept it or reject it, it really doesnt matter.So back on topic, it is strange, here the screen is not blinking.Is the blinking caused by the low resolution ? Is it blinking only when the Graphic window is set to 320,200 or lower ?Is it still blinking, if you change the code as follow:
Code: [Select]
SetBuffer BackBuffer()

Repeat


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal