forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Looping with exact timing?

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
Valohtar



Joined: 02 Jul 2005
Posts: 13

PostPosted: Sat Jul 16, 2005 10:11 am    Post subject: Looping with exact timing? Reply with quote

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
View user's profile Send private message
maddogjt1



Joined: 27 Jun 2005
Posts: 13

PostPosted: Sat Jul 16, 2005 11:31 am    Post subject: Reply with quote

I think what you're looking for is sceKernelSleepThread(uint usec) which is accurate to the micro-second i believe
Back to top
View user's profile Send private message
Valohtar



Joined: 02 Jul 2005
Posts: 13

PostPosted: Sat Jul 16, 2005 11:35 am    Post subject: Reply with quote

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
View user's profile Send private message
Agoln



Joined: 08 Jun 2005
Posts: 326
Location: Fort Wayne, IN

PostPosted: Sat Jul 16, 2005 3:18 pm    Post subject: Reply with quote

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
View user's profile Send private message AIM Address
Valohtar



Joined: 02 Jul 2005
Posts: 13

PostPosted: Sat Jul 16, 2005 3:34 pm    Post subject: Reply with quote

I see where you are coming from. That sounds great, thanks.
Back to top
View user's profile Send private message
intai



Joined: 20 Jul 2005
Posts: 1

PostPosted: Wed Jul 20, 2005 12:20 pm    Post subject: clock() Reply with quote

How come I always get zero from clock() ?
Is there anyone can help me with that ?

Thanks very much.
Back to top
View user's profile Send private message
dila



Joined: 16 Oct 2005
Posts: 1
Location: England

PostPosted: Sun Oct 16, 2005 11:19 am    Post subject: Reply with quote

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
View user's profile Send private message Visit poster's website
Paco



Joined: 09 Oct 2005
Posts: 54

PostPosted: Sun Oct 16, 2005 12:03 pm    Post subject: Reply with quote

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
View user's profile Send private message
sherpya



Joined: 03 Oct 2005
Posts: 61

PostPosted: Sun Oct 16, 2005 1:28 pm    Post subject: Reply with quote

:) a ms Windows GetTickCount() "emulation" ?
Back to top
View user's profile Send private message
Fanjita



Joined: 28 Sep 2005
Posts: 217

PostPosted: Sun Oct 16, 2005 10:30 pm    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group