UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on January 08, 2005, 09:14:50 AM

Title: Load & save Integer Arrays
Post by: kevin on January 08, 2005, 09:14:50 AM
This code is a quick hack of a generic integer array load/saver.  



[pbcode]
; ====================================================================
;                  >> Generic Array Loading/Saving
; ====================================================================
;  This is really simple example to show how you can write your own
; generic (to a point) load & save array functions using array
; instances.   The functions included  allow you To save And reload
; any 1D,2D,3D Integer array.  
;
;  They work by passing the source/dest array into the function, then
;  either dumping that data to the disc, or reloading it into a
; local version of the instance array.  Since instances are basically
; redirections, the data your saving/loading is actually coming from,
; passed array.  
; ====================================================================


Dim Table(10)

Print "Original Array Data"
For lp =0 To GetArrayElements(Table(),1)
  Table(lp)=Rnd(100)
  Print Table(lp)
Next

File$="C:\TEstArray.dat"
SaveIntArray "C:\TEstArray.dat",GetArray(Table())


` ' Make another array
Dim table2(100)

Print "Loaded Array Data"
 
` Load the previously saved TABLE array into the TABLE2() array
LoadIntArray File$,GetArray(table2())
 
For lp=0 To GetArrayElements(Table2(),1)
  Print Table2(lp)
Next  

Sync
WaitKey
End

; ====================================================================
;                      >> Save Integer 1D/2D/3D Arrays <<
; ====================================================================


Function SaveIntArray(Filename$,ArrayHandle)
If FileExist(filename$)=True Then DeleteFile filename$
MakeArray Array()
SetArray Array(),ArrayHandle

Fn=GetFreeFile()
WriteFile filename$,Fn
   Dimensions=GetArrayDimensions(Array())

   WriteWord fn,dimensions
   For lp =1 To Dimensions        
     WriteInt fn,GetArrayElements(Array(),lp)
   Next
   
   Select Dimensions
      Case 1

    MakeArray Array1D()
        SetArray Array1d(),ArrayHandle

        For lp =0 To GetArrayElements(Array1D(),1)
            WriteInt fn,Array1d(lp)
        Next
      Case 2  
        MakeArray Array2D()
        SetArray Array2d(),ArrayHandle
        For ylp =0 To GetArrayElements(Array2D(),2)
         For xlp =0 To GetArrayElements(Array2D(),1)
             WriteInt fn,Array2d(xlp,ylp)
         Next
        Next
      Case 3
        MakeArray Array3D()
        SetArray Array3d(),ArrayHandle
         For zlp =0 To GetArrayElements(Array3D(),3)
          For ylp =0 To GetArrayElements(Array3D(),2)
            For xlp =0 To GetArrayElements(Array3D(),1)
             WriteInt fn,Array3d(xlp,ylp,zlp)
            Next
          Next
        Next

   EndSelect
   
CloseFile Fn     
EndFunction



; ====================================================================
;                      >> Load Integer 1D/2D/3D Arrays <<
; ====================================================================


Function LoadIntArray(Filename$,ArrayHandle)
If FileExist(filename$)=False Then Exitfunction

MakeArray Array()
SetArray Array(),ArrayHandle


Fn=GetFreeFile()
ReadFile filename$,Fn
   Dimensions=ReadWord(fn)
   Dim Dimensions(3)
   For lp =1 To Dimensions        
     Dimensions(lp)=ReadInt(fn)
   Next
   
   Select Dimensions
      Case 1
        ` Save the Data from a 1D array

    MakeArray Array1D()
        SetArray Array1d(),ArrayHandle
        Dim Array1d(dimensions(1))

        For lp =0 To Dimensions(1)
            Array1d(lp)=ReadInt(fn)
        Next

      Case 2  
        ` Save the Data from a 2D array
        MakeArray Array2D()
        SetArray Array2d(),ArrayHandle
    Dim Array2d(Dimensions(1),Dimensions(2))
    
        For ylp =0 To Dimensions(2)
         For xlp =0 To Dimensions(1)
            Array2d(xlp,ylp)=ReadInt(fn)
         Next
        Next

      Case 3
        ` Save the Data from a 3D array
        MakeArray Array3D()
        SetArray Array3d(),ArrayHandle
    Dim Array3d(Dimensions(1),Dimensions(2),Dimensions(2))
         For zlp =0 To Dimensions(3)
          For ylp =0 To Dimensions(2)
            For xlp =0 To Dimensions(1)
             Array3d(xlp,ylp,zlp)=ReadInt(fn)
            Next
          Next
        Next

      Default
        Print "This Function only supports 1D,2D & 3D arrays"
   EndSelect
   
CloseFile Fn     
EndFunction


[/pbcode]

Title: Load & save Integer Arrays
Post by: tomazmb on January 08, 2005, 03:01:03 PM
Hello Kevin,

I can't say I fully understand the meaning of arrays. Can you explain a bit more the purpose of 1D, 2D and 3D arrays in a bit more english way (or what is useful for what in gaming sense). Thanks.

Have a nice day,

Tomaz
Title: Load & save Integer Arrays
Post by: Travis P on January 09, 2005, 09:46:04 PM
@kevin:
Thats just what I needed, Thx.

@tomazmb:
Arrays can store many values, for example.
1 demision

dim TheArray(10)

now you can use TheArray(0),TheArray(1),....,TheArray(9),TheArray(10) Like normal varibles.
2 demisions

dim TheArray(10,1)

now you can use TheArray(0,0),TheArray(0,1),....,TheArray(10,0),TheArray(10,1).
and then 3 demisions

Its can be helpfull in alot of ways.
Heres one simple way.

for num=0 to 10
TheArray(num)=Rnd(99)
next num

instead of...

number_one=Rnd(99)
number_two=Rnd(99)
...


Hope that helps.
Title: Load & save Integer Arrays
Post by: tomazmb on January 10, 2005, 11:12:26 AM
Hello Travis P,

Thanks, every bit of information helps.

Have a nice day,

Tomaz