while: wend
#19
(01-03-2023, 06:09 AM)fistfullofnails Wrote: On Terry Ritchie's function/subroutine part of the QB64 tutorial, line 15 of subs.base,  we have:

Code: (Select All)
WHILE _MOUSEINPUT: WEND

The only way I've seen "WHILE" and "WEND" used is at the start and end of a loop, with some instructions  in between, which then ends with 'WEND".
I'm confused on what is exactly going on here.  Can anyone explain like I'm a five year old?

Code: (Select All)
DO
    _LIMIT 60
    WHILE _MOUSEINPUT: WEND
    IF _MOUSEBUTTON(1) THEN DrawStar _MOUSEX, _MOUSEY
    IF _MOUSEBUTTON(2) THEN Directions
    _DISPLAY
LOOP UNTIL _KEYDOWN(27)

Sorry I'm late to the party here. I didn't see this post until now.

As Steve pointed out the mouse commands in QB64 are buffered. It's not very common with today's computers but back in the Windows 3.x and 9x era it was easy to start clicking around the screen and actually get ahead of the program running. However, each mouse click and mouse movement would would eventually get processed. The reason for this is Windows uses a mouse buffer as well so mouse events are not missed.

Most of the time all you need is the latest mouse event which requires the old mouse events to be cleared (especially in fast paced games). This is where this line of code comes in:

WHILE _MOUSEINPUT: WEND

(As someone else pointed out there are actually two lines of code here separated by a colon)

Once that loop completes the next mouse command issued, for instance _MOUSEX, will report the most up to date or current position. It's also a good idea to gather your mouse data directly after issuing this loop otherwise any delay may result in the buffer starting to fill again resulting in mouse events that are a few steps old.

There may be instances where you need to know every single movement and click performed so you don't miss a user's input, such as how I illustrated Windows 3.x and 9x would keep up. This can be done as well. The second piece of code in this section of the tutorial:

https://www.qb64tutorial.com/lesson7#h.9uisxsndidwf

shows how you can gather every mouse event and process them. This is the same concept as the example code Steve showed here to gather keys from the keyboard buffer.

If WHILE ... WEND is not to your liking there are other loops you can use:

DO WHILE _MOUSEINPUT: LOOP

DO: LOOP UNTIL NOT _MOUSEINPUT

DO: LOOP UNTIL _MOUSEINPUT = 0

etc.. In my opinion, WHILE ... WEND is the cleanest method though.
Reply


Messages In This Thread
while: wend - by fistfullofnails - 01-03-2023, 06:09 AM
RE: while: wend - by SMcNeill - 01-03-2023, 06:28 AM
RE: while: wend - by mnrvovrfc - 01-03-2023, 07:56 AM
RE: while: wend - by bplus - 01-03-2023, 03:39 PM
RE: while: wend - by mnrvovrfc - 01-03-2023, 07:54 PM
RE: while: wend - by bplus - 01-03-2023, 03:57 PM
RE: while: wend - by james2464 - 01-03-2023, 04:07 PM
RE: while: wend - by fistfullofnails - 01-05-2023, 07:36 AM
RE: while: wend - by SMcNeill - 01-05-2023, 08:38 AM
RE: while: wend - by Kernelpanic - 01-03-2023, 08:40 PM
RE: while: wend - by TempodiBasic - 01-04-2023, 12:13 AM
RE: while: wend - by bplus - 01-04-2023, 01:09 AM
RE: while: wend - by bplus - 01-04-2023, 01:55 AM
RE: while: wend - by SMcNeill - 01-04-2023, 02:52 AM
RE: while: wend - by bplus - 01-04-2023, 04:12 AM
RE: while: wend - by mnrvovrfc - 01-04-2023, 04:51 AM
RE: while: wend - by bplus - 01-04-2023, 05:22 PM
RE: while: wend - by Kernelpanic - 01-05-2023, 12:26 PM
RE: while: wend - by TerryRitchie - 01-05-2023, 04:55 PM
RE: while: wend - by TempodiBasic - 01-05-2023, 06:36 PM
RE: while: wend - by TerryRitchie - 01-05-2023, 11:04 PM
RE: while: wend - by fistfullofnails - 01-07-2023, 05:12 AM
RE: while: wend - by SMcNeill - 01-07-2023, 06:51 AM
RE: while: wend - by mnrvovrfc - 01-07-2023, 06:29 AM
RE: while: wend - by OldMoses - 01-07-2023, 01:55 PM



Users browsing this thread: 9 Guest(s)