Sprite Background Tiling Procedure : TileSprite()

Advanced game related topics
User avatar
griz
Enthusiast
Enthusiast
Posts: 167
Joined: Sun Jun 29, 2003 7:32 pm
Location: Canada

Sprite Background Tiling Procedure : TileSprite()

Post by griz »

Similar to Blitz Basic's TileImage() and TileBlock() commands. Basically tiles a sprite over the entire screen with position offsets and optional transparency. I haven't tested it much so please let me know if you have any improvements.

Code: Select all

Procedure TileSprite(spritenum, offsetx, offsety, swidth, sheight, trans)
  Protected cx, cy, btilewidth, btileheight
  btilewidth = SpriteWidth(spritenum)
  btileheight = SpriteHeight(spritenum)
  cx = -btilewidth
  cy = -btileheight
  offsetx = offsetx % btilewidth
  offsety = offsety % btileheight
  Repeat
    Repeat
      If trans
        DisplayTransparentSprite(spritenum,cx+offsetx,cy+offsety)
      Else
        DisplaySprite(spritenum,cx+offsetx,cy+offsety)
      EndIf
      cx + btilewidth
    Until cx > swidth + btilewidth
    cx = -btilewidth
    cy + btileheight
  Until cy > SHeight + btileheight
EndProcedure
Here is a quick demonstration :

Code: Select all


; // TileSprite() Demo Jan 2005 by Griz //

Procedure Frame(x,y,width,height,thickness)
  Protected n
  DrawingMode(4)
  For n = 0 To thickness-1
    Box(x+n,y+n,width-n*2, height-n*2)
  Next
EndProcedure

Procedure Initialization()
  Global ScreenWidth      : ScreenWidth = 1280
  Global ScreenHeight     : ScreenHeight = 1024
  Global ExitGame         : ExitGame = #False
  Global tile1, tile2, tile3
  Global backx, backy, backx2, backy2, backx3, backy3

  ; // initialize direct x and open a screen if possible //
  If InitSprite() = 0 Or InitKeyboard() = 0 Or InitMouse() = 0
    MessageRequester("Error", "Can't open DirectX 7 Or later", 0)
    End
  EndIf
  If OpenScreen(ScreenWidth,ScreenHeight,32, "Laser Gunner")
    ; // prevents full screen flicker //
    Delay(1000)
  Else
    MessageRequester("Error", "Can't open screen : "+Str(ScreenWidth)+" x "+Str(ScreenHeight), 0)
    End
  EndIf

  ; // draw background tile sprites //
  tile1 = CreateSprite(#pb_any,128,128)
  StartDrawing(SpriteOutput(tile1))
    FrontColor(0,32,64)
    Circle(32,32,8)
    Circle(96,96,8)
  StopDrawing()
  tile2 = CreateSprite(#pb_any,128,128)
  StartDrawing(SpriteOutput(tile2))
    FrontColor(0,0,64)
    Box(0,0,128,128)
    FrontColor(0,0,0)
    Circle(64,64,58)
  StopDrawing()
  tile3 = CreateSprite(#pb_any,128,128)
  StartDrawing(SpriteOutput(tile3))
    FrontColor(0,0,128)
    frame(0,0,128,128,4)
  StopDrawing()

EndProcedure

Procedure UserInput()
  ExamineKeyboard()
  If KeyboardPushed(#PB_Key_Escape)
    ExitGame=#True
  EndIf
EndProcedure

Procedure TileSprite(spritenum, offsetx, offsety, swidth, sheight, trans)
  Protected cx, cy, btilewidth, btileheight
  btilewidth = SpriteWidth(spritenum)
  btileheight = SpriteHeight(spritenum)
  cx = -btilewidth
  cy = -btileheight
  offsetx = offsetx % btilewidth
  offsety = offsety % btileheight
  Repeat
    Repeat
      If trans
        DisplayTransparentSprite(spritenum,cx+offsetx,cy+offsety)
      Else
        DisplaySprite(spritenum,cx+offsetx,cy+offsety)
      EndIf
      cx + btilewidth
    Until cx > swidth + btilewidth
    cx = -btilewidth
    cy + btileheight
  Until cy > SHeight + btileheight
EndProcedure

Procedure RenderScreen()
  FlipBuffers()
  If IsScreenActive()
    ClearScreen(0,0,32)
    tilesprite(tile1,backx,backy,screenwidth, screenheight,#true)
    tilesprite(tile2,backx2,backy2,screenwidth, screenheight,#true)
    tilesprite(tile3,backx3,backy3,screenwidth, screenheight,#true)
    If StartDrawing(ScreenOutput())  
      FrontColor(0,0,64)
      frame(0,0,screenwidth, screenheight, 32)
      DrawingMode(1)
      FrontColor(128,128,128)
      m$ = "TILESPRITE Demo - Press ESC to exit"
      Locate((screenwidth/2)-(TextLength(m$)/2),8)
      DrawText(m$)
      StopDrawing()
    EndIf    
  Else
    Delay(50)
  EndIf
EndProcedure

Procedure ScrollLimit(num) 
  If num>screenwidth
    num-screenwidth
  EndIf
  If num<-screenwidth
    num+screenwidth
  EndIf
  ProcedureReturn num
EndProcedure

Procedure AI()
  backy+2
  backx2-1  
  backx3+1
  backy3-1
  backy = ScrollLimit(backy)
  backx2 = ScrollLimit(backx2)
  backx3 = ScrollLimit(backx3)
  backy3 = ScrollLimit(backy3)
EndProcedure

Procedure DeInitialization()
  FreeSprite(tile1)
  FreeSprite(tile2)
  FreeSprite(tile3)
EndProcedure

; // main loop //
initialization()
Repeat
  AI()
  UserInput()
  RenderScreen()
Until ExitGame
deinitialization()
End

GreenGiant
Enthusiast
Enthusiast
Posts: 252
Joined: Fri Feb 20, 2004 5:43 pm

Post by GreenGiant »

Sorry to have to tell you, but you were pipped to the post. Have a look *here* That one was by someone who was missing the Blitz TileImage too I think.
User avatar
griz
Enthusiast
Enthusiast
Posts: 167
Joined: Sun Jun 29, 2003 7:32 pm
Location: Canada

Post by griz »

No problem GreenGiant, thanks for pointing out Krylar's code, I did miss that. Same effect and he uses procedures named after the Blitz commands directly. TileSprite() is named like a Pure Basic command and does what TileImage() and TileBlock() do in one procedure. More options for the PB community, if nothing else.

It would be interesting to do this with Sprite3D and have tiled background layers that can quickly fade in/out, etc.
Post Reply