WITH question

General FreeBASIC programming questions.
Post Reply
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

WITH question

Post by cha0s »

what is wrong with this?

Code: Select all

Function loadmap_0001 ( f As String ) As map_type_0001 Ptr

  
  Dim As map_type_0001 Ptr lmap = CAllocate ( Len ( map_type_0001 )) '' allocate space for map  


  Dim As Integer o = FreeFile

  Open f For Binary As #o


    With lmap[0] ' <-------- compiler stops here "expected identifier"
  
  
      Get #o,, .filename             '' 
      Get #o,, .tileset.filename     '' 
      Get #o,, .rooms                '' get the datas
    
      .room = CAllocate ( .rooms * Len ( room_type_0001 )) '' allocate space for all rooms
    
      Dim As Integer r, l, t 'loops :: room, layer, tile
      Dim fs As uShort 'used to read a uShort value into an Integer; saving file space :: fileshrink
    
  
      For r = 0 To .rooms - 1
    
          Get #o,, .room[r].x          ''
          Get #o,, .room[r].y          '' 
          Get #o,, .room[r].parallax   '' get the datas
    
          .room[r].layout = CAllocate(( 3 + .room[r].parallax ) * Len ( Integer )) '' allocate space for all layers
    
          
          For l = 0 To ( 3 + .room[r].parallax ) - 1
              .room[r].layout[l] = CAllocate(((( .room[r].x + 1 ) * .room[r].y ) + 1 ) * Len ( Integer ))
                                                                                            '' allocate space for layer tiles
    
              For t = 0 To (( .room[r].x + 1 ) * .room[r].y ) + 1 - 1
                  Get #o,, fs
                  .room[r].layout[l][t] = fs '' fill each tile
    
              Next
    
          Next
    
      Next
  
    Close
  
  
    Load_Image .tileset
    
  End With
  


  Function = lmap


End Function
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

...anyone? x.x

can someone give an example of how WITH works, or put it in the wiki?

maybe even give me something to google to find out?
dumbledore
Posts: 680
Joined: May 28, 2005 1:11
Contact:

Post by dumbledore »

it works something like this

Code: Select all

''
'' WITH test
''

type MYTYPE
	c			as integer
	d			as double
	e			as string * 4
end type

	dim shared t(10) as MYTYPE
	
	t(0).c = 1
	t(0).d = 2.0
	t(0).e = "3"
	
	i = 5
	
	print t(0).c, t(0).d, t(0).e
	
	with t(i)
		.c = t(0).c
		.d = t(0).d
		.e = t(0).e
		print .c, .d, .e
	end	with
	
	print t(i).c, t(i).d, t(i).e
	
	''-------------------
	
type bar
	abc		as integer
end type

type foo
	bar		as bar
end type
	
	dim foo as foo
	dim bar as bar
	
	with foo
		with .bar
			.abc = 1234
		end with

		with bar
			.abc = 5678
		end with
		
		print "1234 ="; .bar.abc
		print "5678 ="; bar.abc
	end with
	
	sleep
i guess you can't have stuff like lmap[0] or *lmap, try doing something like

Code: Select all

dim as map_type_0001 mylmap
dim as map_type_0001 ptr lmap
lmap = @mylmap
and then do stuff where you need the ptr.
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

Pointers are not supported, yet. It was going to be changed, but there would be too many changes in the 0.14 release and nobody would download the next one ;).
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

dumbledore: thanks for the idea, ill just pass back a pointer to that struct i create, then

v1c: evil!!! :P
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

i just realized that wont work, because its in a function, and that struct is deallocated when the function returns.
jofers
Posts: 1525
Joined: May 27, 2005 17:18

Post by jofers »

You can do this:

Code: Select all

Dim mystruct As astruct Pointer
mystruct = cAllocate(astruct)
return *mystruct
And the memory will not be deallocated until the end of the program.
Antoni
Posts: 1393
Joined: May 27, 2005 15:40
Location: Barcelona, Spain

Post by Antoni »

You can use LSET to copy the element you need to a temporel structure inside the function

dim shared p as mytype ptr

Code: Select all

p=allocate(sizeof(mytype)*elements)
call foo(i)
...
end

sub foo(i)
dim bar as mytype
lset bar ,p[i]

with bar
  .....
end with

end sub
v1ctor
Site Admin
Posts: 3804
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Post by v1ctor »

Fixed (or changed, that wasn't supported anyways), changes are in CVS.

Any complex expression can be used now, and WITH's depth became unlimited - before, the max nesting was set to 4.
cha0s
Site Admin
Posts: 5319
Joined: May 27, 2005 6:42
Location: USA
Contact:

Post by cha0s »

thanks man. sorry if it seemed like i was nagging or something, but i think this change is beneficial, at least ;p

btw how do you do unlimited stuff? lol :D are you really god
Post Reply