Datan lukeminen onnistuu PeekArrayI(arr, x, y) ja PeekArrayF(arr, x, y):lä. Taulukon koot saa: ArrayWidth() ja ArrayHeight() komennoilla, taulukkoa voi venytellä uudelleen ResizeArray(arr, width, height) komennolla. DeleteArray(arr) poistaa Array2D tyyppijäsenen, ja sen sisältämän muistipalan.
Funktiot + halpa esimerkki:
Code: Select all
Type Array2D
Field Array As Integer
Field Width As Integer
Field Height As Integer
End Type
Function CreateArray(width, height)
width = width + 1
height = height + 1
arr.Array2D = New (Array2D)
arr\Width = width
arr\Height = height
arr\Array = MakeMEMBlock(4*width*height)
Return ConvertToInteger(arr)
End Function
Function PokeArrayI(array, x, y, dat%)
arr.Array2D = ConvertToType(array)
If x < arr\Width And y < arr\Height Then
PokeInt arr\Array, 4*((y*arr\Width)+x), dat
Else
MakeError "You are attempting To Write over the array!"
EndIf
EndFunction
Function PokeArrayF(array, x, y, dat#)
arr.Array2D = ConvertToType(array)
If x < arr\Width And y < arr\Height Then
PokeFloat arr\Array, 4*((y*arr\Width)+x), dat#
Else
MakeError "You are attempting To Write over the array!"
EndIf
EndFunction
Function PeekArrayI%(array, x, y)
arr.Array2D = ConvertToType(array)
If x < arr\Width And y < arr\Height Then
Return PeekInt(arr\Array, 4*((y*arr\Width)+x))
Else
MakeError "Peeking failed! You're attempting to read over the array!"
EndIf
EndFunction
Function PeekArrayF#(array, x, y)
arr.Array2D = ConvertToType(array)
If x < arr\Width And y < arr\Height Then
Return PeekFloat(arr\Array, 4*((y*arr\Width)+x))
Else
MakeError "Peeking failed! You're attempting to read over the array!"
EndIf
EndFunction
Function ResizeArray(array, width, height)
width = width + 1
height = height + 1
arr.Array2D = ConvertToType(array)
newArr = MakeMEMBlock(4*width*height+4)
For y = 0 To (Min(arr\Height, height)-1)
MemCopy arr\Array, y*arr\Width*4, newArr, y*width*4, Int(Min(arr\Width, width))*4
Next y
DeleteMEMBlock arr\Array
arr\Array = newArr
arr\Width = width
arr\Height = height
EndFunction
Function ArrayWidth(array)
arr.Array2D = ConvertToType(array)
Return arr\Width-1
EndFunction
Function ArrayHeight(array)
arr.Array2D = ConvertToType(array)
Return arr\Height-1
EndFunction
Function DeleteArray(array)
arr.Array2D = ConvertToType(array)
DeleteMEMBlock arr\Array
Delete arr
EndFunction
SCREEN 640, 640
tileMap = CreateArray(10, 10)
tilesetti = LoadAnimImage("Media\Tileset.BMP", 32, 32, 0, 256)
For i = 0 To 10
For j = 0 To 10
PokeArrayI(tileMap, i, j, Rand(255))
Next j
Next i
Repeat
For i = 0 To 10
For j = 0 To 10
frame = PeekArrayI(tileMap, i, j)
DrawImage tilesetti, i*32, j*32, frame
Next j
Next i
DrawScreen
Forever
Code: Select all
Type Array2D
Field Array As Integer
Field Width As Integer
Field Height As Integer
End Type
Function CreateArray(width, height)
width = width + 1
height = height + 1
arr.Array2D = New (Array2D)
arr\Width = width
arr\Height = height
arr\Array = MakeMEMBlock(4*width*height)
Return ConvertToInteger(arr)
End Function
Function PokeArrayI(array, x, y, dat%)
arr.Array2D = ConvertToType(array)
If x < arr\Width And y < arr\Height Then
PokeInt arr\Array, 4*((y*arr\Width)+x), dat
Else
MakeError "You are attempting To Write over the array!"
EndIf
EndFunction
Function PokeArrayF(array, x, y, dat#)
arr.Array2D = ConvertToType(array)
If x < arr\Width And y < arr\Height Then
PokeFloat arr\Array, 4*((y*arr\Width)+x), dat#
Else
MakeError "You are attempting To Write over the array!"
EndIf
EndFunction
Function PeekArrayI%(array, x, y)
arr.Array2D = ConvertToType(array)
If x < arr\Width And y < arr\Height Then
Return PeekInt(arr\Array, 4*((y*arr\Width)+x))
Else
MakeError "Peeking failed! You're attempting to read over the array!"
EndIf
EndFunction
Function PeekArrayF#(array, x, y)
arr.Array2D = ConvertToType(array)
If x < arr\Width And y < arr\Height Then
Return PeekFloat(arr\Array, 4*((y*arr\Width)+x))
Else
MakeError "Peeking failed! You're attempting to read over the array!"
EndIf
EndFunction
Function ResizeArray(array, width, height)
width = width + 1
height = height + 1
arr.Array2D = ConvertToType(array)
newArr = MakeMEMBlock(4*width*height+4)
For y = 0 To (Min(arr\Height, height)-1)
MemCopy arr\Array, y*arr\Width*4, newArr, y*width*4, Int(Min(arr\Width, width))*4
Next y
DeleteMEMBlock arr\Array
arr\Array = newArr
arr\Width = width
arr\Height = height
EndFunction
Function ArrayWidth(array)
arr.Array2D = ConvertToType(array)
Return arr\Width-1
EndFunction
Function ArrayHeight(array)
arr.Array2D = ConvertToType(array)
Return arr\Height-1
EndFunction
Function DeleteArray(array)
arr.Array2D = ConvertToType(array)
DeleteMEMBlock arr\Array
Delete arr
EndFunction
off: ASD 1 666. viesti..
EDIT:
Esimerkkiä tehty paremmaksi ja funktio koodista otettu ylimääräinen taulukon luonti koodi pois.