pointers

New to FreeBASIC? Post your questions here.
Post Reply
creekside_miller
Posts: 5
Joined: Nov 04, 2023 1:37

pointers

Post by creekside_miller »

Does
Dim Pointrs(25) As Integer ptr
create an array of pointers or a pointer to an array?
Thanks,
Richard
paul doe
Moderator
Posts: 1793
Joined: Jul 25, 2017 17:22
Location: Argentina
Contact:

Re: pointers

Post by paul doe »

It creates an array of pointers to integers.
Berkeley
Posts: 64
Joined: Jun 08, 2024 15:03

Re: pointers

Post by Berkeley »

paul doe wrote: Sep 12, 2024 3:20 It creates an array of pointers to integers.
It creates an array of 25(26) pointers to integer memory locations - which aren't allocated this way. I dunno why you should do this. I'd simply make an array of integers and hand their address(pointer) wherever needed - for C library functions for instance. Using BYREF makes it automatically in FreeBASIC.
Jattenalle
Posts: 49
Joined: Nov 17, 2023 14:41
Contact:

Re: pointers

Post by Jattenalle »

Berkeley wrote: Sep 12, 2024 14:57
paul doe wrote: Sep 12, 2024 3:20It creates an array of pointers to integers.
It creates an array of 25(26) pointers to integer memory locations
FreeBASIC arrays start at 0, so it creates an array of 26 integer pointers. Because 0 to 25 is 26 positions as we include 0.
Berkeley wrote: Sep 12, 2024 14:57 - which aren't allocated this way.
The array of 26 integer pointers is allocated perfectly fine. What else would you expect it to do?
Berkeley wrote: Sep 12, 2024 14:57I dunno why you should do this.
There are many reasons to want an array of pointers, for example sometimes you want a subset of a larger set without copying data around.
Berkeley wrote: Sep 12, 2024 14:57I'd simply make an array of integers and hand their address(pointer) wherever needed - for C library functions for instance.
That is something completely different. An array of integers is not the same as an array of integer pointers. By using pointers you can reference things without creating a copy.
Berkeley wrote: Sep 12, 2024 14:57Using BYREF makes it automatically in FreeBASIC.
Does what automatically? BYREF is not the same thing as a pointer, it just passes parameters by reference instead of by value when using methods.
creekside_miller
Posts: 5
Joined: Nov 04, 2023 1:37

Re: pointers

Post by creekside_miller »

Thanks for your replies. I want to create an array of 26 pointers to memory locations where I will load bitmaps. (I'm working on a card game for personal use. [The only way I can get a fair deal.]) So I don't want a pointer to an integer but rather a 24K bitmap. Am I barking up the wrong tree?
R
paul doe
Moderator
Posts: 1793
Joined: Jul 25, 2017 17:22
Location: Argentina
Contact:

Re: pointers

Post by paul doe »

creekside_miller wrote: Sep 12, 2024 17:37 Am I barking up the wrong tree?
R
Ah, I see. Then you can use Fb.Image pointers, and you can do it this way:

Code: Select all

#include once "fbgfx.bi" '' Contains needed definitions

dim as Fb.Image ptr cards( 25 )

'' Load your images
for i as integer = 0 to 25
  cards( i ) = imageCreate( bitmapWidth, bitmapHeight ) '' Replace these with your sizes
  bload( "card-image.bmp", cards( i ) ) '' Or any other way to load the file into the image
next

'' Run your program
'' ...

'' Then, when you finish, you dispose of the images like this
for i as integer = 0 to 25
  imageDestroy( cards( i ) )
next
Alternatively, you can use an already coded function to load bmps into those buffers directly:

Code: Select all

  #include once "file.bi"

  function loadBMP( path as const string ) as Fb.Image ptr
    #define __BM_WINDOWS__ &h4D42
    
    type __BITMAPFILEHEADER__ field = 1
      as ushort id
      as ulong size
      as ubyte reserved( 0 to 3 )
      as ulong offset
    end type
    
    type __BITMAPINFOHEADER__ field = 1
      as ulong size
      as long width
      as long height
      as ushort planes
      as ushort bpp
      as ulong compression_method
      as ulong image_size
      as ulong h_res
      as ulong v_res
      as ulong color_palette_num
      as ulong colors_used
    end type
    
    dim as any ptr img = 0
    
    if( fileExists( path ) ) then
      dim as __BITMAPFILEHEADER__ header 
      dim as __BITMAPINFOHEADER__ info
      
      dim as long f = freeFile()
      
      open path for binary as f
        get #f, , header
        get #f, sizeof( header ) + 1, info
      close( f )
      
      '' Check if the file is indeed a Windows bitmap
      if( header.id = __BM_WINDOWS__ ) then
        img = imageCreate( info.width, abs( info.height ) )
        bload( path, img )
      end if
    end if
    
    return( img )
  end function
Then the above code becomes just:

Code: Select all

#include once "fbgfx.bi" '' Contains needed definitions

'' Include or paste the bitmap loading code here

dim as Fb.Image ptr cards( 25 )

'' Load your images
for i as integer = 0 to 25
  cards( i ) = loadBMP( "card-image.bmp" )
next

'' Run your program
'' ...

'' Then, when you finish, you dispose of the images like this
for i as integer = 0 to 25
  imageDestroy( cards( i ) )
next
creekside_miller
Posts: 5
Joined: Nov 04, 2023 1:37

Re: pointers

Post by creekside_miller »

Image pointers! Who woulda thunk it? Many thanks!!!
Richard
dodicat
Posts: 8136
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: pointers

Post by dodicat »

To avoid loads of files, as an example use the six faces of a single dice (a die i think is the correct terminology)
Use a dedicated folder to get two code snippets and the bitmaps inside.
code snippet 1
createbitmaps.bas

Code: Select all


Sub setdice(i() As Any Ptr,size As Long) 'create bitmaps
    Type v
        As Long x,y
    End Type
    Dim As v p(1 To 7)
    Redim i(1 To 6)
    Dim As Long sz=size,dt=sz/12
    p(1)=Type(sz/4,sz/4)
    p(2)=Type(sz/4,sz/2)
    p(3)=Type(sz/4,3*sz/4)
    p(4)=Type(3*sz/4,sz/4)
    p(5)=Type(3*sz/4,sz/2)
    p(6)=Type(3*sz/4,3*sz/4)
    p(7)=Type(sz/2,sz/2)
    
    For n As Long=1 To 6
        i(n)=Imagecreate(sz,sz,Rgb(200,200,200))
        Select Case n
        Case 1
            Circle i(1),(p(7).x,p(7).y),dt,0,,,,f
        Case 2
            Circle i(2),(p(1).x,p(1).y),dt,0,,,,f
            Circle i(2),(p(6).x,p(6).y),dt,0,,,,f
        Case 3
            Circle i(3),(p(1).x,p(1).y),dt,0,,,,f
            Circle i(3),(p(7).x,p(7).y),dt,0,,,,f
            Circle i(3),(p(6).x,p(6).y),dt,0,,,,f
        Case 4
            Circle i(4),(p(1).x,p(1).y),dt,0,,,,f 
            Circle i(4),(p(3).x,p(3).y),dt,0,,,,f 
            Circle i(4),(p(4).x,p(4).y),dt,0,,,,f 
            Circle i(4),(p(6).x,p(6).y),dt,0,,,,f 
        Case 5
            Circle i(5),(p(1).x,p(1).y),dt,0,,,,f 
            Circle i(5),(p(3).x,p(3).y),dt,0,,,,f 
            Circle i(5),(p(4).x,p(4).y),dt,0,,,,f 
            Circle i(5),(p(6).x,p(6).y),dt,0,,,,f 
            Circle i(5),(p(7).x,p(7).y),dt,0,,,,f 
        Case 6
            For z As Long=1 To 6
                Circle i(6),(p(z).x,p(z).y),dt,0,,,,f 
            Next z
        End Select
        Bsave "face"+Str(n)+".bmp",i(n)
    Next
End Sub

Screenres 300,300,32
Width 300\8,300\16 'for bigger print
Dim As Any Ptr f()
setdice f(),200

For n As Long= Lbound(f)To Ubound(f)
    Imagedestroy(f(n))
Next
Print "bitmaps created"

Print "press a key"
Sleep
 
Second code snippet
loadbitmaps.bas

Code: Select all


Screen 20,32
#define range(f,l) Int(Rnd*(((l)+1)-(f))+(f))
Dim As Any Pointer face(1 To 6)
For n As Long=1 To 6
    face(n)= Imagecreate(200,200)
    Bload "face"+Str(n)+".bmp",face(n)
Next n

Do
    Cls
    print "press escape to end"
    Var i=range(1,6)
    Put(range(0,(1023-200)),range(0,(767-200))),face(i),Pset
    Sleep 500
Loop Until Inkey=Chr(27)

For n As Long= Lbound(face)To Ubound(face)
    Imagedestroy(face(n))
Next
 
This is OK for Windows, not tested on Linux.
You have two methods to play with now.
dodicat
Posts: 8136
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: pointers

Post by dodicat »

Here is how to get all bitmaps in a folder into an array of pointers, and show them.

Code: Select all

sub pipeout(array() as string,Byval s As String)
    Var f=Freefile
    Dim As String tmp
    dim as long counter
    Open Pipe s For Input As #f 
    Do Until Eof(f)
        Line Input #f,tmp
        counter+=1
        redim preserve array(1 to counter)
        array(counter)=tmp
    Loop
    Close #f
End sub

sub getsize(bmp As String,byref w as long,byref h as long)
    Open bmp For Binary As #1
    Get #1, 19, w
    Get #1, 23, h
    Close #1
End sub

redim as string s()
pipeout(s(),"dir /b") 'get all files into the array

screen 20,32
redim as any ptr img()
dim as long counter
for n as long=lbound(s) to ubound(s)
    var ext=lcase(right(s(n),4))
    if ext=".bmp" then   'only use .bmp files
        dim as long w,h
        getsize s(n),w,h
    print s(n),w;"  by  ";h
    counter+=1
    redim preserve img(1 to counter)
    img(counter)=imagecreate(w,h)
    bload(s(n),img(counter))
    end if
    next n
print "press any key"
sleep
for n as long=lbound(img) to ubound(img)
    cls
put(0,20),img(n),pset
print n;"   of  ";ubound(img)
print "press escape to end, or any other key to continue"
if inkey=chr(27) then exit for
sleep
next n
for n as long=lbound(img) to ubound(img)
    imagedestroy(img(n))
    next


 
Jattenalle
Posts: 49
Joined: Nov 17, 2023 14:41
Contact:

Re: pointers

Post by Jattenalle »

dodicat wrote: Sep 13, 2024 10:22[...]
Please stop spamming random topics in an attempt to derail them.
Makes it real difficult to look things up when everything is full of random-, vaguely related-, and often poor-, code that lead beginners astray.

If you want to post about your die stuff or poor attempt at a directory browser make a new topic in the Projects section so it can be found for those interested. At this point you're just cluttering everything and it's annoying.

Your code is poor and misleading, and will lead beginners down the wrong path for critical code.
UEZ
Posts: 1006
Joined: May 05, 2017 19:59
Location: Germany

Re: pointers

Post by UEZ »

@Jattenalle: are you the new Mr. Swiss?

You shouldn't dictate to long-time members what they post and how, and you should show more respect.
The tone makes the music...

dodicat is one of the members who always helps others!
jevans4949
Posts: 1188
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: pointers

Post by jevans4949 »

You can also declare the lower bound of an array as non-zero, and the compiler will adjust (unlike C). For example

Code: Select all

Dim a(Asc("A") to Asc("Z")) as integer POINTER
Jattenalle
Posts: 49
Joined: Nov 17, 2023 14:41
Contact:

Re: pointers

Post by Jattenalle »

UEZ wrote: Sep 13, 2024 11:40 @Jattenalle: are you the new Mr. Swiss?

You shouldn't dictate to long-time members what they post and how, and you should show more respect.
I'm not dictating anything, I made a request and laid out my reasoning.
Arguments to seniority are not only not valid to begin with, but also completely moot in this case. I've been around longer than dodicat. He might even know who I am. Hell, I have been around longer than most people still in the FB community. My main FB game project alone has been around longer than dodicat.
UEZ wrote: Sep 13, 2024 11:40The tone makes the music...
You're right, and since dodicat is a "long-time member" I hold him to a higher standard. I expect him to know better. If my tone is harsh it is because I am disappointed.
UEZ wrote: Sep 13, 2024 11:40dodicat is one of the members who always helps others!
Posting a lot is not the same as posting helpful-, insightful-, or good-, things. If a post is poor or outright wrong it should be pointed out, to avoid passing on the poor information to someone who might not yet know the pitfalls the poor code opens up.

Unfortunately, it is the case that poor code might only be detectable as poor code by someone experienced. Leaving beginners, and those still learning the foundations, to blindly trust that what was posted is good and proper.
dodicat
Posts: 8136
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: pointers

Post by dodicat »

The thread belongs to creekside_miller.
What is Jattenalle gibbering about?
Anyway, water off a duck's back to me, I hope creekside_miller can, as a beginner, get some way towards his/her goal with bitmaps.
Berkeley
Posts: 64
Joined: Jun 08, 2024 15:03

Re: pointers

Post by Berkeley »

Jattenalle wrote: Sep 12, 2024 16:19
Berkeley wrote: Sep 12, 2024 14:57 It creates an array of 25(26) pointers to integer memory locations
FreeBASIC arrays start at 0, so it creates an array of 26 integer pointers.
That's what "25(26)" means...
Jattenalle wrote: Sep 12, 2024 16:19
Berkeley wrote: Sep 12, 2024 14:57 - which aren't allocated this way.
The array of 26 integer pointers is allocated perfectly fine.
But not the memory locations. Using pointers without allocating the memory where to point to...
Jattenalle wrote: Sep 12, 2024 16:19 There are many reasons to want an array of pointers, for example sometimes you want a subset of a larger set without copying data around.
But an integer pointer array seems not to be sensible in any way.
Jattenalle wrote: Sep 12, 2024 16:19 BYREF is not the same thing as a pointer, it just passes parameters by reference instead of by value when using methods.
"Reference" means a pointer to a variable on the stack instead of a value, but it's hidden behind the code therefore it's not the same, just in principle. - And it spares to use pointer hacking.
Post Reply