EnableExplicit

#ParticleRenderCountMax = 500
#ParticleRenderCountMaxX = #ParticleRenderCountMax - 1
#PartID = 51885 ; stars/encircle
#LargeRects = 0
#MaxTick = $1000
#RenderMainInfoMySelf = 2 ; 1 = yes, 2=yes with transparency!!!
CompilerIf #RenderMainInfoMySelf = 2
  Global TransparentCenterMul.f = 0.7
  CompilerEndIf


Global Dim xStarAngleSin.l(#ParticleRenderCountMaxX)
Global Dim xStarAngleCos.l(#ParticleRenderCountMaxX)
Global Dim xStarColMul.l(#ParticleRenderCountMaxX, 5)
Global Dim xStarTick.l(#ParticleRenderCountMaxX)
#PI2 = 2 * #PI ;3.1415926536 * 2
;Global StretchSimple.f
Global Dim xCurSpec.f(5)
Global Dim xCurSpecX.l(5)
Global Dim xSpecMax.l(5)


Declare RenderSelfPart(SkipFrame.l, xElapsedTicks.l)
Declare InitStarfield()
InitStarfield()

IncludeFile "LSDemo (Particles).pb"






Procedure InitStarfield()
  Define.l xLong1
  Define.l xLong2
  Define.f xDouble1
  #MagicDifMul = 1.7
  
  Dim xColorAngle.f(5)
  
  xColorAngle(3) = 0.5235987756
  xColorAngle(4) = 1.5707963268
  xColorAngle(5) = 2.617993878
  xColorAngle(2) = 3.6651914292
  xColorAngle(1) = 4.7123889804
  xColorAngle(0) = 5.7595865316
  #Div = #PI2 / $ffff
  Define.f xAngle
  For xLong1 = 0 To #ParticleRenderCountMaxX
    xAngle = Random($ffff) * #Div;/ $ffff * #PI2
    For xLong2 = 0 To 5
      xDouble1 = xAngle - xColorAngle(xLong2)
      If xLong2 = 3 And xAngle > #PI
        xDouble1 = xDouble1 - #PI2
        ;xDouble1 = 0/3
  ;      Debug Str((xDouble1 + #PI2) / #PI * 180) + ", " + Str(xDouble1 / #PI * 180)
        If xDouble1 < 0: xDouble1 = xColorAngle(xLong2) - xAngle + #PI2: EndIf;xDouble1 + #PI2: EndIf
        ElseIf xLong2 = 0 And xAngle < #PI
        xDouble1 = xDouble1 + #PI2
        If xDouble1 < 0: xDouble1 = xColorAngle(xLong2) - xAngle - #PI: EndIf;xDouble1 + #PI2: EndIf
        Else
        If xDouble1 < 0: xDouble1 = xColorAngle(xLong2) - xAngle: EndIf;xDouble1 + #PI2: EndIf
        EndIf
      ;If (xLong1 = 0 Or xLong2 = 3) And xDouble1 > #PI2 * 3 / 4: xDouble1 - #PI2: EndIf
      If xDouble1 < #MagicDifMul
        If xLong2 = 1 Or xLong2 = 4
          xStarColMul(xLong1, xLong2) = (#MagicDifMul - xDouble1) * 158 / #MagicDifMul ; 1.25 * 127 / #MagicDifMul
          Else
          xStarColMul(xLong1, xLong2) = (#MagicDifMul - xDouble1) * 127 / #MagicDifMul
          EndIf
        ;If xLong2 = 1 Or xLong2 = 4: xStarColMul(xLong1, xLong2) * 1.25: EndIf
        EndIf
      Next
      
    xStarAngleCos(xLong1) = Cos(xAngle) * 100
    xStarAngleSin(xLong1) = Sin(xAngle) * 100
    xStarTick(xLong1) = Random(#MaxTick)
    Next
  EndProcedure




;Global MMonitor.l = 1


Procedure.l CLng(xDouble1.f)
  Define.l Result = xDouble1
  ProcedureReturn Result
  EndProcedure

Procedure RenderSelfPart(SkipFrame.l, xElapsedTicks.l)
  Define.l xLong1
  Define.l xLong2
  Define.f X
  Define.f Y
  Define.l B
  Define.f S
CompilerIf #RenderMainInfoMySelf
  Define.f X1
  Define.l X2
  CompilerEndIf

  Define.f Boost = 1.5;00
  
  If SetWaveData(0)
    GetFullSpectrumStereoOpt(0)
    SpectrumCompactStereo(0, 1024, @xCurSpec(0), 3)
    EndIf

;Debug xElapsedTicks

  For xLong1 = 0 To 5
    xLong2 = xCurSpec(xLong1) * 100 * 100 * Boost
    If xLong2 < 1000
      xLong2 = 1000
      ElseIf xLong2 > 10000
      xLong2 = 10000
      EndIf
    If xLong2 > xSpecMax(xLong1)
      xSpecMax(xLong1) = xLong2
      Else
      xSpecMax(xLong1) = xSpecMax(xLong1) - 25
      EndIf
    xCurSpecX(xLong1) = xCurSpec(xLong1) * MaxInit * 100 * Boost
    Next


  If xSpecMax(3) > xSpecMax(0): xSpecMax(0) = xSpecMax(3): EndIf
  If xSpecMax(4) > xSpecMax(1): xSpecMax(1) = xSpecMax(4): EndIf
  If xSpecMax(5) > xSpecMax(2): xSpecMax(2) = xSpecMax(5): EndIf

  For xLong1 = 0 To #ParticleRenderCountMaxX
    xStarTick(xLong1) = ((xStarTick(xLong1) + xElapsedTicks) & #MaxTickAND)
    B = xStarTick(xLong1) * xStarTick(xLong1) * 10 / #MaxTickExp
    
    X = ParticleCenterX + xStarAngleSin(xLong1) * B * MaxSize / 100000
    Y = ParticleCenterY + xStarAngleCos(xLong1) * B * MaxSize / 100000
    S = B * MaxSize / 160000
    If S < 0.75: S = 1: EndIf
;If xLong1 = 0
;Debug "#" + Str(xStarTick(xLong1)) + " / " + Str(B)
;Debug Str(X) + " / " + Str(Y) + " / " + Str(S)
;Debug "!" + Str(xStarAngleSin(xLong1)) + " / " + Str(xStarAngleCos(xLong1)) + " / " + Str(MaxSize)
;EndIf

    
    ParticleRenderData(ParticleRenderCount)\Left = X - S
    ParticleRenderData(ParticleRenderCount)\Top = Y - S
    ParticleRenderData(ParticleRenderCount)\Right = X + S
    ParticleRenderData(ParticleRenderCount)\Bottom = Y + S
    
    B = B * MaxInit / 100 * 128 / 1000

    
;    xLong2 = (xCurSpecX(0) * xStarColMul(xLong1, 0) + xCurSpecX(3) * xStarColMul(xLong1, 3)) / 10000
    xLong2 = (xCurSpecX(0) * xStarColMul(xLong1, 0) + xCurSpecX(3) * xStarColMul(xLong1, 3)) / xSpecMax(0)
    If xLong2 > 127: xLong2 = 127: EndIf
    ParticleRenderData(ParticleRenderCount)\Brush = CLng(B + xLong2) * $10000
;    xLong2 = (xCurSpecX(1) * xStarColMul(xLong1, 1) + xCurSpecX(4) * xStarColMul(xLong1, 4)) / 10000
    xLong2 = (xCurSpecX(1) * xStarColMul(xLong1, 1) + xCurSpecX(4) * xStarColMul(xLong1, 4)) / xSpecMax(1)
    If xLong2 > 127: xLong2 = 127: EndIf
    ParticleRenderData(ParticleRenderCount)\Brush + CLng(B + xLong2) * $100
;    xLong2 = (xCurSpecX(2) * xStarColMul(xLong1, 2) + xCurSpecX(5) * xStarColMul(xLong1, 5)) / 10000
    xLong2 = (xCurSpecX(2) * xStarColMul(xLong1, 2) + xCurSpecX(5) * xStarColMul(xLong1, 5)) / xSpecMax(2)
    If xLong2 > 127: xLong2 = 127: EndIf
;ParticleRenderData(ParticleRenderCount)\Brush = $ffffff
    ParticleRenderData(ParticleRenderCount)\Brush + CLng(B + xLong2)

    OptimizeAndAddRect()
    Next
  EndProcedure


; IDE Options = PureBasic 5.20 LTS (Windows - x64)
; CursorPosition = 2
; Folding = -
; EnableAsm
; Executable = Simple spectrum x4.lpanim.dll
; DisableDebugger
; IncludeVersionInfo
; AddResource = LSDemo (Particles).rc