Page 1 of 1

How to wait a second?

Posted: June 6th, 2013, 1:44 am
by earlyProg
PLEASE HELP ME! :crying

Re: How to wait a second?

Posted: June 6th, 2013, 1:23 pm
by t0rxe
You can make a for loop and use VSync(0) for example to make a timer.
VSync(x) is defined in the Psy-Q manual, where 'x' is the systems definitive.

You will need to do some basic math to figure out the amount of time needed to make a second.
In my example below, 10 is just a number I randomly chose, and so may be to high or to little...

EG:

Code: Select all

void Timer()
{
  int i;

  for (i=0; i<10; i++) VSync(0);
}

Re: How to wait a second?

Posted: June 6th, 2013, 10:49 pm
by DEBRO
Hi there,

The Kernel section of the Run-Time Library 4.4 document makes reference to a System Clock. Read the section on Root Counter Control. This may help you.

You could also write a VSyncCallback that increments a frame counter every VSync. You could then check the value to see if 60 frames (for NTSC) or 50 frames (for PAL) have passed.

The question doesn't give a lot of information about what you are really trying to do so I'm not sure which technique would work better for you. I haven't done either on the PlayStation so I can't supply a sample. I have done the latter for other platforms (i.e. Atari2600, Atari5200, Atari7800, Atari 8-bits computers) and it works.

For instance, if you were trying to check when a second has passed after pressing a button, you could set a counter to 0 when the button is pressed (also keeping track of when the button is released so the counter gets reset) then for each VsyncCallback increment the counter. In your loop or program you would then check if the appropriate time has elapsed (i.e. (counter % 60) == 0 for NTSC or (counter % 50) == 0 for PAL).

I hope this helps in solving your question. Please share your final solution with the forum. I'm sure it will help others as well.

Re: How to wait a second?

Posted: June 7th, 2013, 1:50 am
by inc^lightforce
AGREE :) thats more than enough .
use this code
t0rxe wrote:

Code: Select all

void Timer()
{
  int i;

  for (i=0; i<10; i++) VSync(0); 
}
if you wanna wait more than 1 sec. you can change the value "i<10;" to 60 or higher

Re: How to wait a second?

Posted: June 7th, 2013, 12:16 pm
by DEBRO
Hi there,
inc^lightforce wrote:AGREE :) thats more than enough .
use this code
t0rxe wrote:

Code: Select all

void Timer()
{
  int i;

  for (i=0; i<10; i++) VSync(0); 
}
if you wanna wait more than 1 sec. you can change the value "i<10;" to 60 or higher
I'm still learning this system and would like to understand how this code waits a second.

If my understanding of this code is correct it will call VSync 11 times. VSync(0) waits for the next VSync to execute. For NTSC machines this is every 1/60 of a second and for PAL systems every 1/50 of a second. If VSync is called 11 times then wouldn't that be 0.183 seconds for NTSC and 0.22 seconds for PAL? Is that correct?

Also, this assumes no other callbacks are happening during this tight loop. If anything (though not likely) is done while the the loop is executing then isn't it possible for a VSync to happen before the VSync(0) call? This may not matter...I don't know the context of what the original asker is planning. Maybe an exact second pause is not needed. Maybe an approximation is okay.

Again, I'm learning this system and trying to better understand. Thanks.

Re: How to wait a second?

Posted: June 7th, 2013, 8:47 pm
by t0rxe
You have the right idea :ugeek:

Re: How to wait a second?

Posted: June 24th, 2013, 11:10 pm
by bizarro2011
I thought of this idea. (not tested)
try this.

void Timer(int n)
{
int i;

for (i=0; i<n; i++) VSync(0);
}

with it.

int segundos;

double n = ((double)segundos)/60.;

Re: How to wait a second?

Posted: January 4th, 2016, 11:19 pm
by Administrator
Another way is to just do 'VSync(60)'. Depending on your setup video mode and PSX (since VSync will either use 50Hz or 60Hz), this is one way to do a delay.

The most accurate way is to use the free-running root counter and use some math to generate a seconds counter. This is how Bust A Groove does it with their dance system so it's always in sync.