'=============================================================== 
' A F l i b   I I I   S p r i t e   C o l l i s i o n   T e s t
'=============================================================== 
'In this test for AFlib III, it simply shows you how to use and
'master the pixel-perfect collision method successfully for use
'with your sprites: "AFlibIII_SprCollide"!!!  This one is
'designed to provide such beautifully flawless results every
'time in any 15- to 32-bit graphics mode of your choice, making
'game creation MUCH more seriously fun than ever in FreeBASIC
'without even having to initialize individual sprite sizes, you
'know what I mean?  (^-^)//
'       
'Be seeing you, and I sure hope this truly inspires you in your
'FB game-making most indeed!!!  ^_-=b !
' 
' 
'                                        - Adigun Azikiwe Polack 
'                               One of the Founders of Aura Flow 
'                                       Developer of "AFlib III" 
'---------------------------------------------------------------


#include "AFlib3_Sprite.bi"
#include "AFlib3_Font.bi"
#include "AFlib3_Primitives.bi"
#include "AFlib3_Miscellaneous.bi"
#include "AFlib3_Constants.bi"

Declare Sub ShadowPrint ( Byref text as String, _ 
                          Byval x as Integer, Byval y as Integer, _ 
                          Byval col as Integer, Byval sh_x as Integer, _ 
                          Byval sh_y as Integer, Byval sh_col as Integer, _ 
                          Byval chara_id as Integer ) 



'========================================================================== 
'/////// S T A R T   O F   M A I N   P O R T I O N 
'========================================================================== 

Dim BitDepth as integer


'-------------------------------------------------------------------------- 
' These are our custom sprite arrays that we will be working on here!!
'-------------------------------------------------------------------------- 
Dim as any ptr Sprite1, Sprite2
Dim as any ptr titleload
'-------------------------------------------------------------------------- 


Dim as Integer Spr1_x, Spr1_y, Spr2_x, Spr2_y, Spr2ctrl_x, Spr2ctrl_y, Spr2_Movement
Dim as Integer GFX_h, GFX_v
Dim as String CollideDet, CollideDet_BB

ScreenRes 424, 96 , , 0
Windowtitle "AFlib III -- Sprite Collision Test"
Color 15, 1: ? "          Please select your color depth!!!          "
Locate 4: Color 15, 0
Locate , 17: Color 10: ? "[1] ";: Color 14: ? "- 15-bit color": ?
Locate , 17: Color 10: ? "[2] ";: Color 14: ? "- 16-bit color": ?
Locate , 17: Color 10: ? "[3] ";: Color 14: ? "- 24-bit color": ?
Locate , 17: Color 10: ? "[4] ";: Color 14: ? "- 32-bit color"

Do
  Select case Inkey$
    Case "1": BitDepth = 15: Exit Do
    Case "2": BitDepth = 16: Exit Do
    Case "3": BitDepth = 24: Exit Do
    Case "4": BitDepth = 32: Exit Do
  End Select
Loop
Cls


ScreenRes 424, 136, , , 0
Color 15, 1: ? "        Please select your fullscreen mode!!!        "
Locate 3: Color 15, 0
Locate , 27: Color 10: ? "[1] ";: Color 14: ? "- 320x200"
Locate , 27: Color 10: ? "[2] ";: Color 14: ? "- 320x240"
Locate , 27: Color 10: ? "[3] ";: Color 14: ? "- 400x300"
Locate , 27: Color 10: ? "[4] ";: Color 14: ? "- 512x384"
Locate , 27: Color 10: ? "[5] ";: Color 14: ? "- 640x400"
Locate , 27: Color 10: ? "[6] ";: Color 14: ? "- 640x480"
Locate , 27: Color 10: ? "[7] ";: Color 14: ? "- 720x480"
Locate , 27: Color 10: ? "[8] ";: Color 14: ? "- 720x576"
Locate , 27: Color 10: ? "[9] ";: Color 14: ? "- 800x600"
Locate , 27: Color 10: ? "[0] ";: Color 14: ? "- 1024x768"
Locate , 27: Color 10: ? "[A] ";: Color 14: ? "- 1152x864"
Locate , 27: Color 10: ? "[B] ";: Color 14: ? "- 1280x1024"
Locate , 27: Color 10: ? "[C] ";: Color 14: ? "- 1600x1200"
Locate 6, 5: Color 15: ? BitDepth; "-bit color"
Locate 7, 10: Color 15: ? "is selected!"

Do
  Select case Inkey$
    Case "1": ScreenRes 320, 200, BitDepth, 2, 1: Exit Do
    Case "2": ScreenRes 320, 240, BitDepth, 2, 1: Exit Do
    Case "3": ScreenRes 400, 300, BitDepth, 2, 1: Exit Do
    Case "4": ScreenRes 512, 384, BitDepth, 2, 1: Exit Do
    Case "5": ScreenRes 640, 400, BitDepth, 2, 1: Exit Do
    Case "6": ScreenRes 640, 480, BitDepth, 2, 1: Exit Do
    Case "7": ScreenRes 720, 480, BitDepth, 2, 1: Exit Do
    Case "8": ScreenRes 720, 576, BitDepth, 2, 1: Exit Do
    Case "9": ScreenRes 800, 600, BitDepth, 2, 1: Exit Do
    Case "0": ScreenRes 1024, 768, BitDepth, 2, 1: Exit Do
    Case "A", "a": ScreenRes 1152, 864, BitDepth, 2, 1: Exit Do
    Case "B", "b": ScreenRes 1280, 1024, BitDepth, 2, 1: Exit Do
    Case "C", "c": ScreenRes 1600, 1200, BitDepth, 2, 1: Exit Do
  End Select
Loop
SetMouse , , 0

ScreenInfo GFX_h, GFX_v

Windowtitle "AFlib III -- Sprite Collision Test"

ScreenSet 1, 0
AFlibIII_PrepareCharTrans 0, 0
AFlibIII_DisplayAllChars 0, 0, 0, &hffffff, 0, 7
AFlibIII_OptimizeFontSet 0, 0, 1
ScreenSet 0, 0


' Now initializing our test sprites into play, and telling you a little bit
' about them for our 'lil collision test here!!  ;)
'-------------------------------------------------------------------------- 
AFlibIII_ClearDisplay RGB(0, 0, 0)

titleload = imagecreate( 306, 114 )
bload "AF3_logo320x240.bmp", titleload


'-------------------------------------------------------------------------- 
' Here is our first FB sprite to be converted into an AFlib2 Remixed
' collision sprite solely for collision-detection purposes...
'-------------------------------------------------------------------------- 
Sprite1 = ImageCreate(64, 64) 
AFlibIII_Globe 32, 32, 1, 29, RGB(100,30,0), RGB(255,255,255), 25, 255, sprite1
'-------------------------------------------------------------------------- 

AFlibIII_SprBlit 0, 0, Sprite1
ShadowPrint "This is Sprite #1....", 0, 72, RGB(255,235,235), 1, 1, RGB(255,0,0), 1

AFlibIII_ClearKeyBuffer
Sleep 6000

AFlibIII_ClearDisplay RGB(0, 0, 0)


'-------------------------------------------------------------------------- 
' ...and here is our second FB sprite to be converted into another AFlib2
' Remixed collision sprite as well!!
'-------------------------------------------------------------------------- 
Sprite2 = ImageCreate(64, 64)
For Corner as Integer = 0 to 31 step 1
  Line Sprite2, (0 + Corner, 0 + Corner)-(63 - Corner, 63 - Corner), RGB(Corner * 6, (Corner * 5) + 60, Corner * 8), b
Next
'-------------------------------------------------------------------------- 

AFlibIII_SprBlit 0, 0, Sprite2
ShadowPrint "....and this is Sprite #2!", 0, 72, RGB(255,235,235), 1, 1, RGB(255,0,0), 1

AFlibIII_ClearKeyBuffer
Sleep 6000

AFlibIII_ClearDisplay RGB(0, 0, 0)


ScreenSet 1, 0 

AFlibIII_ClearKeyBuffer


' Here now, we witness a loop that shows you some *excellent* pixel-perfect
' sprite collision detection that is as real simple for you to do in FB and
' then some!!!  ^-^=b !  Press the "ESC" key to exit.
'-------------------------------------------------------------------------- 

Do
  AFlibIII_ClearDisplay RGB(0, 0, 160)

  AFlibIII_SprBlit_alpha 0, 48, 30, titleload

  AFlibIII_SprBlit Spr1_x, Spr1_y, Sprite1
  AFlibIII_SprBlit Spr2_x, Spr2_y, Sprite2
  
  AFlibIII_Box Spr1_x, Spr1_y, Spr1_x + 63, Spr1_y + 63, RGB(255, 255, 255), 64

  If MultiKey(&h4B) then Spr1_x -= 2
  If MultiKey(&h4D) then Spr1_x += 2
  If MultiKey(&h48) then Spr1_y -= 2
  If MultiKey(&h50) then Spr1_y += 2
  If MultiKey(&h01) then Exit Do
  If MultiKey(&h02) then Spr2_Movement = 1
  If MultiKey(&h03) then Spr2_Movement = 0

  If Spr2_Movement = 1 then
    If Spr2ctrl_x = 0 then
      Spr2_x += 1
      If Spr2_x > GFX_h then Spr2ctrl_x = 1
    ElseIf Spr2ctrl_x = 1 then
      Spr2_x -= 1
      If Spr2_x < -80 then Spr2ctrl_x = 0
    End if

    If Spr2ctrl_y = 0 then
      Spr2_y += 1
      If Spr2_y > GFX_v then Spr2ctrl_y = 1
    ElseIf Spr2ctrl_x = 1 then
      Spr2_y -= 1
      If Spr2_y < -80 then Spr2ctrl_y = 0
    End if
  End if


  ' Now comes the fun part: a pixel-perfect sprite collision detection called
  ' "AFlibIII_SprCollide", which simply checks for collisions between two
  ' (2) selected AFlib III collision sprites!!  Awesome stuff, eh?
  ' d=^-^=b
  '-------------------------------------------------------------------------- 
  If AFlibIII_SprCollide_BB(Spr1_x, Spr1_y, Sprite1, Spr2_x, Spr2_y, Sprite2) then
    CollideDet_BB = "TRUE"
  else
    CollideDet_BB = "FALSE"
  End if

  If AFlibIII_SprCollide(Spr1_x, Spr1_y, Sprite1, Spr2_x, Spr2_y, Sprite2) then
    CollideDet = "TRUE"
  else
    CollideDet = "FALSE"
  End if
  
  '-------------------------------------------------------------------------- 

  ShadowPrint "PIXEL-PERFECT COLLISION DETECTION", 0, 0, RGB(255, 255, 85), 1, 1, RGB(60, 60, 60), 1
  ShadowPrint "in AFlib3!!!", 224, 8, RGB(255, 255, 85), 1, 1, RGB(60, 60, 60), 1

  ShadowPrint "Bounding Box collision: " + CollideDet_BB, 0, 24, RGB(255, 202, 72), 1, 1, RGB(0, 0, 0), 1
  ShadowPrint "Pixel-Perfect collision: " + CollideDet, 0, 32, RGB(255, 202, 72), 1, 1, RGB(0, 0, 0), 1
  ShadowPrint "Arrow keys - controls Sprite #1.", 0, GFX_v - 24, RGB(170, 170, 170), 1, 1, RGB(85, 85, 85), 1
  ShadowPrint "[1] key - starts movement of Sprite #2.", 0, GFX_v - 16, RGB(170, 170, 170), 1, 1, RGB(85, 85, 85), 1
  ShadowPrint "[2] key - stops movement of Sprite #2.", 0, GFX_v - 8, RGB(170, 170, 170), 1, 1, RGB(85, 85, 85), 1
  ScreenCopy
  AFlibIII_CustomSync fps60
Loop


' Just remember to clean up all of our fonts, sprites, collision sprites,
' and other stuff safely here to avoid memory leaks!  This is an excellent
' example of how to do it all in this program!!  ;)
'-------------------------------------------------------------------------- 
ImageDestroy ( titleload )
ImageDestroy ( Sprite1 )
ImageDestroy ( Sprite2 )
AFlibIII_RemoveFontSet 1

AFlibIII_EndProgram



'========================================================================== 
'/////// E N D   O F   M A I N   P O R T I O N 
'========================================================================== 




'__________________________________________________________________________ 
Sub ShadowPrint ( Byref text as String, _ 
                  Byval x as Integer, Byval y as Integer, _ 
                  Byval col as Integer, Byval sh_x as Integer, _ 
                  Byval sh_y as Integer, Byval sh_col as Integer, _ 
                  Byval chara_id as Integer ) 

AFlibIII_OptimizedPrint_color text, x + sh_x, y + sh_y, chara_id, 0, sh_col
AFlibIII_OptimizedPrint_color text, x, y, chara_id, 0, col

End Sub
