Hello, the game i created takes alot of cpu time because its continuosly going through a loop. I got the cpu down to 10% in the windowed state. When the mouse hovers over a button there is a delay when the button lights up because i slowed the loop down to conserve cpu time. Is there a way around this?
Event driven program
Blitz3D Forums/Blitz3D Beginners Area/Event driven program You will really need to post the code regarding the loop in question to get the help you need.
I think you don't need such a long delay to reduce the cpu usage to 10%. How much does the CPU use when your app is not running?
I'd suggest to use a loop that will check for mouseposition
every 5 or so millisecs. This will let windows use most of the cpu time, but limit the lag with your hover effect.
Make sure not to update graphics and Flip etc. when there's no need for it.
You can make a test:
as you can see, checking the mouseposition 1000 times takes about 1 or two millisecs() on eg. a 1.6 GHz puter. So if you check it only once every 5 Millisecs, your app really won't eat a lot of CPU power. But as I said, make sure not to use any graphics update as long as there's no user action.
you may use a variable that indicates if the user is active. something like
So in this example, after about 5 seconds of inactivity, the graphics won't be updated anymore until the mouse is moved again.
I'd suggest to use a loop that will check for mouseposition
every 5 or so millisecs. This will let windows use most of the cpu time, but limit the lag with your hover effect.
Make sure not to update graphics and Flip etc. when there's no need for it.
You can make a test:
tt=millisecs() for i=0 to 1000 if mousex() > 100 and mousex()<200 if mousey()>200 and mousey()<300 ; endif endif next tt2=millisecs() print tt2-tt waitkey()
as you can see, checking the mouseposition 1000 times takes about 1 or two millisecs() on eg. a 1.6 GHz puter. So if you check it only once every 5 Millisecs, your app really won't eat a lot of CPU power. But as I said, make sure not to use any graphics update as long as there's no user action.
you may use a variable that indicates if the user is active. something like
active=0 while keydown(1)=0 mx=mousex() my=mousey() if (mx<>old_mx) or (my<>old_my) old_mx=mx old_my=my active =300 endif if active>0 then active=active-1 cls if my>buty1(0) and my<buty2(0) and mx>butx1(0) and mx<butx2(0) butimg(0)=butimg_hi(0) else butimg(0)=butimg_lo(0) endif drawimage butx1(0),buty1(0),butimg(0) flip else delay 5 ; active is zero, slumber mode endif wend
So in this example, after about 5 seconds of inactivity, the graphics won't be updated anymore until the mouse is moved again.
Uh luke, I just see you've got a 90 MHz Pentium. That isn't very fast. Nevertheless my code should work. Tho, I just used to write it from the top of my head, it's not tested, but I hope you see what I mean.
I don't understand how blitz3d can work on a 90 Mhz Pentium - I used to have a Celeron 366 Mhz with a TNT 32 MB Ram video card on windows 95 and couldn't ever get the demo of blitz3d to work until I upgraded.
There are some patches for Win95.
There was a fundamental bug in my code, I just fixed it.
There was a fundamental bug in my code, I just fixed it.