Screensavers & FPS(Frames Per Second)

Blitz3D Forums/Blitz3D Beginners Area/Screensavers & FPS(Frames Per Second)

Aussie(Posted 2011) [#1]
Hey everyone, it’s been quite a while since I have been able to do anything in B3d & now have a bit of spare time on my hands.

I have found heaps of info regarding screensavers & thought I would do one myself. I just have a couple of questions regarding FPS(Frames Per Second) limiting.

Here is a piece of code I usually use.

Function LimitFrameRate(FPS = 65)
If FrameTime = 0 Then
Period = 1000 / FPS
FrameTime = MilliSecs()
EndIf

While (FrameTime + Period) > MilliSecs()
Delay 1
Wend
FrameTime = MilliSecs()
End Function

My questions are.
1. I have read on other posts of programs crashing when MilliSecs() reaches a certain point, if so what would be an alternative so the program doesn’t crash?

2. This may be a stupid question, but If I limit FPS in the screensaver will it affect anything else running in the background, eg rendering, downloading or the general performance of a PC?

Cheers.


Yasha(Posted 2011) [#2]
I have read on other posts of programs crashing when MilliSecs() reaches a certain point, if so what would be an alternative so the program doesn’t crash?


This is because MilliSecs() returns an int, and ints are signed 32-bit numbers which means they can only represent 4.2 billion unique numbers, half of which are negative. If your computer has been running for longer than ~2.1 billion milliseconds, it will "roll over" into a negative number (which is what happens if you add one to the maximum), which in turn will continue to grow and eventually reach zero again... etc.

2.1 billion milliseconds is a constant amount of time, obviously, so this issue only affects computers that have been left running for over three weeks. Most Blitzers tend to ignore it, at least for client code, as this is quite rare for a home desktop user (however, it is important on a server).

Even then, it doesn't cause a problem internally - the designers of the functions are aware of the limitation, so calling MilliSecs() is safe - but it does cause a problem if you only do simple checks to see whether the current MilliSecs() is greater than the last MilliSecs(), which will return false if it rolled over in between. So there may be an error in your own program logic. It is possible to solve this issue and it has been discussed at length in various other threads (do a search on "millisecs" and "rollover" to get a few relevant results) The simplest effective answer is here: http://www.blitzbasic.com/Community/posts.php?topic=49285#548324.

The answer given by Floyd in the linked post will work for small intervals such as a few seconds (...or hours); you can test if the interval was longer than a given value by testing if time1 - time2 > INTERVAL, so 1000 for checking FPS. This will ignore rollover as long as the interval itself is less than the maximum int value (i.e. not just the computer, but your actual game would have to be running for 25 days to break this simple change - for most people this is a non-issue).

If I limit FPS in the screensaver will it affect anything else running in the background, eg rendering, downloading or the general performance of a PC?


Yes, it will improve the performance of things running in the background by freeing more resources for your computer to assign to other tasks! Simple as that. You can't affect the general speed of your machine from within Blitz3D, only waste more CPU time than you need.

Last edited 2011


Warner(Posted 2011) [#3]
You could change to:
While Abs(MilliSecs() - FrameTime) < Period



Aussie(Posted 2011) [#4]
Thanks guys.
Am i correct in saying that buy changing

While (FrameTime + Period) > MilliSecs()
to
While Abs(MilliSecs() - FrameTime) < Period

that the Abs function will turn a negative value into a positive so after the 25 days of running the program wouldn't crash?

My screensaver is finished, just need to make sure it won't crash.

Cheers.