Quote: "I am trying to convert some DB Pro Code functions to give me a function in AppGameKit to convert a decimal number i.e. 97 to hours-mins-secs-10th sec"
Based on what you wrote, I'm going to have to make a few assumptions. It seems that you're not interested in displaying a "clock", but are instead trying to display an
amount of time, such as a laptime.
Further, I'm going to assume that your
decimal number will be an amount of time in
milliseconds, as that's what the example function you posted uses.
Quote: "Here's what works in DB Pro, anyone got a function for AppGameKit ?"
AGK does not recognize the
Str$() function, so all you really need to do, is replace it by
Str(), and it compiles.
Note that the example function is
not useful as a generic clock display, because it's purely incremental - but it could be useful to display a laptime.
Here's the fixed example that compiles in AGK.
// Our main loop
do
// Put text on screen
Print("Laptime = " + time$(timer() * 1000))
// Update screen
Sync()
// Wait for click
if (GetPointerPressed()) then exit
loop
// Terminate application
End
// Function called with print time$(laptime) etc.
// Give it a time in milliseconds and it will return the time in the form of "mm:ss:ff" (minutes:seconds:fractions_of_a_second)
function time$(t)
// Initialize local variables
s$=""
// if the time is greater than 0
if t>0
t = t / 10.0
mseconds0 = t
seconds0 = 0
Minites0 = 0
if t>=100
seconds0 = (t) / 100
while mseconds0 > 99
dec mseconds0,100
endwhile
endif
if seconds0 >= 60
Minites0 = seconds0 / 60
while seconds0 > 59
dec seconds0,60
endwhile
endif
// if single digit, add a zero
if Minites0 < 10 then s$ = s$ + "0"
s$ = s$ + Str(Minites0) + ":"
// if single digit, add a zero
if seconds0 < 10 then s$ = s$ + "0"
s$ = s$ + Str(seconds0) + ":"
// if single digit, add a zero
if mseconds0 < 10 then s$ = s$ + "0"
s$ = s$ + Str(mseconds0)
// else return 00:00:00
else
s$="00:00:00"
endif
endfunction s$
However, the above function is rather badly coded... (I assume it was taken from the DB Pro forum.) There's no need for any of those while-loops and the bunch of if-statements. The snippet posted by "Zoq2" was on the right track, but definately not functional.
Here's how I would do it. (Check out the functions at the end of the code.)
//=============================================================================
//### FILEHEADER
//-----------------------------------------------------------------------------
//
// Filename: main.agc
// Title: AGK EXAMPLE - How to display a laptime properly!
//
// Version: 1.00, Build 0001
// Created: 2013-09-22 by AgentSam
// Last modified: 2013-09-22 by AgentSam
//
// Project state: Released on the TGC forums.
// Release type: Public Domain
//
// Compatibility: Compiles with AGK 107.06, AGK 108.11, AGK 108.12
//
// Programmer's notes
// ==================
//
// o This example was created in response to a post on the AGK forums:
// http://forum.thegamecreators.com/?m=forum_view&t=207914&b=41&msg=2486893#m2486893
//
// Revision History
// ================
//
// o 2013-09-22, AgentSam
// - NEW BUILD: 1.00, Build 0001
// - NOTES: Project started
//
// To Do
// =====
//
// o 2013-09-22, AgentSam
// - No pending tasks.
//
// Author contact information
// ==========================
//
// o AgentSam
// - Email: ---
// - Notes: The original author of this script.
//
//---------------------------------------------------------------------------
//=============================================================================
//### SECTION - DISPLAY SETUP (VIRTUAL RESOLUTION)
//-----------------------------------------------------------------------------
// Landscape orientation using pixel based coordinates @60 FPS
SetVirtualResolution (1024, 768)
SetSyncRate (60, 0)
// Set text output properties
SetPrintSize (16)
SetPrintColor (255, 255, 255)
// Set backbuffer properties
SetClearColor (0, 0, 0)
SetBorderColor (32, 32, 255)
//=============================================================================
//### SECTION - MAIN LOOP
//-----------------------------------------------------------------------------
// Declare and initialize a helper variable (used for incrementing time faster)
v_iTest as integer
v_iTest = 0
// Our main loop
do
// Show usage information
Print("CONTROLS:")
Print(" LMB .... Increment time by 1 minute.")
Print(" ESC .... Quit")
Print("")
// Show amount of time since the beginning of the app
Print("LAPTIME = " + LaptimeToStr(GetMilliseconds() + v_iTest))
// Update screen
Sync()
// On LMB, increase time by 1 minute!
if (GetPointerState() = 1) then v_iTest = v_iTest + 60000
// On ESC, terminate application
if (GetRawKeyPressed(0x1B)) then exit
loop
// Terminate application
End
//=============================================================================
//### SECTION - SUPPORT ROUTINES
//-----------------------------------------------------------------------------
//=============================================================================
//# FUNCTION - LaptimeToStr
//-----------------------------------------------------------------------------
//
// Purpose
// =======
// Given an amount of time in milliseconds, returns elapsed
// time as a string, formatted as "HH:MM:SS.FFF".
//
// To call
// =======
// a_iMilliSeconds = time in milliseconds
//
// Returns
// =======
// A string representing the current laptime, formatted as:
// "HH:MM:SS:FFF" (hours, minutes, seconds, fractions)
//
// Dependencies
// ============
// None.
//
// Programmer's notes
// ==================
//
// o This routine is NOT useful as a generic clock-display,
// because we're dealing with an incremental "AMOUNT OF TIME".
//
// o This routine could easily be enhanced to also include the
// amount of days, months, and years - if we were dealing with
// very large time periods - and if we had 64-bit (or greater)
// integers.
//
//---------------------------------------------------------------------------
//
function LaptimeToStr(a_iMilliSeconds as integer)
// Declare local variables
v_iHours as integer
v_iMinutes as integer
v_iSeconds as integer
v_iFractions as integer
v_strOutput as string
// Convert MilliSeconds to Hours, Minutes, Seconds and Fractions
v_iHours = Trunc(a_iMilliSeconds / 3600000)
v_iMinutes = Mod(Trunc(a_iMilliSeconds / 60000), 60)
v_iSeconds = Mod(Trunc(a_iMilliSeconds / 1000), 60)
v_iFractions = Mod(a_iMilliSeconds, 1000)
// Build the result string
v_strOutput = PadLeft(Str(v_iHours ), "0", 2) + ":"
v_strOutput = v_strOutput + PadLeft(Str(v_iMinutes ), "0", 2) + ":"
v_strOutput = v_strOutput + PadLeft(Str(v_iSeconds ), "0", 2) + "."
v_strOutput = v_strOutput + PadLeft(Str(v_iFractions), "0", 3)
// Return result to caller
endfunction v_strOutput
//=============================================================================
//# FUNCTION - PadLeft
//-----------------------------------------------------------------------------
//
// Purpose
// =======
// Pad given string to specified length using specified character.
//
// To call
// =======
// a_strPadMe = string to be padded
// a_strPadChar = character to use for padding
// a_iPadLen = minimum length of the result string
//
// Returns
// =======
// Returns the padded string.
//
// Dependencies
// ============
// None.
//
// Programmer's notes
// ==================
//
// o Only supports left-padding at the moment, but can easily be
// modified to do right-padding also.
//
//---------------------------------------------------------------------------
//
function PadLeft(a_strPadMe as string, a_strPadChar as string, a_iPadLen as integer )
// While length is less than desired ...
while ( Len(a_strPadMe) < a_iPadLen)
// Add padding
a_strPadMe = a_strPadChar + a_strPadMe
endwhile
// Return result
endfunction a_strPadMe
Also noteworthy: Because AppGameKit stores integers as 32-bit signed values, the largest number of milliseconds we can represent using them, is 2147483647. That translates to 596 hours, 31 minutes, 23 seconds, and 647/1000 of a second.
Cheers,
AgentSam