a simple 3 minute program

Blitz3D Forums/Blitz3D Beginners Area/a simple 3 minute program

Yahfree(Posted 2007) [#1]
... Hello my friend wanted me to come up with somthing that makes a noise everytime 3 minutes pass, after 2 hours i came up with somthing, the problem is: its making everything laggy....


I cant figure out why... but it is taking like 77% CPU usage ect, its a small program, any ideas to make it less of a power hogging clock? heres the source:

AppTitle "3minutes"

Graphics 200,100,1,2
SetBuffer BackBuffer()

second=0

minute=0

secondtimer=MilliSecs()

sound = LoadSound("Ungodlysound.wav")

While Not KeyHit(1)
Cls

If MilliSecs() > secondtimer+1000 Then
second=second+1
secondtimer=MilliSecs()
End If

If second>59 Then
minute=minute+1
second=0
End If

If minute=3 Then
minute=0
PlaySound sound
second=0
End If

Text 20,80,"Press ESC to Exit"
Text 80,30,minute+":"+second

  Flip
Wend
End


just get some type of .WAV file and call it: Ungodlysound.wav

put it in the same place as the source code and run. it should play that noise every 3 minutes, but notice how much to laggs stuff down even on my fast computer.


any ideas?


LineOf7s(Posted 2007) [#2]
Stick a 'Delay' of some length in your main loop (before the 'Flip', for example). I went as high as a "Delay 500" and noticed no apparent reduction in performance of your program.

This reduced the CPU usage to about 2% on my rig.


Yahfree(Posted 2007) [#3]
freaking sweet!! so the reason why it was slow was cause it was flying through the gameloops to fast?


IPete2(Posted 2007) [#4]
yeah,

...and hogging the cpu without let up. Adding a delay releases at least some time for housekeeping and other progs.

IPete2.


Yahfree(Posted 2007) [#5]
cool, any other tricks to stop laggy programs?


Yahfree(Posted 2007) [#6]
also would this mess with some stuff in the program like delays half a second, so it checks stuff every half a second, if i made that more then 1 second would my time delay updating?


LineOf7s(Posted 2007) [#7]
You'd think so, huh?

Why not try it and find out? You'll learn something, and you'll get that particular warm fuzzy feeling you get when you sort something out for yourself.

It's rather addictive, actually. :o)


OJay(Posted 2007) [#8]
http://www.blitzbasic.com/codearcs/codearcs.php?code=749
that will reduce cpu-load dynamically based on a fixed framerate to be reached. great for applications that run in windowed mode and dont need maximum performance

http://www.blitzbasic.com/codearcs/codearcs.php?code=853
and that one even nearly uses no cpu at all, if the window lost focus (and therefore doesnt need any updates)


cheers :)


Yahfree(Posted 2007) [#9]
Cool, and to LineOf7s, I wasnt on any computer to test it, but i just tested it and it was right, but thanks for the input :\


Yahfree(Posted 2007) [#10]
New problem, Stop-Start switch, basicly i want the timer to stop/reset to 0 and hold there till its started again, i'v tried a few things not working... Any ideas?

basicly if start=false and "s" is hit it start will = true

and if start=true and the "s" key is hit it will stop and zero out.

this is my code:

AppTitle "3minutes"

Graphics 200,100,1,2
SetBuffer BackBuffer()

s=31

second=0

minute=0

start=False

secondtimer=MilliSecs()

sound = LoadSound("Ungodlysound.wav")

While Not KeyHit(1)
Cls

If MilliSecs() > secondtimer+1000 Then
second=second+1
secondtimer=MilliSecs()
End If

If second>59 Then
minute=minute+1
second=0
End If

If minute=3 Then
minute=0
PlaySound sound
second=0
End If

If KeyHit(s) And start=False
start=True
End If

If start=False
secondtimer=MilliSecs
End If


Text 20,80,"Press ESC to Exit"
Text 80,30,minute+":"+second


Delay 500
  Flip
Wend
End


notice the try to make it reset the timer every game loop if start = false, that didnt work... any ideas?


Yahfree(Posted 2007) [#11]
Ok heres the new code i added comments to make it more readable, and if you look you'll see where i need help.

AppTitle "3minutes"

Graphics 300,100,1,2
SetBuffer BackBuffer()

;some veriables
s=31

t=20

second=0

minute=0

start=False

secondtimer=MilliSecs()

;loads the sound.
sound = LoadSound("Ungodlysound.wav")

While Not KeyHit(1)
Cls

;clock and timer code:
If MilliSecs() > secondtimer+1000 Then
second=second+1
secondtimer=MilliSecs()
End If
If second>59 Then
minute=minute+1
second=0
End If
If minute=3 Then
minute=0
PlaySound sound
second=0
End If

;stop and start switchs
If KeyHit(s) And start=False
start=True
Else If KeyHit(t) And start=True
start=False
End If

;atempt to make the start/stop switch stop and start...
If start=False
; What goes here???
; nothing i'v tried works ...
; Help!
End If

;display the switch status
If start=False
Text 20,60,"Press 'S' to Start the timer."
Else If start=True
Text 20,60,"Press 'T' to Stop the timer."
End If

;some other displays
Text 60,80,"Press ESC to Exit."
Text 130,30,minute+":"+second

;delays the program updating every half a second. slows down the lagg big time
Delay 500

;flips the buffer, ends the loop and when it ends it ends the program.
  Flip
Wend
End


Notice the problem, i got the switch working, it just needs somthing to stop the timer, so basicly, what code stops a timer?

;atempt to make the start/stop switch stop and start...
If start=False
; What goes here???
; nothing i'v tried works ...
; Help!
End If



Yahfree(Posted 2007) [#12]
cool i think i figured it out... i'll let you know..

Edit: It works!! thanks for the help guys!