' BIGGEST Special Thanks to Mateusz Viste for the ANSI routines that
' are successfully implemented for AFlib III in FreeBASIC!!!
'
' This example uses only a single page, but supports Page-Flipping
' which enables the screen to flow even more smoothly in FreeBasic (FB)
' than with Double Buffering with "ScreenCopy" that tends to cause
' the most frame stutters of all (which is NOT what you would want in
' your own game that you are developing using AFlib III).  So, I'd say
' go with Page-Flipping quite indeed, and do note the carefully precise
' use of FB's own "ScreenLock" and "ScreenUnlock" commands in this
' entire code below to see how this amazing effect is achieved,
' WITHOUT any need for any extra pages at all!!!  (^-^)=b !!



#Include Once "AFlib3_Palette.bi"
#Include Once "AFlib3_ASCii.bi"
#Include Once "AFlib3_Miscellaneous.bi"
#Include Once "AFlib3_Constants.bi"

Declare Sub MiddleClick_CursorCreate ( Byval SpriteSlot as Integer = 8192 )

Dim as Integer TextResX, TextResY, ColorDepth
Dim as Integer ScrollANSI
Dim as Double BeginTime, FadeLvl
Dim as String ANSIFileTarget

Dim as Integer ANSIcapture_X, ANSIcapture_Y, ANSIcapture_Y_8x8char
Dim as any ptr TempANSIbuffer
Dim as Integer MidClick_Scr_X, MidClick_Scr_Y, MidClick_Scr_Marker, MidClick_Scr_MarkerDelay
Dim as Integer MouseWheelShadow

TextResX = 80
TextResY = 122
ColorDepth = 24

ANSIFileTarget = COMMAND(1)

If not AFlibIII_CheckFile (ANSIFileTarget) then AFlibIII_EndProgram

AFlibIII_SetTextGFX TextResX, TextResY, ColorDepth, , 0

AFlibIII_Initialize_ASCii
AFlibIII_ASCii_ClearDisplay RGB(0, 0, 0)

BeginTime = TIMER

WindowTitle "AFlib III -- Scrollable 16-color ANSI/ASCii viewer test (in a " + Ltrim(str(int(ColorDepth))) + "-bit color graphics mode!!!)"

Width (TextResX * 8) / 8, (TextResY * 8) / 16
Locate 1, 1: Color RGB(255, 255, 255), RGB(0, 0, 170): ? "------------ Scrollable 16-color ANSI/ASCii viewer test (AFlib III) ------------"
Color RGB(255, 255, 255), RGB(0, 0, 0)
?
? "Now setting up and getting the ANSI/ASCii viewer ready for you."
? "Please wait a short bit while it is working..."

ANSIcapture_X = (80 * 9)
ANSIcapture_Y_8x8char = AFlibIII_ASCii_CheckANSIHeight (ANSIFileTarget)
ANSIcapture_Y = (ANSIcapture_Y_8x8char * 9)

TempANSIbuffer = ImageCreate(ANSIcapture_X, ANSIcapture_Y)

AFlibIII_ASCii_LoadANSI ANSIFileTarget, , 10, TempANSIbuffer

AFlibIII_ASCii_CaptureSprite_Text 0, 0, 80, (ANSIcapture_Y / 9), 1, TempANSIbuffer

MiddleClick_CursorCreate 

AFlibIII_ClearKeyBuffer
FadeLvl = -7
WindowTitle "AFlib III -- Scrollable 16-color ANSI/ASCii viewer test (in a " + Ltrim(str(int(ColorDepth))) + "-bit color graphics mode!!!)"

Do
  ScreenLock
  AFlibIII_ASCii_ClearDisplay RGB(0, 0, 0)
  AFlibIII_ASCii_GrabMouse 

  If ANSIcapture_Y_8x8char > (ANSIcapture_Y_8x8char - TextResY) then
    If AFlibIII_ASCii_CheckMouse_LeftClick = Active or AFlibIII_ASCii_CheckMouse_RightClick = Active then
      If MidClick_Scr_Marker = Active then
        MidClick_Scr_Marker = Inactive
        MidClick_Scr_MarkerDelay = 20
      End If
    End if
    
    If AFlibIII_ASCii_CheckMouse_MiddleClick = Active then
      If MidClick_Scr_MarkerDelay <= 0 then
        If not MidClick_Scr_Marker = Active then
          MidClick_Scr_Marker = Active
          MidClick_Scr_X = ( AFlibIII_ASCii_CheckMouse_X - 4 )
          MidClick_Scr_Y = ( AFlibIII_ASCii_CheckMouse_Y - 3 )
        Else
          MidClick_Scr_Marker = Inactive
        End if
        MidClick_Scr_MarkerDelay = 20
      End if
    End if
    
    If (AFlibIII_ASCii_CheckMouse_Wheel - MouseWheelShadow) > 0 then ScrollANSI -= 8
    If (AFlibIII_ASCii_CheckMouse_Wheel - MouseWheelShadow) < 0 then ScrollANSI += 8
    If MultiKey(&h48) then ScrollANSI -= 1
    If MultiKey(&h50) then ScrollANSI += 1
    If MultiKey(&h49) then ScrollANSI -= 4
    If MultiKey(&h51) then ScrollANSI += 4
    If MultiKey(&h47) then ScrollANSI = 0
    If MultiKey(&h4F) then ScrollANSI = (ANSIcapture_Y_8x8char - TextResY)

    If MidClick_Scr_Marker = Active and AFlibIII_ASCii_CheckMouse_Screen = Active then
      If AFlibIII_ASCii_CheckMouse_Y < ( MidClick_Scr_Y ) then ScrollANSI -= (1 + ((MidClick_Scr_Y - AFlibIII_ASCii_CheckMouse_Y)) / 10)
      If AFlibIII_ASCii_CheckMouse_Y > ( MidClick_Scr_Y + 6 ) then ScrollANSI += (((AFlibIII_ASCii_CheckMouse_Y - MidClick_Scr_Y)) / 10)
    End if

    If ScrollANSI >= (ANSIcapture_Y_8x8char - TextResY) then ScrollANSI = (ANSIcapture_Y_8x8char - TextResY)
  End if

  If ScrollANSI < 0 then ScrollANSI = 0
  MouseWheelShadow = AFlibIII_ASCii_CheckMouse_Wheel
  
  AFlibIII_ASCii_SprBlit 0, 0 - ScrollANSI, 1

  If MidClick_Scr_Marker = Active then 
    AFlibIII_ASCii_SprBlit MidClick_Scr_X, MidClick_Scr_Y, 8192
  End if

  MidClick_Scr_MarkerDelay -= 1: If MidClick_Scr_MarkerDelay <= 0 then MidClick_Scr_MarkerDelay = 0

  '-------------------------------------------------------------------------------------------------------------
  ' This snippet of code right here represents the real test in AFlib III of how absolutely *very* stable the
  ' custom limited-color palette control really is within any 24-/32-bit color graphics mode of your choice!!!
  
  If FadeLvl <= 0 then
    AFlibIII_FadeEGA , FadeLvl
    FadeLvl += .1
    If FadeLvl > 0 then FadeLvl = 0
  End if
  
  '-------------------------------------------------------------------------------------------------------------

  ScreenUnlock
  AFlibIII_CustomSync fps_OPTIMAL
Loop Until MultiKey(&h01) or AFlibIII_Detect_CloseWindow

AFlibIII_ClearKeyBuffer

If ColorDepth >= 24 then
  For FadeOutLvl as Integer = Int(FadeLvl) to -7 step -1
    ScreenLock
    AFlibIII_ASCii_ClearDisplay RGB(0, 0, 0)
    AFlibIII_ASCii_SprBlit 0, 0 - ScrollANSI, 1
    AFlibIII_FadeEGA , FadeOutLvl
    ScreenUnlock
    AFlibIII_CustomSync 6
  Next
End if

AFlibIII_ASCii_ClearDisplay RGB(0, 0, 0)

ImageDestroy TempANSIbuffer
AFlibIII_ASCii_RemoveSprite_CleanSweep
AFlibIII_DeInit_ASCii

AFlibIII_EndProgram





'__________________________________________________________________________
Sub MiddleClick_CursorCreate ( Byval SpriteSlot as Integer = 8192 )

  AFlibIII_ASCii_InitBuffer 9, 9, 1
  AFlibIII_ASCii_PrepareSpriteTrans AF3_ASCii_Buffer(1)

  AFlibIII_ASCii_Print Chr(219, 220), 5, 0, EGAcol(0), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(219, 221), 6, 1, EGAcol(0), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(219, 221), 6, 2, EGAcol(0), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(219, 221), 6, 3, EGAcol(0), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(219, 221), 6, 4, EGAcol(0), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(222, 219, 219, 219, 219, 219, 219, 221), 0, 5, EGAcol(0), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(223, 219, 219, 219, 219, 223), 1, 6, EGAcol(0), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)

  AFlibIII_ASCii_Print Chr(220, 219, 219, 219, 220), 1, 0, EGAcol(11), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(219, 219, 219, 219, 219), 1, 1, EGAcol(14), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(222, 219, 219, 219, 219, 219, 221), 0, 2, EGAcol(15), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(222, 219, 219, 219, 219, 219, 221), 0, 3, EGAcol(15), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(219, 219, 219, 219, 219), 1, 4, EGAcol(14), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(223, 219, 219, 219, 223), 1, 5, EGAcol(11), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(222, 32, 32, 32, 32, 32, 221), 0, 1, EGAcol(7), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(222, 32, 32, 32, 32, 32, 221), 0, 2, EGAcol(14), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(222, 32, 32, 32, 32, 32, 221), 0, 3, EGAcol(14), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(222, 32, 32, 32, 32, 32, 221), 0, 4, EGAcol(7), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(30), 3, 1, EGAcol(1), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print "o", 3, 2, EGAcol(7), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print "o", 3, 3, EGAcol(7), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_Print Chr(31), 3, 4, EGAcol(1), RGB(255, 0, 255), 10, AF3_ASCii_Buffer(1)

  AFlibIII_ASCii_CaptureSprite_Text 0, 0, 7, 7, SpriteSlot, AF3_ASCii_Buffer(1)
  AFlibIII_ASCii_RemoveBuffer 1
  
End Sub
