'
'                           ___________________________________________
'         _______ _ _ _     \___           ___________________________/
'    /\  (_______) (_) |       /  //  //  /  
'   /  \  _  __  | |_| | _    /  //  //  / << version 0.86 alpha test >>
'  / /\ \| |(__| | | | || \  /  //  //  /  For FreeBASIC (v0.20b or later).
' | |__| | |     | | | |_) )/  //  //  /     
' |_|  |_|_|     |_|_|____//  //  //  /   Original Development by
'  _______________________/  //  //  /___ Adigun Azikiwe Polack for Aura Flow.
' /_____________________________________/
'
'  A  u  r  a      F  l  o  w      L  i  b  r  a  r  y      3
'_________________________________________________________________________
'
' (c)2005-2016 Adigun Azikiwe Polack/Aura Flow.  All Rights Reserved.
'
'
'
'    ____       _           _ __  _                    __  ___          __      __   
'   / __ \_____(_)___ ___  (_) /_(_)   _____  _____   /  |/  /___  ____/ /_  __/ /__ 
'  / /_/ / ___/ / __ `__ \/ / __/ / | / / _ \/ ___/  / /|_/ / __ \/ __  / / / / / _ \
' / ____/ /  / / / / / / / / /_/ /| |/ /  __(__  )  / /  / / /_/ / /_/ / /_/ / /  __/
'/_/   /_/  /_/_/ /_/ /_/_/\__/_/ |___/\___/____/  /_/  /_/\____/\__,_/\__,_/_/\___/ 
'                                                                                    
'
'_________________________________________________________________________
'                        ----- N O T I C E -----
'_________________________________________________________________________
' The following is this piece of source code to this version of AFlib III.
' You are free to look at it to give you some inspiring ideas in your own
' programming in FreeBASIC, but you are *never* to even change or adjust
' any of this source code whatsoever in any way, shape, or form at all.
' EVER.  ;)
'
' The reason for this is to ensure that all of the routines in this source
' code are working as properly as they should in FreeBASIC v0.20b or
' newer.  That is all.
'
' With that said, do enjoy the library and be inspired even more
' greatly than ever!!!  (^-^)//
'_________________________________________________________________________



'$profile
'$stop_scan

#include once "fbgfx.bi"
ScreenControl(FB.SET_DRIVER_NAME, "GDI")

#define isBufferNew_c( __buffer__ ) iif( __buffer__ <> 0, *cast( Integer Ptr, __buffer__ ) = 7, 0 )

'---------------------------------------------------------------------
' Special Thanks to Mysoft for this Percent defining command here!!!
'---------------------------------------------------------------------
#define Percent2(A,B,P,M) ((A)+(((B)-(A))/(M))*(P))
'---------------------------------------------------------------------


Declare Sub AFlibIII_Pixel ( Byval sx as Integer, Byval sy as Integer, Byval col as Integer, _
                             Byval alpha_blend as Integer = 255, Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Pixel_add ( Byval sx as Integer, Byval sy as Integer, _
                                 Byval col as Integer, Byval add_blend as Integer, _
                                 Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Pixel_custom ( Byval sx as Integer, Byval sy as Integer, _
                                    Byval col as Integer, Byval draw_mode as Integer, _
                                    Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Line ( Byval sx1 as Integer, Byval sy1 as Integer, _
                            Byval sx2 as Integer, Byval sy2 as Integer, _
                            Byval col as Integer, Byval alpha_blend as Integer = 255, _
                            Byval buffer as any Ptr = 0 )
                            
Declare Sub AFlibIII_Line_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                Byval sx2 as Integer, Byval sy2 as Integer, _
                                Byval col as Integer, Byval add_blend as Integer, _
                                Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Line_custom ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                   Byval sx2 as Integer, Byval sy2 as Integer, _
                                   Byval col as Integer, Byval draw_mode as Integer, _
                                   Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Box ( Byval sx1 as Integer, Byval sy1 as Integer, _
                           Byval sx2 as Integer, Byval sy2 as Integer, _
                           Byval col as Integer, Byval alpha_blend as Integer = 255, _
                           Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Box_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                               Byval sx2 as Integer, Byval sy2 as Integer, _
                               Byval col as Integer, Byval add_blend as Integer, _
                               Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Box_custom ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                  Byval sx2 as Integer, Byval sy2 as Integer, _
                                  Byval col as Integer, Byval draw_mode as Integer, _
                                  Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_FilledBox ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                 Byval sx2 as Integer, Byval sy2 as Integer, _
                                 Byval col as Integer, Byval alpha_blend as Integer = 255, _
                                 Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_FilledBox_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                     Byval sx2 as Integer, Byval sy2 as Integer, _
                                     Byval col as Integer, Byval add_blend as Integer, _
                                     Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_FilledBox_custom ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                        Byval sx2 as Integer, Byval sy2 as Integer, _
                                        Byval col as Integer, Byval draw_mode as Integer, _
                                        Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Button ( Byval sx1 as Integer, Byval sy1 as Integer, _
                              Byval sx2 as Integer, Byval sy2 as Integer, _
                              Byval col1 as Integer, Byval col2 as Integer, _
                              Byval alpha_blend as Integer = 255, _
                              Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Button_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                  Byval sx2 as Integer, Byval sy2 as Integer, _
                                  Byval col1 as Integer, Byval col2 as Integer, _
                                  Byval add_blend as Integer, _
                                  Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_FilledButton ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                    Byval sx2 as Integer, Byval sy2 as Integer, _
                                    Byval col1 as Integer, Byval col2 as Integer, _
                                    Byval alpha_blend as Integer, _
                                    Byval col_to_fill as Integer, _
                                    Byval alpha_blend_fill as Integer, _
                                    Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_FilledButton_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                        Byval sx2 as Integer, Byval sy2 as Integer, _
                                        Byval col1 as Integer, Byval col2 as Integer, _
                                        Byval add_blend as Integer, _
                                        Byval col_to_fill as Integer, _
                                        Byval add_blend_fill as Integer, _
                                        Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_SubPixel ( Byval sx as single, Byval sy as single, _
                                Byval col as Integer, Byval alpha_blend as Integer = 255, _
                                Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_SubPixel_add ( Byval sx as single, Byval sy as single, _
                                    Byval col as Integer, Byval add_blend as Integer, _
                                    Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_SubLine ( Byval sx1 as single, Byval sy1 as single, _
                               Byval sx2 as single, Byval sy2 as single, _
                               Byval col as Integer, Byval alpha_blend as Integer = 255, _
                               Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_SubLine_add ( Byval sx1 as single, Byval sy1 as single, _
                                   Byval sx2 as single, Byval sy2 as single, _
                                   Byval col as Integer, Byval add_blend as Integer, _
                                   Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Triangle ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                Byval sx2 as Integer, Byval sy2 as Integer, _
                                Byval sx3 as Integer, Byval sy3 as Integer, _
                                Byval col as Integer, Byval alpha_blend as Integer = 255, _
                                Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Triangle_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                    Byval sx2 as Integer, Byval sy2 as Integer, _
                                    Byval sx3 as Integer, Byval sy3 as Integer, _
                                    Byval col as Integer, Byval add_blend as Integer, _
                                    Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Triangle_solid ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                      Byval sx2 as Integer, Byval sy2 as Integer, _
                                      Byval sx3 as Integer, Byval sy3 as Integer, _
                                      Byval col as Integer, Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_SubTriangle ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                   Byval sx2 as Integer, Byval sy2 as Integer, _
                                   Byval sx3 as Integer, Byval sy3 as Integer, _
                                   Byval col as Integer, Byval alpha_blend as Integer = 255, _
                                   Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_SubTriangle_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                       Byval sx2 as Integer, Byval sy2 as Integer, _
                                       Byval sx3 as Integer, Byval sy3 as Integer, _
                                       Byval col as Integer, Byval add_blend as Integer, _
                                       Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_FilledTriangle ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                      Byval sx2 as Integer, Byval sy2 as Integer, _
                                      Byval sx3 as Integer, Byval sy3 as Integer, _
                                      Byval col as Integer, Byval alpha_blend as Integer = 255, _
                                      Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_FilledTriangle_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                          Byval sx2 as Integer, Byval sy2 as Integer, _
                                          Byval sx3 as Integer, Byval sy3 as Integer, _
                                          Byval col as Integer, Byval add_blend as Integer, _
                                          Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_FilledTriangle_solid ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                            Byval sx2 as Integer, Byval sy2 as Integer, _
                                            Byval sx3 as Integer, Byval sy3 as Integer, _
                                            Byval col as Integer, _
                                            Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Circle ( Byval Xcenter as Single, Byval Ycenter as Single, _
                              Byval radius as Single, Byval size as Single, _
                              Byval col as Integer, Byval alpha_blend as Integer = 255, _
                              Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Circle_add ( Byval Xcenter as Single, Byval Ycenter as Single, _
                                  Byval radius as Single, Byval size as Single, _
                                  Byval col as Integer, Byval add_blend as Integer, _
                                  Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_FilledCircle ( Byval Xcenter as Single, Byval Ycenter as Single, _
                                    Byval radius as Single, Byval size as Single, _
                                    Byval col as Integer, Byval alpha_blend as Integer, _
                                    Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_FilledCircle_add ( Byval Xcenter as Single, Byval Ycenter as Single, _
                                        Byval radius as Single, Byval size as Single, _
                                        Byval col as Integer, Byval add_blend as Integer, _
                                        Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Globe ( Byval Xcenter as Single, Byval Ycenter as Single, _
                             Byval radius as Single, Byval size as Single, _
                             Byval col_from as Integer, Byval col_to as Integer, _
                             Byval col_blend as Integer, Byval alpha_blend as Integer = 255, _
                             Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Globe_add ( Byval Xcenter as Single, Byval Ycenter as Single, _
                                 Byval radius as Single, Byval size as Single, _
                                 Byval col_from as Integer, Byval col_to as Integer, _
                                 Byval col_blend as Integer, Byval add_blend as Integer, _
                                 Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_Globe_solid ( Byval Xcenter as Single, Byval Ycenter as Single, _
                                   Byval radius as Single, Byval size as Single, _
                                   Byval col_from as Integer, Byval col_to as Integer, _
                                   Byval col_blend as Integer, Byval buffer as any Ptr = 0 )

Declare Sub AFlibIII_FloodFill ( Byval x as Integer, Byval y as Integer, _
                                 Byval col as Integer, Byval buffer as any Ptr = 0 )




Declare Function r_component(Byval c as Integer) as Integer
Declare Function g_component(Byval c as Integer) as Integer
Declare Function b_component(Byval c as Integer) as Integer

Declare Sub Private2_check_buffersize ( Byval buffer as any Ptr, Byref x as Integer, _
                                        Byref y as Integer )

Type AF3_Fill_IntPt2D
  as Integer x, y
End Type







'_________________________________________________________________________
Sub AFlibIII_Pixel ( Byval sx as Integer, Byval sy as Integer, Byval col as Integer, _
                     Byval alpha_blend as Integer = 255, Byval buffer as any Ptr = 0 )

dim tempbuffer as any Ptr = Imagecreate(1, 1, col)
ScreenLock
  Put buffer, (sx, sy), tempbuffer, alpha, alpha_blend
ScreenUnlock

ImageDestroy tempbuffer

End sub


'_________________________________________________________________________
Sub AFlibIII_Pixel_add ( Byval sx as Integer, Byval sy as Integer, Byval col as Integer, _
                         Byval add_blend as Integer, Byval buffer as any Ptr = 0 )

dim tempbuffer as any Ptr = Imagecreate(1, 1, col)
ScreenLock
  Put buffer, (sx, sy), tempbuffer, add, add_blend
ScreenUnlock

ImageDestroy tempbuffer

End sub


'_________________________________________________________________________
Sub AFlibIII_Pixel_custom ( Byval sx as Integer, Byval sy as Integer, Byval col as Integer, _
                            Byval draw_mode as Integer, Byval buffer as any Ptr = 0 )

dim tempbuffer as any Ptr = Imagecreate(1, 1, col)
ScreenLock
  Select Case as const draw_mode
    Case 1: Put buffer, (sx, sy), tempbuffer, pset
    Case 2: Put buffer, (sx, sy), tempbuffer, preset
    Case 3: Put buffer, (sx, sy), tempbuffer, and
    Case 4: Put buffer, (sx, sy), tempbuffer, or
    Case 5: Put buffer, (sx, sy), tempbuffer, xor
  End Select
ScreenUnlock

ImageDestroy tempbuffer

End sub


'_________________________________________________________________________
Sub AFlibIII_Line ( Byval sx1 as Integer, Byval sy1 as Integer, _
                    Byval sx2 as Integer, Byval sy2 as Integer, _
                    Byval col as Integer, Byval alpha_blend as Integer = 255, _
                    Byval buffer as any Ptr = 0 )

'Special Thanks to Dr_Davenstein for the ideas for this routine!!!  ^-^=b ! 

Dim as Single Xdif, Ydif, TempX, TempY, Xit, Yit

Xdif = sx2 - sx1 
Ydif = sy2 - sy1 
    
If Xdif < 0 then Xdif = -Xdif 
If Ydif < 0 then Ydif = -Ydif 
    
If Xdif >= Ydif then 
  If sx2 < sx1 then 
    Swap sx2, sx1 
    Swap sy2, sy1 
  End if 
  If sy2 < sy1 then 
    Xit = -1 
  else 
    Xit = 1 
  End if 
      
  TempY = sy1 
  For TempX = sx1 to sx2 
    If not buffer = 0 then
      AFlibIII_pixel TempX, TempY, col, alpha_blend, buffer     'plot to a buffer
    else
      AFlibIII_pixel TempX, TempY, col, alpha_blend             'plot normally
    End if
    Yit -= Ydif 
    If Yit < 0 THEN 
      Yit += Xdif 
      TempY += Xit 
    End if 
  Next 
else 
  If sy2 < sy1 then 
    Swap sx2, sx1 
    Swap sy2, sy1 
  End if 
  If sx2 < sx1 then 
    Xit = -1 
  else 
    Xit = 1 
  End if 
    
  TempX = sx1 
  For TempY = sy1 to sy2 
    If not buffer = 0 then
      AFlibIII_pixel TempX, TempY, col, alpha_blend, buffer     'plot to a buffer
    else
      AFlibIII_pixel TempX, TempY, col, alpha_blend             'plot normally
    End if
    Yit = Yit - Xdif 
    If Yit < 0 then 
      Yit += Ydif 
      TempX += Xit 
    End if 
  Next 
End if 

End Sub


'_________________________________________________________________________
Sub AFlibIII_Line_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                        Byval sx2 as Integer, Byval sy2 as Integer, _
                        Byval col as Integer, Byval add_blend as Integer, _
                        Byval buffer as any Ptr = 0 )

'Special Thanks to Dr_Davenstein for the ideas for this routine!!!  ^-^=b ! 

Dim as Single Xdif, Ydif, TempX, TempY, Xit, Yit

Xdif = sx2 - sx1 
Ydif = sy2 - sy1 
    
If Xdif < 0 then Xdif = -Xdif 
If Ydif < 0 then Ydif = -Ydif 
    
If Xdif >= Ydif then 
  If sx2 < sx1 then 
    Swap sx2, sx1 
    Swap sy2, sy1 
  End if 
  If sy2 < sy1 then 
    Xit = -1 
  else 
    Xit = 1 
  End if 
      
  TempY = sy1 
  For TempX = sx1 to sx2 
    If not buffer = 0 then
      AFlibIII_pixel_add TempX, TempY, col, add_blend, buffer     'plot to a buffer
    else
      AFlibIII_pixel_add TempX, TempY, col, add_blend             'plot normally
    End if
    Yit -= Ydif 
    If Yit < 0 THEN 
      Yit += Xdif 
      TempY += Xit 
    End if 
  Next 
else 
  If sy2 < sy1 then 
    Swap sx2, sx1 
    Swap sy2, sy1 
  End if 
  If sx2 < sx1 then 
    Xit = -1 
  else 
    Xit = 1 
  End if 
    
  TempX = sx1 
  For TempY = sy1 to sy2 
    If not buffer = 0 then
      AFlibIII_pixel_add TempX, TempY, col, add_blend, buffer     'plot to a buffer
    else
      AFlibIII_pixel_add TempX, TempY, col, add_blend             'plot normally
    End if
    Yit = Yit - Xdif 
    If Yit < 0 then 
      Yit += Ydif 
      TempX += Xit 
    End if 
  Next 
End if 

End Sub


'_________________________________________________________________________
Sub AFlibIII_Line_custom ( Byval sx1 as Integer, Byval sy1 as Integer, _
                           Byval sx2 as Integer, Byval sy2 as Integer, _
                           Byval col as Integer, Byval draw_mode as Integer, _
                           Byval buffer as any Ptr = 0 )

'Special Thanks to Dr_Davenstein for the ideas for this routine!!!  ^-^=b ! 

Dim as Single Xdif, Ydif, TempX, TempY, Xit, Yit

Xdif = sx2 - sx1 
Ydif = sy2 - sy1 
    
If Xdif < 0 then Xdif = -Xdif 
If Ydif < 0 then Ydif = -Ydif 
    
If Xdif >= Ydif then 
  If sx2 < sx1 then 
    Swap sx2, sx1 
    Swap sy2, sy1 
  End if 
  If sy2 < sy1 then 
    Xit = -1 
  else 
    Xit = 1 
  End if 
      
  TempY = sy1 
  For TempX = sx1 to sx2 
    If not buffer = 0 then
      AFlibIII_pixel_custom TempX, TempY, col, draw_mode, buffer      'plot to a buffer
    else
      AFlibIII_pixel_custom TempX, TempY, col, draw_mode              'plot normally
    End if
    Yit -= Ydif 
    If Yit < 0 THEN 
      Yit += Xdif 
      TempY += Xit 
    End if 
  Next 
else 
  If sy2 < sy1 then 
    Swap sx2, sx1 
    Swap sy2, sy1 
  End if 
  If sx2 < sx1 then 
    Xit = -1 
  else 
    Xit = 1 
  End if 
    
  TempX = sx1 
  For TempY = sy1 to sy2 
    If not buffer = 0 then
      AFlibIII_pixel_custom TempX, TempY, col, draw_mode, buffer      'plot to a buffer
    else
      AFlibIII_pixel_custom TempX, TempY, col, draw_mode              'plot normally
    End if
    Yit = Yit - Xdif 
    If Yit < 0 then 
      Yit += Ydif 
      TempX += Xit 
    End if 
  Next 
End if 

End Sub


'_________________________________________________________________________
Sub AFlibIII_Box ( Byval sx1 as Integer, Byval sy1 as Integer, _
                   Byval sx2 as Integer, Byval sy2 as Integer, _
                   Byval col as Integer, Byval alpha_blend as Integer = 255, _
                   Byval buffer as any Ptr = 0 )

dim as Integer x_difference, y_difference
If sx1 < sx2 then Swap sx1, sx2
If sy1 < sy2 then Swap sy1, sy2

x_difference = ((sx1 - sx2) + 1)
y_difference = ((sy1 - sy2) + 1)
If x_difference < 2 then x_difference = 2
If y_difference < 2 then y_difference = 2

dim tempbuffer_point1 as any Ptr = Imagecreate(1, y_difference - 1)
dim tempbuffer_point2 as any Ptr = Imagecreate(x_difference - 1, 1)
dim tempbuffer_point3 as any Ptr = Imagecreate(1, y_difference - 1)
dim tempbuffer_point4 as any Ptr = Imagecreate(x_difference - 1, 1)

ScreenLock
  Line tempbuffer_point2, (0, 0)-(x_difference - 1, 0), col, b
  Line tempbuffer_point1, (0, 0)-(0, y_difference - 1), col, b
  Line tempbuffer_point4, (0, 0)-(x_difference - 1, 0), col, b
  Line tempbuffer_point3, (0, 0)-(0, y_difference - 1), col, b
  Put buffer, (sx2, sy2), tempbuffer_point4, alpha, alpha_blend
  Put buffer, (sx2 + 1, sy2 + (y_difference - 1)), tempbuffer_point2, alpha, alpha_blend
  Put buffer, (sx2, sy2 + 1), tempbuffer_point3, alpha, alpha_blend
  Put buffer, (sx2 + (x_difference - 1), sy2), tempbuffer_point1, alpha, alpha_blend
ScreenUnlock

ImageDestroy tempbuffer_point1
ImageDestroy tempbuffer_point2
ImageDestroy tempbuffer_point3
ImageDestroy tempbuffer_point4

End sub


'_________________________________________________________________________
Sub AFlibIII_Box_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                       Byval sx2 as Integer, Byval sy2 as Integer, _
                       Byval col as Integer, Byval add_blend as Integer, _
                       Byval buffer as any Ptr = 0 )

dim as Integer x_difference, y_difference
If sx1 < sx2 then Swap sx1, sx2
If sy1 < sy2 then Swap sy1, sy2

x_difference = ((sx1 - sx2) + 1)
y_difference = ((sy1 - sy2) + 1)
If x_difference < 2 then x_difference = 2
If y_difference < 2 then y_difference = 2

dim tempbuffer_point1 as any Ptr = Imagecreate(1, y_difference - 1)
dim tempbuffer_point2 as any Ptr = Imagecreate(x_difference - 1, 1)
dim tempbuffer_point3 as any Ptr = Imagecreate(1, y_difference - 1)
dim tempbuffer_point4 as any Ptr = Imagecreate(x_difference - 1, 1)

ScreenLock
  Line tempbuffer_point2, (0, 0)-(x_difference - 1, 0), col, b
  Line tempbuffer_point1, (0, 0)-(0, y_difference - 1), col, b
  Line tempbuffer_point4, (0, 0)-(x_difference - 1, 0), col, b
  Line tempbuffer_point3, (0, 0)-(0, y_difference - 1), col, b
  Put buffer, (sx2, sy2), tempbuffer_point4, add, add_blend
  Put buffer, (sx2 + 1, sy2 + (y_difference - 1)), tempbuffer_point2, add, add_blend
  Put buffer, (sx2, sy2 + 1), tempbuffer_point3, add, add_blend
  Put buffer, (sx2 + (x_difference - 1), sy2), tempbuffer_point1, add, add_blend
ScreenUnlock

ImageDestroy tempbuffer_point1
ImageDestroy tempbuffer_point2
ImageDestroy tempbuffer_point3
ImageDestroy tempbuffer_point4

End sub


'_________________________________________________________________________
Sub AFlibIII_Box_custom ( Byval sx1 as Integer, Byval sy1 as Integer, _
                          Byval sx2 as Integer, Byval sy2 as Integer, _
                          Byval col as Integer, Byval draw_mode as Integer, _
                          Byval buffer as any Ptr = 0 )

dim as Integer x_difference, y_difference
If sx1 < sx2 then Swap sx1, sx2
If sy1 < sy2 then Swap sy1, sy2

x_difference = ((sx1 - sx2) + 1)
y_difference = ((sy1 - sy2) + 1)
If x_difference < 2 then x_difference = 2
If y_difference < 2 then y_difference = 2

dim tempbuffer_point1 as any Ptr = Imagecreate(1, y_difference - 1)
dim tempbuffer_point2 as any Ptr = Imagecreate(x_difference - 1, 1)
dim tempbuffer_point3 as any Ptr = Imagecreate(1, y_difference - 1)
dim tempbuffer_point4 as any Ptr = Imagecreate(x_difference - 1, 1)

ScreenLock
  Line tempbuffer_point2, (0, 0)-(x_difference - 1, 0), col, b
  Line tempbuffer_point1, (0, 0)-(0, y_difference - 1), col, b
  Line tempbuffer_point4, (0, 0)-(x_difference - 1, 0), col, b
  Line tempbuffer_point3, (0, 0)-(0, y_difference - 1), col, b
  Select Case as const draw_mode
    Case 1: 
      Put buffer, (sx2, sy2), tempbuffer_point4, pset
      Put buffer, (sx2 + 1, sy2 + (y_difference - 1)), tempbuffer_point2, pset
      Put buffer, (sx2, sy2 + 1), tempbuffer_point3, pset
      Put buffer, (sx2 + (x_difference - 1), sy2), tempbuffer_point1, pset
    Case 2: 
      Put buffer, (sx2, sy2), tempbuffer_point4, preset
      Put buffer, (sx2 + 1, sy2 + (y_difference - 1)), tempbuffer_point2, preset
      Put buffer, (sx2, sy2 + 1), tempbuffer_point3, preset
      Put buffer, (sx2 + (x_difference - 1), sy2), tempbuffer_point1, preset
    Case 3:
      Put buffer, (sx2, sy2), tempbuffer_point4, and
      Put buffer, (sx2 + 1, sy2 + (y_difference - 1)), tempbuffer_point2, and
      Put buffer, (sx2, sy2 + 1), tempbuffer_point3, and
      Put buffer, (sx2 + (x_difference - 1), sy2), tempbuffer_point1, and
    Case 4
      Put buffer, (sx2, sy2), tempbuffer_point4, or
      Put buffer, (sx2 + 1, sy2 + (y_difference - 1)), tempbuffer_point2, or
      Put buffer, (sx2, sy2 + 1), tempbuffer_point3, or
      Put buffer, (sx2 + (x_difference - 1), sy2), tempbuffer_point1, or
    Case 5
      Put buffer, (sx2, sy2), tempbuffer_point4, xor
      Put buffer, (sx2 + 1, sy2 + (y_difference - 1)), tempbuffer_point2, xor
      Put buffer, (sx2, sy2 + 1), tempbuffer_point3, xor
      Put buffer, (sx2 + (x_difference - 1), sy2), tempbuffer_point1, xor
  End Select
ScreenUnlock

ImageDestroy tempbuffer_point1
ImageDestroy tempbuffer_point2
ImageDestroy tempbuffer_point3
ImageDestroy tempbuffer_point4

End sub


'_________________________________________________________________________
Sub AFlibIII_FilledBox ( Byval sx1 as Integer, Byval sy1 as Integer, _
                         Byval sx2 as Integer, Byval sy2 as Integer, _
                         Byval col as Integer, Byval alpha_blend as Integer = 255, _
                         Byval buffer as any Ptr = 0 )

dim as Integer x_difference, y_difference
If sx1 < sx2 then Swap sx1, sx2
If sy1 < sy2 then Swap sy1, sy2

x_difference = ((sx1 - sx2) + 1)
y_difference = ((sy1 - sy2) + 1)
If x_difference < 1 then x_difference = 1
If y_difference < 1 then y_difference = 1

dim tempbuffer as any Ptr = Imagecreate(x_difference, y_difference, col)
ScreenLock
  Put buffer, (sx2, sy2), tempbuffer, alpha, alpha_blend
ScreenUnlock

ImageDestroy tempbuffer

End sub


'_________________________________________________________________________
Sub AFlibIII_FilledBox_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                             Byval sx2 as Integer, Byval sy2 as Integer, _
                             Byval col as Integer, Byval add_blend as Integer, _
                             Byval buffer as any Ptr = 0 )

dim as Integer x_difference, y_difference
If sx1 < sx2 then Swap sx1, sx2
If sy1 < sy2 then Swap sy1, sy2

x_difference = ((sx1 - sx2) + 1)
y_difference = ((sy1 - sy2) + 1)
If x_difference < 1 then x_difference = 1
If y_difference < 1 then y_difference = 1

dim tempbuffer as any Ptr = Imagecreate(x_difference, y_difference, col)
ScreenLock
  Put buffer, (sx2, sy2), tempbuffer, add, add_blend
ScreenUnlock

ImageDestroy tempbuffer

End sub


'_________________________________________________________________________
Sub AFlibIII_FilledBox_custom ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                Byval sx2 as Integer, Byval sy2 as Integer, _
                                Byval col as Integer, Byval draw_mode as Integer, _
                                Byval buffer as any Ptr = 0 )

dim as Integer x_difference, y_difference
If sx1 < sx2 then Swap sx1, sx2
If sy1 < sy2 then Swap sy1, sy2

x_difference = ((sx1 - sx2) + 1)
y_difference = ((sy1 - sy2) + 1)
If x_difference < 1 then x_difference = 1
If y_difference < 1 then y_difference = 1

dim tempbuffer as any Ptr = Imagecreate(x_difference, y_difference, col)
ScreenLock
  Select Case as const draw_mode
    Case 1: Put buffer, (sx2, sy2), tempbuffer, pset
    Case 2: Put buffer, (sx2, sy2), tempbuffer, preset
    Case 3: Put buffer, (sx2, sy2), tempbuffer, and
    Case 4: Put buffer, (sx2, sy2), tempbuffer, or
    Case 5: Put buffer, (sx2, sy2), tempbuffer, xor
  End Select
ScreenUnlock

ImageDestroy tempbuffer

End sub


'_________________________________________________________________________
Sub AFlibIII_Button ( Byval sx1 as Integer, Byval sy1 as Integer, _
                      Byval sx2 as Integer, Byval sy2 as Integer, _
                      Byval col1 as Integer, Byval col2 as Integer, _
                      Byval alpha_blend as Integer = 255, _
                      Byval buffer as any Ptr = 0 )

dim as Integer x_difference, y_difference
If sx1 < sx2 then Swap sx1, sx2
If sy1 < sy2 then Swap sy1, sy2

x_difference = ((sx1 - sx2) + 1)
y_difference = ((sy1 - sy2) + 1)
If x_difference < 1 then x_difference = 1
If y_difference < 1 then y_difference = 1

dim tempbuffer_point1 as any Ptr = Imagecreate(1, y_difference)
dim tempbuffer_point2 as any Ptr = Imagecreate(x_difference, 1)
dim tempbuffer_point3 as any Ptr = Imagecreate(x_difference, y_difference)

ScreenLock
  Line tempbuffer_point3, (0, 0)-(x_difference - 1, y_difference - 1), col1, b
  Line tempbuffer_point2, (0, 0)-(x_difference - 1, 0), col2, b
  AFlibIII_Pixel x_difference - 1, 0, col1, 128, tempbuffer_point2
  Line tempbuffer_point1, (0, 0)-(0, y_difference - 1), col2, b
  AFlibIII_Pixel 0, y_difference - 1, col1, 128, tempbuffer_point1
  Put buffer, (sx2, sy2), tempbuffer_point3, alpha, alpha_blend
  Put buffer, (sx2, sy2), tempbuffer_point2, alpha, alpha_blend
  Put buffer, (sx2, sy2), tempbuffer_point1, alpha, alpha_blend
ScreenUnlock

ImageDestroy tempbuffer_point1
ImageDestroy tempbuffer_point2
ImageDestroy tempbuffer_point3

End sub


'_________________________________________________________________________
Sub AFlibIII_Button_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                          Byval sx2 as Integer, Byval sy2 as Integer, _
                          Byval col1 as Integer, Byval col2 as Integer, _
                          Byval add_blend as Integer, _
                          Byval buffer as any Ptr = 0 )

dim as Integer x_difference, y_difference
If sx1 < sx2 then Swap sx1, sx2
If sy1 < sy2 then Swap sy1, sy2

x_difference = ((sx1 - sx2) + 1)
y_difference = ((sy1 - sy2) + 1)
If x_difference < 1 then x_difference = 1
If y_difference < 1 then y_difference = 1

dim tempbuffer_point1 as any Ptr = Imagecreate(1, y_difference)
dim tempbuffer_point2 as any Ptr = Imagecreate(x_difference, 1)
dim tempbuffer_point3 as any Ptr = Imagecreate(x_difference, y_difference)

ScreenLock
  Line tempbuffer_point3, (0, 0)-(x_difference - 1, y_difference - 1), col1, b
  Line tempbuffer_point2, (0, 0)-(x_difference - 1, 0), col2, b
  AFlibIII_Pixel x_difference - 1, 0, col1, 128, tempbuffer_point2
  Line tempbuffer_point1, (0, 0)-(0, y_difference - 1), col2, b
  AFlibIII_Pixel 0, y_difference - 1, col1, 128, tempbuffer_point1
  Put buffer, (sx2, sy2), tempbuffer_point3, add, add_blend
  Put buffer, (sx2, sy2), tempbuffer_point2, add, add_blend
  Put buffer, (sx2, sy2), tempbuffer_point1, add, add_blend
ScreenUnlock

ImageDestroy tempbuffer_point1
ImageDestroy tempbuffer_point2
ImageDestroy tempbuffer_point3

End sub


'_________________________________________________________________________
Sub AFlibIII_FilledButton ( Byval sx1 as Integer, Byval sy1 as Integer, _
                            Byval sx2 as Integer, Byval sy2 as Integer, _
                            Byval col1 as Integer, Byval col2 as Integer, _
                            Byval alpha_blend as Integer, _
                            Byval col_to_fill as Integer, _
                            Byval alpha_blend_fill as Integer, _
                            Byval buffer as any Ptr = 0 )

If sx1 < sx2 then Swap sx1, sx2
If sy1 < sy2 then Swap sy1, sy2

If sx1 < 1 then sx1 = 1
If sx2 < 1 then sx2 = 1
If sy1 < 1 then sy1 = 1
If sy2 < 1 then sy2 = 1

AFlibIII_Button sx1, sy1, sx2, sy2, col1, col2, alpha_blend, buffer
AFlibIII_FilledBox sx1 - 1, sy1 - 1, sx2 + 1, sy2 + 1, col_to_fill, alpha_blend_fill, buffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_FilledButton_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                                Byval sx2 as Integer, Byval sy2 as Integer, _
                                Byval col1 as Integer, Byval col2 as Integer, _
                                Byval add_blend as Integer, _
                                Byval col_to_fill as Integer, _
                                Byval add_blend_fill as Integer, _
                                Byval buffer as any Ptr = 0 )

If sx1 < sx2 then Swap sx1, sx2
If sy1 < sy2 then Swap sy1, sy2

If sx1 < 1 then sx1 = 1
If sx2 < 1 then sx2 = 1
If sy1 < 1 then sy1 = 1
If sy2 < 1 then sy2 = 1

AFlibIII_Button_add sx1, sy1, sx2, sy2, col1, col2, add_blend, buffer
AFlibIII_FilledBox_add sx1 - 1, sy1 - 1, sx2 + 1, sy2 + 1, col_to_fill, add_blend_fill, buffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_SubPixel ( Byval sx as single, Byval sy as single, Byval col as Integer, _
                        Byval alpha_blend as Integer = 255, Byval buffer as any Ptr = 0 )

'------------------------------------------------------------------------
' Special Thanks to both Eclipzer and axipher for the ideas for this
' AWESOME routine!!!  ^-^=b ! 
'------------------------------------------------------------------------


  Dim as Integer x,y
  Dim as Integer r0,r1,r2,r3,r4,g0,g1,g2,g3,g4,b0,b1,b2,b3,b4
  Dim as Integer bkg1,bkg2,bkg3,bkg4
  Dim as Integer c1,c2,c3,c4
  Dim as Double x0,y0,x1,y1,x2,y2
  Dim as Double ax1,ax2,ay1,ay2,a1,a2,a3,a4
  
  ' Define sub-pixel attributes
  x = Int(sx):  x0 = x+1:  x1 = sx:  x2 = x1+1
  y = Int(sy):  y0 = y+1:  y1 = sy:  y2 = y1+1
  
  ' Determine area lengths
  ax1 = x0 - x1:  ax2 = x2 - x0
  ay1 = y0 - y1:  ay2 = y2 - y0
  
  ' Calculate areas
  a1 = ax1*ay1:  a2 = ax2*ay1
  a3 = ax1*ay2:  a4 = ax2*ay2
  
  ' Determine 4 background colors
  bkg1 = Point(x  , y  )
  bkg2 = Point(x+1, y  )
  bkg3 = Point(x  , y+1)
  bkg4 = Point(x+1, y+1)
  
  ' Determine RBG components of background colors
  r1 = r_component(bkg1):  r2 = r_component(bkg2):  r3 = r_component(bkg3):  r4 = r_component(bkg4)
  g1 = g_component(bkg1):  g2 = g_component(bkg2):  g3 = g_component(bkg3):  g4 = g_component(bkg4)
  b1 = b_component(bkg1):  b2 = b_component(bkg2):  b3 = b_component(bkg3):  b4 = b_component(bkg4)
  
  ' Determine RGB components of sub-pixel color
  r0 = r_component(col)
  g0 = g_component(col)
  b0 = b_component(col)
  
  ' Calculate  RGB components of sub-pixel's 4 pixels
  c1 = rgb(a1*(r0 - r1) + r1, a1*(g0 - g1) + g1, a1*(b0 - b1) + b1)
  c2 = rgb(a2*(r0 - r2) + r2, a2*(g0 - g2) + g2, a2*(b0 - b2) + b2)
  c3 = rgb(a3*(r0 - r3) + r3, a3*(g0 - g3) + g3, a3*(b0 - b3) + b3)
  c4 = rgb(a4*(r0 - r4) + r4, a4*(g0 - g4) + g4, a4*(b0 - b4) + b4)
  
  ' Draw 4 pixels
  AFlibIII_Pixel x  , y  , c1, alpha_blend, buffer
  AFlibIII_Pixel x+1, y  , c2, alpha_blend, buffer
  AFlibIII_Pixel x  , y+1, c3, alpha_blend, buffer
  AFlibIII_Pixel x+1, y+1, c4, alpha_blend, buffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_SubPixel_add ( Byval sx as single, Byval sy as single, Byval col as Integer, _
                            Byval add_blend as Integer, Byval buffer as any Ptr = 0 )

'------------------------------------------------------------------------
' Special Thanks to both Eclipzer and axipher for the ideas for this
' AWESOME routine!!!  ^-^=b ! 
'------------------------------------------------------------------------


  Dim as Integer x,y
  Dim as Integer r0,r1,r2,r3,r4,g0,g1,g2,g3,g4,b0,b1,b2,b3,b4
  Dim as Integer bkg1,bkg2,bkg3,bkg4
  Dim as Integer c1,c2,c3,c4
  Dim as Double x0,y0,x1,y1,x2,y2
  Dim as Double ax1,ax2,ay1,ay2,a1,a2,a3,a4
  
  ' Define sub-pixel attributes
  x = Int(sx):  x0 = x+1:  x1 = sx:  x2 = x1+1
  y = Int(sy):  y0 = y+1:  y1 = sy:  y2 = y1+1
  
  ' Determine area lengths
  ax1 = x0 - x1:  ax2 = x2 - x0
  ay1 = y0 - y1:  ay2 = y2 - y0
  
  ' Calculate areas
  a1 = ax1*ay1:  a2 = ax2*ay1
  a3 = ax1*ay2:  a4 = ax2*ay2
  
  ' Determine 4 background colors
  bkg1 = Point(x  , y  )
  bkg2 = Point(x+1, y  )
  bkg3 = Point(x  , y+1)
  bkg4 = Point(x+1, y+1)
  
  ' Determine RBG components of background colors
  r1 = r_component(bkg1):  r2 = r_component(bkg2):  r3 = r_component(bkg3):  r4 = r_component(bkg4)
  g1 = g_component(bkg1):  g2 = g_component(bkg2):  g3 = g_component(bkg3):  g4 = g_component(bkg4)
  b1 = b_component(bkg1):  b2 = b_component(bkg2):  b3 = b_component(bkg3):  b4 = b_component(bkg4)
  
  ' Determine RGB components of sub-pixel color
  r0 = r_component(col)
  g0 = g_component(col)
  b0 = b_component(col)
  
  ' Calculate  RGB components of sub-pixel's 4 pixels
  c1 = rgb(a1*(r0 - r1) + r1, a1*(g0 - g1) + g1, a1*(b0 - b1) + b1)
  c2 = rgb(a2*(r0 - r2) + r2, a2*(g0 - g2) + g2, a2*(b0 - b2) + b2)
  c3 = rgb(a3*(r0 - r3) + r3, a3*(g0 - g3) + g3, a3*(b0 - b3) + b3)
  c4 = rgb(a4*(r0 - r4) + r4, a4*(g0 - g4) + g4, a4*(b0 - b4) + b4)
  
  ' Draw 4 pixels
  AFlibIII_Pixel_add x  , y  , c1, add_blend, buffer
  AFlibIII_Pixel_add x+1, y  , c2, add_blend, buffer
  AFlibIII_Pixel_add x  , y+1, c3, add_blend, buffer
  AFlibIII_Pixel_add x+1, y+1, c4, add_blend, buffer

End Sub


Private Function r_component(Byval c as Integer) as Integer
  Return ((c Shr 16) And &hff)
End Function

Private Function g_component(Byval c as Integer) as Integer
  Return ((c Shr 8) And &hff)
End Function

Private Function b_component(Byval c as Integer) as Integer
  Return (c And &hff)
End Function


'_________________________________________________________________________
Sub AFlibIII_SubLine ( Byval sx1 as single, Byval sy1 as single, _
                       Byval sx2 as single, Byval sy2 as single, _
                       Byval col as Integer, Byval alpha_blend as Integer = 255, _
                       Byval buffer as any Ptr = 0 )

'Special Thanks to Dr_Davenstein for the ideas for this routine!!!  ^-^=b ! 

Dim as Single Xdif, Ydif, TempX, TempY, Xit, Yit

Xdif = sx2 - sx1 
Ydif = sy2 - sy1 
    
If Xdif < 0 then Xdif = -Xdif 
If Ydif < 0 then Ydif = -Ydif 
    
If Xdif >= Ydif then 
  If sx2 < sx1 then 
    Swap sx2, sx1 
    Swap sy2, sy1 
  End if 
  If sy2 < sy1 then 
    Xit = -1 
  else 
    Xit = 1 
  End if 
      
  TempY = sy1 
  For TempX = sx1 to sx2
    If not buffer = 0 then
      AFlibIII_SubPixel TempX + .5, TempY + .25, col, alpha_blend, buffer     'plot to a buffer
    else
      AFlibIII_SubPixel TempX + .5, TempY + .25, col, alpha_blend             'plot normally
    End if
    Yit -= Ydif
    If Yit < 0 THEN 
      Yit += Xdif 
      TempY += Xit 
    End if 
  Next 
else 
  If sy2 < sy1 then 
    Swap sx2, sx1 
    Swap sy2, sy1 
  End if 
  If sx2 < sx1 then 
    Xit = -1 
  else 
    Xit = 1 
  End if 
    
  TempX = sx1 
  For TempY = sy1 to sy2
    If not buffer = 0 then
      AFlibIII_SubPixel TempX + .1, TempY + .5, col, alpha_blend, buffer     'plot to a buffer
    else
      AFlibIII_SubPixel TempX + .1, TempY + .5, col, alpha_blend             'plot normally
    End if
    Yit -= Xdif
    If Yit < 0 then 
      Yit += Ydif 
      TempX += Xit 
    End if 
  Next 
End if 

End Sub


'_________________________________________________________________________
Sub AFlibIII_SubLine_add ( Byval sx1 as single, Byval sy1 as single, _
                           Byval sx2 as single, Byval sy2 as single, _
                           Byval col as Integer, Byval add_blend as Integer, _
                           Byval buffer as any Ptr = 0 )

'Special Thanks to Dr_Davenstein for the ideas for this routine!!!  ^-^=b ! 

Dim as Single Xdif, Ydif, TempX, TempY, Xit, Yit

Xdif = sx2 - sx1 
Ydif = sy2 - sy1 
    
If Xdif < 0 then Xdif = -Xdif 
If Ydif < 0 then Ydif = -Ydif 
    
If Xdif >= Ydif then 
  If sx2 < sx1 then 
    Swap sx2, sx1 
    Swap sy2, sy1 
  End if 
  If sy2 < sy1 then 
    Xit = -1 
  else 
    Xit = 1 
  End if 
      
  TempY = sy1 
  For TempX = sx1 to sx2
    If not buffer = 0 then
      AFlibIII_SubPixel_add TempX + .5, TempY + .25, col, add_blend, buffer      'plot to a buffer
    else
      AFlibIII_SubPixel_add TempX + .5, TempY + .25, col, add_blend              'plot normally
    End if
    Yit -= Ydif
    If Yit < 0 THEN 
      Yit += Xdif 
      TempY += Xit 
    End if 
  Next 
else 
  If sy2 < sy1 then 
    Swap sx2, sx1 
    Swap sy2, sy1 
  End if 
  If sx2 < sx1 then 
    Xit = -1 
  else 
    Xit = 1 
  End if 
    
  TempX = sx1 
  For TempY = sy1 to sy2
    If not buffer = 0 then
      AFlibIII_SubPixel_add TempX + .1, TempY + .5, col, add_blend, buffer      'plot to a buffer
    else
      AFlibIII_SubPixel_add TempX + .1, TempY + .5, col, add_blend              'plot normally
    End if
    Yit -= Xdif
    If Yit < 0 then 
      Yit += Ydif 
      TempX += Xit 
    End if 
  Next 
End if 

End Sub


'_________________________________________________________________________
Sub AFlibIII_Triangle ( Byval sx1 as Integer, Byval sy1 as Integer, _
                        Byval sx2 as Integer, Byval sy2 as Integer, _
                        Byval sx3 as Integer, Byval sy3 as Integer, _
                        Byval col as Integer, Byval alpha_blend as Integer = 255, _
                        Byval buffer as any Ptr = 0 )

'             /|
'        d1  / |
'           /  |
'          /   |
'         /    |
'         \    |
'          \   |d3
'        d2 \  |
'            \ |
'             \|

AFlibIII_Line sx1, sy1, sx2, sy2, col, alpha_blend, buffer
AFlibIII_Line sx2, sy2, sx3, sy3, col, alpha_blend, buffer
AFlibIII_Line sx3, sy3, sx1, sy1, col, alpha_blend, buffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_Triangle_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                            Byval sx2 as Integer, Byval sy2 as Integer, _
                            Byval sx3 as Integer, Byval sy3 as Integer, _
                            Byval col as Integer, Byval add_blend as Integer, _
                            Byval buffer as any Ptr = 0 )

'             /|
'        d1  / |
'           /  |
'          /   |
'         /    |
'         \    |
'          \   |d3
'        d2 \  |
'            \ |
'             \|

AFlibIII_Line_add sx1, sy1, sx2, sy2, col, add_blend, buffer
AFlibIII_Line_add sx2, sy2, sx3, sy3, col, add_blend, buffer
AFlibIII_Line_add sx3, sy3, sx1, sy1, col, add_blend, buffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_Triangle_solid ( Byval sx1 as Integer, Byval sy1 as Integer, _
                              Byval sx2 as Integer, Byval sy2 as Integer, _
                              Byval sx3 as Integer, Byval sy3 as Integer, _
                              Byval col as Integer, Byval buffer as any Ptr = 0 )

'             /|
'        d1  / |
'           /  |
'          /   |
'         /    |
'         \    |
'          \   |d3
'        d2 \  |
'            \ |
'             \|

Line buffer, (sx1, sy1)-(sx2, sy2), col
Line buffer, (sx2, sy2)-(sx3, sy3), col
Line buffer, (sx3, sy3)-(sx1, sy1), col

End Sub


'_________________________________________________________________________
Sub AFlibIII_SubTriangle ( Byval sx1 as Integer, Byval sy1 as Integer, _
                           Byval sx2 as Integer, Byval sy2 as Integer, _
                           Byval sx3 as Integer, Byval sy3 as Integer, _
                           Byval col as Integer, Byval alpha_blend as Integer = 255, _
                           Byval buffer as any Ptr = 0 )

'             /|
'        d1  / |
'           /  |
'          /   |
'         /    |
'         \    |
'          \   |d3
'        d2 \  |
'            \ |
'             \|

AFlibIII_SubLine sx1, sy1, sx2, sy2, col, alpha_blend, buffer
AFlibIII_SubLine sx2, sy2, sx3, sy3, col, alpha_blend, buffer
AFlibIII_SubLine sx3, sy3, sx1, sy1, col, alpha_blend, buffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_SubTriangle_add ( Byval sx1 as Integer, Byval sy1 as Integer, _
                               Byval sx2 as Integer, Byval sy2 as Integer, _
                               Byval sx3 as Integer, Byval sy3 as Integer, _
                               Byval col as Integer, Byval add_blend as Integer, _
                               Byval buffer as any Ptr = 0 )

'             /|
'        d1  / |
'           /  |
'          /   |
'         /    |
'         \    |
'          \   |d3
'        d2 \  |
'            \ |
'             \|

AFlibIII_SubLine_add sx1, sy1, sx2, sy2, col, add_blend, buffer
AFlibIII_SubLine_add sx2, sy2, sx3, sy3, col, add_blend, buffer
AFlibIII_SubLine_add sx3, sy3, sx1, sy1, col, add_blend, buffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_FilledTriangle ( Byval sx1 as Integer, _
                              Byval sy1 as Integer, _
                              Byval sx2 as Integer, _
                              Byval sy2 as Integer, _
                              Byval sx3 as Integer, _
                              Byval sy3 as Integer, _
                              Byval col as Integer, _
                              Byval alpha_blend as Integer = 255, _
                              Byval buffer as any Ptr = 0 )

'             /|
'        d1  / |
'           /  |
'          /   |
'         /    |
'         \    |
'          \   |d3
'        d2 \  |
'            \ |
'             \|

'This implementation of the filled triangle routine is the fast fixed-point
'version.  Originally created by Richard Eric M. Lope (Relsoft), and
'implemented for AFlib2 in FreeBASIC by Adigun Azikiwe Polack.

Dim as Integer SCREEN_WID, SCREEN_HEI

ScreenInfo SCREEN_WID, SCREEN_HEI

If not buffer = 0 then Private2_check_buffersize buffer, SCREEN_WID, SCREEN_HEI
dim tempbuffer as any Ptr = Imagecreate(SCREEN_WID, SCREEN_HEI)

Dim as Integer x1, y1, x2, y2, x3, y3, y
Dim as Integer dx1, dy1, dx2, dy2, dx3, dy3

Dim as Single delta1, delta2, delta3, Lx, Rx

ScreenLock
  x1 = sx1
  y1 = sy1

  x2 = sx2
  y2 = sy2

  x3 = sx3
  y3 = sy3

  If y2 < y1 then
    Swap y1, y2
    Swap x1, x2
  End if
  If y3 < y1 then
    Swap y3, y1
    Swap x3, x1
  End if
  If y3 < y2 then
    Swap y3, y2
    Swap x3, x2
  End if

  dx1 = x2 - x1
  dy1 = y2 - y1
  If dy1 <> 0 then
    delta1 = dx1 * 65536 \ dy1
  else
    delta1 = 0
  End if

  dx2 = x3 - x2
  dy2 = y3 - y2
  If dy2 <> 0 then
    delta2 = dx2 * 65536 \ dy2
  else
    delta2 = 0
  End if

  dx3 = x1 - x3
  dy3 = y1 - y3
  If dy3 <> 0 then
    delta3 = dx3 * 65536 \ dy3
  else
    delta3 = 0
  End if


  'Flat bottom
  'Top part of triangle

  Lx = x1 * 65536
  Rx = Lx

  For y = y1 TO y2 - 1
    Line tempbuffer, (Lx \ 65536, y)-(Rx \ 65536, y), col
    Lx = Lx + delta1
    Rx = Rx + delta3
  Next y

  'Flat top
  'Lower part of triangle

  Lx = x2 * 65536
  For y = y2 TO y3
    Line tempbuffer, (Lx \ 65536, y)-(Rx \ 65536, y), col
    Lx = Lx + delta2
    Rx = Rx + delta3
  Next y

  Put buffer, (0, 0), tempbuffer, alpha, alpha_blend
ScreenUnlock

ImageDestroy tempbuffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_FilledTriangle_add ( Byval sx1 as Integer, _
                                  Byval sy1 as Integer, _
                                  Byval sx2 as Integer, _
                                  Byval sy2 as Integer, _
                                  Byval sx3 as Integer, _
                                  Byval sy3 as Integer, _
                                  Byval col as Integer, _
                                  Byval add_blend as Integer, _
                                  Byval buffer as any Ptr = 0 )

'             /|
'        d1  / |
'           /  |
'          /   |
'         /    |
'         \    |
'          \   |d3
'        d2 \  |
'            \ |
'             \|

'This implementation of the filled triangle routine is the fast fixed-point
'version.  Originally created by Richard Eric M. Lope (Relsoft), and
'implemented for AFlib2 in FreeBASIC by Adigun Azikiwe Polack.

Dim as Integer SCREEN_WID, SCREEN_HEI

ScreenInfo SCREEN_WID, SCREEN_HEI

If not buffer = 0 then Private2_check_buffersize buffer, SCREEN_WID, SCREEN_HEI
dim tempbuffer as any Ptr = Imagecreate(SCREEN_WID, SCREEN_HEI)

Dim as Integer x1, y1, x2, y2, x3, y3, y
Dim as Integer dx1, dy1, dx2, dy2, dx3, dy3

Dim as Single delta1, delta2, delta3, Lx, Rx

ScreenLock
  x1 = sx1
  y1 = sy1

  x2 = sx2
  y2 = sy2

  x3 = sx3
  y3 = sy3

  If y2 < y1 then
    Swap y1, y2
    Swap x1, x2
  End if
  If y3 < y1 then
    Swap y3, y1
    Swap x3, x1
  End if
  If y3 < y2 then
    Swap y3, y2
    Swap x3, x2
  End if

  dx1 = x2 - x1
  dy1 = y2 - y1
  If dy1 <> 0 then
    delta1 = dx1 * 65536 \ dy1
  else
    delta1 = 0
  End if

  dx2 = x3 - x2
  dy2 = y3 - y2
  If dy2 <> 0 then
    delta2 = dx2 * 65536 \ dy2
  else
    delta2 = 0
  End if

  dx3 = x1 - x3
  dy3 = y1 - y3
  If dy3 <> 0 then
    delta3 = dx3 * 65536 \ dy3
  else
    delta3 = 0
  End if


  'Flat bottom
  'Top part of triangle

  Lx = x1 * 65536
  Rx = Lx

  For y = y1 TO y2 - 1
    Line tempbuffer, (Lx \ 65536, y)-(Rx \ 65536, y), col
    Lx = Lx + delta1
    Rx = Rx + delta3
  Next y

  'Flat top
  'Lower part of triangle

  Lx = x2 * 65536
  For y = y2 TO y3
    Line tempbuffer, (Lx \ 65536, y)-(Rx \ 65536, y), col
    Lx = Lx + delta2
    Rx = Rx + delta3
  Next y

  Put buffer, (0, 0), tempbuffer, add, add_blend
ScreenUnlock

ImageDestroy tempbuffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_FilledTriangle_solid ( Byval sx1 as Integer, _
                                    Byval sy1 as Integer, _
                                    Byval sx2 as Integer, _
                                    Byval sy2 as Integer, _
                                    Byval sx3 as Integer, _
                                    Byval sy3 as Integer, _
                                    Byval col as Integer, _
                                    Byval buffer as any Ptr = 0 )

'             /|
'        d1  / |
'           /  |
'          /   |
'         /    |
'         \    |
'          \   |d3
'        d2 \  |
'            \ |
'             \|

'This implementation of the filled triangle routine is the fast fixed-point
'version.  Originally created by Richard Eric M. Lope (Relsoft), and
'implemented for AFlib2 in FreeBASIC by Adigun Azikiwe Polack.

Dim as Integer x1, y1, x2, y2, x3, y3, y
Dim as Integer dx1, dy1, dx2, dy2, dx3, dy3

Dim as Single delta1, delta2, delta3, Lx, Rx

x1 = sx1
y1 = sy1

x2 = sx2
y2 = sy2

x3 = sx3
y3 = sy3

If y2 < y1 then
  Swap y1, y2
  Swap x1, x2
End if
If y3 < y1 then
  Swap y3, y1
  Swap x3, x1
End if
If y3 < y2 then
  Swap y3, y2
  Swap x3, x2
End if

dx1 = x2 - x1
dy1 = y2 - y1
If dy1 <> 0 then
  delta1 = dx1 * 65536 \ dy1
else
  delta1 = 0
End if

dx2 = x3 - x2
dy2 = y3 - y2
If dy2 <> 0 then
  delta2 = dx2 * 65536 \ dy2
else
  delta2 = 0
End if

dx3 = x1 - x3
dy3 = y1 - y3
If dy3 <> 0 then
  delta3 = dx3 * 65536 \ dy3
else
  delta3 = 0
End if


'Flat bottom
'Top part of triangle

Lx = x1 * 65536
Rx = Lx

For y = y1 TO y2 - 1
  Line buffer, (Lx \ 65536, y)-(Rx \ 65536, y), col
  Lx = Lx + delta1
  Rx = Rx + delta3
Next y

'Flat top
'Lower part of triangle

Lx = x2 * 65536
For y = y2 TO y3
  Line buffer, (Lx \ 65536, y)-(Rx \ 65536, y), col
  Lx = Lx + delta2
  Rx = Rx + delta3
Next y

End Sub


'_________________________________________________________________________
Sub AFlibIII_Circle ( Byval Xcenter as Single, Byval Ycenter as Single, _
                      Byval radius as Single, Byval size as Single, _
                      Byval col as Integer, Byval alpha_blend as Integer = 255, _
                      Byval buffer as any Ptr = 0 )

If size > 300 then size = 300
If size <= 0 then Exit Sub

dim tempbuffer as any Ptr = Imagecreate(((Xcenter * 2) + (size * 2)) + 1, ((Ycenter * 2) + (size * 2)) + 1)

Circle tempbuffer, (Xcenter, Ycenter), size, col, , , radius

ScreenLock
  Put buffer, ((Xcenter / 80) - 1, (Ycenter / 80) - 1), tempbuffer, alpha, alpha_blend
ScreenUnlock

ImageDestroy tempbuffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_Circle_add ( Byval Xcenter as Single, Byval Ycenter as Single, _
                          Byval radius as Single, Byval size as Single, _
                          Byval col as Integer, Byval add_blend as Integer, _
                          Byval buffer as any Ptr = 0 )

If size > 300 then size = 300
If size <= 0 then Exit Sub

dim tempbuffer as any Ptr = Imagecreate(((Xcenter * 2) + (size * 2)) + 1, ((Ycenter * 2) + (size * 2)) + 1)

Circle tempbuffer, (Xcenter, Ycenter), size, col, , , radius

ScreenLock
  Put buffer, ((Xcenter / 80) - 1, (Ycenter / 80) - 1), tempbuffer, add, add_blend
ScreenUnlock

ImageDestroy tempbuffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_FilledCircle ( Byval Xcenter as Single, Byval Ycenter as Single, _
                            Byval radius as Single, Byval size as Single, _
                            Byval col as Integer, Byval alpha_blend as Integer, _
                            Byval buffer as any Ptr = 0 )

If size > 300 then size = 300
If size <= 0 then Exit Sub

dim tempbuffer as any Ptr = Imagecreate(((Xcenter * 2) + (size * 2)) + 1, ((Ycenter * 2) + (size * 2)) + 1)

Circle tempbuffer, (Xcenter, Ycenter), size, col, , , radius, F

ScreenLock
  Put buffer, ((Xcenter / 80) - 1, (Ycenter / 80) - 1), tempbuffer, alpha, alpha_blend
ScreenUnlock

ImageDestroy tempbuffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_FilledCircle_add ( Byval Xcenter as Single, Byval Ycenter as Single, _
                                Byval radius as Single, Byval size as Single, _
                                Byval col as Integer, Byval add_blend as Integer, _
                                Byval buffer as any Ptr = 0 )

If size > 300 then size = 300
If size <= 0 then Exit Sub

dim tempbuffer as any Ptr = Imagecreate(((Xcenter * 2) + (size * 2)) + 1, ((Ycenter * 2) + (size * 2)) + 1)

Circle tempbuffer, (Xcenter, Ycenter), size, col, , , radius, F

ScreenLock
  Put buffer, ((Xcenter / 80) - 1, (Ycenter / 80) - 1), tempbuffer, add, add_blend
ScreenUnlock

ImageDestroy tempbuffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_Globe ( Byval Xcenter as Single, Byval Ycenter as Single, _
                     Byval radius as Single, Byval size as Single, _
                     Byval col_from as Integer, Byval col_to as Integer, _
                     Byval col_blend as Integer, Byval alpha_blend as Integer = 255, _
                     Byval buffer as any Ptr = 0 )

If size > 300 then size = 300
If size <= 0 then Exit Sub

Static as Integer RRA,RRB,GGA,GGB,BBA,BBB
Static as Integer RR,GG,BB,HARAD,DBRAD  
Dim as Integer blendcontrol

RRA = (col_to Shr 16) And 255
GGA = (col_to Shr 8) And 255
BBA = (col_to And 255)
RRB = (col_from Shr 16) And 255
GGB = (col_from Shr 8) And 255
BBB = (col_from And 255)
DBRAD = col_blend 
HARAD = DBRAD

dim tempbuffer as any Ptr = Imagecreate(((Xcenter * 2) + (size * 2)) + 1, ((Ycenter * 2) + (size * 2)) + 1)

For CircGlobe as Integer = size to 0 step -1
  If blendcontrol <= col_blend then
    RR = Percent2(RRA,RRB,HARAD,DBRAD)
    GG = Percent2(GGA,GGB,HARAD,DBRAD)
    BB = Percent2(BBA,BBB,HARAD,DBRAD)
    Circle tempbuffer, (Xcenter, Ycenter), CircGlobe, RGB(RR, GG, BB), , , radius, F
    blendcontrol += 1
    HARAD -= 1
  else
    Exit For
  end if
Next  

ScreenLock
  Put buffer, ((Xcenter / 80) - 1, (Ycenter / 80) - 1), tempbuffer, alpha, alpha_blend
ScreenUnlock

ImageDestroy tempbuffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_Globe_add ( Byval Xcenter as Single, Byval Ycenter as Single, _
                         Byval radius as Single, Byval size as Single, _
                         Byval col_from as Integer, Byval col_to as Integer, _
                         Byval col_blend as Integer, Byval add_blend as Integer, _
                         Byval buffer as any Ptr = 0 )

If size > 300 then size = 300
If size <= 0 then Exit Sub

Static as Integer RRA,RRB,GGA,GGB,BBA,BBB
Static as Integer RR,GG,BB,HARAD,DBRAD  
Dim as Integer blendcontrol

RRA = (col_to Shr 16) And 255
GGA = (col_to Shr 8) And 255
BBA = (col_to And 255)
RRB = (col_from Shr 16) And 255
GGB = (col_from Shr 8) And 255
BBB = (col_from And 255)
DBRAD = col_blend 
HARAD = DBRAD

dim tempbuffer as any Ptr = Imagecreate(((Xcenter * 2) + (size * 2)) + 1, ((Ycenter * 2) + (size * 2)) + 1)

For CircGlobe as Integer = size to 0 step -1
  If blendcontrol <= col_blend then
    RR = Percent2(RRA,RRB,HARAD,DBRAD)
    GG = Percent2(GGA,GGB,HARAD,DBRAD)
    BB = Percent2(BBA,BBB,HARAD,DBRAD)
    Circle tempbuffer, (Xcenter, Ycenter), CircGlobe, RGB(RR, GG, BB), , , radius, F
    blendcontrol += 1
    HARAD -= 1
  else
    Exit For
  end if
Next  

ScreenLock
  Put buffer, ((Xcenter / 80) - 1, (Ycenter / 80) - 1), tempbuffer, add, add_blend
ScreenUnlock

ImageDestroy tempbuffer

End Sub


'_________________________________________________________________________
Sub AFlibIII_Globe_solid ( Byval Xcenter as Single, Byval Ycenter as Single, _
                           Byval radius as Single, Byval size as Single, _
                           Byval col_from as Integer, Byval col_to as Integer, _
                           Byval col_blend as Integer, Byval buffer as any Ptr = 0 )

If size > 300 then size = 300
If size <= 0 then Exit Sub

Static as Integer RRA,RRB,GGA,GGB,BBA,BBB
Static as Integer RR,GG,BB,HARAD,DBRAD  
Dim as Integer blendcontrol

RRA = (col_to Shr 16) And 255
GGA = (col_to Shr 8) And 255
BBA = (col_to And 255)
RRB = (col_from Shr 16) And 255
GGB = (col_from Shr 8) And 255
BBB = (col_from And 255)
DBRAD = col_blend 
HARAD = DBRAD

For CircGlobe as Integer = size to 0 step -1
  If blendcontrol <= col_blend then
    RR = Percent2(RRA,RRB,HARAD,DBRAD)
    GG = Percent2(GGA,GGB,HARAD,DBRAD)
    BB = Percent2(BBA,BBB,HARAD,DBRAD)
    Circle (Xcenter, Ycenter), CircGlobe, RGB(RR, GG, BB), , , radius, F
    blendcontrol += 1
    HARAD -= 1
  else
    Exit For
  end if
Next  

End Sub


'_________________________________________________________________________
Sub AFlibIII_FloodFill ( Byval x as Integer, Byval y as Integer, _
                         Byval col as Integer, Byval buffer as any Ptr = 0 )
  
  '---------------------------------------------------------------------
  ' BIG Special Thanks to fxm for this great routine!!!  ;)
  '---------------------------------------------------------------------

  Dim as UInteger oldcol = Point(x, y, buffer)
  Dim as Integer Top
  Redim as AF3_Fill_IntPt2D Stack(3)
  Stack(0) = Type(x, y)
  ScreenLock
  Do until Top = -1
    If oldcol = Point(Stack(Top).x, Stack(Top).y, buffer) then
      If col = oldcol then Exit do
      Pset buffer, (Stack(Top).x, Stack(Top).y), col
      If Top + 3 > Ubound(Stack) then Redim Preserve Stack(Ubound(Stack) * 2)
      Stack(Top + 1) = Type(Stack(Top).x - 1, Stack(Top).y)
      Stack(Top + 2) = Type(Stack(Top).x, Stack(Top).y + 1)
      Stack(Top + 3) = Type(Stack(Top).x, Stack(Top).y - 1)
      Stack(Top).x += 1
      Top += 4
    End If
    Top -= 1
  Loop
  ScreenUnlock
End Sub






Private Sub Private2_check_buffersize ( Byval buffer as any Ptr, Byref x as Integer, _
                                        Byref y as Integer )

'----------------------------------------------------------------------
' BIG special thanks to cha0s on this *excellent* buffer detection
' routine!!!  d=^-^=b !!
'----------------------------------------------------------------------

x = 0: y = 0
If buffer = 0 Then Exit Sub

If isBufferNew_c( buffer ) Then
  x = cast( UInteger Ptr, buffer )[2]
  y = cast( UInteger Ptr, buffer )[3]
else
  x = cast( Ushort Ptr, buffer )[0] Shr 3
  y = cast( Ushort Ptr, buffer )[1]
end If
        
end sub
