Code archives/Miscellaneous/3 functions Timer_Sec Timer_mSec and Screen

This code has been declared by its author to be Public Domain code.

Download source code

3 functions Timer_Sec Timer_mSec and Screen by Dan2015
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)
;====================================================================
;====================================================================
; 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)
;====================================================================

;Screen(320,250)
;
;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
;Until KeyDown (1)
 
Function Screen(x,y)
    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,2
	Graphics x,y,bits,3
	api_MoveWindow(api_GetActiveWindow(),0,0,DeskX,DeskY,True)

; User32.decls	
;;.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"
;
; GDI32.decls
;.lib "gdi32.dll"
;api_GetDeviceCaps% (hdc%, nIndex%) : "GetDeviceCaps"

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

Comments

dna2015
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?


Dan2015
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 then
Calls blitz basic Graphic function with theese parameters:

Graphic Width,Height,Desktop ColorDepth,2
Graphic Width,Height,Desktop ColorDepth,3 < makes the window resizeable

Then it uses the Api call to Set the Window to Desktop Resolution
and 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 using
and
paste these lines:
; 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:

.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 blizbasic\userlibs\ folder

(eg.my blitz3d is installed in c:\bb3d\ so the userlibs folder here is C:\BB3D\userlibs\"

Do the same for

.lib "gdi32.dll"
api_GetDeviceCaps% (hdc%, nIndex%) : "GetDeviceCaps"


save it as GDI32.decls

Or if you have the Decls files in your userlibs folder, you can check if the functions are allready in there
and 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 >click<

gdi32
kernel32
User32


Dan2016
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).

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

api_MoveWindow% (hwnd%, x%, y%, nWidth%, nHeight%, bRepaint%) : "MoveWindow"



Guy Fawkes2016
Here's a fix for that GOD FORSAKEN WINDOW BLINKING ><

WindowCode.bb:



~GF


Dan2016
Window is blinking ? Where ? How ?

if you mean the blue and pink bars, then comment or remove following lines :

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 Fawkes2016
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.


Dan2016
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:

SetBuffer BackBuffer()

Repeat



Code Archives Forum