7 years
edited 7 years
Game Clock
It allows you format the time and get the elapsed time.
//! zinc
library GameClock { /* v1.0.0.7
*************************************************************************************
*
* Game Clock
* by nel
*
* It allows you format the time and get the elapsed time.
*
************************************************************************************
*
* SETTING
*
*/
private constant string DELIMITER = " : "; /* It used to seperate the hour,
minute, and second */
/*
************************************************************************************
*
* struct GameClock extends array
*
* static method FormatTime takes real seconds return string
* - It returns time as hh/mm/ss format.
*
* static method operator ElapsedTime takes nothing returns real
* - It returns elapsed time.
*
* static method operator ElapsedTimeEx takes nothing returns string
* - It returns elapsed time as hh/mm/ss format.
*
* static method operator Delimiter takes nothing returns string
* - It returns GameClock's delimiter.
*
*************************************************************************************
*
* Issue:
*
* This library will not give you an exact current elapsed time.
*
************************************************************************************/
//! textmacro GAMECLOCK_TIMEFORMAT takes Time, delimiter
if( $Time$ < 10 ) {
format = format + "0" + I2S( $Time$ ) $delimiter$;
} else {
format = format + I2S( $Time$ ) $delimiter$;
}
//! endtextmacro
private constant timer TIMER = CreateTimer();
public { struct GameClock [] {
public {
static method FormatTime( real seconds ) -> string {
integer s = R2I( ModuloReal( seconds, 60 ) );
integer m = R2I( ModuloReal( seconds, 3600 ) / 60 );
integer h = R2I( seconds / 3600 );
string format = "";
//! runtextmacro GAMECLOCK_TIMEFORMAT( "h", "+ DELIMITER" )
//! runtextmacro GAMECLOCK_TIMEFORMAT( "m", "+ DELIMITER" )
//! runtextmacro GAMECLOCK_TIMEFORMAT( "s", "" )
return format;
}
static method operator ElapsedTime() -> real {
return TimerGetElapsed( TIMER );
}
static method operator ElapsedTimeEx() -> string {
return thistype.FormatTime( TimerGetElapsed(TIMER) );
}
static method operator Delimiter() -> string {
return DELIMITER;
}
}
private static method onInit() {
TimerStart( TIMER, 100000000, true, null );
}
}}
}
//! endzinc
