Hi all!
I've been away for a bit. (US holiday and a nephew's graduation.)
Here is a bit of code I created to benchmark the Inkey$() vs. Key() commands. We know Key() is faster, but how much faster is it? This program will give you some idea. (You may want to create a 'smalfont.png' file with somewhat larger text for ease of reading. Save it to the Media folder. The program should scale fine.)
SETSCREEN 1366,768,FALSE
SETCURRENTDIR(GETCURRENTDIR$()+"Media")
GLOBAL fontkey% = GENFONT()
LOADFONT "smalfont.png",fontkey
SETFONT fontkey
LOCAL fw%,fh%;GETFONTSIZE fw,fh
LOCAL Cycle_Count% = 0,Test2_Cycle_Count%=0,EndTime# = GETTIMERALL()+30000
LOCAL MaxChars = 30
PRINT "Test 1: ShowScreen called every cycle.",1,0*fh
PRINT " Processing speed limited by FPS.",1,1*fh
PRINT "-------------------------------------",1,2*fh
PRINT "-------------------------------------",1,10*fh
DRAWRECT 65+4,3*fh-1,5+(MaxChars+1)*fw+7,fh+2,RGB(255,255,255)
DRAWRECT 65+5,3*fh,5+(MaxChars+1)*fw+5,fh,RGB(0,0,255)
USEASBMP
LOCAL K$,String$,hold%
REPEAT
K$ = INKEY$()
IF K$ <> ""
IF LEN(String$)<=MaxChars THEN String$ = String$+K$
PRINT String$,75,3*fh
USEASBMP
ENDIF
PRINT ShowFPS()+" FPS = Program Cycles Per Second." ,10,12*fh
PRINT "Type Up to "+MaxChars+" Characters of Text.",65,4*fh+5
PRINT "Test will run for 30 seconds.",50,6*fh
PRINT Cycle_Count + " cycles completed so far.",50,7*fh
SHOWSCREEN; Cycle_Count = Cycle_Count+1
UNTIL KEY(211) OR GETTIMERALL()>EndTime// Exit if [DELETE] key is pressed.
CLEARSCREEN
PRINT "Cycles in 30 seconds = "+Cycle_Count,10,200
PRINT "Press Key to begin Test 2.",10,230
SHOWSCREEN;KEYWAIT
String$ = ""
LOCAL K%,Count%
EndTime = GETTIMERALL()+30000
CLEARSCREEN
PRINT "Test 2: Showscreen called only when screen update needed.",1,0*fh
PRINT " Processing speed not limited by FPS.",1,1*fh
PRINT "-------------------------------------",1,2*fh
PRINT "-------------------------------------",1,10*fh
DRAWRECT 65+4,3*fh-1,5+(MaxChars+1)*fw+7,fh+2,RGB(255,255,255)
DRAWRECT 65+5,3*fh,5+(MaxChars+1)*fw+5,fh,RGB(0,0,255)
PRINT "Type Up to "+MaxChars+" Characters of Text.",65,4*fh+5
PRINT "Test will run for 30 seconds.",50,6*fh
USEASBMP
REPEAT
FOR K = 1 TO 255
IF KEY(K)=TRUE THEN BREAK
NEXT
IF K<255
K$ = INKEY$()
IF K$ <> ""
IF LEN(String$)<31 THEN String$ = String$+K$
PRINT String$,75,3*fh
USEASBMP
ENDIF
PRINT ShowFPS()+" FPS, yet the program is cycling at max speed." ,10,12*fh
PRINT Test2_Cycle_Count + " cycles completed so far.",50,7*fh
SHOWSCREEN
ENDIF
Test2_Cycle_Count = Test2_Cycle_Count+1
UNTIL KEY(211) OR GETTIMERALL()>EndTime// Exit if [DELETE] key is pressed.
CLEARSCREEN
PRINT "RESULTS",100,2*fh
PRINT "InKey$() allowed "+Cycle_Count+" cycles in 30 seconds.",10,5*fh
PRINT " Key() allowed "+Test2_Cycle_Count+" cycles in 30 seconds.",10,6*fh
PRINT "Key() was "+(Test2_Cycle_Count / Cycle_Count)+" times faster than InKey$().",10,8*fh
PRINT "Click Mouse to Exit.",10,10*fh
SHOWSCREEN
MOUSEWAIT
END
// ------------------------------------------------------------- //
// --- SHOWFPS ---
// ------------------------------------------------------------- //
FUNCTION ShowFPS:
// FPS counter
LOCAL dtime#,fps#
STATIC delay#,FPS#
dtime = GETTIMER()
fps = ((1000/dtime)+fps)/2
delay=delay+dtime
IF delay>500 // 1/2 sec
delay=0
FPS=fps
ENDIF
RETURN FPS
ENDFUNCTION // SHOWFPS
If we could only unhitch Inkey$() from the necessity to call Showscreen each time, maybe by adding a Inkey$(-1) parameter, it could mean a real boost to program speeds when processing is done in the background while watching for input at the keyboard. With the tremendous overhead imposed by Showscreen, it may actually be quicker to duplicate all of the core features of Inkey$() with Key() using basic. (UGG!)
Thank you all for your suggestions. Ocean, Kanonet, I don't know how to launch separate threads in GLbasic. Could you or someone do a tutorial on that topic? I would love to learn that ability. Mr. Toad, I tried CLEARSCREEN, but it doesn't seem to trigger a INKEY$() reset. Kanonet, I didn't know GLbasic used a keyboard buffer. If this is the hitch necessitating SHOWSCREEN with Inkey$(), then perhaps it begins to make some sense now. I don't know much about the inner workings of GLbasic, but I know my ignorance is a handicap. Still, if the command could be freed in some way to operate like Key(), it would be a powerful upgrade to the command and to the language version we both love.
I will try coding my input routines both ways to see which gives the bigger payoff. Given the overhead, maybe reinventing the wheel using K=key() really would yield a significant gain. If so, I will post the results to my other thread relating to this topic, rather than here under Bugs and Feature Suggestions. There is nothing more I can add to this thread, so I will leave off on it now.
Cheers!
-CW