02-01-2023, 06:09 AM
A while back I started a discussion on determining frames rates within other frame rates here:
https://staging.qb64phoenix.com/showthread.php?tid=1107
The method I came up with for Pac-Man I thought was quite clever. Turns out not so much. Not only is it ugly (string manipulation) but it only worked for one global frame rate. If another global rate was needed the entire set of strings need to be recreated. Ugly, yes, but functional for the game.
I have a project I'm working on that needs the ability to have the global FPS change at any time but still have the ability to know when lower frame rates change within that global frame rate, in real time. So, while investigating (and pulling my hair out for an hour) my son walks up and asks, "What ya doing?"
I explain to him what I'm trying to accomplish. He listens, says "huh?", then wanders off. Ten minutes later he came back with a super simple solution! (He's autistic on the Asperger's scale and his mind amazes me)
The code below contains a function called FrameChange that can report lower frame rates within a global frame in real time, even if the global frame rate changes. Now I have to go back to my Pac-Man code and put this in place of my ugly solution. His solution is so freaking simple.
https://staging.qb64phoenix.com/showthread.php?tid=1107
The method I came up with for Pac-Man I thought was quite clever. Turns out not so much. Not only is it ugly (string manipulation) but it only worked for one global frame rate. If another global rate was needed the entire set of strings need to be recreated. Ugly, yes, but functional for the game.
I have a project I'm working on that needs the ability to have the global FPS change at any time but still have the ability to know when lower frame rates change within that global frame rate, in real time. So, while investigating (and pulling my hair out for an hour) my son walks up and asks, "What ya doing?"
I explain to him what I'm trying to accomplish. He listens, says "huh?", then wanders off. Ten minutes later he came back with a super simple solution! (He's autistic on the Asperger's scale and his mind amazes me)
The code below contains a function called FrameChange that can report lower frame rates within a global frame in real time, even if the global frame rate changes. Now I have to go back to my Pac-Man code and put this in place of my ugly solution. His solution is so freaking simple.
Code: (Select All)
' A better frame counter
' By Brandon Ritchie
' 01/31/23
' The function FrameChange determines lower frame rates within a global frame rate.
' The function will return -1 when a lower frame rate increases to the next frame number.
DIM GlobalFPS AS INTEGER
DIM Frame AS INTEGER
DIM FPS(23) AS INTEGER
DIM Count(23) AS INTEGER
DIM i AS INTEGER
GlobalFPS = 60 ' change to any value above 45 - the individual frame rates will remain constant
' (above 45 simply because example rates below are from 2 to 46)
DO ' begin proof of concept demo
_LIMIT 10 ' or use GlobalFPS (10 used to slow things down)
CLS
Frame = Frame + 1
IF Frame = GlobalFPS THEN Frame = 0 ' reset global frame counter when last frame reached
LOCATE 1, 2: PRINT "Global "; _TRIM$(STR$(GlobalFPS)); " FPS >"; Frame
FOR i = 1 TO 23
IF Frame = 0 THEN Count(i) = 0 ' reset count when frame resets
IF FrameChange(GlobalFPS, i * 2, Frame) THEN Count(i) = Count(i) + 1
LOCATE i + 1, 2
PRINT _TRIM$(STR$(i * 2)); " FPS >"; Count(i);
NEXT i
LOOP UNTIL _KEYDOWN(27) ' press ESC to exit
'----------------------------------------------------------------------------
FUNCTION FrameChange (Global AS INTEGER, Target AS INTEGER, Frame AS INTEGER)
' Global = global frame rate
' Target = target frame rate
' Frame = the current global frame (0 to Global-1)
' Returns -1 (true) if target frame changes within the global frame rate
DIM Fraction AS SINGLE
DIM x AS SINGLE '
FrameChange = 0
Fraction = Target / Global
x = Frame * Fraction
IF INT(x) <> INT(x - Fraction) THEN FrameChange = -1
END FUNCTION