'=============================================================== 
' A F l i b   I I I   M o d e - 7   E x a m p l e
'=============================================================== 
'Remember the wonderfully magnificent Mode-7 that was used on
'the Super Nintendo/Super Famicom in such greatest classics as 
'F-Zero, Super Metroid, Final Fantasy III, and Chrono Trigger? 
'Well in this program for AFlib III, you will experience that
'very effect and more as it is now so faithfully reproduced for
'all 24- and 32-bit graphics modes in FreeBASIC, right here!!
'(^-^)v !  And best of all, you can even have your Mode-7 maps
'out of any .BMP file you like that is higher than an 8-bit
'color depth, and the maps themselves can be up to a whopping
'4096x4096 IN SIZE!!! 
' 
'In this example, two of the custom Mode-7 maps are used for the
'top and bottom halves of the screen: one, a 1024x1024 map; and
'the other a 2048x2048 map!!  Interesting stuff, and imagine
'the kind of splendid game programming that you are gonna create
'in FB indeed, I must tell 'ya for sure!!!  :D !!
' 
'Hope you enjoy this one, and thank you so very much indeed!!!
' 
' 
'                                        - Adigun Azikiwe Polack 
'                               One of the Founders of Aura Flow 
'                                       Developer of "AFlib III" 
'---------------------------------------------------------------


#include "AFlib3_Sprite.bi"
#include "AFlib3_Font.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 chara_id as Integer ) 

Declare Sub Mode7_Test


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

Randomize Timer

Dim Shared as integer scalx = 200, scaly = 200, scalz = 80, horiz, ang, cx, cy 
Dim Shared As Any Ptr Mode7Top_map, Mode7Bottom_map

Dim Shared as integer sx1, sx2, sy1, sy2, col_table, WaitControl
Dim Shared array_size as Integer 

Dim Shared as Integer SCR_WID, SCR_HEI, CurrentFPS, BitDepth

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

Do
  Select case Inkey$
    Case "1": BitDepth = 24: Exit Do
    Case "2": BitDepth = 32: Exit Do
  End Select
Loop

Dim as String Path
path = ""

SCR_WID = 320
SCR_HEI = 200
ScreenRes SCR_WID, SCR_HEI, BitDepth, 2, Full_or_Windowed

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


'-------------------------------------------------------
Mode7Top_map = imagecreate( 1024, 1024 )
bload "AF3_Cloud1024.bmp", Mode7Top_map

Mode7Bottom_map = imagecreate( 2048, 2048 )
bload "AF3_Cloud2048.bmp", Mode7Bottom_map
'-------------------------------------------------------


AFlibIII_ClearDisplay 0

Dim as integer FPSresult (1 to 8)

Mode7_Test
FPSresult(1) = CurrentFPS

SCR_WID = 320
SCR_HEI = 240
ScreenRes SCR_WID, SCR_HEI, BitDepth, 2, Full_or_Windowed
Mode7_Test
FPSresult(2) = CurrentFPS

SCR_WID = 400
SCR_HEI = 300
ScreenRes SCR_WID, SCR_HEI, BitDepth, 2, Full_or_Windowed
Mode7_Test
FPSresult(3) = CurrentFPS

SCR_WID = 512
SCR_HEI = 384
ScreenRes SCR_WID, SCR_HEI, BitDepth, 2, Full_or_Windowed
Mode7_Test
FPSresult(4) = CurrentFPS

SCR_WID = 640
SCR_HEI = 400
ScreenRes SCR_WID, SCR_HEI, BitDepth, 2, Full_or_Windowed
Mode7_Test
FPSresult(5) = CurrentFPS

SCR_WID = 640
SCR_HEI = 480
ScreenRes SCR_WID, SCR_HEI, BitDepth, 2, Full_or_Windowed
Mode7_Test
FPSresult(6) = CurrentFPS

SCR_WID = 800
SCR_HEI = 600
ScreenRes SCR_WID, SCR_HEI, BitDepth, 2, Full_or_Windowed
Mode7_Test
FPSresult(7) = CurrentFPS

SCR_WID = 1024
SCR_HEI = 768
ScreenRes SCR_WID, SCR_HEI, BitDepth, 2, Full_or_Windowed
Mode7_Test
FPSresult(8) = CurrentFPS

ScreenCopy
ScreenRes 416, 144, 8, 0, 0
Windowtitle "AFlib III Mode-7 Test Results!"
Locate 1, 1
Color 15, 1: ? "---------- AFlib III Mode-7 Test Results: ----------"
?
Locate , 14: Color 14, 0: ? " 320x200 - ";: Color 15, 0: ? FPSresult(1); "fps"
Locate , 14: Color 14, 0: ? " 320x240 - ";: Color 15, 0: ? FPSresult(2); "fps"
Locate , 14: Color 14, 0: ? " 400x300 - ";: Color 15, 0: ? FPSresult(3); "fps"
Locate , 14: Color 14, 0: ? " 512x384 - ";: Color 15, 0: ? FPSresult(4); "fps"
Locate , 14: Color 14, 0: ? " 640x400 - ";: Color 15, 0: ? FPSresult(5); "fps"
Locate , 14: Color 14, 0: ? " 640x480 - ";: Color 15, 0: ? FPSresult(6); "fps"
Locate , 14: Color 14, 0: ? " 800x600 - ";: Color 15, 0: ? FPSresult(7); "fps"
Locate , 14: Color 14, 0: ? "1024x768 - ";: Color 15, 0: ? FPSresult(8); "fps"
Locate 17, 1: ? "Press any key to end, please."


AFlibIII_ConsoleEdit_Open
AFlibIII_ConsoleEdit_Print "---------- AFlib III Mode-7 Test Results: ----------"
AFlibIII_ConsoleEdit_Print ""
AFlibIII_ConsoleEdit_Print ""
AFlibIII_ConsoleEdit_Print "Color Depth Selected:"
AFlibIII_ConsoleEdit_Print Ltrim(Str$(BitDepth)) + "-bit color"
AFlibIII_ConsoleEdit_Print ""
AFlibIII_ConsoleEdit_Print " 320x200 - " + Ltrim(Str$(FPSresult(1))) + "fps"
AFlibIII_ConsoleEdit_Print " 320x240 - " + Ltrim(Str$(FPSresult(2))) + "fps"
AFlibIII_ConsoleEdit_Print " 400x300 - " + Ltrim(Str$(FPSresult(3))) + "fps"
AFlibIII_ConsoleEdit_Print " 512x384 - " + Ltrim(Str$(FPSresult(4))) + "fps"
AFlibIII_ConsoleEdit_Print " 640x400 - " + Ltrim(Str$(FPSresult(5))) + "fps"
AFlibIII_ConsoleEdit_Print " 640x480 - " + Ltrim(Str$(FPSresult(6))) + "fps"
AFlibIII_ConsoleEdit_Print " 800x600 - " + Ltrim(Str$(FPSresult(7))) + "fps"
AFlibIII_ConsoleEdit_Print "1024x768 - " + Ltrim(Str$(FPSresult(8))) + "fps"
AFlibIII_ConsoleEdit_Close

AFlibIII_ClearKeyBuffer
Sleep

'----------------------------------------------------
ImageDestroy ( Mode7Top_map )
ImageDestroy ( Mode7Bottom_map )
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


'__________________________________________________________________________ 
Sub Mode7_Test ()

Windowtitle "AFlib III " + Ltrim$(Str$(SCR_WID)) + "x" + Ltrim$(Str$(SCR_HEI)) + " Mode-7 Test - (c)2016 Adigun Azikiwe Polack and Aura Flow."
ScreenSet 1, 0

horiz = 1
AFlibIII_CaptureScreen 1

Do
  AFlibIII_ClearDisplay RGBA(0, 0, 0, 0)
  If BitDepth >= 24 then
    For BlitMul as Integer = 0 to 31 step 1
      Line (0, ((SCR_HEI / 2) - BlitMul)) - (SCR_WID, ((SCR_HEI / 2) - BlitMul)), rgba(0, 0, 0, 255 - (BlitMul * 8))
      Line (0, ((SCR_HEI / 2) + BlitMul)) - (SCR_WID, ((SCR_HEI / 2) + BlitMul)), rgba(0, 0, 0, 255 - (BlitMul * 8))
    Next
  End if
  AFlibIII_UpdateScreen 1

  'For the Whole Screen!
  '==========================
  AFlibIII_ClearDisplay RGB(0, 0, 0)
  AFlibIII_Mode7_Top ( scalx, scaly, scalz, horiz, ang, cx, cy, (SCR_HEI / 2), (SCR_HEI / 2), Mode7Top_map, 7 ) 
  AFlibIII_Mode7_Bottom ( scalx, scaly, scalz, horiz, ang, cx, cy, (SCR_HEI / 2), ((SCR_HEI / 2) - 1), Mode7Bottom_map, 8 ) 
  AFlibIII_BlitScreen_alpha 0, 0, -1, 1

  ShadowPrint "Scale X", 0, 0, rgb(255,255,0), 1, 1, rgb(0,0,0), 1: ShadowPrint "(keys Z, X):", 64, 0, rgb(255,174,45), 1, 1, rgb(0,0,0), 1: ShadowPrint LTRIM$(STR$(scalx)), 168, 0, rgb(255,255,255), 1, 1, rgb(0,0,0), 1
  ShadowPrint "Scale Y", 0, 8, rgb(255,255,0), 1, 1, rgb(0,0,0), 1: ShadowPrint "(keys A, S):", 64, 8, rgb(255,174,45), 1, 1, rgb(0,0,0), 1: ShadowPrint LTRIM$(STR$(scaly)), 168, 8, rgb(255,255,255), 1, 1, rgb(0,0,0), 1
  ShadowPrint "Scale Z", 0, 16, rgb(255,255,0), 1, 1, rgb(0,0,0), 1: ShadowPrint "(keys C, V):", 64, 16, rgb(255,174,45), 1, 1, rgb(0,0,0), 1: ShadowPrint LTRIM$(STR$(scalz)), 168, 16, rgb(255,255,255), 1, 1, rgb(0,0,0), 1
  ShadowPrint "Horiz", 0, 24, rgb(255,255,0), 1, 1, rgb(0,0,0), 1: ShadowPrint "(keys D, F):", 48, 24, rgb(255,174,45), 1, 1, rgb(0,0,0), 1: ShadowPrint LTRIM$(STR$(horiz)), 152, 24, rgb(255,255,255), 1, 1, rgb(0,0,0), 1
  ShadowPrint "Angle", 0, 32, rgb(255,255,0), 1, 1, rgb(0,0,0), 1: ShadowPrint "(keys Q, W):", 48, 32, rgb(255,174,45), 1, 1, rgb(0,0,0), 1: ShadowPrint LTRIM$(STR$(ang)), 152, 32, rgb(255,255,255), 1, 1, rgb(0,0,0), 1
  ShadowPrint "Camera X", 0, 40, rgb(255,255,0), 1, 1, rgb(0,0,0), 1: ShadowPrint "(keys " + CHR$(27) + ", " + CHR$(26) + "):", 72, 40, rgb(255,174,45), 1, 1, rgb(0,0,0), 1: ShadowPrint LTRIM$(STR$(cx)), 172, 40, rgb(255,255,255), 1, 1, rgb(0,0,0), 1
  ShadowPrint "Camera Y", 0, 48, rgb(255,255,0), 1, 1, rgb(0,0,0), 1: ShadowPrint "(keys " + CHR$(24) + ", " + CHR$(25) + "):", 72, 48, rgb(255,174,45), 1, 1, rgb(0,0,0), 1: ShadowPrint LTRIM$(STR$(cy)), 172, 48, rgb(255,255,255), 1, 1, rgb(0,0,0), 1
  ShadowPrint ltrim$(str((scr_wid))) + "x" + ltrim$(str((scr_hei)))+ "x" + ltrim$(str((bitdepth))) + " Graphics Mode", 0, 64, rgb(255,255,255), 1, 1, rgb(0,0,0), 1
  
  If MultiKey(&h2C) then scalx += 1 
  If MultiKey(&h2D) then scalx -= 1: If scalx < 1 then scalx = 1 

  If MultiKey(&h1E) then scaly += 1 
  If MultiKey(&h1F) then scaly -= 1: If scaly < 1 then scaly = 1 

  If MultiKey(&h2E) then scalz += 1 
  If MultiKey(&h2F) then scalz -= 1: If scalz < 1 then scalz = 1 

  If MultiKey(&h20) then horiz += 1 
  If MultiKey(&h21) then horiz -= 1: If horiz < 1 then horiz = 1 

  If MultiKey(&h10) then ang += 1 
  If MultiKey(&h11) then ang -= 1: If ang < 1 then ang = 1 

  If MultiKey(&h4D) then cx += 3 
  If MultiKey(&h4B) then cx -= 3: If cx < 1 then cx = 1 

  If MultiKey(&h48) then cy += 3 
  If MultiKey(&h50) then cy -= 3: If cy < 1 then cy = 1 

  CurrentFPS = AFlibIII_ShowFPS
  WaitControl -= 1
  If WaitControl < 0 then WaitControl = 0
  AFlibIII_CustomSync fps_OPTIMAL
  
  ScreenCopy
loop until WaitControl <=0 and MultiKey(&h01)    ' <------------ Press "ESC" key to exit the loop.
WaitControl = 105
AFlibIII_RemoveScreen 1
End Sub

