Messing with arrays
Blitz3D Forums/Blitz3D Beginners Area/Messing with arrays
| ||
Hi, I have several 2d arrays of different sizes. For my program I'd like to access these arrays using an array to point to anyone of the arrays. array_1( 100, 100 ) array_2( 34, 34) array_3(1024,1024) I'd like to do something like work_array(,) = array_1 and then later do work_array(,) = array_2, etc. and access array_1,2,3 through work_array() Any ideas? Would Bmax be better for this kind of thing? Thanks, Jim. |
| ||
With Blitzmax, you can do it easily.' create a 2 dimension array Function CreateArray2I:int[][](w,h) Local array:int[][h] For local a:int = 0 until h array[a] = new int[w] Next return array End Function Local array_1:int[][] = CreateArray2I(100,100) Local array_2:int[][] = CreateArray2I(34,34) Local array_3:int[][] = CreateArray2I(1024,1024) Local work_array:int[][] work_array = array_1 work_array = array_2 With blitz3d it's a bit more complicated ... You have "Dim" arrays that are Global and can't be pointed with a variable You have static arrays that can be passed as reference to function but are ... static ! (you can't define a new dimension and the size of the array must be a constant) And then you have banks and types. You can create a "fake array" using type but it requires some work to get it working. ; with arrays Const BUFFER_SIZE% = 1024 Type TArray Field length% Field ni, nj Field capacity% Field buffer.TArrayBuffer End Type Type TArrayBuffer Field mData%[BUFFER_SIZE] Field mNext.TArrayBuffer End Type Function CreateArray2I.TArray(w,h) Local a.TArray = New TArray a\length = w*h a\ni = w a\nj = h a\buffer = New TArrayBuffer a\capacity = BUFFER_SIZE Local b.TArrayBuffer = a\buffer While a\capacity < a\length b\mNext = New TArrayBuffer b = b\mNext a\capacity=a\capacity+BUFFER_SIZE Wend Return a End Function Function GetArray2I%(a.TArray,i,j) Local o = i+j*a\ni Local b.TArrayBuffer = a\buffer While o>=BUFFER_SIZE b=b\mNext o=o-BUFFER_SIZE Wend Return b\mData[o] End Function Function SetArray2I%(a.TArray,i,j, v) Local o = i+j*a\ni Local b.TArrayBuffer = a\buffer While o>=BUFFER_SIZE b=b\mNext o=o-BUFFER_SIZE Wend b\mData[o] = v End Function ; with banks Function Bank2I(w,h) Local bank = CreateBank(4+w*h*4) PokeShort(bank, 0, w) PokeShort(bank, 2, h) Return bank End Function Function PeekBank2I(bank, i,j) Local w=PeekShort(bank,0) Return PeekInt(bank, 4+4*(i+j*PeekShort(bank,0))) End Function Function PokeBank2I(bank, i,j, v) Local w=PeekShort(bank,0) PokeInt(bank, 4+4*(i+j*PeekShort(bank,0)), v) End Function ; create 3 arrays Local a1.TArray = CreateArray2I(100,100) Local a2.TArray = CreateArray2I(34,34) Local a3.TArray = CreateArray2I(1024,1024) ; push some data in array2 For y = 0 To 33 For x = 0 To 33 SetArray2I(a2, x,y, x+y*34) Next Next ; set a pointer to a2 Local a.TArray = a2 ; get value at index Print GetArray2I(a, 12,12) ; works the same with 2D bank arrays Local b1 = Bank2I(100,100) Local b2 = Bank2I(34,34) Local b3 = Bank2I(1024,1024) For y = 0 To 33 For x = 0 To 33 PokeBank2I(b2, x,y, x+y*34) Next Next Local b = b2 Print PeekBank2I(b, 12,12) WaitKey() End But you have to know it's not very fast. |
| ||
Though things may seem a bit more complicated. How much harder would it be to simply up your dimensions? Performing all applicable actions within 1 array ~albeit 3 dimensions. For Example: Dim ArrayList(max_x,max_y,2) The first two values are the current points you're attempting to work. The third value signifies what collection of points to work. ArrayList(0,0,0) any defined point within the array of (any_x,any_y,0) could be lets say..terrain data. Whereas, ArrayList(0,0,1) could contain the information for foliage data... At this point the array only needs to be as big as your largest data set. Doing so, will allow all information of subsequent sets to still be put into terms the mind can easily understand. Depending on how far you go with your data compression. ;) |
| ||
A bank is more in line with what you are wanting to do if using b3d....a bit clumsier to use however. Unfortunately you cant change the array to point at another array reference in blitz3d. However as said you can with a bank and with a little effort you can create a set of functions that treat a bank like an array. |
| ||
I went with a version of Omnicode's sugestion within one of my projects, good luck. |
| ||
Thanks for the help guys. Jim. |