EnableExplicit

#LargePeaks = 1
#PeaksCountMax = 256 ;1024;256
#PeaksCountMaxX = #PeaksCountMax - 1
#PeaksCountMaxStereoAS = #PeaksCountMax * 2 - 1

#ParticleRenderCountMax = #PeaksCountMax * 2
#ParticleRenderCountMaxX = #ParticleRenderCountMax - 1


#PartID = 51885 ; stars/encircle
#LargeRects = 1
#MaxTick = $8000
#RenderMainInfoMySelf = 2 ; 1 = yes, 2=yes with transparency!!!
CompilerIf #RenderMainInfoMySelf = 2
  Global TransparentCenterMul.f = 0.66
  CompilerEndIf

#PI2 = 2 * #PI ;3.1415926536 * 2
Global Dim xCurSpec.f(#PeaksCountMaxStereoAS)
Global Dim PreX.l(#PeaksCountMax)
Global Dim PreY.l(#PeaksCountMaxX)

Declare RenderSelfPart(SkipFrame.l, xElapsedTicks.l)
Declare InitCircles()
Global Dim DefColors(#PeaksCountMaxX)
InitCircles()

IncludeFile "LSDemo (Particles).pb"


Procedure InitCircles()
  Define.l xLong1
  Define.l xLong2
  Define.f xSingle1

  #MPt0H = 0.01
  #MPtX = 0.48
  #MPt0 = #MPt0H * 2
  #MPt1 = #MPt0 + 1 * #MPtX
  #MPt2 = #MPt0 + 2 * #MPtX
  #Mult = (#MPt0 + 2 * #MPtX + #MPt0)

  DefColors(0) = $ff00ff
  For xLong1 = 1 To #PeaksCountMaxX
    ;xSingle1 = (xLong1 * #Mult)  / #PeaksCountMax
    xSingle1 = xLong1 / #PeaksCountMax
    If xSingle1 < #MPt0
      xLong2 = xSingle1 / #MPt0 * 127
      DefColors(xLong1) = $800080 - xLong2 + (xLong2 << 16)
      ElseIf xSingle1 < #MPt1
      xLong2 = (xSingle1 - #MPt0) / #MPtX * 255
      DefColors(xLong1) = $ff0000 - (xLong2 << 16) + (xLong2 << 8)
      ElseIf xSingle1 < #MPt2
      xLong2 = (xSingle1 - #MPt1) / #MPtX * 255
      DefColors(xLong1) = $00ff00 - (xLong2 << 8) + xLong2
      Else
      xLong2 = (xSingle1 - #Mpt2) / #MPt0 * 127
      DefColors(xLong1) = $0000ff + (xLong2 << 16) - xLong2
      EndIf
    Next
  EndProcedure





ProcedureDLL.l DimColor(Color.l, Factor.f)
  Define.l R, G, B
  MOV eax, Color
  MOVZX ecx, al
  MOV R, ecx
  MOV cl, ah
  MOV G, ecx
  SHR eax, 16
  MOV cl, al
  MOV B, ecx
  R * Factor
  G * Factor
  B * Factor
  MOV ecx, B
  MOV al, cl
  SHL eax, 16
  MOV ecx, G
  MOV ah, cl
  MOV ecx, R
  MOV al, cl
  ProcedureReturn
  EndProcedure





Procedure RenderSelfPart(SkipFrame.l, xElapsedTicks.l)
  Define.l xLong1
  Define.f X1
  Define.l X2
  Define.f Y1
  Define.f Y2
  ;Define.f S
  ;Define.l PreCounter
  Define.f xDifL
  Define.f xDifR
  Define.f xDif
  Static CurRot.l
  CurRot = (CurRot + xElapsedTicks) & #MaxTickAND

  Define.f Boost = 1.5 ; 1.5;00
  ;LineSpec_GetSpectrum(@xCurSpec(0), #PeaksCountMax, 3, 2, @Boost)
  If SetWaveData(0)
    GetFullSpectrumStereoOpt(0)
    SpectrumCompactStereo(0, 1024, @xCurSpec(0), #PeaksCountMax)
    endif

;Debug xElapsedTicks

  For xLong1 = 0 To #PeaksCountMaxX
    X1 = Sin(CurRot / #MaxTick * #PI2 + xLong1 / #PeaksCountMax * #PI2) * 1.5
    Y1 = Cos(CurRot / #MaxTick * #PI2 + xLong1 / #PeaksCountMax * #PI2)
    Y2 = Y1 - X1 / 3
    PreX(xLong1) = ParticleCenterX + X1 * MaxSize / 4 ;* (1 + Sin(CurRot / #MaxTick * #PI2) / 4)
    PreY(xLong1) = ParticleCenterY + Y2 * MaxSize / 4 ;* (1 + Sin(CurRot / #MaxTick * #PI2) / 4)
    Next
  PreX(#PeaksCountMax) = PreX(0)

  For xLong1 = 0 To #PeaksCountMaxX
    If PreX(xLong1) < PreX(xLong1 + 1)
CompilerIf #LargePeaks
      ParticleRenderData(ParticleRenderCount)\Left = PreX(xLong1) + #LargePeaks
CompilerElse
      ParticleRenderData(ParticleRenderCount)\Left = PreX(xLong1)
CompilerEndIf
      ParticleRenderData(ParticleRenderCount)\Right = PreX(xLong1 + 1)
      Else
      ParticleRenderData(ParticleRenderCount)\Left = PreX(xLong1 + 1)
CompilerIf #LargePeaks
      ParticleRenderData(ParticleRenderCount)\Right = PreX(xLong1) - #LargePeaks
CompilerElse
      ParticleRenderData(ParticleRenderCount)\Right = PreX(xLong1)
CompilerEndIf
      EndIf
    If ParticleRenderData(ParticleRenderCount)\Left = ParticleRenderData(ParticleRenderCount)\Right
      ParticleRenderData(ParticleRenderCount)\Right + 1
      EndIf  
    xDifL = xCurSpec(xLong1) * MaxSize * Boost / 8
    xDifR = xCurSpec(xLong1 + #PeaksCountMax) * MaxSize * Boost / 8

    ParticleRenderData(ParticleRenderCount)\Top = PreY(xLong1) - xDifL
    ParticleRenderData(ParticleRenderCount)\Bottom = PreY(xLong1) + xDifR

    If ParticleRenderData(ParticleRenderCount)\Top = ParticleRenderData(ParticleRenderCount)\Bottom
      ParticleRenderData(ParticleRenderCount)\Bottom + 1
      
      
      ;ParticleRenderData(ParticleRenderCount)\Brush = DimColor(DefColors(xLong1), 0.5 + (xDifL + xDifR) / 4)
;-* DimColor3
      X1 = 0.5 + (xDifL + xDifR) / 4
      X2 = DefColors(xLong1)
    DimColorASMPrepared()
    ParticleRenderData(ParticleRenderCount)\Brush = X2
      
      
      If xLong1 = 0
        ParticleRenderData(ParticleRenderCount)\Brush = $ff00ff
        EndIf
      Else
      ParticleRenderData(ParticleRenderCount)\Brush = DefColors(xLong1)
      EndIf

    ;If xDif < 1
      ;Else
    
    ;;If xDif < 0.5
    ;  Else
    ;  ParticleRenderData(ParticleRenderCount)\Brush = $ffffff
    ;  EndIf
    OptimizeAndAddRect()
    Next
  EndProcedure


; IDE Options = PureBasic 4.61 (Windows - x64)
; CursorPosition = 114
; FirstLine = 108
; Folding = -
; EnableAsm
; Executable = Simple spectrum x4.lpanim.dll
; DisableDebugger
; IncludeVersionInfo
; AddResource = LSDemo (Particles).rc