QueryWorldRegion

Started by kevin, December 26, 2005, 02:27:40 PM

Previous topic - Next topic

kevin

 This PlayBASIC V1.089z code reads from a worlds partition buffer and returns a list of GFX items that share this space.

This can used to poll the world and quickly work out what items are near by.

Note:  Matched is performed upon the captures gfx bounding box.  So shapes like Circles (currently) would need to be further verified that are within the requested region.





PlayBASIC Code: [Select]
; Note:   This Code requires PB1.089z or higher
;

Setfps 60

; Convert the default font to a bitmap font
Makebitmapfont 1,$ffffff



; Create 2 viable to will be used as ours worlds size
WorldWidth =3000
WorldHeight =3000

; create a Camera (this camera will attach it self to the current surface, in
; this that example, current surface will be the Screen)
CreateCamera 1
Limitcamera 1,true
LimitCameraBounds 1,0,0,WorldWidth+1,WorldHeight+1


Restart:

; check if the world previously exists, if so, delete it
if GetWorldStatus(2) then deleteworld 2


; Create world
CreateWorld 2

; ReDirect All Drawing to be captured to world buffer #2
CaptureToWorld 2


; Create a list of Constants that will be used to ID the different gfx types
; that were captured into the world buffer
Acset =0
Constant Gfx_Item_Dot =ac(1)
Constant Gfx_Item_Line =ac(1)
Constant Gfx_Item_Box =ac(1)
Constant Gfx_Item_Circle =ac(1)
Constant Gfx_Item_Shape =ac(1)
Constant Gfx_Item_Max =ac(1)

; max size of a gfx item
Size=500

; NUmber of gfx items to capture
Max_Gfx_Items=250


; some arrays to hold the position/type information about each item in
Dim CaptureItems_ItemType(Max_Gfx_Items)
Dim CaptureItems_Coords(Max_Gfx_Items,4)
Dim CaptureItems_Radius(Max_Gfx_Items)
Dim CaptureItems_FillState(Max_Gfx_Items)
Dim CaptureItems_Colour(Max_Gfx_Items)
Dim CaptureItems_MediaIndex(Max_Gfx_Items)


; Set the Capture Z Depth to 100
CaptureDepth 100

; Capture the Gfx items
For lp=0 to Max_Gfx_Items

; Random select a Item Type ot capture into the world buffer

ThisGFXitem = Rnd(Gfx_Item_Max-1)

; reset the coords
x=0:y=0
x2=x:y2=y
col =rndrgb() and $7f7f7f
FillState =rnd(1)
radius=0
MediaIndex=0

Select ThisGfxItem

case Gfx_Item_Dot
X=rnd(WorldWidth)
Y=rnd(WorldHeight)

; draw the dot the world buffer
Dotc X,Y,Col

case Gfx_Item_Line
X=rnd(WorldWidth)
Y=rnd(WorldHeight)

X2=x+rnd(size)
Y2=Y+rnd(size)
; draw the dot the world buffer
Linec X,Y,x2,y2,Col

case Gfx_Item_Box
X=rnd(WorldWidth)
Y=rnd(WorldHeight)

X2=x+rnd(size)
Y2=Y+rnd(size)
; draw the dot the world buffer
boxc X,Y,x2,y2,fillstate,Col

case Gfx_Item_Circle
X=rnd(WorldWidth)
Y=rnd(WorldHeight)

Radius=rnd(100)
; draw the dot the world buffer
Circlec X,Y,radius,fillstate,Col



case Gfx_Item_Shape
Radius=rnd(100)
MediaIndex=GetFreeShape()
CreateCOnvexShape MediaIndex,Radius,rndrange(3,20)

X=rnd(WorldWidth)
Y=rnd(WorldHeight)

; draw/capture the Shape the world buffer
DRawShape MediaIndex,X,Y,1

endselect

; Store the info about this ITem in array, so we can read it later
CaptureItems_ItemType(lp)=ThisGfxItem
CaptureItems_Coords(lp,1)=x
CaptureItems_Coords(lp,2)=y
CaptureItems_Coords(lp,3)=x2
CaptureItems_Coords(lp,4)=y2
CaptureItems_Radius(lp)=Radius
CaptureItems_FillState(lp)=FillState
CaptureItems_Colour(lp)=col
CaptureItems_MediaIndex(lp)=MediaIndex


next

; Partition The world up into 64 by 64 cells
PartitionWorld 2,64

; Tell PB to return to Immediate drawing mode
DrawGfxImmediate
Login required to view complete source code