News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Using 1D arrays as a 2D array

Started by kevin, February 02, 2011, 09:52:13 PM

Previous topic - Next topic

kevin

  Using 1D arrays as a 2D array

   This example uses a fixed size 1d array within a type and addresses it as if it were a 2D array, which is represented on screen as a grid pattern.  To do this we declare the array field as being Width*Height cells.  To address the correct cell, we're multiplying the Y coordinate by the array width and adding the X coordinate.    

   To simplified the set/getting of values from our emulated 2d array, I've wrapped some function that will read fvalues from and write values to the array, with bound clipping.


PlayBASIC Code: [Select]
;   Addressing a 1D array as if was a 2D array

Constant GridWidth =10
Constant GridHeight =20

Type tGrid

; Screen position
Xpos,Ypos

; Movement Speed along X axis
SpeedX

; Movement Speed along Y axis
SpeedY

// width* height ofthe grid cells on screen
BlockWidth
BlockHeight

// COlour of the grid
Colour

// our 2d array declared as a 1D array
GridArray(GridWidth*GridHeight)
EndType

; declare a list of grids
Dim Grid as tGrid list


; ruin through and add/initialize them randomly
For lp=1 to 10

; alloc and add new item to the Grid List
Grid = New tGrid

; declare a pointer of tGrid Type,
Dim Me as tGrid pointer

; Get a pointer to the current list item
; and store it in ME. SO now ME is pointing at
; this item inside the list.
me=GetListPtr(Grid())

; Init the fields with some random values
me.Xpos=rnd(800)
me.ypos=rnd(600)
me.speedx=rnd(2)
me.speedy=rnd(2)
me.Colour=rndrgb()

me.BlockWidth =rndrange(1,3)*16
me.BlockHeight=rndrange(1,3)*16

; Init the 2d array within this field
for ylp=0 to GridHeight-1
for xlp=0 to GridWidth-1
; Call the SetGrid function to store
; this value at this X/Y coordinate within
; the
SetGrid(Me, Xlp,ylp,rnd(100))
next
next

next


Setfps 60

Do
Cls
// draw them
For Each Grid()
Me=GetListPtr(Grid())
Me.xpos+=Me.SpeedX
if Me.xpos>800 then Me.xpos=-800
Me.ypos+=Me.SpeedY
if Me.ypos>600 then Me.ypos=-600

DrawGrid(Me)
next

Sync
loop



psub DrawGrid(Me as tGrid pointer)
if int(me)

Ypos=Me.Ypos
BlockWidth =me.blockWidth
BlockHeight =me.blockHeight

for ylp=0 to GridHeight-1
Xpos=Me.Xpos

Ypos2=Ypos+BlockHeight
for xlp=0 to GridWidth-1
// Call the get grid function to get
// this item from the array
Level=GetGrid(Me, Xlp,ylp)

// Use this value as the fade level of the grid colour
ThisRgb=rgbFade(Me.Colour,Level)

// draw a blox to represent this cell on screen
Boxc Xpos,Ypos,Xpos+BlockWidth,Ypos2,true,ThisRGB


Xpos+=BlockWidth
next

Ypos+=BlockHeight
next




endif
EndPsub



Psub SetGrid(Me as tGrid pointer, Xpos,Ypos,Value)
; Perform some safety checks on the cordinates, so we can't
; read/write outside of the array data block
if Xpos>-1 and Xpos<GridWidth
if Ypos>-1 and Ypos<GridHeight
Ypos*=GridWidth
me.GridArray(Ypos+Xpos) = Value
endif
endif
endPsub

Psub GetGrid(Me as tGrid pointer, Xpos,Ypos)
; Some safety check on the cordinates
if Xpos>-1 and Xpos<GridWidth
if Ypos>-1 and Ypos<GridHeight
Ypos*=GridWidth
Value=me.GridArray(Ypos+Xpos)
endif
endif
endPsub Value