Posted: 21st Jul 2017 15:06
Interrupt Timer - by Code Maker 2017

This program can periodic execute code without any calls from the
main loop

I am sure there is many applications opportunities for an interrupt
timer like this.


Notes.

1.
You sould try to keep the timer delay above 10 milliseconds or
else it will overload your system, to quote MS API SDK help files
"Periodic timer events with an event delay of 10 milliseconds or less
consume a significant portion of CPU resources."

2.
The interrupt timer will bee killed if the window to which it has been
associated is closed.


+ Code Snippet
`Interrupt Timer - by Code Maker 2017

`this code needs the Matrix1 plugin to compile

`Note 1.
`You sould try to keep the timer delay above 10 milliseconds or
`else it will overload your system, to quote MS API SDK help files
`"Periodic timer events with an event delay of 10 milliseconds or less
`consume a significant portion of CPU resources."

`Note 2.
`The interrupt timer will bee killed if the window to which it has been
`associated is closed.
 
init:
	global counter
	global ittext$
	
	sync on
	sync rate 60
	backdrop on
	
	user32DLL = 1
	timerdelay = 1000	`timer delay in milliseconds
	x = screen width()
	y = screen height()+50
	text$ = "mainloop is dooing this..."
	set window title "Interrupt Timer by Code Maker 2017"

	`load user 32 bit dll
	load dll "user32.dll", user32DLL

	`get window handle, identifies the window to be associated with the timer
	WindowHWND = call dll(user32DLL, "GetActiveWindow")
	`get pointer to function
	InterruptTimerPTR = get ptr to function("InterruptTimer")
	`make timer, it starts immediately
	TimerID = call dll(user32DLL, "SetTimer", WindowHWND, 1, timerdelay, InterruptTimerPTR)

	`main loop
	do
		dec x
		dec y
		if x<-text width(text$) then x=screen width()
		if y<-text height(text$) then y=screen height()+15
		if inkey$()<>"" then exit
		sync
		text x, 200, text$
		text screen width()/2-text width(ittext$)/2, y, ittext$
	loop

	`clean up before exit
	s = call dll(user32DLL, "KillTimer", WindowHWND, TimerID)
	delete dll user32DLL
	`exit program
	end

`function, this is what the interrupt timer will do
function InterruptTimer()
	inc counter
	ittext$ = "interrupt timer is doing this, without any calls from the mainloop " + str$(counter)
endfunction
Posted: 28th Jul 2017 7:33
Looks cool

What's that get ptr to function,

+ Code Snippet
InterruptTimerPTR = get ptr to function("InterruptTimer")


I didn't know that DBP had such a command.
Posted: 28th Jul 2017 16:46
That command is part off the Matrix1 plugin, you will need it to run the program
wath it basically dose is to get the memory address of the InterruptTimer
function in the program, also known in programming terms as a pointer,
so it can execute the function by calling that address periodically
Posted: 13th Sep 2017 14:16
That is pretty cool. It has been a long time since I have used an interrupt routine. Back in the day I used to program my AtariST in Assembly language and I used to play around with interrupt routines. Nice job.