UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on December 26, 2005, 02:27:40 PM

Title: QueryWorldRegion
Post by: kevin on December 26, 2005, 02:27:40 PM
 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.





[pbcode]


; 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



   Camx#=WorldWidth/2
   Camy#=WorldHeight/2
 



; =================================================
; CReate a Memory bank.  This Bank is where we'll store the QueryWorldRegion results
; =================================================

; CReate the bank with plenty space
   CreateBank 1,1000   

; Get the pointer (address) of this bank in memory
   DestAddress=GetBankPtr(1)



; start of DO/Loop
   Do

   ; Tell PB to redirect all GFX drawing commands to the scene buffer
 CaptureToScene
 
   ; Clear the scene buffer
 ClsScene

 
   ;Move the Camera With Arrow Keys
 CamSpeed#=5
 If LeftKey() then CamX#=CamX#-CamSpeed#
 If RightKey() then CamX#=CamX#+CamSpeed#
 If UpKey() then CamY#=CamY#-CamSpeed#
 If DownKey() then CamY#=CamY#+CamSpeed#
   


 
   ; Calc A region 200*200 around the current camera position

 x1#=Camx#-100
 y1#=Camy#-100
 x2#=Camx#+100
 y2#=Camy#+100

   ; draw this region the user can see it
 capturedepth 1
 box x1#,y1#,x2#,Y2#,0


; Query World Region check the worlds parition buffer for a list of objects
; that share this region.
   
 ; The function returns the number of Items that who's bounding box share the
 ;  same region.
 
 
; NOTE:  Objects are returned in a pre-allocated memory buffer, function
;        expected you to pass it a POINTER to this buffer.  So you
;        actually get it write the valeus into an INTEGER array if you
;        want also.  While it's not a easy for the user, it's a  
 
   
 ItemsFound=QueryWorldRegion(2,x1#,y1#,x2#,y2#,DestAddress)

 if ItemsFound>0

    CaptureDepth 10
 
    For lp=0 to (ItemsFound-1)
   
   GfxItem=peekBankInt(1,lp*4)

     x=CaptureItems_Coords(GfxItem,1)
     y=CaptureItems_Coords(GfxItem,2)
     x2=CaptureItems_Coords(GfxItem,3)
     y2=CaptureItems_Coords(GfxItem,4)

   Radius  =CaptureItems_Radius(GfxItem)
   FillState   =CaptureItems_FillState(GfxItem)
   MediaIndex   =CaptureItems_MediaIndex(GfxItem)
   Col     =rgbfade(CaptureItems_Colour(GfxItem),200)

       
   Select CaptureItems_ItemType(GfxItem)

      case Gfx_Item_Dot
      ; draw the dot the world buffer
     Dotc X,Y,Col         
     circlec x,y,3,0,rndrgb()

      case Gfx_Item_Line
      ; draw the dot the world buffer
     Linec X,Y,x2,y2,Col         
     circlec x,y,3,0,rndrgb()
     circlec x2,y2,3,0,rndrgb()

      case Gfx_Item_Box
      ; draw the dot the world buffer
     boxc X,Y,x2,y2,fillstate,Col         
     circlec x,y,3,0,rndrgb()
     circlec x2,y2,3,0,rndrgb()
     circlec x2,y,3,0,rndrgb()
     circlec x,y2,3,0,rndrgb()

      case Gfx_Item_Circle
      ; draw the dot the world buffer
     Circlec X,Y,radius,fillstate,Col         

     circlec x,y,2,0,rndrgb()


      case Gfx_Item_Shape
     ink Col
      ; draw/capture the Shape the world buffer
     DRawShape MediaIndex,X,Y,2

   endselect
 
 ; Store the info about this ITem
    CaptureItems_ItemType(lp)=ThisGfxItem


    

    next
 
 endif
 

 PositionCamera 1,CamX#-(getscreenwidth()/2),Camy#-(getScreenHeight()/2)

 capturedepth 100
 CameraGrabWorld 1,2


 
; draw the camera  
 DrawCamera 1

; show the fps rate and continue this loop
 text 0,0,fps()
 text 0,10,itemsfound

 if spacekey() then goto restart


 sync
   loop
   


[/pbcode]