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

Dim Full_or_Windowed as integer = 0 '<----- Pass '0' for Windowed Mode; or
                                    '            '1' for Fullscreen Mode.



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 fontface as Integer ) 

Dim as Integer GFX_x, GFX_y

Type SprMovement
  x as Double
  y as Double
  SpeedX as Double
  SpeedY as Double
  SpeedCtrlX as Double
  SpeedCtrlY as Double
End Type
  
Dim as Integer waiting, KeyDelay, FadePos
Dim as Integer BlitWrite, OrbCtrl, OrbTotal, ScreenDisplay (1 to 3), Scr, ScrCheck

Dim Orb(1 to 2000) as SprMovement 
Dim as Any Ptr OrbSprite
Dim as Integer CurrentFPS
Dim as Single FadeLv, PosX(1 to 2), PosY(1 to 2)
Dim as String ColDetect

Dim BitDepth as Integer

ScreenRes 424, 96 , , 0
Windowtitle "AFlib III -- Screen 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, 280, , , 0
Color 15, 1: ? "         Please select your graphics mode!!!         "
Locate 4: Color 15, 0
Locate , 27: Color 10: ? "[1] ";: Color 14: ? "- 320x200"
Locate , 27: ?
Locate , 27: Color 10: ? "[2] ";: Color 14: ? "- 320x240"
Locate , 27: ?
Locate , 27: Color 10: ? "[3] ";: Color 14: ? "- 400x300"
Locate , 27: ?
Locate , 27: Color 10: ? "[4] ";: Color 14: ? "- 512x384"
Locate , 27: ?
Locate , 27: Color 10: ? "[5] ";: Color 14: ? "- 640x400"
Locate , 27: ?
Locate , 27: Color 10: ? "[6] ";: Color 14: ? "- 640x480"
Locate , 27: ?
Locate , 27: Color 10: ? "[7] ";: Color 14: ? "- 720x480"
Locate , 27: ?
Locate , 27: Color 10: ? "[8] ";: Color 14: ? "- 720x576"
Locate , 27: ?
Locate , 27: Color 10: ? "[9] ";: Color 14: ? "- 800x600"
Locate , 27: ?
Locate , 27: Color 10: ? "[0] ";: Color 14: ? "- 1024x768"
Locate , 27: ?
Locate , 27: Color 10: ? "[A] ";: Color 14: ? "- 1152x864"
Locate , 27: ?
Locate , 27: Color 10: ? "[B] ";: Color 14: ? "- 1280x720"
Locate , 27: ?
Locate , 27: Color 10: ? "[C] ";: Color 14: ? "- 1280x1024"
Locate , 27: ?
Locate , 27: Color 10: ? "[D] ";: Color 14: ? "- 1600x1200"
Locate , 27: ?
Locate , 27: Color 10: ? "[E] ";: Color 14: ? "- 1920x1080"
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, Full_or_Windowed: Exit Do
    Case "2": ScreenRes 320, 240, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "3": ScreenRes 400, 300, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "4": ScreenRes 512, 384, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "5": ScreenRes 640, 400, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "6": ScreenRes 640, 480, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "7": ScreenRes 720, 480, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "8": ScreenRes 720, 576, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "9": ScreenRes 800, 600, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "0": ScreenRes 1024, 768, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "A", "a": ScreenRes 1152, 864, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "B", "b": ScreenRes 1280, 720, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "C", "c": ScreenRes 1280, 1024, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "D", "d": ScreenRes 1600, 1200, BitDepth, 2, Full_or_Windowed: Exit Do
    Case "E", "e": ScreenRes 1920, 1080, BitDepth, 2, Full_or_Windowed: Exit Do
  End Select
Loop
SetMouse , , 0

Windowtitle "AFlib III -- Screen Collision Test"

ScreenInfo GFX_x, GFX_y

SetMouse , , 0
ScreenSet 0, 0

Color RGB(255, 255, 30), RGB(0, 0, 128): Locate 1, 1: ? "------- A F l i b   I I I -------"
Color RGB(255, 205, 155), 0: Locate 2, 5: ? "Screen Collision Test"

If GFX_x > 320 then
  Color RGB(255, 255, 255), 0: Locate 4, 1: ? "(c)2005-2016 Adigun Azikiwe Polack/Aura Flow."
else
  Color RGB(255, 255, 255), 0: Locate 4, 1: ? "(c)2005-2016 AAP/Aura Flow."
End if
Color RGB(255, 255, 255), 0: Locate 5, 1: ? "All Rights Reserved."

Locate 7, 1: ? "Now Starting...";

For waiting = 0 to 9
  ? ".";: Sleep 500
Next

AFlibIII_ClearDisplay RGB(255, 0, 255)
For BlitWrite = 1 to 80
  AFlibIII_Globe int(rnd(1) * GFX_x) + 1, int(rnd(1) * GFX_y) + 1, (int(rnd(1) * 20) + 1) / 10, int(rnd(1) * 20) + 10, int(rnd(1) * &hffffff) + 1, int(rnd(1) * &hffffff) + 1, int(rnd(1) * 80) + 1, 255
Next
AFlibIII_CaptureScreen 1
Locate 1, 1: ? "This is Screen #1 captured here."
AFlibIII_ClearKeyBuffer
Sleep 5000

AFlibIII_ClearDisplay RGB(255, 0, 255)
For BlitWrite = 0 to 15
  Line (0, (GFX_y - 1) - BlitWrite)-(GFX_x - 1, (GFX_y - 1) - BlitWrite), RGB(BlitWrite * 15, BlitWrite * 15, BlitWrite * 15), B
Next
Line (0, (GFX_y - 1) - 15)-((GFX_x / 2) - 1, (GFX_y - 1) - 35), RGB(255, 255, 255)
Line ((GFX_x / 2), (GFX_y - 1) - 35)-(GFX_x - 1, (GFX_y - 1) - 15), RGB(255, 255, 255)
AFlibIII_CaptureScreen 2
Locate 1, 1: ? "This is Screen #2 captured here."
AFlibIII_ClearKeyBuffer
Sleep 5000

AFlibIII_ClearDisplay RGB(255, 0, 255)
AFlibIII_CaptureScreen 3
Locate 1, 1: ? "Now captured Screen #3 here to be used"
Locate 2, 1: ? "as an " + chr$(34) + "update" + chr$(34) + " screen for a custom fade"
Locate 3, 1: ? "effect later on.  "; : Color 10, 0: ? ";)"
Color RGB(255, 255, 255), 0
Locate 5, 1: ? "(The screen captured here is a blank"
Locate 6, 2: ? "one, by the way.)"
AFlibIII_ClearKeyBuffer
Sleep 8000

Line (0, 0)-(GFX_x, GFX_y), 0, BF

Windowtitle "AFlib III -- Screen Collision Test"

OrbSprite = ImageCreate(5, 5)
AFlibIII_Globe 3, 3, 1, 2, RGB(80, 80, 80), RGB(255, 255, 255), 3, 255, OrbSprite
AFlibIII_SprBlit 0, 0, OrbSprite
Color RGB(255, 255, 255), 0
Locate 1, 3: ? "<---- And this is our sprite that we"
Locate 2, 9: ? "will be using right here!"
Locate 4, 1: ? "For this part, let's bring out 2,000 of"
Locate 5, 1: ? "these babies for checking the collisions"
Locate 6, 1: ? "between them and the first two captured"
Locate 7, 1: ? "screens.  The sprite(s) that do collide"
Locate 8, 1: ? "with any captured screen will bounce up,"
Locate 9, 1: ? "adding some interaction along the way!"

Locate 11, 1: ? "Here are the controls for this segment:"
Locate 13, 3: Color RGB(80, 255, 80): ? "[1] ";: Color RGB(255, 255, 80): ? "- Toggle Captured Screen #1 on/off"
Locate 14, 3: Color RGB(80, 255, 80): ? "[2] ";: Color RGB(255, 255, 80): ? "- Toggle Captured Screen #2 on/off"
Locate 15, 1: Color RGB(80, 255, 80): ? "[ESC] ";: Color RGB(255, 255, 80): ? "- Exit Segment"
AFlibIII_ClearKeyBuffer
Color RGB(255, 255, 255), 0: Locate 23, 1: ? "Press any key to begin, please."
Sleep

AFlibIII_ClearKeyBuffer
OrbTotal = 2000

For OrbCtrl = 1 to OrbTotal
  With Orb(OrbCtrl)
    .x = int(rnd(1) * (GFX_x - 6)) + 6
    .y = int(rnd(1) * (GFX_y - 6)) + 6
    .SpeedX = 1
    .SpeedY = 5
  End With
Next

ScreenDisplay(1) = 1
ScreenDisplay(2) = 1

ScreenSet 1, 0
FadeLv = 255
KeyDelay = 70

Windowtitle "AFlib III -- Screen Collision Test"

Do
  AFlibIII_ClearDisplay RGB(0, 0, 0)
  If ScreenDisplay(1) = 1 then AFlibIII_BlitScreen 0, 0, 1
  If ScreenDisplay(2) = 1 then AFlibIII_BlitScreen 0, 0, 2
  For OrbCtrl = 1 to OrbTotal
    With Orb(OrbCtrl)
      .x += .SpeedX
      .y += .SpeedY
      .SpeedY += .04: If .SpeedY > 10 then .SpeedY = 10
      AFlibIII_SprBlit_alpha .x, .y, 128, OrbSprite
    End With
    For ScrCheck = 1 to 2
      If ScreenDisplay(ScrCheck) = 1 then 
        If AFlibIII_ScreenCollide_Spr (0, 0, ScrCheck, Orb(OrbCtrl).x, Orb(OrbCtrl).y, OrbSprite) then
          Orb(OrbCtrl).SpeedY = -((int(rnd(1)* 60) + 1) / 25)
        End if
      End if
    Next
    If Orb(OrbCtrl).X > GFX_x then 
      Orb(OrbCtrl).X = -25
      Orb(OrbCtrl).SpeedX = ((int(rnd(1)* 60) + 1) / 25)
      Orb(OrbCtrl).y = int(rnd(1) * (GFX_y - 6)) + 12
    End If
  Next

  KeyDelay -= 1 
  If KeyDelay <= 0 then
    KeyDelay = 0 
    If MultiKey(&h01) then FadePos = 1
    If MultiKey(&h02) then
      KeyDelay = 14
      ScreenDisplay(1) = (ScreenDisplay(1) xor 1)
    End if
    If MultiKey(&h03) then
      KeyDelay = 14
      ScreenDisplay(2) = (ScreenDisplay(2) xor 1)
    End if
  End if

  ShadowPrint "FPS: " + Ltrim$(str$(CurrentFPS)), 0, 0, RGB(255, 255, 30), 1, 1, RGB(128, 128, 24), 4

  AFlibIII_CustomSync fps_OPTIMAL

  If FadePos = 1 then
    AFlibIII_UpdateScreen 3
    Line (0, 0)-(GFX_x, GFX_y), RGB(0, 0, 0), BF
    FadeLv -= 2
    AFlibIII_BlitScreen_alpha 0, 0, FadeLv, 3
    If FadeLv <= 0 then Exit Do
  End if

  CurrentFPS = AFlibIII_ShowFPS
  ScreenCopy
Loop
ScreenSet 0, 0

Line (0, 0)-(GFX_x, GFX_y), 0, BF

Color RGB(255, 255, 255), 0
Locate 1, 1: ? "Now removing all three (3) screens from"
Locate 2, 1: ? "memory safely........"

AFlibIII_ClearKeyBuffer
Sleep 500

For Scr = 1 to 3
  AFlibIII_RemoveScreen Scr
Next

Color 14, 0: Locate 4, 1: ? "-- DONE! --"
AFlibIII_ClearKeyBuffer
Sleep 3000

ImageDestroy OrbSprite

Windowtitle "AFlib III -- Screen Collision Test"

Color RGB(255, 255, 255)
Locate 1, 1: ? "As you just saw, the collisions between"
Locate 2, 1: ? "screens as well as the collisions"
Locate 3, 1: ? "between a screen and any sprite are all"
Locate 4, 1: ? "as both marvelously precise and accurate"
Locate 5, 1: ? "as ever!!"

Locate 7, 1: ? "The fiery beauty of it is in the pixel-"
Locate 8, 1: ? "perfect collision detection on them all."

Locate 10, 1: ? "And you are sure to find them all and a"
Locate 11, 1: ? "ton lot more in the most advanced native"
Locate 12, 1: ? "2d hi-/true-color graphics library of"
Locate 13, 1: ? "AFlib III for Angelo Mottola's GFXlib2"
Locate 14, 1: ? "in none other than FreeBASIC itself!!!"

Color RGB(80, 255, 80)
Locate 15, 1: ? "d=^-^=b !"

Color RGB(255, 255, 255)

Locate 17, 1: ? "See you later, and thanks so much!!!"

Color RGB(255, 255, 80)

Locate 19, 18: ? "- Adigun Azikiwe Polack"
Locate 20, 7: ? "One of the Founders of " + Chr$(34) + "Aura Flow" + Chr$(34)

Color RGB(255, 255, 255)
Locate 23, 1: ? "Press ESC to end, please."
AFlibIII_ClearKeyBuffer
Do: Loop until inkey$ = chr$(27)

AFlibIII_EndProgram




'__________________________________________________________________________ 
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 fontface as Integer ) 

AFlibIII_Print text, x + sh_x, y + sh_y, 1, 1, sh_col, 0, 0, fontface
AFlibIII_Print text, x, y, 1, 1, col, 0, 0, fontface

End Sub