[futurebasic] Re: [FB] [FB3] Passing RECT variables to Local FNs

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : April 2001 : Group Archive : Group : All Groups

From: Alain Pastor <apastor@...>
Date: Thu, 26 Apr 2001 19:01:41 +0200

scram@... wrote:
> 
> This may be related to other posts recently about parameters being passed
> to functions in FB3 by value or by reference.
> 
> I would like to work with different rectangles (RECT data type) within a
> LOCAL FN and call that function with various RECT variables. Here is a
> simple example shown below.  When the user clicks on one of the three
> rectangles, I want that rectangle to blink; I call FN FlashRect( x ) where
> x is a paramter indicating the selected rectangle rectangle. The 3
> rectangles are defined as global variables. As written, using FB^3.4,
> FlashRect is indeed called (the program beeps) but the rectangle does not
> flash on and off.
> 
> How can I correctly pass a RECT variable to the Local FN to accomplish this
> goal?
> 
> Someone said that records pass the address of the record and RECT variables
> are predefined records. Is that the problem?

Stu,

The incoming parameter in your FN is an address or if you prefer a
pointer to the record that is supposed to be sent.

> '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'
> LOCAL FN flashRect( theRect  ) <= there is a problem here
> ' flash the interior of a given rectangle
>   BEEP
>   InvertRect( theRect ) <= the Toolbox call expects a variable here
>   DELAY 100
>   InvertRect( theRect )
> END FN

You must declare correctly the type of the formal argument. You have
different possibilities at your disposal:

- using the FB old syntax

    LOCAL FN flashRect( theRect&  )

- declaring the argument as a generic pointer

    LOCAL FN flashRect ( theRect AS PTR )

- declaring a specific pointer

    LOCAL FN flashRect ( theRect AS PTR TO RECT )

That's fine and dandy, but that's not all.

The InvertRect Toolbox call wants to deal with a variable,
preferably of the RECT type. You haven't got any yet, you have just
a pointer (I would even say a pointer to a remote record variable).
So, what you can do is to declare a local RECT variable within your
LOCAL FN.

    DIM localRect AS RECT for example.

Then you need to copy the data located at the address that the FN
has received into your local RECT structure.
That means that 8 bytes (size of a rect structure) must be copied
from the pointer (which is an address) to the address of your local RECT.
Depending on the syntax you have used to declare the incoming
parameter you will have to:

- move "manually" the data with a BLOCKMOVE statement (1st & 2d syntaxes)

    BLOCKMOVE theRect&,@localRect,SIZEOF(RECT)

- let FB do the thing for you. The 3d syntax acknowledges FB that a
RECT structure is involved in that story. FB knows as well that the
RECT structure is 8 bytes in size. So, it can cheat, and we are
allowed to use the regular assignment operator:

    localRect = theRect

Notice also that you can still copy "manually" the data with a
BLOCKMOVE statement using the third syntax if you wish.

Finally, you can pass your local rect onto the Toolbox call.

But, you can make that story shorter...
Don't pass your rectangles as parameters since they are globally
defined ;-)
OK, I'm kidding, here is the real trick: tell the Toolbox you are
sending an address and not a variable in the parameter list of the
call. For that to happen:

  you don't need to advise
  apple with a phone call,
  you just have to be wise
  and use the # symbol.
             [Bowerbirding]

In that manner you can directly use the pointer received by your
LOCAL FN:

InvertRect ( #theRect )

The whole picture:

LOCAL FN flashRect( theRect AS ^RECT ) 'incoming pointer to a rect
' flash the interior of a given rectangle
  BEEP
  InvertRect( #theRect )  'force the use of the address received
  DELAY 100
  InvertRect( #theRect )
END FN
-- 

Cheers

Alain

-----------------------------------------------------
FB^3 in Europe:  http://euro.futurebasic.com/
FB II Pouch:     http://www.pixmix.com/FB/outils.html
-----------------------------------------------------