oki here
Sync On : Sync Rate 0 : ` Set the Sync Rate to Maximum Possible
Disable EscapeKey ` Disable the Escapekey, this stops it from automatically exiting
Dim Time(4) ` Time Array, Holds 5 Integer Values / 0 = Current, 1 = Old, 2 = Duration, 3 = Total, 4 = Reset
` Colour Array, this is used to hold global values, in this case the 3 colours for the progressbar
` 1 = Edge, 2 = Bar, 3 = Text
Dim Color(16) : Color(1) = RGB(128, 128, 196) : Color(2) = RGB(128, 128, 196) : Color(3) = RGB(240, 240, 240)
Repeat : `I prefer using Repeat..Until just my personal preference really
UpdateTime() `Update the Timer
If Time(3) >= 150 `Check to see if 150milisecond have elapsed, you have 1000 milisecond to the second
Time(4) = 1 `Tells the Timer to Reset the Total to 0
Inc Percent `Now we add 1%
EndIf
If Percent <= 100 `Check to see if the Percentage complete is less than 100%
ProgressBar(10, 10, 100, 14, Percent, "Loading", 0)
Else `If it's more than 99% i.e. 100% then we tell the user loading in complete
ProgressBar(10, 10, 100, 14, Percent, "Complete", 0)
EndIf
Sync ` update the scene
Until EscapeKey() ` with the escapekey disabled you need to put in your own exit, so here it is
Function ProgressBar(X, Y, Width, Height, Percent, Message$, Flag)
If Percent > 100 Then Percent = 100 ` make sure pecentage is under 100
If Percent < 0 Then Percent = 0 ` and above 0
Ink Color(1), 0
Box X, Y, X + Width + 2, Y + Height + 2 ` draw the outline, it's quicker to do a box
Ink 0, 0
Box X + 1, Y + 1, X + Width + 1, Y + Height + 1 ` draw a 1 pixel smaller box, now we have an outlined area
Ink Color(2), 0
Box X + 1, Y + 1, X + ((Width / 100.0) * Percent) + 1, Y + Height + 1 ` draw the percentage box
Ink Color(3), 0
If Flag = 1 ` if flag is active then we output the % done
Output$ = Message$ + " " + Str$(Percent) + "%"
Else ` otherwise we just output our message
Output$ = Message$
EndIf
Center Text (X + (Width / 2)) + 1, (Y + ((Height / 2) - (Text Height(Message$) / 2))), Output$ `draw text
RemStart
most of what's here is just basic mathematics, the actual area of the progress bar itself is +2 to account
for the 1px border all around. so only the active progress bar is the size you set.
just makes it look neater.
RemEnd
EndFunction
Function UpdateTime()
Time(0) = Timer() ` grab the system time
Time(2) = Time(0) - Time(1) ` now we find out how long it took between the last time update
Time(1) = Time(0) ` put the current time into the old time, as now it can be used next update
If Time(4) = 1 ` if the total is reset
Time(3) = Time(2) ` total = duration
Else
Inc Time(3), Time(2) ` other wise we just add it to the duration
EndIf
Time(4) = 0 ` make sure that reset is 0 when we exit
EndFunction
is the code commented. Though now I know your using DarkBASIC Professional, I've gone and altered the code a bit, with some new comments to explain it for you.
`/ True and False Constant Variables, these variables never change.
#Constant True = 1
#Constant False = 0
`/ I'd recommend against using comments on thoe lines though, can cause off errors.
`/ This will show you how to use a simple Flag system
#Constant FlagShowProgress = 0x02
#Constant FlagSolidBackground = 0x04
Global Refresh As Integer = null `/ You can set values to variables when you create them, in this case null
`/ I use null to = 0 when I want a variable to mean nothing.
Sync On : Sync Rate Refresh `/ Set the Sync Rate to Maximum Possible
Disable EscapeKey `/ Disable the Escapekey, this stops it from automatically exiting
`/ Type Data is quite useful as you can customly create them for all sorts of situations
Type TimeType
Current, Duration, Previous, Elapsed, ResetTime As Integer
EndType
Global Dim Time(void) As TimeType `/ void isn't a real number, I use it to represent 0
`/ A Slighty more Complex type
Type ProgressBarType
X, Y As Integer
Width, Height As Integer
Progress As Integer
Caption As String
EndType
Global ExampleBar As ProgressBarType
ExampleBar.X = 100
ExampleBar.Y = 120
ExampleBar.Width = 120
Global EdgeColor As Integer = 0x8080C0 `/ The reason I didn't use RGB() is because you can't use a function
Global BarColor As Integer = 0x80C080 `/ while declaring a variable.
Global BackgroungColor As Integer = 0xFFFFFF `/ You may recognise the number style being used, it's Hexidecimal.
Global TextColour As Integer = 0x000000 `/ Generally it's used by HTML for Colours. e.g. #8080C0
`/ Figuring out Colours like this is simple with Microsoft Calculator
`/ Start it and set view to Scientific, then put in the number in Dec
`/ Click Hex and voila the Hexidecimal version of it.
`/ The Colors go... 0xRRGGBB
Global Program As Integer : Program = NewTime(80) `/ Set the update time
Global mediaCube As Integer = 1
Make Object Cube mediaCube, 10.0
Color Backdrop 0x808080
Repeat
`/ Update the 'Program' Timer & Check For the loop time.
If UpdateTime(Program) Then Inc ExampleBar.Progress
If ExampleBar.Progress < 100 `/ If Less Than 100%
ExampleBar.Caption = "Loading"
Else : `/ Otherwise ... (can't comment on an else>)
ExampleBar.Caption = "Complete"
EndIf
ProgressBar( ExampleBar, FlagSolidBackground )`/ Update The Progress Bar,
`/ We Combine the Flags using the || Binary OR (works like Addition but on the Binary level)
Rotate Object mediaCube, WrapValue(Object Angle X(mediaCube) + 0.4), WrapValue(Object Angle Y(mediaCube) + 0.4), WrapValue(Object Angle Z(mediaCube) + 0.4)
Sync `/ Update Program
Until EscapeKey() `/ Set Manual Escape
Function ProgressBar( This As ProgressBarType, Flag As Integer )
If This.Height < Text Height(This.Caption) Then This.Height = Text Height(This.Caption) + 2
If (Flag && FlagSolidBackground) `/ Check that the flag we want is alright by removing the other(s)
Local isProgress As Boolean = True
Local Dim Pixels(This.Width, This.Height)
Lock Pixels `/ This REALLY Speeds up this Task
For X = This.X To (This.X + This.Width)
For Y = This.Y To (This.Y + This.Height)
Pixels(X - This.X, Y - This.Y) = Point(X, Y) `/ Grab the Pixel Color from where the Bar is
Next
Next
UnLock Pixels `/ Once we've finished plotting pixels, release the pixels
EndIf
If This.Progress > 100 `/ Though I would show you another way to use the If statement
This.Progress = 100
Else
If This.Progress < 0 Then This.Progress = 0
EndIf
Ink EdgeColor, 0x000000
Box This.X, This.Y, This.X + This.Width, This.Y + This.Height `/ Outline Box
If isProgress
Lock Pixels
For X = This.X + 1 To (This.X + This.Width - 2)
For Y = This.Y + 1 To (This.Y + This.Height - 2)
Dot X, Y, Pixels(X - This.X, Y - This.Y) `/ Now that the background is down we can repaint these pixels
`/ it makes a nice Transparent Effect
Next
Next
UnLock Pixels
Else
Ink BackgroundColor, 0x000000
Box This.X + 1, This.Y + 1, This.X + This.Width - 1, This.Y + This.Height - 1
EndIf
Ink BarColor, 0x000000
Box This.X + 1, This.Y + 1, This.X + ((This.Width / 100.0) * This.Progress) - 1, This.Y + This.Height - 1
Ink TextColor, 0x000000
Local Message As String
If (Flag && FlagShowProgress) `/ Always be sure to && against the Binary Addition of the others
Message = This.Caption + " " + Str$(This.Progress) + "%"
Else
Message = This.Caption
EndIf
`/ Draw the Text
Text (This.X + (This.Width / 2) - (Text Width(Message) / 2)),(This.Y + ((This.Height / 2) - (Text Height(Message) / 2))), Message
EndFunction
Function UpdateTime( Ref As Integer )
Time(Ref).Current = Timer() `/ Get current system time
Time(Ref).Duration = Time(Ref).Current - Time(Ref).Previous `/ Calculate the time between this and the last update
Time(Ref).Previous = Time(Ref).Current `/ Set the previous time to the curren time
If Time(Ref).Elapsed >= Time(Ref).ResetTime
Time(Ref).Elapsed = Time(Ref).Duration `/ Set the Accumulated Time to the Duration
ExitFunction True `/ Exit and Return True (1)
Else
Inc Time(Ref).Elapsed, Time(Ref).Duration `/ Otherwise add that time
EndIf
EndFunction False `/ Exit and Return False (0)
Function NewTime( MiliSecond As Integer )
Local This As Integer
Array Insert At Bottom Time(void) `/ This will add a New Element to the Time Array
This = Array Count(Time(void)) `/ Get the new Array Size
Time(This).ResetTime = MiliSecond `/ Set the Update Time
EndFunction This `/ Pass it back so we can use it
Have a play