Qbasicnews.com

Full Version: pointing at pointers? blarg
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

Anonymous

i am having some real trouble understanding how pointers need to work to create a dynamic type structure... here is my code so far

Code:
TYPE mapheader FIELD=1

  numofrooms AS UBYTE
  tilesetname AS string

END TYPE


TYPE RoomHeader FIELD=1

  dimensions AS SHORT
  numofdoors AS UBYTE
  layout as ushort ptr

END TYPE


type maptype field=1

  info as mapheader
  rooms as roomheader ptr
  doors as integer ptr
  numoftiles as short
  tileset as short ptr

end type

dim map as maptype ptr


map = callocate(len(maptype))  

map->info.numofrooms = 1

map->info.tilesetname = ""
map->info.tilesetname = "grass.spr"

map->rooms = callocate(map->info.numofrooms * len(roomheader))
map->rooms->dimensions = 7710
map->rooms->numofdoors = 1



    thisroomx = (map->rooms->dimensions shr 8) + 1
    thisroomy = (map->rooms->dimensions and 255) + 1
    
    map->rooms->layout = callocate((thisroomx * thisroomy) * len(ushort))
    
map->rooms->layout[600] = 9
    
? map->rooms->dimensions 'prints 7710 at runtime (correct)
? map->rooms->numofdoors 'prints 1 at runtime (correct)
? thisroomx 'prints 31 at runtime (correct)
? thisroomy 'prints 31 at runtime (correct)
? len(*map) 'prints 23 at runtime (correct for an empty structure)
? len(map.info) 'print 4 at runtime (correct?)
? len(*map->rooms) 'print 7 at runtime (correct?)
? len(map->rooms->layout) 'prints 4 at runtime (can't be correct...)
? map->rooms->layout[600] 'prints 9 (correct)

sleep

end

open "totaltest.map" for binary as #1

put #1,, *map

close



deallocate map->rooms->layout
deallocate map->rooms
deallocate map



edited: asically rewrote the code from scratch, and still completely clueless as to how to do what im trying..

edited again: well, "? map->rooms->layout[600] 'prints 9 (correct)"
returns the right number, so the array IS allocated... how the fuck do i look at the LEN? and how do i save the entire "map" structure to a file?
Look at this BS test I did awhile back, it should help. I'm too lazy to use your code Smile.

Code:
#define OBJID ((Asc("O") Shl 16) Or (Asc("B") Shl 8) Or Asc("J"))

Type Vert
     x As Single
     y As Single
     z As Single
End Type

Type Tri
     verts(2) As Vert
End Type

Type Object
     id As Integer
     numoftris As Integer
     tris As Tri Ptr
End Type

Dim obj As Object

'Writing Struct
'***********
With obj
     .id = OBJID
     .numoftris = 7
     .tris = Allocate(.numoftris * Len(Tri))
     For i = 0 To .numoftris - 1
        For v = 0 To 2
            .tris[i].verts(v).x = Rnd * 23
            .tris[i].verts(v).y = Rnd * 75
            .tris[i].verts(v).z = Rnd * 51
        Next v
     Next i
End With

newfile = FreeFile
Open "data.obj" For Binary As #newfile
Put #newfile,,obj.id
Put #newfile,,obj.numoftris
For i = 0 To obj.numoftris - 1
    Put #newfile,,obj.tris[i].verts()
Next i

Close #newfile

Deallocate(obj.tris)

'Reading File
'**********
Dim newobj As Object, temptri As Tri

readfile = FreeFile
Open "data.obj" For Binary As #readfile

Screen 20,16,,1


With newobj
     Get #readfile,,.id
     If .id = OBJID Then
       Print "Id: "; .id; " -> "; Chr$(.id Shr 16) + Chr$(.id Shr 8) + Chr$(.id)
       Get #readfile,,.numoftris
       Print "Number of triangles: "
       Color Rgb(255,0,0) : Locate 2, 21 : Print .numoftris
       .tris = Allocate(.numoftris * Len(Tri))
       Color Rgb(255,255,20)
       Print String$(45, Asc("-"))
       For i = 0 To .numoftris - 1
           Get #readfile,,.tris[i].verts()
           Print "Triangle: ";i + 1
           Print "X: "; .tris[i].verts(0).x; " Y: "; .tris[i].verts(0).y; " Z: "; .tris[i].verts(0).z
           Print "X: "; .tris[i].verts(1).x; " Y: "; .tris[i].verts(1).y; " Z: "; .tris[i].verts(1).z
           Print "X: "; .tris[i].verts(2).x; " Y: "; .tris[i].verts(2).y; " Z: "; .tris[i].verts(2).z
           Print String$(45, Asc("-"))
       Next i
       Close #readfile
     End If
End With

Sleep
[/quote]

Anonymous

thanks! that made me realize i couldnt just be a bum and put the whole map in one put =P


heres my code now if youd like to cheg it out

Code:
TYPE mapheader FIELD=1

  numofrooms AS UBYTE
  tilesetname AS string

END TYPE


TYPE RoomHeader FIELD=1

  dimensions AS ushort
  numofdoors AS UBYTE
  numoflayers as ubyte
  layout as ushort ptr ptr

END TYPE


type maptype field=1

  info as mapheader
  rooms as roomheader ptr
  totaldoors as short
  doors as integer ptr
  numoftiles as short
  tilesizex as ubyte
  tilesizey as ubyte
  tileset as short ptr

end type

dim map as maptype ptr

map = callocate(len(maptype))  

map->info.numofrooms = 1

map->info.tilesetname = ""
map->info.tilesetname = "grass.spr"

map->rooms = callocate(map->info.numofrooms * len(roomheader))

for stuffderooms = 0 to map->info.numofrooms - 1

map->rooms[stuffderooms].dimensions = 7710
map->rooms[stuffderooms].numofdoors = 1
map->rooms[stuffderooms].numoflayers = 4

map->rooms[stuffderooms].layout = callocate(map->rooms[stuffderooms].numoflayers * len(ushort))

for alloclayouts = 0 to map->rooms[stuffderooms].numoflayers - 1
    thisroomx = (map->rooms[stuffderooms].dimensions shr 8) + 1
    thisroomy = (map->rooms[stuffderooms].dimensions and 255) + 1
    thisroomsize = thisroomx * thisroomy

    map->rooms[stuffderooms].layout[alloclayouts] = callocate(thisroomsize * len(ushort))

next
next    
map->rooms[0].layout[0][600] = 9

map->totaldoors = 1

map->doors = callocate(map->totaldoors * len(integer))
map->doors[0] = 1016119

map->numoftiles = 98
map->tilesizex = 16
map->tilesizey = 16

map->tileset = callocate((((map->tilesizey * map->tilesizex) \ 2) + 2) * map->numoftiles * len(short))



open "totaltest.map" for binary as #1

put #1,, map->info

for filerooms = 0 to map->info.numofrooms - 1
    
    put #1,, map->rooms[filerooms].dimensions
    put #1,, map->rooms[filerooms].numofdoors

    thisroomx = (map->rooms[filerooms].dimensions shr 8) + 1
    thisroomy = (map->rooms[filerooms].dimensions and 255) + 1
    thisroomsize = thisroomx * thisroomy
    

for filelayers = 0 to map->rooms->numoflayers - 1

    for filelayouts = 0 to (thisroomsize)
        
      put #1,, map->rooms[filerooms].layout[filelayers][filelayouts]
        
    next
    
next

next

put #1,, map->totaldoors

for filedoors = 0 to map->totaldoors - 1

    put #1,, map->doors[filedoors]
    
next

put #1,, map->numoftiles
put #1,, map->tilesizex
put #1,, map->tilesizey
    
    
    for filetileset = 0 to ((((map->tilesizey * map->tilesizex) \ 2) + 2) * map->numoftiles) - 1
        'put #1,, map->tileset[filetileset]
        
    next

note: i tested this with dimensions at 65535 (256x255 room) with 3 layers and it generated the output file in less than 2 seconds :o
Quote:note: i tested this with dimensions at 65535 (256x255 room) with 3 layers and it generated the output file in less than 2 seconds :o


Hell yeah! Tongue
So what type of game are you working on?

Anonymous