Problem with Timer

SGDK only sub forum

Moderator: Stef

Post Reply
Tatchou
Interested
Posts: 12
Joined: Sun Feb 23, 2014 10:49 pm

Problem with Timer

Post 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.
Chilly Willy
Very interested
Posts: 2993
Joined: Fri Aug 17, 2007 9:33 pm

Post 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.
Tatchou
Interested
Posts: 12
Joined: Sun Feb 23, 2014 10:49 pm

Post by Tatchou »

Hum okay thank you !
But I don't see how can I do a timer with sgdk have you an idea ? =/
Chilly Willy
Very interested
Posts: 2993
Joined: Fri Aug 17, 2007 9:33 pm

Post 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.
Tatchou
Interested
Posts: 12
Joined: Sun Feb 23, 2014 10:49 pm

Post by Tatchou »

Ok thank you :) I'll test that !
Post Reply