BBC BASIC
« Wait - for action »

Welcome Guest. Please Login or Register.
Jan 20th, 2018, 6:08pm


Cross-platform BBC BASIC (Win32, Linux x86, Android, Mac OS-X, Raspberry Pi)

« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Wait - for action  (Read 239 times)
michael
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 141
xx Wait - for action
« Thread started on: Apr 25th, 2017, 04:56am »

Ever since I got the Raspberry Pi 3, efficiency in code use has become important. I have used WAIT (num) to pace it so as to not overwork the pi.

I was wondering if there was a way to make the program wait for user mouse movement or keyboard action, before the program proceeds.

Would be super efficient and fast when action occurred.
Although, I am not looking for tracking of position, rather, just user action.

Any ideas?
User IP Logged

I like reinventing the wheel, but for now I will work on tools for D3D
Richard Russell
Administrator
ImageImageImageImageImage


member is offline

Avatar




Homepage PM


Posts: 689
xx Re: Wait - for action
« Reply #1 on: Apr 25th, 2017, 09:09am »

on Apr 25th, 2017, 04:56am, michael wrote:
I was wondering if there was a way to make the program wait for user mouse movement or keyboard action

Waiting for the keyboard is just INKEY(cs) of course but there's no equivalent for the mouse. So one usually ends up with an 'idle' loop which looks something like this:

Code:
      REPEAT
        k% = INKEY(4)
        MOUSE x%,y%,b%
      UNTIL k%<>-1 OR b% 

(with an additional test for x%/y% changing if you are monitoring mouse movement as well as clicks).

You shouldn't find that this is too inefficient, because the mouse test should be a very small proportion of the 40 milliseconds waiting time. The profiler will quantify this if you need to know accurately (although not on the RPi). Note that a very short-duration mouse click won't always be detected by this code so if necessary it can be supplemented with an ON MOUSE interrupt which sets a flag.

Whatever you do there's a limit to how far you can reduce CPU usage because SDL was originally designed and optimised for games. For example rendering and event polling have to take place in the same thread, so BBCSDL cannot afford to wait too long for an event because that would stall rendering, and conversely while it is waiting to synchronise the rendering to the vertical sync, events will not be polled. This results in a rather ad-hoc arrangement of timeouts to try to balance the CPU usage without hitting performance too much.

Richard.
« Last Edit: Apr 25th, 2017, 10:47am by Richard Russell » User IP Logged

michael
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 141
xx Re: Wait - for action
« Reply #2 on: Apr 27th, 2017, 11:10am »

Thanks Richard. I will work on some modifications to my tools
User IP Logged

I like reinventing the wheel, but for now I will work on tools for D3D
michael
Full Member
ImageImageImage


member is offline

Avatar




PM


Posts: 141
xx Re: Wait - for action
« Reply #3 on: May 4th, 2017, 03:45am »

Here is a possible sleep mode control for mouse and keyboard. ( I fixed a flaw )
Code:
 WAIT 1
      REPEAT
        k% = INKEY(4)
        resp%=FNmoucheck
      UNTIL k%<>-1 OR resp% >0
      PRINT "done"
      END
      DEF FNmoucheck
      PRIVATE mx%,my%,mb%,x%,y%,b%
      MOUSE x%,y%,b%
      IF mx%=0 AND my%=0 AND mb%=0 THEN mx%=x%:my%=y%:mb%=b%: =-1
      IF mx%=x% AND my%=y% AND mb%=b% THEN =-1 ELSE mx%=x%:my%=y%:mb%=b%:=1
  

« Last Edit: May 4th, 2017, 12:13pm by michael » User IP Logged

I like reinventing the wheel, but for now I will work on tools for D3D
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Donate $6.99 for 50,000 Ad-Free Pageviews!


This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls