#include "AFlib3_Font.bi"
#include "AFlib3_Sprite.bi"
#include "AFlib3_Screen.bi"
#include "AFlib3_Primitives.bi"
#include "AFlib3_Miscellaneous.bi"

Screenres 800, 600, 24, 2, 0
SetMouse , , 0

Randomize Timer

Declare Sub NextPattern

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 fontface as Integer ) 

Dim Shared as integer WaitTime, Savenumber, colcycle
Dim Shared as integer SliderBottom, SliderTop
Dim Shared as integer SliderBottom_time
Dim Shared as integer transpreview

Dim Shared ShipBuffer as any ptr
ShipBuffer = ImageCreate (402, 302)

cls
WindowTitle "Shmup Pixel Craft Generator (ver. 2015-07-29)"
Locate 1, 1: Color RGB(255, 255, 0), RGB(0, 0, 255): ? "=========== Shmup Pixel Craft Generator =========="
Locate 2, 1: Color RGB(255, 255, 255), RGB(0, 0, 104): ? "                 (ver. 2015-07-29)                "

Locate 4, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "(c)2008-2015 Adigun Azikiwe Polack."
Locate 5, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "All Rights Reserved."
Locate 8, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "Inspired by the Dave Bollinger's EXCELLENT Pixel"
Locate 9, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "Spaceships Tutorial!!  You can read it here at:"
Locate 11, 1: Color RGB(8, 255, 255), RGB(0, 0, 0): ? "http://web.archive.org/web/20080228054410/"
Locate 12, 6: Color RGB(8, 255, 255), RGB(0, 0, 0): ? "www.davebollinger.com/works/pixelspaceships/"
Locate 14, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "Well, I used that tutorial to quite my advantage,"
Locate 15, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "in that I *actually* created my own custom"
Locate 16, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "algorithms using FreeBASIC to create an even"
Locate 17, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "BETTER-looking fleet of small ships, each one"
Locate 18, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "randomly-generated and absolutely more slicker"
Locate 19, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "than the last!!  Think of it as the Gran Turismo"
Locate 20, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "of custom-drawn pixel ships with the size of just"
Locate 21, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "under 20x20 pixels each!!!  ";: Color RGB(80, 255, 80), RGB(0, 0, 0): ? "(^-^)v !!"

Locate 23, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "Enjoy, and do not forget to give me full credit"
Locate 24, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "you use any of these sprites in your programs, ok"
Locate 25, 1: Color RGB(255, 255, 255), RGB(0, 0, 0): ? "now?  Thank you so much and God bless you all!!!"

Locate 27, 27: Color RGB(255, 255, 80), RGB(0, 0, 0): ? "- Adigun Azikiwe Polack"
Locate 28, 1: Color RGB(255, 255, 80), RGB(0, 0, 0): ? "Author and Creator of Shmup Pixel Craft Generator"

'View Print 1 to 75
AFlibIII_LowRes2X
Do
AFlibIII_Print "Press any key to begin, please.", 0, 584, 1.5, 1.5, int(rnd(1) * &hffffff) + 1, 0, 2, 1
AFlibIII_CustomSync 70
Loop Until Inkey$ <> ""
AFlibIII_ClearKeyBuffer
WaitTime = 5

ScreenSet 1, 0
SliderBottom = 40
Dim Shared as Integer ShipSize_x, ShipSize_y, _
                      MulFactor_x, MulFactor_y, _
                      ScaleDown_x, ScaleDown_y

ScaleDown_x = 0
ShipSize_x = 16 - ScaleDown_x
MulFactor_x = 20 - (ScaleDown_x / 2)

ScaleDown_y = 0
ShipSize_y = 17 - ScaleDown_y
MulFactor_y = 20 - (ScaleDown_y / 2)

'ShipSize_x = 16
'MulFactor_x = 20
'
'ShipSize_y = 17
'MulFactor_y = 20

NextPattern

Do
  If transpreview >= 0 then
    AFlibIII_ClearDisplay RGB(0, 0, 0)
  
    For LineHor as integer = 255 to 0 step -1
      Line (0, LineHor + 45)-(400, LineHor + 45), RGB(0, 0, LineHor)
    Next
  else
    AFlibIII_ClearDisplay RGB(255, 4, 255)
  end if
  
  Put (-1, -1), ShipBuffer, Trans
  AFlibIII_FilledBox 0, 0, 399, 68, 0, 152
  If ShipSize_x < 2 then ShipSize_x = 2
  If ShipSize_y < 2 then ShipSize_y = 2

  If transpreview then ShadowPrint "- Transparency -", 264, 0, RGB(255, 200, 255), 1, 1, RGB(255, 0, 255), 4
  ShadowPrint "[ESC]", 16, 0, RGB(80, 255, 80), 1, 1, RGB(0, 120, 0), 4
  ShadowPrint "[Space]", 0, 8, RGB(80, 255, 80), 1, 1, RGB(0, 120, 0), 4
  ShadowPrint "[CTRL]", 8, 16, RGB(80, 255, 80), 1, 1, RGB(0, 120, 0), 4
  ShadowPrint "[A]", 32, 24, RGB(80, 255, 80), 1, 1, RGB(0, 120, 0), 4
  ShadowPrint "[S]", 32, 32, RGB(80, 255, 80), 1, 1, RGB(0, 120, 0), 4
  ShadowPrint "- Exit", 64, 0, RGB(255, 255, 255), 1, 1, RGB(120, 120, 120), 4
  ShadowPrint "- Next Pattern", 64, 8, RGB(255, 255, 255), 1, 1, RGB(120, 120, 120), 4
  ShadowPrint "- Next Pattern + Toggle Ship Colors", 64, 16, RGB(255, 255, 255), 1, 1, RGB(120, 120, 120), 4
  ShadowPrint "- Toggle the Transparency Preview on/off", 64, 24, RGB(255, 255, 255), 1, 1, RGB(120, 120, 120), 4
  ShadowPrint "- Save Pattern to .BMP file", 64, 32, RGB(255, 255, 255), 1, 1, RGB(120, 120, 120), 4

  ShadowPrint "Ship Size (Change with [Z], [X], [C], and [V]):", 0, 48, RGB(255, 255, 30), 1, 1, RGB(128, 128, 24), 4
  ShadowPrint "Around " + Ltrim$(str(ShipSize_x + 3)) + "x" + Ltrim$(str(ShipSize_y + 3)) + " pixels", 16, 56, RGB(255, 255, 255), 1, 1, RGB(120, 120, 120), 4

  If SliderBottom_time <= 0 then
    SliderBottom += 1: If SliderBottom > 40 then SliderBottom = 40
    SliderBottom_time = 0
  else
    SliderBottom -= 2: If SliderBottom < 0 then SliderBottom = 0
    SliderBottom_time -= 1
  End if
  AFlibIII_FilledBox 0, 276 + SliderBottom, 399, 300 + SliderBottom, 0, 152
  ShadowPrint "Pattern saved to a .BMP as", 8, 280 + SliderBottom, RGB(255, 255, 255), 1, 1, RGB(120, 120, 120), 4
  ShadowPrint chr$(34) + "PixelCraft_sheet_" + ltrim$(str$(Savenumber)) + ".bmp" + chr$(34), 8, 288 + SliderBottom, RGB(255, 255, 30), 1, 1, RGB(128, 128, 24), 4
  AFlibIII_LowRes2X
  ScreenCopy  
  WaitTime -= 1

  
  If multikey(&h01) then Exit Do
  
  If WaitTime < 0 then
    WaitTime = 0
    If multikey(&h39) then
      WaitTime = 5: NextPattern
    End if
    If multikey(&h1d) then
      colcycle = not colcycle
      WaitTime = 5: NextPattern
    End if
    If multikey(&h1e) then
      transpreview = not transpreview
      WaitTime = 5
    End if
    If multikey(&h2c) then
      ScaleDown_x += 1
      If ScaleDown_x > 14 then ScaleDown_x = 14
      ShipSize_x = 16 - ScaleDown_x
      WaitTime = 5: NextPattern
    End if
    If multikey(&h2d) then
      ScaleDown_x -= 1
      If ScaleDown_x < 0 then ScaleDown_x = 0
      ShipSize_x = 16 - ScaleDown_x
      WaitTime = 5: NextPattern
    End if
    If multikey(&h2e) then
      ScaleDown_y += 1
      If ScaleDown_y > 15 then ScaleDown_y = 15
      ShipSize_y = 17 - ScaleDown_y
      WaitTime = 5: NextPattern
    End if
    If multikey(&h2f) then
      ScaleDown_y -= 1
      If ScaleDown_y < 0 then ScaleDown_y = 0
      ShipSize_y = 17 - ScaleDown_y
      WaitTime = 5: NextPattern
    End if
    If multikey(&h1f) then
      Savenumber += 1
      AFlibIII_ClearDisplay RGB(255, 255, 255)
      ScreenCopy
      Sleep 50, 1
      SliderBottom = 40: SliderBottom_time = 200
      BSave "PixelCraft_sheet_" + ltrim$(str$(Savenumber)) + ".bmp", ShipBuffer
      WaitTime = 5
    End if
  End if
  AFlibIII_CustomSync 70
Loop

ImageDestroy ShipBuffer
End



Sub NextPattern

Line ShipBuffer, (0, 0)-(402, 302), &hff00ff, bf
For ShipLine_x as Integer = 0 to 21 step 1
  For ShipLine_y as Integer = 0 to 14 step 1
    If colcycle = -1 then
      If int(rnd(1) * 100) + 1 > int(rnd(1) * 25) + 10 then
        AFlibIII_CreatePixelCraft (ShipLine_x * MulFactor_x) + 3, (ShipLine_y * MulFactor_y) + 3, _
                                  int(rnd(1)* 205) + 51, _
                                  int(rnd(1)* 205) + 51, _
                                  int(rnd(1)* 205) + 51, _
                                  ShipSize_x, ShipSize_y, ShipBuffer
      else
        AFlibIII_CreatePixelCraft (ShipLine_x * MulFactor_x) + 3, (ShipLine_y * MulFactor_y) + 3, _
                                  255, 255, 255, _
                                  ShipSize_x, ShipSize_y, ShipBuffer
      End if
    else
      AFlibIII_CreatePixelCraft (ShipLine_x * MulFactor_x) + 3, (ShipLine_y * MulFactor_y) + 3, _
                                255, 255, 255, _
                                ShipSize_x, ShipSize_y, ShipBuffer
    End if
  Next
Next
End Sub





'__________________________________________________________________________ 
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 fontface as Integer ) 

AFlibIII_Print text, x + sh_x, y + sh_y, 1, 1, sh_col, 0, 0, fontface
AFlibIII_Print text, x, y, 1, 1, col, 0, 0, fontface

End Sub