One might say its in a class... and thats enough... but I won't because that's not good enough.
Your approach is fine, except you do call the timer function twice in your example.
In my method of coding, I try not to do stuff twice - like call a function that calls the operating system that calls a bios function that returns values in the system clock area (It might be less than this.. but I think you get the idea)
I actually have a clock class I use that I instantiate once in my program, and call its update method once per loop. I then "Ask this clock" what the timer "was" when I called it - more or less to recycle the value I got from the timer. I also convert the timer to a float and save that - so any math I may do later in my program (as many time based calculations might accur and without thinking ahead - there might be a lot of CPU cycles lost due to converting integer to floats during math calcs - say for timer based animation, movement or whatever.
so the Timer class I wrote takes the "dbtimer()" value as a parameter - and thats what I used in my above example - I tried to show this technique by having the int mytimer thing declared outside the loop - and calling timer at the top - (that you asked about earlier) so that I only caller Timer once per loop.
here is my clock class:
jgc_clock.h
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
#pragma once
//---------------------------------------------------------------------
class JGC_CLOCK{
//---------------------------------------------------------------------
public:
int Time_New;
int Time_Old;
int Time_Elapsed;
float Time_ElapsedFloat;
void Update(void);
JGC_CLOCK();
~JGC_CLOCK();
};
//---------------------------------------------------------------------
jgc_clock.cpp
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
#include <DarkGDK.h>
#include "jgc_clock.h"
//============================================================================
// Begin JGC_CLOCK
//============================================================================
//----------------------------------------------------------------------------
JGC_CLOCK::JGC_CLOCK(){
//----------------------------------------------------------------------------
this->Time_New=dbTimer();
this->Time_Old=this->Time_New-1; // force i millisec elapsed to start
this->Time_Elapsed = this->Time_New - this->Time_Old; // Self documenting
// not necessary - Not Harmful.
this->Time_ElapsedFloat = (float)this->Time_Elapsed;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
JGC_CLOCK::~JGC_CLOCK(){
//----------------------------------------------------------------------------
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JGC_CLOCK::Update(void){
//----------------------------------------------------------------------------
int TempTime = dbTimer();
this->Time_Old = this->Time_New;
this->Time_New=TempTime;
// PREVENT HUGE Time spans (1000=second)
if((this->Time_Elapsed = (this->Time_New - this->Time_Old))>1000){
this->Time_Old=this->Time_New-1;
this->Time_Elapsed=1;
};
this->Time_ElapsedFloat = (float)this->Time_Elapsed;
};
//----------------------------------------------------------------------------
//============================================================================
// End JGC_CLOCK
//============================================================================
Example:
#include "jgc_clock.h"
#include "jgc_timer.h"
//----------------------------------------------------------------------------
void DarkGDK ( void ){
//----------------------------------------------------------------------------
JGC_CLOCK *Clock=new JGC_CLOCK();
JGC_TIMER *BulletTimer = new JGC_TIMER();
BulletTimer->Set(100,0); // make so first time no delay
while ( LoopGDK ( ) ){
Clock->Update();
if(dbKeystate(Key_Spacebar) &&
BulletTimer->DelayPassed(Clock->Time_New)
){
// Make sound, Draw Bullets Here etc.
};
... etc etc
};
delete Clock;
delete BulletTimer;
};
//----------------------------------------------------------------------------
It's not so much about the one timer - it more about planning for having 50 timers, and tons of guns firing, and trying not to call slow functions more than necessary. One or two slow calls doesn't matter - not even noticable - but overall - because projects get large quickly - I try to keep a REAL close eye on efficiency. This doesn't mean I always hit the mark - and frankly your code is perfect in my opinion if you called dbTimer() just the once.
I don't know if this answers your question - is food for thought - or you think this rubbish (I do write rubbish code on occasion I assure you) - but this is why I do it this way.