Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Program Announcements / DBP Timer Plugin ( timer based movement and counters)

Author
Message
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 7th Jan 2010 07:06 Edited at: 1st May 2018 20:18
Hey guys,

I have just finished a DarkBASIC Pro plugin that provides two simple timing systems. which are:

A simple Timer Based Movement System that consists of 3 easy commands.

A simple Counter system. You can create counters (up to 255 at any one time), kill counters, count ups, count downs, get/set the counters value and more.

This also comes with help files that explain how each command works.

Heres a list of the commands.


EDIT May 2018: Download is attached to this post.

Attachments

Login to view attachments
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 7th Jan 2010 17:18
Time Based Movement is based on maintaining a specific frame rate.
Does your routine allow that to be set?

Math89
20
Years of Service
User Offline
Joined: 23rd Jan 2004
Location: UK
Posted: 7th Jan 2010 20:27
No, Timer Based Movement is based on having everything going at the same speed regardless of the frame rate.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 7th Jan 2010 21:32 Edited at: 7th Jan 2010 21:34
Right, but it's still keyed off of the "desired" framerate.

The most common code I've seen for TBM is..



I could be mistaken, and may not have seen code for another way to do it, but this is what I've seen passed around here for the last several years.

ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 8th Jan 2010 01:04 Edited at: 8th Jan 2010 04:39
Quote: "Time Based Movement is based on maintaining a specific frame rate.
Does your routine allow that to be set?"

No, whats wrong with sync rate?

Quote: "No, Timer Based Movement is based on having everything going at the same speed regardless of the frame rate."

Exactly what my system does.

EDIT: Here's my routine:


Feel free to give any advise on how i could optimize it or even add extra functionality.


tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 8th Jan 2010 07:14 Edited at: 8th Jan 2010 07:15
A couple suggestions.

You could optimize it a little by doing the *0.001 math when the TBM is updated. Users will do one Update but many many Moves in their code. It's probably a mere pittance of cycles, but you asked. Oh adnd Initialize could just call Update twice.

I believe you are using the same low resolution timer that DBPro uses. This timer isn't that accurate and usually jumps from 0 to 16 ms, sometimes going to 14 or 5 or some other weird number. IanM's Matrix1 Utils uses a higher resolution timer that is much better. Perhaps he can tell you what he uses? Newton wrapper uses the performance counter I mention below...

DBPro has a PERFTIMER() function (QueryPerformanceCounter) but it is (nearly) useless without its sister function, the QueryPerformanceFrequency. Even if you just made these both available for us to use, that would be helpful to a lot of people.


A 3D marble platformer using Newton physics.
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 8th Jan 2010 10:21
Quote: "You could optimize it a little by doing the *0.001 math when the TBM is updated. Users will do one Update but many many Moves in their code. It's probably a mere pittance of cycles, but you asked. Oh adnd Initialize could just call Update twice."


Thank you, I'll change them now.

Quote: "DBPro has a PERFTIMER() function (QueryPerformanceCounter) but it is (nearly) useless without its sister function, the QueryPerformanceFrequency. Even if you just made these both available for us to use, that would be helpful to a lot of people."


I've made those two functions into functions in my dll. but just so i can understand, does the function QueryPerformanceFrequency just outputs the timer in seconds?
And instead of using gettickcount, use QueryPerformanceCounter am i right or do i have to do something with the Frequency function?

Thanks for the help tiresius.


tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 8th Jan 2010 17:52
From what I understand QueryPerformanceFrequency returns the number of cycles per second and does not change so you don't need to keep calling it. QueryPerformanceCounter returns the number of cycles that have run since the PC was turned on.

To get the number of elapsed seconds you do

(NewCounter - OldCounter) / Frequency

Which will return 0.001 for a single millisecond.

I've heard * is faster than / so you can probably do some trick to pre-convert the frequency into a value you can multiply with. Anyway that's all I know!


A 3D marble platformer using Newton physics.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 8th Jan 2010 18:00
Quote: "No, whats wrong with sync rate?"


Sync rate is fine for capping the frame rate, but up until now every piece of TBM code I've seen always used the user's desired frame rate as a reference for the calculation. Your method may be better.

I'm not trying to be a pain, just trying to understand.

To be a little helpful, here's an article on how to use the high performance timer in C++.
http://cplus.about.com/od/howtodothingsi2/a/timing.htm

tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 8th Jan 2010 19:34 Edited at: 8th Jan 2010 19:35
KISTech-
The only real difference in your code is that factor# might have a different (higher?) value. Variable fps is still constant throughout the program, so why bother using it? I think in most cases a constant in an equation is a waste unless it has some real meaning.

By putting the "desired fps" into the equation, you are now bound to that, causing difficulty of changing it in the future. If you decide later that your game should run at 30 FPS instead of 60 FPS and change the fps variable to 30, now all your calculations will be messed up in the rest of your game code, anywhere that references factor#.


A 3D marble platformer using Newton physics.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 8th Jan 2010 23:52
My usage of the FPS variable really isn't relevant, except that I use it to set the sync rate AND use it in the TBM calculation. So whether FPS is set to 60, or 30, or 5 the result from the TBM calculation will be the same.

Following the logic of the code, ShaunRW's code does basically the same thing as mine, but uses a desired framerate of 1. (0.001 * 1000)

Let's test them both mathematically.
Let's say the movement speed is 10.

Your code.
Sync rate is 60, framerate is 60.
17ms * 0.001 = 0.017
Speed 10 * 0.017 = 0.17
Sync rate is 30, framerate is 30.
33ms * 0.001 = 0.033
Speed 10 * 0.033 = 0.33

My code.
Sync rate is 60, framerate is 60.
17ms / 16.667 = 1.019
Speed 10 * 1.019 = 10.19
Sync rate is 30, framerate is 30.
33ms / 33.333 = 0.990
Speed 10 * 0.990 = 9.90

Now let's say a frame takes longer, due to heavy load on a function.

Your code.
sync rate 60, framerate 25.
40ms * 0.01 = 0.040
Speed 10 * 0.040 = 0.40
sync rate 30, framerate 25.
40ms * 0.01 = 0.040
Speed 10 * 0.040 = 0.40

My code.
sync rate 60, framerate 25.
40ms / 16.667 = 2.399
Speed 10 * 2.399 = 23.99
sync rate 30, framerate 25.
40ms / 33.333 = 1.200
Speed 10 * 1.200 = 12.00

Ratio of sync rate to frame rate 60 / 25 = 2.4
Ratio of your code's answer 0.40 / 0.17 = 2.35
Ratio of my code's answer 23.99 / 10.19 = 2.35
Ratio of sync rate to frame rate 30 / 25 = 1.2
Ratio of your code's answer 0.40 / 0.33 = 1.33 <--
Ratio of my code's answer 12.0 / 9.90 = 1.21

They are giving nearly the same results. The only difference is mine taking the desired framerate into consideration returns the actual speed that was requested when the framerate is ideal, and as the framerate drops, yours loses accuracy. So yes, tying it to the desired framerate is important.

ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 9th Jan 2010 04:29 Edited at: 9th Jan 2010 04:58
@tiresius: I think i understand now, thank you.

Quote: "To be a little helpful, here's an article on how to use the high performance timer in C++."


Thanks that was a big help as well.

Here's the new UpdateTBM function. I have changed the *0.001 into the update so it's only called once per loop. I have Used the perfomance counter and frequency functions instead of GetTickCount. I think it's right, but if it isn't just say so.


And also if tying it to the desired framerate really makes in more accurate i will implement that next.

Thank guy for your help. It's greatly appreciated.


tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 9th Jan 2010 05:12
Quote: "Ratio of your code's answer 0.40 / 0.33 = 1.33 <--
"

You had me going, you really did. I'm not good with numbers so I went through everything of yours top to bottom and it looked good, until I got to one of the last lines. The result of 0.40 / 0.33 is 1.21 not 1.33 so it matches what the other ratio has, and both are equally accurate in those examples.

Like I said earlier, in my mind using a constant in an equation like (1000 / fps) really doesn't do anything to help a dynamic situation such as inconsistent fps rates in a game. It's just extra in my opinion (if it stays constant).

But what it does do if you change the fps variable to something else is modify the factor#. And unless you adjust all the rest of your speed values to compensate after doing this, it will change the speed of your game. I consider that undesirable, unless perhaps in the game code you are doing some trick like "bullet time" and need to slow everything down or speed it up?

@ShaunRW-
Doesn't *1000 and *0.001 cancel each other out?


A 3D marble platformer using Newton physics.
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 9th Jan 2010 07:02
Quote: "Doesn't *1000 and *0.001 cancel each other out?"

Haha yeah , I was focusing on the performance counter too much.

Well because if returns a float like 0.001 then i will not need to *1000 or *0.001 will I?

I'll upload the updated file when i know it works.


tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 9th Jan 2010 07:10
It all depends on how you want elasped time represented. If you just use the counter divided by frequency then you will get it in seconds. If you want milliseconds then you should *1000. Personally I like it in seconds so I will see values of 0.001 - 0.017 depending on how fast I let DBPro rip. At least that's what Newton wrapper has done so I'm used to it.


A 3D marble platformer using Newton physics.
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 9th Jan 2010 07:30
Ok i will keep it as seconds.

I'm now gonna change the TMR functions to the performance counters so they are more accurate.

I will upload the dll when i'm done.

Thank for all your help tiresius and KISTech, it's appreciated


Gunslinger
16
Years of Service
User Offline
Joined: 29th May 2007
Location:
Posted: 9th Jan 2010 17:53
when will it be?
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 9th Jan 2010 22:12
Quote: "The result of 0.40 / 0.33 is 1.21 not 1.33 so it matches"


You're right. Must have made an error with the calculator. My bad.

Quote: "But what it does do if you change the fps variable to something else is modify the factor#. And unless you adjust all the rest of your speed values to compensate after doing this, it will change the speed of your game."


Ok, it took me a bit of mulling that over to see what you mean.

The way I have it set up, over a full second my object would move 600 units at a framerate of 60, and 300 units at a framerate of 30.

With Shaun's code, over a full second it would move 10.2 units at 60 FPS, and 9.9 units at 30 FPS.

Funny how nobody has caught that in the code that all the experts have been pointing people to for the last several years. We'll have to make sure people become aware of this if they aren't already.

@ShaunRW, don't change it. It's great the way it is.

ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 10th Jan 2010 02:35 Edited at: 10th Jan 2010 02:48
@Gunslinger: I don't really know when i will be done. You can still use the first version in the mean time because none of the commands will change.

I'm actually still having trouble with the QueryPerformanceCounter and QueryPerformanceFrequency.

I'll most probably be able to say when it will be read later, so i will post then.


ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 10th Jan 2010 08:36
I have finished implementing QueryPerformanceCounter and QueryPerformanceFrequency into the Timer based movement and counter commands.

I have also added two new commands which returns the value of QueryPerformanceCounter and QueryPerformanceFrequency so you can make you own timer functions. I have not updated the help files yet, i will do that another day.

You will find the download to the updated version on my site.http://www.shaunrw.webs.com/TimerPlugin.htm

Thanks for everyones help.


KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 10th Jan 2010 23:27
Glad to have learned something new.

Thanks.

tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 11th Jan 2010 01:37
Yes Shawn nice work! Once you get the help all setup and a nice download for the whole thing, send it to BatVink for the newsletter.


A 3D marble platformer using Newton physics.
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 11th Jan 2010 04:19 Edited at: 11th Jan 2010 04:20
I have updated the help files and Keywords files and bundled it with the plugin, so you can now download the plugin with the help files.
Quote: "Yes Shawn nice work!"
Thank you.
Quote: "send it to BatVink for the newsletter."
What would the procedure be to do that? just email him the zip file and all the information?


tiresius
21
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 11th Jan 2010 15:25
Email BatVink a link to the forum post as a heads up. Maybe with a little blurb in there he can just paste into the newsletter. They have a section just for forum happenings that this could go under.


A 3D marble platformer using Newton physics.
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 11th Jan 2010 16:28
bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 9th Apr 2010 15:43
I know that I am asking too much, but would it be possible to see the final code, as I would really like to use this Timer system in DARKgdk.

It seems very simple & straightforward... just what i'm looking for.
KISTech
16
Years of Service
User Offline
Joined: 8th Feb 2008
Location: Aloha, Oregon
Posted: 9th Apr 2010 18:32
Here's what I ended up coming out of this conversation with for DBPro.



They originally had factor# = Diff# / 1000.0 but that was making things to slow for the speeds I was setting, so rather than rewrite all my speed calculations I just changed the multiplier here. It seems to work fine.

ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 10th Apr 2010 08:17 Edited at: 16th May 2010 03:44
Well here is the Project files for it. I didn't comment it because i didn't think of releasing it, but it should be fairly easy to understand anyway. But you have any troubles just ask.

bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 10th Apr 2010 09:54
Marvellous!

Thanks a lot Shaun. This is really great of you.
I don't really need to compile the dll, just use the timer functions, so i will just cut & paste those.
Will let you know how they perform in my GDK project.

Thanks a agian
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 10th Apr 2010 10:08
Quote: "Thanks a lot Shaun. This is really great of you."

Your Welcome. Glad i could help.

bjadams
AGK Backer
16
Years of Service
User Offline
Joined: 29th Mar 2008
Location:
Posted: 10th Apr 2010 10:21
I have just put the 3 TBM functions in my code.
All seems to work fine.

do you suggest i put the TBM_Update after Sync() or before for optimal performance?
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 10th Apr 2010 14:46
It shouldn't make a difference, I don't think. Sync just draws to the screen, while the update function just performs calculations.

Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 11th Apr 2010 20:32 Edited at: 11th Apr 2010 20:33
I tested out your example, it worked great only I had to divide the movement factor of 100 by 1000 because the command works in miliseconds . Anyway, I was wondering how one might make a pause menu with this? I tried putting TBM_Update right after the pause but it didn't work. Any ideas? Here is the code I'm using, I have a 2 second pause at the top. The line keeps starting too far to the right, as if it were moving during the pause.



<-- Spell based team dueling game!
The Slayer
Forum Vice President
14
Years of Service
User Offline
Joined: 9th Nov 2009
Playing: (Hide and) Seek and Destroy on my guitar!
Posted: 11th Apr 2010 21:47 Edited at: 11th Apr 2010 21:50
Hi ShaunRW,

Great work on a plugin that many people can use! Unfortunaly, I've tried downloading it, but it did'nt work.
Upon clicking your link, it brings me to your homepage, and there I click on the dowload latest version link, which goes to another website called FileFront. Is there anything I did wrong?

Thanks

Cheers

Slayer rules!!! Yeaaah, man!
Mr Bigglesworth
16
Years of Service
User Offline
Joined: 4th Mar 2008
Location:
Posted: 12th Apr 2010 06:18
Quote: "Upon clicking your link, it brings me to your homepage, and there I click on the dowload latest version link, which goes to another website called FileFront. Is there anything I did wrong?"


Yeah, the latest download link seems to be broken.
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 12th Apr 2010 11:29 Edited at: 12th Apr 2010 11:35
@Sixty Squares: I just reused the TBM_Initialize command and it seemed to work.


Quote: "Yeah, the latest download link seems to be broken"

Fixed

The Slayer
Forum Vice President
14
Years of Service
User Offline
Joined: 9th Nov 2009
Playing: (Hide and) Seek and Destroy on my guitar!
Posted: 13th Apr 2010 19:47
Hi ShaunRW,

Thanks for fixing the download link. However, now I get another error. After saving the file, I try to open it with winrar, but it gives me the error 'unexpected end of archive'. Any idea what could be the problem?

Thanks

Slayer rules!!! Yeaaah, man!
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 14th Apr 2010 05:59
The Slayer, should be fixed now. If it doesn't work this time, i will just upload it here.

Sixty Squares
17
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 18th Apr 2010 19:00
@ShaunRW: Ah that works Thanks!

<-- Spell based team dueling game!
ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 19th Apr 2010 05:39
Serge Adjo
17
Years of Service
User Offline
Joined: 3rd Aug 2006
Location:
Posted: 15th May 2010 20:31 Edited at: 15th May 2010 20:34
@ShaunRW, It doesn't work for me because my program move the camera forward and there's no difference wether I put 'TBM_Move( 5 )' or TBM_Move( 100 ) the camera move at the same speed!
here is the code used:
at the begining of the program:


then in the main loop :
V_Joueur_Vitesse_# = TBM_Move( 160 )
Move camera V_Joueur_Vitesse_#

(Note that puting a lower value won't change the speed... example:V_Joueur_Vitesse_# = TBM_Move( 4 ) )


then at the end of the same main loop along with sync and AI UPDATE:


I'm using DBP U7.4, using DarkAI, Sparky... Win XP

ShaunRW
DBPro Developer
16
Years of Service
User Offline
Joined: 7th Jan 2008
Location: Brisbane, Australia
Posted: 16th May 2010 03:35
Hi Serge Adjo,
Would you be able to post more code? I'm having trouble trying to recreate the problem.

The following code works fine for me. The speed changes as i would expect when changing the value.


Big C
15
Years of Service
User Offline
Joined: 13th Dec 2008
Location:
Posted: 17th May 2010 22:39
Shaun,

n1 dll... Can I use this dll in other languages? If so, how can i access the inherit functions?

thx for answer

Login to post a reply

Server time is: 2024-03-29 11:54:00
Your offset time is: 2024-03-29 11:54:00