Looping with exact timing?

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Valohtar
Posts: 13
Joined: Sat Jul 02, 2005 4:28 am

Looping with exact timing?

Post by Valohtar »

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: Select all

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: Select all

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.
maddogjt1
Posts: 13
Joined: Mon Jun 27, 2005 3:06 pm

Post by maddogjt1 »

I think what you're looking for is sceKernelSleepThread(uint usec) which is accurate to the micro-second i believe
Valohtar
Posts: 13
Joined: Sat Jul 02, 2005 4:28 am

Post by Valohtar »

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.
User avatar
Agoln
Posts: 326
Joined: Wed Jun 08, 2005 3:14 am
Location: Fort Wayne, IN

Post by Agoln »

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!
Valohtar
Posts: 13
Joined: Sat Jul 02, 2005 4:28 am

Post by Valohtar »

I see where you are coming from. That sounds great, thanks.
intai
Posts: 1
Joined: Wed Jul 20, 2005 12:15 pm

clock()

Post by intai »

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

Thanks very much.
dila
Posts: 1
Joined: Sun Oct 16, 2005 10:52 am
Location: England
Contact:

Post by dila »

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) ?
Paco
Posts: 54
Joined: Sun Oct 09, 2005 6:53 pm

Post by Paco »

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
User avatar
sherpya
Posts: 61
Joined: Mon Oct 03, 2005 5:49 pm

Post by sherpya »

:) a ms Windows GetTickCount() "emulation" ?
Fanjita
Posts: 217
Joined: Wed Sep 28, 2005 9:31 am

Post by Fanjita »

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.
Post Reply