' 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 "AFlib3_Sprite.bi"
#include "AFlib3_Font.bi"
#include "AFlib3_Screen.bi"
#include "AFlib3_Miscellaneous.bi"
#include "AFlib3_Constants.bi"
#include "AFlib3_Resize2X.bi"
#include "AFlib3_GraphicsFilter.bi"

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


Dim a As Double

Dim As Double rotation_angle=0
Dim As Integer GFX_wid, GFX_hei, Follow_the_Lens, WaitKey

ScreenRes 640, 480, 24, , Full_or_Windowed
ScreenLock
ScreenInfo GFX_wid, GFX_hei
ScreenUnlock

WindowTitle "WELCOME to AFlib III (Aura Flow Library 3)!!!"
SetMouse , 0, 0

Dim As Any Ptr spheremap, bmpload, bmpload_trans, titleload

titleload = imagecreate( 306, 57 )
bload "AF3_logo320x120.bmp", titleload

bmpload = imagecreate( 2048, 2048 )
bload "AF3_Cloud2048.bmp", bmpload

bmpload_trans = imagecreate( 2048, 2048 )
bload "AF3_TransCloud2048.bmp", bmpload_trans

spheremap = imagecreate( 512, 512 )
bload "AF3_Cloud512.bmp", spheremap

Dim As Integer v = 1

Dim As Single lens_x, lens_y, lens_x_ctrl, lens_y_ctrl
Dim As Single Titlemove, TitleFlash, TitleBright, ScreenFade, MosaicFade, FadeStart
Dim As Integer spheremove

AFlibIII_CaptureScreen 1

lens_x = 200
lens_y = 20 
lens_x_ctrl = 0
lens_y_ctrl = 0
ScreenFade = 255

Do
  ScreenLock
  AFlibIII_ClearDisplay 0
  AFlibIII_Plasma 0, 0, 370, 180, 1, 0, 4, bmpload, 8
  AFlibIII_SphereMap 64, -30, spheremove, spheremove, 100, spheremap, 6
  AFlibIII_SphereMap 64, -30, spheremove, 0, 100, bmpload_trans, 8
  
  AFlibIII_Lens lens_x, lens_y, 40
  AFlibIII_Lens (GFX_wid / 2) - (lens_x + 60), (GFX_hei / 4) - (lens_y + 30), 32
  
  rotation_angle += v
  If rotation_angle > 360 Then
    rotation_angle = 0
  End If

  spheremove += v
  If spheremove > 2048 Then
    spheremove = 0
  End If

  If lens_x_ctrl = 1 then
    lens_x += 1
    If lens_x > 290 then lens_x_ctrl = 0
  else
    lens_x -= 1
    If lens_x < -20 then lens_x_ctrl = 1
  end if
    
  If lens_y_ctrl = 1 then
    lens_y += 1
    If lens_y > 120 then lens_y_ctrl = 0
  else
    lens_y -= 1
    If lens_y < -40 then lens_y_ctrl = 1
  end if


  Titlemove += 1
  If Titlemove > 255 then
    If TitleFlash = 0 then
      TitleFlash = 1: TitleBright = 0
    End if
    Titlemove = 255
  End if

  Select Case TitleFlash
    Case 0: AFlibIII_SprBlit_scalerotate 5, 30, titleload, (256 - Titlemove), 1, (255 - Titlemove), 0, 0, Titlemove
    Case 1:
      AFlibIII_SprBlit_tint 5, 30, rgb(255, 255, 255), TitleBright, titleload
      TitleBright += 2: If TitleBright > 255 then TitleBright = 255
  End Select

  AFlibIII_Print "--- WELCOME TO THE ALL-NEW AFLIB3!!! ---", 1, 1, 1, 1, rgb(48, 85, 6), 0, 2, 16
  AFlibIII_Print "--- WELCOME TO THE ALL-NEW AFLIB3!!! ---", 1, 0, 1, 1, rgb(48, 85, 6), 0, 2, 16
  AFlibIII_Print "--- WELCOME TO THE ALL-NEW AFLIB3!!! ---", 0, 1, 1, 1, rgb(48, 85, 6), 0, 2, 16
  AFlibIII_PrintGradient "--- WELCOME TO THE ALL-NEW AFLIB3!!! ---", 0, 0, 1, 1, rgb(255, 255, 255), rgb(128, 255, 40), 0, 2, 16

  AFlibIII_LowRes_vert
  AFlibIII_Resize2X_640x480

  'AFlibIII_Scanlines_H
  If Follow_the_Lens = 1 then AFlibIII_CustomZoom2X (lens_x * 2) - 96, (lens_y * 4) + 12
  
  If multikey(&h01) then FadeStart = 1
  If multikey(&h39) or multikey(&h1C) then
    If WaitKey > 10 then
      Follow_the_Lens += 1: WaitKey = 0
      If Follow_the_Lens > 1 then Follow_the_Lens = 0
    End if
  End if
  WaitKey += 1

  If FadeStart then
    ScreenFade -= 2
    MosaicFade += 1
    AFlibIII_Mosaic_Width MosaicFade
  End If
  
  AFlibIII_UpdateScreen 1
  AFlibIII_ClearDisplay RGB(0, 0, 0)

  If ScreenFade <= 0 then ScreenUnlock: Exit Do
  AFlibIII_BlitScreen_alpha 0, 0, ScreenFade, 1
  
  ScreenUnlock
  AFlibIII_CustomSync fps_OPTIMAL

Loop Until ScreenFade <= 0 or AFlibIII_Detect_CloseWindow

imagedestroy( bmpload )
imagedestroy( bmpload_trans )
imagedestroy( titleload )
imagedestroy( spheremap )
AFlibIII_RemoveScreen 1

AFlibIII_EndProgram





