Page 1 of 1

Problem with Timer

Posted: Sat Jul 26, 2014 3:15 pm
by Tatchou
Hey everyone, I have a little problem. I don't understand how the timers in sgdk work...

I start a timer with that : startTimer(0);
I wish restart this timer so I do that : getTimer(0,1);

I see that the timer stop and it never restart.

This is the code of "getTimer" :

Code: Select all

u32 getTimer(u16 numTimer, u16 restart)
{
    const u32 t = getSubTick();
    u32* time = &timer[numTimer & (MAXTIMER - 1)];
    const u32 res = t - *time;

    if (restart) *time = t;

    return res;
}
This function allow me to restart a timer no ?
Maybe I didn't understand something.. =\

Thank you.

Posted: Sat Jul 26, 2014 5:41 pm
by Chilly Willy
A "timer" is just an array of variables to keep track of elapsed time. You call startTimer(timerNumber) to set the start time, do some stuff, then call getTimer(timerNumber, restart) to see how much time elapsed. The restart flag tells it to do startTimer(timerNumber) automatically right before returning the elapsed time so that if you need to keep measuring the elapsed time, it saves you the trouble of doing another startTimer() call. Also, by doing that inside getTimer(), the next elapsed time result should be more accurate.

These are not timers as people tend to think of them in PCs - they don't generate ints or count periods of time. They're strictly for measuring how many subticks occur between calls to startTimer/getTimer.

Posted: Sun Jul 27, 2014 5:33 pm
by Tatchou
Hum okay thank you !
But I don't see how can I do a timer with sgdk have you an idea ? =/

Posted: Sun Jul 27, 2014 9:29 pm
by Chilly Willy
Your best bet is to use the vblank to decrement a variable, and when it hits zero, do what you need. If you need higher resolution than the vblank, you'd need to complicate things by using HINTs. For example, if you set the VDP to generate an HINT every line, that would give a resolution of one scan line... about 64 us. The FM chip has two timers in it, but they didn't connect the INT line to anything, so they're not very useful. :(

That's one of the GOOD things about the SegaCD - it has a timer that you can use. I use it in my SCD demos for music timing to allow for different BPM than the standard.

Posted: Mon Jul 28, 2014 10:05 am
by Tatchou
Ok thank you :) I'll test that !