03 November 2022, 18:57 | #1 |
Registered User
Join Date: May 2001
Location: ?
Posts: 19,645
|
Timed events?
I am trying to repeat some routine X times per time unit and I would like to know what the best approach is.
Let's say I have a routine that increases a number +1 every time it is called, and I want to call it 60 times per minute. With this I mean, take a minute, divide it by 60 evenly and execute this function each time. It should add +12 to the variable once every second. Any ideas? Is there a timer I can work with, do I create my own>? I never created timed events like this in any other language let alone on the Amiga, any help appreciated. |
03 November 2022, 21:24 | #2 |
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
|
There are a few approaches you can take, depending on the precision you need. One of the simplest ways of doing it is to set up an interrupt to count video frames. This will give you a variable that increases every frame, so either 50 or 60 times per second, and you can read that variable to know when a given number of frames have passed. You can use the NTSC function to determine whether the program is running on a pal or NTSC screenmode. The downside of this approach is that it won't work well when you run a non-standard screenmode, or have other screens open with different vertical frequencies and switch between them.
After that, there are OS calls you can use for timing or you can bang the CIA hardware if you don't care about being system-friendly. AmiBlitz 3 provides a couple of very nice includes that use these timing functions and provide an easy interface for Blitz programs to use - eclock.include allows timing via the OS timer.device and ciatimer.include allows hardware-banging direct access to the CIAs for timing. Blitz 2 doesn't include such luxuries unfortunately, so you're kinda limited to the first option or manually invoking the timer.device functions. It might be possible to backport the AB3 timer includes to suit if you really needed. Edit: Here's a quick little demonstration of the first method: Code:
framecount.w = 0 If NTSC framemax.w = 60 Else framemax.w = 50 End If SetInt 5 framecount + 1 End SetInt mycounter.w = 0 Repeat Delay_ 1 ; Do whatever you need to do here. Delay_ stops it from busy-looping If framecount >= framemax framecount - framemax mycounter + 12 NPrint mycounter End If Until Joyb(0) End Last edited by Daedalus; 03 November 2022 at 21:38. Reason: Added code |
05 November 2022, 00:58 | #3 | |
Registered User
Join Date: May 2001
Location: ?
Posts: 19,645
|
Quote:
Banging the hardware is OK, I already am doing it. But it probably is better to poke at the CIA because I don't want to deal with 50/60 Hz shenanigans, although I am pretty sure (untested) that my program runs only in PAL. So what should I do with the CIA then? |
|
05 November 2022, 14:07 | #4 |
Registered User
Join Date: Jan 2022
Location: Wales
Posts: 91
|
I'm using Blitz2 for my game which has a timer that counts down to zero on which you lose a life. Kind of the reverse of you adding 1 every time a routine is called and then adding 12 to a variable.
To be honest, I'm not even checking for NTSC... As Daedalus pointed out, your best bet is to setup an interrupt (SetInt 5) to do the timing for counting every second that goes by - that command runs on interrupt and stops for no man. In your main program loop, you then check to see if the interrupt has reached 0 and if so, call your routine to what you need. In my setup code if have something like this to count every second that goes by... Code:
SetInt 5 tminus=tminus-1 If tminus=0 tminus=59 EndIf EndInt In your case, your main loop would check for 'tminus' being zero, call your routine and add 12. Unless I've misunderstood your original question and I'm making an idiot of myself by trying to help out. If so, I'll shut-up! Last edited by aNdy/AL/COS; 05 November 2022 at 14:08. Reason: odd formatting |
05 November 2022, 17:07 | #5 | |
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
|
Quote:
For a more fuller-featured and easy to use set of functions, have a look at the AmiBlitz 3 CIA Timer include. It uses some AmiBlitz 3 features, but it looks like it could be relatively simple to adapt for Blitz 2 use. |
|
08 November 2022, 09:43 | #6 |
Banana
Join Date: Jul 2016
Location: Darmstadt
Posts: 1,213
|
You could also use the 'ticks' command to get a system timer and subtract a previous value. It's 50Hz on PAL. Then add that many counts to your counter.
Something like: lastTicks = Ticks (do a thing) tickDiff=Ticks - lastTicks myCounter += tickDiff * countPerTick lastTicks += ticksDiff Basically similar to the interrupt approach but without needing an interrupt. Set countPerTick as needed to scale 50Hz to whatever you're counting |
08 November 2022, 17:29 | #7 |
Registered User
Join Date: May 2001
Location: ?
Posts: 19,645
|
OK I thank you all for your input, seems like method 1 is the easiest.
Why set an interrupt though, I have this main loop with VWait which happens every frame, wouldn't changing the variable at every frame yield the same results? |
08 November 2022, 18:25 | #8 |
Registered User
Join Date: Jun 2009
Location: Dublin, then Glasgow
Posts: 6,334
|
Yes, unless something happens that takes more than a frame to execute, in which case your timing will start to slow down. If you know your main loop will never take as long as a frame, even on the slowest machine or with some heavy multitasking going on, then you don't need the interrupts.
|
09 November 2022, 16:30 | #9 | |
Registered User
Join Date: May 2001
Location: ?
Posts: 19,645
|
Quote:
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
mouse wheel events | meynaf | Coders. System | 1 | 26 August 2018 16:06 |
Adelaide Events! | amigaman101 | News | 0 | 15 October 2015 11:07 |
Custom input events | CFou! | support.WinUAE | 15 | 20 May 2012 17:54 |
timed wait using CIAs | jotd | support.Other | 3 | 23 March 2008 14:55 |
Events: ComputerParty @ SyntaxSociety | Paul | News | 0 | 15 October 2004 16:56 |
|
|