Main Menu

A polygon drawing array example.

Started by LemonWizard, January 15, 2009, 08:25:49 PM

Previous topic - Next topic

LemonWizard

I went through this and commented. I was just playing around, getting a better grip on arrays to see if the same old tricks would work in PlayBasic. And, go figure. THEY DO!
I'll use an array similiar to this for keeping track of several hundred NPCS in a game later.
I figure if any newbies want something to learn from I would throw this in. ^_^

PlayBASIC Code: [Select]
; PROJECT : Polygon Example
; AUTHOR : LemonWizard
; CREATED : 1/15/2009
; EDITED : 1/15/2009
;Project name: Shapes Runtime Tester
;Use: Testing rendering, Randomized Shapes, Randomized RGB
;Purpose: Test polygons circles and squares storing data in a two dimensional array
sw=getscreenwidth() ;use sw as a shortcut to screen width
sh=getscreenheight() ;use sh as a shortcut to screen height
polys=100000 ;modify for amount of polygons drawn.
dim polys(polys,11) ;create the polygon array two dimensionally. polys is equal to number of polygons in line 6
;11 slots for second dimension explained below
;(polys index for second array dimension:
;1=x1
;2=y1
;3=x2
;4=y2
;5=x3
;6=y3
;7=x4
;8=y4
;9=rgb red
;10=rgb blue
;11=rgb green

for temp=1 to polys ;create a temp loop equal to amount of polygons in array
polys(temp,1)=rnd(sw) ;set polygon x1 to array
polys(temp,2)=rnd(sh) ;set polygon y1 to array
polys(temp,3)=rnd(sw) ;set polygon x2 to array
polys(temp,4)=rnd(sh) ;set polygon y2 to array
polys(temp,5)=rnd(sw) ;set polygon x3 to array
polys(temp,6)=rnd(sh) ;set polygon y3 to array
polys(temp,7)=rnd(sw) ;set polygon x4 to array
polys(temp,8)=rnd(sh) ;set polygon y4 to array
polys(temp,9)=rnd(255) ;set polygon Red to array
polys(temp,10)=rnd(255);set polygon Blue to array
polys(temp,11)=rnd(255);set polygon Green to array
ink rgb(polys(temp,9), polys(temp,10), polys(temp,11)) ;set ink color to current polygon out of array
Quad polys(temp,1), polys(temp,2), polys(temp,3), polys(temp,4), polys(temp,5), polys(temp,6), polys(temp,7), polys(temp,8) ;draw a polygon
circle polys(temp,1), polys(temp,2), polys(temp,9), 0 ;draw a circle
box 0, polys/sh, sw, polys/sh+1, 1 ;draw a box
sync
wait 60 ;wait so the program runs slowly enough to see everything
next temp ;next part of loop. or next polygon in loop




kevin

#1
 Personally i wouldn't recommend this approach for handling objects, as it makes maintaining the program more difficult, the larger the program gets.  

 if you must use this approach, then set up a some constants that define the structure (the field offsets) of each the objects properties.   This will allow you to change the order, add/delete/insert fields without having to change every field reference in the program.  
 
PlayBASIC Code: [Select]
;Project name: Shapes Runtime Tester
;Use: Testing rendering, Randomized Shapes, Randomized RGB
;Purpose: Test polygons circles and squares storing data in a two dimensional array
sw=getscreenwidth() ;use sw as a shortcut to screen width
sh=getscreenheight() ;use sh as a shortcut to screen height

polys=10000 ;modify for amount of polygons drawn.


// Define a list of enumerated constants. These will be used for our various data fields in our array
// This way we can change the order, or add new field at any time without having to change our code
Constant Offset_X1=ac(1)
Constant Offset_Y1=ac(1)
Constant Offset_X2=ac(1)
Constant Offset_Y2=ac(1)
Constant Offset_X3=ac(1)
Constant Offset_Y3=ac(1)
Constant Offset_X4=ac(1)
Constant Offset_Y4=ac(1)
Constant Offset_RGB=ac(1)
Constant Offset_END=ac(1)



dim polys(polys,Offset_END) ;create the polygon array two dimensionally. polys is equal to number of polygons in line 6


Setfps 20
for temp=1 to polys ;create a temp loop equal to amount of polygons in array

polys(temp,Offset_X1)=rnd(sw) ;set polygon x1 to array
polys(temp,Offset_Y1)=rnd(sh) ;set polygon y1 to array
polys(temp,Offset_X2)=rnd(sw) ;set polygon x2 to array
polys(temp,Offset_Y2)=rnd(sh) ;set polygon y2 to array
polys(temp,Offset_X3)=rnd(sw) ;set polygon x3 to array
polys(temp,Offset_Y3)=rnd(sh) ;set polygon y3 to array
polys(temp,Offset_X4)=rnd(sw) ;set polygon x4 to array
polys(temp,Offset_Y4)=rnd(sh) ;set polygon y4 to array
polys(temp,Offset_RGB)=rndRGB() ; request a random RG colour


ink polys(temp,Offset_RGB) ;set ink color to current polygon out of array

Quad polys(temp,Offset_X1), polys(temp,Offset_Y1), polys(temp,Offset_X2), polys(temp,Offset_Y2), polys(temp,Offset_X3), polys(temp,Offset_Y2), polys(temp,Offset_X4), polys(temp,Offset_Y4) ;draw a polygon

circle polys(temp,Offset_X1), polys(temp,Offset_Y1), rgbR(polys(temp,Offset_RGB)), 0 ;draw a circle

box 0, polys/sh, sw, polys/sh+1, 1 ;draw a box

sync
next temp ;next part of loop. or next polygon in loop
print "DONE"
Sync
Waitkey




Alternatively,   a TYPED array will do the same thing.


PlayBASIC Code: [Select]
;Project name: Shapes Runtime Tester
;Use: Testing rendering, Randomized Shapes, Randomized RGB
;Purpose: Test polygons circles and squares storing data in a two dimensional array
sw=getscreenwidth() ;use sw as a shortcut to screen width
sh=getscreenheight() ;use sh as a shortcut to screen height

polys=10000 ;modify for amount of polygons drawn.


// Define a list of enumerated constants. These will be used for our various data fields in our array
// This way we can change the order, or add new field at any time without having to change our code
Type MyObject
x1 as integer
y1 as integer
x2 as integer
y2 as integer
x3 as integer
y3 as integer
x4 as integer
y4 as integer
Colour as integer
EndTYpe

dim polys(polys) as myobject ;create the polygon array two dimensionally. polys is equal to number of polygons in line 6


Setfps 20
for temp=1 to polys ;create a temp loop equal to amount of polygons in array

polys(temp).X1=rnd(sw) ;set polygon x1 to array
polys(temp).Y1=rnd(sh) ;set polygon y1 to array
polys(temp).X2=rnd(sw) ;set polygon x2 to array
polys(temp).Y2=rnd(sh) ;set polygon y2 to array
polys(temp).X3=rnd(sw) ;set polygon x3 to array
polys(temp).Y3=rnd(sh) ;set polygon y3 to array
polys(temp).X4=rnd(sw) ;set polygon x4 to array
polys(temp).Y4=rnd(sh) ;set polygon y4 to array
polys(temp).Colour=rndRGB() ; request a random RG colour


ink polys(temp).Colour ;set ink color to current polygon out of array

Quad polys(temp).X1, polys(temp).Y1, polys(temp).X2, polys(temp).Y2, polys(temp).X3, polys(temp).Y2, polys(temp).X4, polys(temp).Y4 ;draw a polygon

circle polys(temp).X1, polys(temp).Y1, rgbR(polys(temp).Colour), 0 ;draw a circle

box 0, polys/sh, sw, polys/sh+1, 1 ;draw a box

sync
next temp ;next part of loop. or next polygon in loop
print "DONE"
Sync
Waitkey