'
'                           ___________________________________________
'         _______ _ _ _     \___           ___________________________/
'    /\  (_______) (_) |       /  //  //  /  
'   /  \  _  __  | |_| | _    /  //  //  / << 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, and on *only* this piece of source code, you
' are free just to change the graphics resolution "X" and "Y" settings on
' the "GFXres_X" and "GFXres_Y" parameters, respectively, according to the
' exact 15- to 32-bit color graphics resolution you are gonna be using for
' your game or project (fullscreen or windowed).
'
' With that said, do enjoy the library and be inspired even more
' greatly than ever!!!  (^-^)//
'_________________________________________________________________________
'
'Special Thanks to Mysoft for the AWESOME Scale2X/Resize2X conversions to
'FB on any 15- to 32-bit color graphics mode of your choice!!!  ;) !!

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

'_________________________________________________________________________
' Only on these two lines, change the "GFXres_X" and "GFXres_Y" parameters
' ACCORDING to the graphics resolution you are gonna be using!  After
' that, it is recommended that you save this file under a *different*
' filename (such as "MyCustom_AF3Scale2X.bi", for example), ok now?  ^-^ !
'_________________________________________________________________________
Const GFXres_X = 640
Const GFXres_Y = 480
'_________________________________________________________________________


#define SC2X_X (GFXres_X / 2)
#define SC2X_Y (GFXres_Y / 2)


Declare Sub AFlibIII_Scale2X
Declare Sub AFlibIII_Resize2X





Declare sub raw_Scale2X_16 (Byval PTORG as any ptr, Byval PTDST as any ptr)
Declare sub raw_Resize2X_16 (Byval PTORG as any ptr,Byval PTDST as any ptr)
Declare sub raw_Scale2X_32 (Byval PTORG as any ptr,Byval PTDST as any ptr)
Declare sub raw_Resize2X_32 (Byval PTORG as any ptr, Byval PTDST as any ptr)





'__________________________________________________________________________
Sub AFlibIII_Scale2X

Dim as Integer Gfx_horiz, Gfx_vert, GFX_depth
Dim buffer as any ptr
ScreenInfo Gfx_horiz, Gfx_vert, GFX_depth

If GFX_depth < 15 then Exit Sub
Buffer = screenptr

Dim image as any ptr

Gfx_horiz /= 2
Gfx_vert /= 2

image = ImageCreate(Gfx_horiz, Gfx_vert)
Get (0,0)-(Gfx_horiz - 1, Gfx_vert - 1), image

Select Case as Const GFX_depth
  Case 15: raw_Scale2X_16(image+sizeof(fb.image),Buffer)
  Case 16: raw_Scale2X_16(image+sizeof(fb.image),Buffer)
  Case 24: raw_Scale2X_32(image+sizeof(fb.image),Buffer)
  Case 32: raw_Scale2X_32(image+sizeof(fb.image),Buffer)
End Select

ImageDestroy image
End Sub


'__________________________________________________________________________
Sub AFlibIII_Resize2X

Dim as Integer Gfx_horiz, Gfx_vert, GFX_depth
Dim buffer as any ptr
ScreenInfo Gfx_horiz, Gfx_vert, GFX_depth

If GFX_depth < 15 then Exit Sub
Buffer = screenptr

Dim image as any ptr

Gfx_horiz /= 2
Gfx_vert /= 2

image = ImageCreate(Gfx_horiz, Gfx_vert)
Get (0,0)-(Gfx_horiz - 1, Gfx_vert - 1), image

Select Case as Const GFX_depth
  Case 15: raw_Resize2X_16(image+sizeof(fb.image),Buffer)
  Case 16: raw_Resize2X_16(image+sizeof(fb.image),Buffer)
  Case 24: raw_Resize2X_32(image+sizeof(fb.image),Buffer)
  Case 32: raw_Resize2X_32(image+sizeof(fb.image),Buffer)
End Select

ImageDestroy image
End Sub







Private Sub raw_Resize2X_16 (Byval PTORG as any ptr, Byval PTDST as any ptr)
  
  #define ESZ 2
  
  const OPI = ESZ
  const OPIB = OPI*2
  const PILI = (SC2X_X*2)*ESZ
  
  asm
    mov ESI,[PTORG]
    mov EDI,[PTDST]
    mov BX,SC2X_Y
    shl EBX,16
    mov CX,SC2X_X
    _R2XCUST16_NEXTLINE_:
    mov BX,CX
    _R2XCUST16_NEXTPOINT_:
    lodsw
    mov DX,AX
    shl EAX,16
    mov AX,DX    
    mov [EDI],EAX
    mov [EDI+PILI],EAX
    add EDI,OPIB
    dec BX
    jnz _R2XCUST16_NEXTPOINT_
    add EDI,PILI
    sub EBX,65536    
    jnz _R2XCUST16_NEXTLINE_
  end asm
  
end sub

Private Sub raw_Scale2X_16 (Byval PTORG as any ptr, Byval PTDST as any ptr)
  static as integer Y  
  Y = SC2X_Y
  
  #define ESZ 2  
  
  const PILI = SC2X_X
  const PILIS = SC2X_X-1
  const OPI = ESZ
  const TPI = ESZ*2  
  const OLIS = (SC2X_X-1)*ESZ
  const OLIA = (SC2X_X+1)*ESZ
  const TLISS = ((SC2X_X*2)-2)*ESZ
  const TLIS = ((SC2X_X*2)-1)*ESZ
  const TLI = ((SC2X_X*2))*ESZ
  const TLIA = ((SC2X_X*2)+1)*ESZ
  
  asm        
    mov ESI,[PTORG]
    mov EDI,[PTDST]
    lodsw
    stosw
    stosw
    mov [EDI+TLISS],AX
    mov [EDI+TLIS],AX
    mov ECX,PILIS
    __Sc2XCUST16_FLNEXTPOINT_:
    lodsw
    mov BX,[ESI]
    mov DX,[ESI]
    cmp BX,[ESI+OLIS]
    je __Sc2XCUST16_FLBEHODEF_
    cmp [ESI-TPI],DX
    je __Sc2XCUST16_FLBEHODEF_
    cmp BX,[ESI-TPI]
    je __Sc2XCUST16_FLNOCHGE0_
    mov BX,AX
    __Sc2XCUST16_FLNOCHGE0_:
    mov [EDI],BX
    cmp [ESI],DX
    je __Sc2XCUST16_FLNOCHGE1_
    mov DX,AX
    __Sc2XCUST16_FLNOCHGE1_:
    mov [EDI+OPI],DX
    mov BX,[ESI-TPI]
    mov DX,[ESI+OLIS]
    cmp BX,DX
    je __Sc2XCUST16_FLNOCHGE2_
    mov BX,AX
    __Sc2XCUST16_FLNOCHGE2_:
    mov [EDI+TLI],BX
    cmp DX,[ESI]
    je __Sc2XCUST16_FLNOCHGE3_
    mov DX,AX
    __Sc2XCUST16_FLNOCHGE3_:
    mov [EDI+TLIA],DX
    add EDI,TPI
    dec ECX
    jnz __Sc2XCUST16_FLNEXTPOINT_
    add EDI,TLI
    dec dword ptr [Y]
    jnz __Sc2XCUST16_NEXTLINE_
    __Sc2XCUST16_FLBEHODEF_:
    stosw
    stosw
    mov [EDI+TLISS],AX
    mov [EDI+TLIS],AX
    dec ECX
    jnz __Sc2XCUST16_FLNEXTPOINT_
    add EDI,TLI
    dec dword ptr [Y]
    __Sc2XCUST16_NEXTLINE_:
    mov ECX,PILI
    __Sc2XCUST16_NEXTPOINT_:
    lodsw
    mov BX,[ESI-OLIA]
    mov DX,[ESI]
    cmp BX,[ESI+OLIS]
    je __Sc2XCUST16_BEHODEF_
    cmp [ESI-TPI],DX
    je __Sc2XCUST16_BEHODEF_
    cmp BX,[ESI-TPI]
    je __Sc2XCUST16_NOCHGE0_
    mov BX,AX
    __Sc2XCUST16_NOCHGE0_:
    mov [EDI],BX
    cmp [ESI-OLIA],DX
    je __Sc2XCUST16_NOCHGE1_
    mov DX,AX
    __Sc2XCUST16_NOCHGE1_:
    mov [EDI+OPI],DX
    mov BX,[ESI-TPI]
    mov DX,[ESI+OLIS]
    cmp BX,DX
    je __Sc2XCUST16_NOCHGE2_
    mov BX,AX
    __Sc2XCUST16_NOCHGE2_:
    mov [EDI+TLI],BX
    cmp DX,[ESI]
    je __Sc2XCUST16_NOCHGE3_
    mov DX,AX
    __Sc2XCUST16_NOCHGE3_:
    mov [EDI+TLIA],DX
    add EDI,TPI
    dec ECX
    jnz __Sc2XCUST16_NEXTPOINT_
    add EDI,TLI
    dec dword ptr [Y]
    jnz __Sc2XCUST16_NEXTLINE_
    jmp __Sc2XCUST16_FINALEND_
    __Sc2XCUST16_BEHODEF_:
    stosw
    stosw
    mov [EDI+TLISS],AX
    mov [EDI+TLIS],AX
    dec ECX
    jnz __Sc2XCUST16_NEXTPOINT_
    add EDI,TLI
    dec dword ptr [Y]
    jnz __Sc2XCUST16_NEXTLINE_
    __Sc2XCUST16_FINALEND_:
  end asm  
end sub

Private Sub raw_Resize2X_32 (Byval PTORG as any ptr, Byval PTDST as any ptr)
  
  #define ESZ 4
  
  const OPI = ESZ
  const OPIB = OPI*2
  const PILI = (SC2X_X*2)*ESZ
  const PILIB = PILI+OPI
  
  asm
    mov ESI,[PTORG]
    mov EDI,[PTDST]
    mov EDX,SC2X_Y    
    mov EBX,SC2X_X
    _R2XCUST32_NEXTLINE_:
    mov ECX,EBX
    _R2XCUST32_NEXTPOINT_:
    lodsd    
    mov [EDI],EAX
    mov [EDI+OPI],EAX
    mov [EDI+PILI],EAX
    mov [EDI+PILIB],EAX
    add EDI,OPIB
    dec ECX
    jnz _R2XCUST32_NEXTPOINT_
    add EDI,PILI
    dec EDX
    jnz _R2XCUST32_NEXTLINE_
  end asm
  
end sub

Private Sub raw_Scale2X_32 (Byval PTORG as any ptr, Byval PTDST as any ptr)
  static as integer Y  
  Y = SC2X_Y
  
  #define ESZ 4  
  
  const PILI = SC2X_X
  const PILIS = SC2X_X-1
  const OPI = ESZ
  const TPI = ESZ*2  
  const OLIS = (SC2X_X-1)*ESZ
  const OLIA = (SC2X_X+1)*ESZ
  const TLISS = ((SC2X_X*2)-2)*ESZ
  const TLIS = ((SC2X_X*2)-1)*ESZ
  const TLI = ((SC2X_X*2))*ESZ
  const TLIA = ((SC2X_X*2)+1)*ESZ
  
  asm        
    mov ESI,[PTORG]
    mov EDI,[PTDST]
    lodsd
    stosd
    stosd
    mov [EDI+TLISS],EAX
    mov [EDI+TLIS],EAX
    mov ECX,PILIS
    __Sc2XCUST32_FLNEXTPOINT_:
    lodsd
    mov EBX,[ESI]
    mov EDX,[ESI]
    cmp EBX,[ESI+OLIS]
    je __Sc2XCUST32_FLBEHODEF_
    cmp [ESI-TPI],EDX
    je __Sc2XCUST32_FLBEHODEF_
    cmp EBX,[ESI-TPI]
    je __Sc2XCUST32_FLNOCHGE0_
    mov EBX,EAX
    __Sc2XCUST32_FLNOCHGE0_:
    mov [EDI],EBX
    cmp [ESI],EDX
    je __Sc2XCUST32_FLNOCHGE1_
    mov EDX,EAX
    __Sc2XCUST32_FLNOCHGE1_:
    mov [EDI+OPI],EDX
    mov EBX,[ESI-TPI]
    mov EDX,[ESI+OLIS]
    cmp EBX,EDX
    je __Sc2XCUST32_FLNOCHGE2_
    mov EBX,EAX
    __Sc2XCUST32_FLNOCHGE2_:
    mov [EDI+TLI],EBX
    cmp EDX,[ESI]
    je __Sc2XCUST32_FLNOCHGE3_
    mov EDX,EAX
    __Sc2XCUST32_FLNOCHGE3_:
    mov [EDI+TLIA],EDX
    add EDI,TPI
    dec ECX
    jnz __Sc2XCUST32_FLNEXTPOINT_
    add EDI,TLI
    dec dword ptr [Y]
    jnz __Sc2XCUST32_NEXTLINE_
    __Sc2XCUST32_FLBEHODEF_:
    stosd
    stosd
    mov [EDI+TLISS],EAX
    mov [EDI+TLIS],EAX
    dec ECX
    jnz __Sc2XCUST32_FLNEXTPOINT_
    add EDI,TLI
    dec dword ptr [Y]
    __Sc2XCUST32_NEXTLINE_:
    mov ECX,PILI
    __Sc2XCUST32_NEXTPOINT_:
    lodsd
    mov EBX,[ESI-OLIA]
    mov EDX,[ESI]
    cmp EBX,[ESI+OLIS]
    je __Sc2XCUST32_BEHODEF_
    cmp [ESI-TPI],EDX
    je __Sc2XCUST32_BEHODEF_
    cmp EBX,[ESI-TPI]
    je __Sc2XCUST32_NOCHGE0_
    mov EBX,EAX
    __Sc2XCUST32_NOCHGE0_:
    mov [EDI],EBX
    cmp [ESI-OLIA],EDX
    je __Sc2XCUST32_NOCHGE1_
    mov EDX,EAX
    __Sc2XCUST32_NOCHGE1_:
    mov [EDI+OPI],EDX
    mov EBX,[ESI-TPI]
    mov EDX,[ESI+OLIS]
    cmp EBX,EDX
    je __Sc2XCUST32_NOCHGE2_
    mov EBX,EAX
    __Sc2XCUST32_NOCHGE2_:
    mov [EDI+TLI],EBX
    cmp EDX,[ESI]
    je __Sc2XCUST32_NOCHGE3_
    mov EDX,EAX
    __Sc2XCUST32_NOCHGE3_:
    mov [EDI+TLIA],EDX
    add EDI,TPI
    dec ECX
    jnz __Sc2XCUST32_NEXTPOINT_
    add EDI,TLI
    dec dword ptr [Y]
    jnz __Sc2XCUST32_NEXTLINE_
    jmp __Sc2XCUST32_FINALEND_
    __Sc2XCUST32_BEHODEF_:
    stosd
    stosd
    mov [EDI+TLISS],EAX
    mov [EDI+TLIS],EAX
    dec ECX
    jnz __Sc2XCUST32_NEXTPOINT_
    add EDI,TLI
    dec dword ptr [Y]
    jnz __Sc2XCUST32_NEXTLINE_
    __Sc2XCUST32_FINALEND_:
  end asm  
end sub
