GetSpriteRect
Status = GetSpriteRect(SpriteIndex, RectPointer)
 
Parameters:

    SpriteIndex = The index of the sprite you wish to query
    RectPointer = Pointer to a Rectangle structure.
Returns:

    Status = Returns a 0 when it failed, other wise it'll return one
 

      The GetSpriteRect function returns the bounding coordinates that surround a rotated and scaled sprite. GetSpriteRect acts as a short cut to manually querying the sprite via the GetSpriteVertexX and GetSPriteVertexY commands. However, unlike other sprite commands which only return a single value, GetSpriteRect requires we pass it a pointer to suitabel formatted RECT structure. A RECT structure is just a type with four integer fields. The command treats the first pair of fields as the top left XY coordinate and the second pair is the bottom right XY coordinate. It works this way so we can avoid numerous functions calls to the sprite, which can be fairly time consuming.






FACTS:


      * GetSpriteRect can't validate the Rect pointer you give it is legayc or not. So if you pass a pointer that doesn't exist, it'll most likely crash.


      * Also see GetSpriteX,GetSpriteY, GetSpriteVertexX,GetSpriteVertexY



Example #1:


      Create a sprite, set it's XYZ positions, then read and display its position to the screen.

  
  
  
  SetFPS 60
  
  // locate the help files media folder
  Path$=ProgramDir$()+"Help\Commands/Media/"
  
  // Load the bubble image
  BallImage     =LoadNewImage(Path$+"bubble_64x64.bmp",2)
  
  
  
  Type MyRect
     x1,y1
     x2,y2
  EndType
  
  // define a pointer to this rect structre
  Dim BoundingBox As MyRect Pointer
  
  // allocate / create an instance of this structure
  BoundingBox = New MyRect
  
  
  // CReate sprite for us to query
  Sprite=NewSprite(400,300,BallImage)
  SpriteDrawMode Sprite,2
  ScaleSprite Sprite,2
  
  
  
  // Main Loop
  Do
     // Clear the screen to the default colour
     Cls
     
     // turn the sprite 1 degree
     TurnSprite Sprite,1
     
     // draw all the sprites
     DrawAllSprites
     
     // Query the sprites bounding box
     If GetSpriteRect(Sprite,BoundingBox)=true
        
        // read the sprites vertex manually
        x1=GetSpriteVertexX(Sprite,0)
        y1=GetSpriteVertexY(Sprite,0)
        x2=GetSpriteVertexX(Sprite,1)
        y2=GetSpriteVertexY(Sprite,1)
        x3=GetSpriteVertexX(Sprite,2)
        y3=GetSpriteVertexY(Sprite,2)
        x4=GetSpriteVertexX(Sprite,3)
        y4=GetSpriteVertexY(Sprite,3)
        
        // Connect the egdes in red
        LineC x1,y1,x2,y2,$ff0000
        LineC x2,y2,x3,y3,$ff0000
        LineC x3,y3,x4,y4,$ff0000
        LineC x4,y4,x1,y1,$ff0000
        
        
        // draw the bounding box in GREEN
        BoxC BoundingBox.X1,BoundingBox.Y1,BoundingBox.X2,BoundingBox.Y2,false$00ff00
        
     EndIf
     
     Sync
  Loop
  
  





Example #2:


      The following shows a more complex usage where the bounding area of a sprite is used to selective refresh the displays backdrop picture. This particular example creates a back drop image, converts that into a MAP, then prior to each frame being drawn it only restores the blocks on the screen that were drawn over last frame. The idea of such approach is to reduce the amount of the drawing the program does. Many classic games use this type of rendering loop to keep the speed up as high as possible, it's still very useful today !

 
Example Source: Download This Example
  LoadFont  "arial",1,16,0,8
  
  
  // -----------------------------------------------------------
  // Size the Map blocks we'll use.
  // -----------------------------------------------------------
  Constant TileWidth   =32
  Constant TileHeight   =32
  
  // -----------------------------------------------------------
  // Make the Backdrop image.  This buffer is also used for the 'FX surface'
  // -----------------------------------------------------------
  BackDropFX =MakeBackdropImage()
  
  
  // -----------------------------------------------------------
  // Make a blob image to represent the sprites in the scene
  // -----------------------------------------------------------
  Blob= MakeBlobImage(64,64)
  
  
  // -----------------------------------------------------------
  // Define our RECT structure to hold the bounds of each sprite
  // -----------------------------------------------------------
  
  Type tRect
     x1,y1
     x2,y2
  EndType
  
  // -----------------------------------------------------------
  // Define a type to simulate some characters in the scene.
  // -----------------------------------------------------------
  Type tAlien
     Sprite
     speed#
     direction#
     Rect As tRect
  EndType
  
  // ------------------------------------------------------
  // Dim a list called Character to hold the objects
  // ------------------------------------------------------
  Dim Character As TAlien List
  
  
  // ------------------------------------------------------
  // Set the clip zone of the object
  // ------------------------------------------------------
  clipSize=64
  ViewportX1=-clipSize
  ViewportX2=GetScreenWidth()+clipSize
  ViewportY1=-clipSize
  ViewportY2=GetScreenHeight()+clipSize
  
  
  // ------------------------------------------------------
  // MAKE MAP / LEVEL / BLOCKS from our backdrop picture
  // ------------------------------------------------------
  
  Map,OriginalLevel,RefreshLevel=Make_Map_FRom_Image(BackDropFX)
  
  
  
  
;*=-----------------------------------------------------------------------------=*
;         >> Main Loop <<
;*=-----------------------------------------------------------------------------=*
  
  
  Screen=NewImage(GetScreenWidth(),GetScreenHeight(),2)
  
  
  Do
     
     // Add new charcter to scene if the space key is being pressed
     If SpaceKey()  Or  LastTime<Timer()
        
        LastTime=Timer()+50
        Add_Random_Character(Blob)
        
     EndIf
     
     
     // Process and render to the objects in the character list
     RenderToImage Screen
     
     
     // Draw the refresh level to our FX image screen
     DrawMap Map,RefreshLevel,0,0
     
     // reset the refresh level
     ClearLevel Map,RefreshLevel,0
     
     
     // Process our list of characters
     For Each Character()
        
        ThisSprite=Character.Sprite
        Speed#          =Character.speed#
        Angle#          =Character.direction#
        
        MoveSprite ThisSprite,CosRadius(angle#,speed#),SinRadius(angle#,speed#)
        
      ; Check if the character is inside the screen ?
        If RectHitSprite(ViewportX1,ViewportY1,ViewportX2,ViewportY2,ThisSprite)=false
           DeleteSprite Sprite
           Character=null
           Continue
        EndIf
        
        // Get the rect around this sprite
        If GetSpriteRect(ThisSprite,Character.Rect)
           // Convert Pixels coordinates to Map Tile positions
           x1=Character.Rect.x1/TileWidth
           y1=Character.Rect.y1/TileHeight
           x2=Character.Rect.x2/TileWidth
           y2=Character.Rect.y2/TileHeight
           CopyLevel   Map,OriginalLevel,x1,y1,x2+1,y2+1,Map,RefreshLevel,TilePad+x1,TilePad+y1
        EndIf
     Next
     
     
     // Draw the Sprite List to the FX buffer image
     DrawAllSprites
     
     
     // Direct rendering to the PB display
     RenderToScreen
     
     // Draw the current state of the Backdrop to the SCREEN
     DrawImage Screen,0,0,false
     
     // Draw the info to the screen for the viewer
     Text 00,"Refresh %:"+CalcRefreshPercentage(Map,RefreshLevel)
     Text 0,20,"Fps:"+Str$(FPS())
     
     Sync
  Loop
  
  
  
  
;*=-----------------------------------------------------------------------------=*
;                        >> Add Random Character <<
;*=-----------------------------------------------------------------------------=*
;
;  This function adds a new character to our liost and fills it with random
; position and speed data.
  
;*=-----------------------------------------------------------------------------=*
  
  
Psub Add_Random_Character(ThisImage)
  
  Character             = New tAlien
  x         =Rnd(GetScreenWidth())
  y           =Rnd(GetScreenHeight())
  Character.Sprite          =NewSprite(X,y,ThisIMage)
  Character.Speed      =RndRange(1,5)
  Character.direction# =Rnd(360)
  
EndPsub
  
  
  
;*=-----------------------------------------------------------------------------=*
;                        >> Calc ReFresh Percentage <<
;*=-----------------------------------------------------------------------------=*
;
;  This function converts an image into the array of tiles representing this
; image.  Each cell in the array contains the image handle of that 'sector'
; of the original image.   This is used to refresh the image during refresh.
;
;*=-----------------------------------------------------------------------------=*
  
  
Psub CalcRefreshPercentage(Map,Level)
  Count=0
  Width=GetLevelWidth(Map,Level)
  Height=GetLevelHeight(Map,Level)
  
  Total = (Width+1)*(Height+1)
  
  For ylp=0 To Height
     For xlp=0 To Width
        Tile=PeekLevelTile(Map,Level,xlp,ylp)
        Count+=Tile<>0
     Next
  Next
  
  p#=Float(Count)/Total
  
  s$=Str$(p#*100   )
  
EndPsub s$
  
  
  
  
  
  
;*=-----------------------------------------------------------------------------=*
;                           >> Make Blob image <<
;*=-----------------------------------------------------------------------------=*
;
;    This function makes a Blob (circle/ellipse) image with alpha channel, which
; is used to represent a hand drawn sprite in your game.
;
;*=-----------------------------------------------------------------------------=*
  
  
Function MakeBlobImage(width,height)
  
  oldsurface=GetSurface()
  Thisimage=GetFreeImage()
  CreateFXImageEx ThisImage,Width,Height,32
  RenderToImage Thisimage
  radiusX=Width/2
  radiusY=Height/2
  
  LockBuffer
  EllipseC (width/2),(height/2),radiusX,radiusY,true,RGB(255,255,255)
  
  nullpixel=Point(0,0)
  For ylp=0 To height-1
     A=Float(ylp)/height*255
     A=LSL32(a,24)
     For xlp=0 To width-1
        ThisPixel=FastPoint(xlp,ylp) And $00ffffff
        If ThisPixel
           FastDot xlp,ylp,ThisPixel Or A
        EndIf
     Next
  Next
  UnLockBuffer
  PrepareAFXImage ThisImage
  RenderToImage oldsurface
EndFunction ThisImage
  
  
  
;*=-----------------------------------------------------------------------------=*
;                           >> Make Backdrop image <<
;*=-----------------------------------------------------------------------------=*
;
;   This function create a backdrop image.  It's a bit of mess, but it'll do for
; the sake of the demo.  In a real game you'd obviously load your own backdrop.
;*=-----------------------------------------------------------------------------=*
  
  
Function MakeBackdropImage()
  
  // Make the backdrop
  sw=GetScreenWidth()
  sh=GetScreenHeight()
  backdrop=NewImage(sw,sh,2)
  RenderToImage backdrop
  c1=ARGB(255,0,0,100)
  c2=ARGB(255,70,50,0)
  c3=ARGB(255,0,200,0)
  c4=ARGB(255,0,0,0)
  ShadeBox 0,0,sw,sh,c1,c2,c3,c4
  LoadFont  "Arial",2,64,0
  
  SetFont 2
  For lp=0 To 200
     Ink RndRGB()| $ff000000
     CenterText Rnd(sw),Rnd(sh),"<>"
  Next
  
  Ink ARGB(255,55,55,55)
  x=sw/2
  y=sh*0.4
  CenterText x-2,y-2,"Game Backdrop Image"
  Ink ARGB(255,255,255,255)
  CenterText x,y,"Game Backdrop Image"
  SetFont 1
  DeleteFont 2
  RenderToScreen
  
EndFunction BackDrop
  
  
  
Function Make_Map_FRom_Image(ThisIMage)
  
  // ------------------------------------------------------
  // MAKE MAP / LEVEL / BLOCKS from our backdrop picture
  // ------------------------------------------------------
  
  Map=NewMap(10)
  
  IMageWidth   =GetImageWidth(ThisIMage)
  IMageHeight   =GetImageHeight(ThisIMage)
  
  TilesWide   =IMageWidth/TileWidth
  TilesHigh   =IMageHeight/TileHeight
  
  If (TilesWide*TileWidth)>ImageWidth Then TilesWide++
  If (TilesHigh*TileHeight)>ImageHieght Then TilesHigh++
  
  BlockCount=(TilesWide*TilesHigh)+1
  CreateMapGFX Map,TileWidth,TileHeight,BlockCount,RGB(0,0,0),2
  
  OriginalLevel=NewLevel(Map,TilesWide-1,TilesHigh-1)
  
  RenderToImage ThisImage
  
  // ----------------------------------
  // Build map representation of this image
  // ----------------------------------
  Index=1
  For ylp=0 To TilesHigh-1
     For xlp=0 To TilesWide-1
        Xpos=xlp*TileWidth
        Ypos=ylp*TileHeight
        GetMapBlk Map,Index,Xpos,Ypos
        PokeLevelTile Map,OriginalLevel, Xlp,ylp,Index
        Index++
     Next
  Next
  
  
  RefreshLevel   =NewLevel(Map,TilesWide-1,TilesHigh-1)
  
  PasteLevel map,OriginalLevel,Map,RefreshLevel,0,0,0
  
  LevelTransparent  Map,RefreshLevel,0
  
  
EndFunction Map,OriginalLevel,RefreshLevel
  
  
  
 
Related Info: GetSpriteOldX | GetSpriteOldY | GetSpriteOldZ | GetSpriteX | GetSpriteY | GetSpriteZ | MoveSprite | MoveSpriteX | MoveSpriteY | MoveSpriteZ | PositionSprite | PositionSpriteX | PositionSpriteXYZ | PositionSpriteY | PositionSpriteZ :
 


(c) Copyright 2002 - 2024 - Kevin Picone - PlayBASIC.com