32/64 bit @custom function question

New to FreeBASIC? Post your questions here.
sero
Posts: 11
Joined: Mar 06, 2018 13:26
Location: USA

32/64 bit @custom function question

Postby sero » Apr 01, 2018 13:51

I'm working on a bit of code that relies on the @custom graphic function. Below is the function structure taken directly from the help file :

Code: Select all

Declare Function identifier ( _
     ByVal source_pixel As UInteger, _
     ByVal destination_pixel As UInteger, _
     ByVal parameter As Any Ptr _
     ) As UInteger

Since 32/64 bit handles integers differently I was wondering if there is a "correct" way to make this 64 bit friendly or am I misunderstanding? Below is a test demonstrating that replacing "UInteger" with "ULong" failes to compile with an "Error 20: Type mismatch" in both 32/64

Code: Select all

Function dither ( ByVal source_pixel As UInteger, ByVal destination_pixel As UInteger, ByVal parameter As Any Ptr ) As UInteger
   
    ''either returns the source pixel or the destination pixel, depending on the outcome of rnd
   
    Dim threshold As Single = 0.5
    If parameter <> 0 Then threshold = *CPtr(Single Ptr, parameter)
   
    If Rnd() < threshold Then
        Return source_pixel
    Else
        Return destination_pixel
    End If
   
End Function

Function dither_2 ( ByVal source_pixel As ulong, ByVal destination_pixel As ulong, ByVal parameter As Any Ptr ) As ulong
   
    ''either returns the source pixel or the destination pixel, depending on the outcome of rnd
   
    Dim threshold As Single = 0.5
    If parameter <> 0 Then threshold = *CPtr(Single Ptr, parameter)
   
    If Rnd() < threshold Then
        Return source_pixel
    Else
        Return destination_pixel
    End If
   
End Function


Dim img As Any Ptr, threshold As Single

'' set up a screen
ScreenRes 320, 200, 16, 2
ScreenSet 0, 1

'' create an image
img = ImageCreate(32, 32)
Line img, ( 0,  0)-(15,  15), RGB(255,   0,   0), bf
Line img, (16,  0)-(31,  15), RGB(  0,   0, 255), bf
Line img, ( 0, 16)-(15,  31), RGB(  0, 255,   0), bf
Line img, (16, 16)-(31,  31), RGB(255,   0, 255), bf

'' dither the image with varying thresholds
Do Until Len(Inkey)
   
    Cls
   
    threshold = 0.2
    'Put ( 80 - 16, 100 - 16), img, Custom, @dither, @threshold
    Put ( 80 - 16, 100 - 16), img, Custom, @dither_2, @threshold
   
    '' default threshold = 0.5
    'Put (160 - 16, 100 - 16), img, Custom, @dither
    Put (160 - 16, 100 - 16), img, Custom, @dither_2
   
    threshold = 0.8
    'Put (240 - 16, 100 - 16), img, Custom, @dither, @threshold
    Put (240 - 16, 100 - 16), img, Custom, @dither_2, @threshold
   
    ScreenCopy
    Sleep 25
   
Loop

'' free the image memory
ImageDestroy img
counting_pine
Site Admin
Posts: 5919
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: 32/64 bit @custom function question

Postby counting_pine » Apr 02, 2018 18:03

Does it work ok with Integer parameters?
I guess it’s not ideal this way, and may have been an oversight when adapting the code for 64-bit, but at least it doesn’t actually break anything.
But I guess if it gets changed in the future to use Long parameters, we should maybe retain support for Integer parameters to prevent breakages.
fxm
Posts: 8023
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: 32/64 bit @custom function question

Postby fxm » Apr 02, 2018 18:36

Already discussed here:
sero
Posts: 11
Joined: Mar 06, 2018 13:26
Location: USA

Re: 32/64 bit @custom function question

Postby sero » Apr 02, 2018 18:52

counting_pine wrote:Does it work ok with Integer parameters?

No problems compiling/executing 32 or 64 bit. Thanks for responding so quickly!
fxm wrote:Already discussed here:

Thank you and sorry I unintentionally brought forth an old topic.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 1 guest