| View previous topic :: View next topic |
| Author |
Message |
Valohtar
Joined: 02 Jul 2005 Posts: 13
|
Posted: Sat Jul 16, 2005 10:11 am Post subject: Looping with exact timing? |
|
|
Sorry if this question sounds a bit trivial, but I haven't been able to figure it out. I want to create a loop that will loop at a constant speed, taking into account that the functions within the loop will take up some of the time.
I want something like:
| Code: |
while(1) {
//loop exactly 1 second
dosomething();
}
|
Could someone please give an example? It should be accurate to the millisecond if possible, but any starting point will help. I've taken a look at sceKernelLibcTime (time_t *t) and sceKernelLibcClock (void), but I'm not sure how they work.
[EDIT] I just saw clock(), so I'll take a look at that.
| Code: | start = clock();
while (1) {
end = clock();
if ((((double) (end - start)) / CLOCKS_PER_SEC * 1000) > millisperbeat) {
dosomething();
start = clock();
}
} |
Can anyone see a problem in this? It doesn't seem to work for me. Also, is this the best route to take? I'm developing a rhythm-type game.
Thanks. |
|
| Back to top |
|
 |
maddogjt1
Joined: 27 Jun 2005 Posts: 13
|
Posted: Sat Jul 16, 2005 11:31 am Post subject: |
|
|
| I think what you're looking for is sceKernelSleepThread(uint usec) which is accurate to the micro-second i believe |
|
| Back to top |
|
 |
Valohtar
Joined: 02 Jul 2005 Posts: 13
|
Posted: Sat Jul 16, 2005 11:35 am Post subject: |
|
|
| I tried that =/. It worked fine, but the only problem is that I'm not exactly sure how long the doSomething() method takes to execute. Also, it may not have the same timing everytime. So I end up with 1 second plus a little tiny amount, but in a rhythm game, it adds up. I need something that can take into account the execution time of the method. |
|
| Back to top |
|
 |
Agoln

Joined: 08 Jun 2005 Posts: 326 Location: Fort Wayne, IN
|
Posted: Sat Jul 16, 2005 3:18 pm Post subject: |
|
|
Do this, call the function that tells you the current time, save the value.
Call your method that needs to run, then call the time again. Subtract times, to see how long you need to sleep for.
I know that pseudo code is always easier than real code, and I know that there are functions for getting time, and sleeping, so it's just up to good ole mr. search to find them. :) _________________ Lego of my Ago! |
|
| Back to top |
|
 |
Valohtar
Joined: 02 Jul 2005 Posts: 13
|
Posted: Sat Jul 16, 2005 3:34 pm Post subject: |
|
|
| I see where you are coming from. That sounds great, thanks. |
|
| Back to top |
|
 |
intai
Joined: 20 Jul 2005 Posts: 1
|
Posted: Wed Jul 20, 2005 12:20 pm Post subject: clock() |
|
|
How come I always get zero from clock() ?
Is there anyone can help me with that ?
Thanks very much. |
|
| Back to top |
|
 |
dila
Joined: 16 Oct 2005 Posts: 1 Location: England
|
Posted: Sun Oct 16, 2005 11:19 am Post subject: |
|
|
| maddogjt1 wrote: | | I think what you're looking for is sceKernelSleepThread(uint usec) which is accurate to the micro-second i believe |
Perhaps you meant sceKernelDelayThread(uint usec) ? |
|
| Back to top |
|
 |
Paco
Joined: 09 Oct 2005 Posts: 54
|
Posted: Sun Oct 16, 2005 12:03 pm Post subject: |
|
|
I just fixed some weird bugs in our codebase, which uses sceRtcGetCurrentTick() and sceRtcGetTickResolution() for timing. sceRtcGetCurrentTick() seems to return really huge values, which may be a problem if you are not careful. Bottom line: ALWAYS get sceRtcGetCurrentTick() at the start of your app, and subtract that value from any further calls you make to that function before you do any processing, or you'll likely run into overflows and precision issues. _________________ Paco |
|
| Back to top |
|
 |
sherpya

Joined: 03 Oct 2005 Posts: 61
|
Posted: Sun Oct 16, 2005 1:28 pm Post subject: |
|
|
| :) a ms Windows GetTickCount() "emulation" ? |
|
| Back to top |
|
 |
Fanjita
Joined: 28 Sep 2005 Posts: 217
|
Posted: Sun Oct 16, 2005 10:30 pm Post subject: |
|
|
| If the execution loop is short, and you don't care about the actual value of the constant time you want to achieve, then you might find a simpler solution would be to use the waitForVblank functions - these should give you a pretty accurate 1/60 sec period, if that's useful. |
|
| Back to top |
|
 |
|