EnableExplicit

; Copyright (C) 2007-2010 Jan Vorel
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
;
; If you have any questions, suggestions or bug reports about this product,
; contact me: info@ctuser.net; Do not contact me about general B.A.S.I.C.
; language or A.P.I. issues





; Wavedata To spectrum Data conversation is based on a free Visual Basic 6
; example by Ulli "Mühlenweg" <umgedv@yahoo.com>, which can be found at
; http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=65007&lngWId=1

#WidthPX = 24
#HeightPX =32

#AnimPlugin = 2
#RenderSelf = 2
#PSFMode = 0
#FPS=0
#FixedUse = 0
#SpectrumArraySize = #WidthPX
#MMonitor = 1
#WarnMsg = 1
#HAlign = 1
#StereoBMP = 1
#WarnQuit = 0
#OnPicRender = 0
#RenderMainInfoMySelf = 0
#MaxTick = 0
#HideText = 0

Declare ResizedSelf()
Declare RenderSelf(SkipFrame.l)
Global MaxInit.f

#WidthPX_ForDimX = #WidthPX * 2 - 1
Global Dim xCurSpec.f(#WidthPX_ForDimX)

#DoWidth = ((#HeightPX - 1) / 2)
#DoWidthP = #DoWidth + 1

#HeightPX_ForDim = #HeightPX - 1
Global Dim FadeCons.f(#HeightPX_ForDim)

CompilerIf #Delay
  #WidthPX4s = #WidthPX * 8
  Global *Delays = LocalAlloc_(0, #WidthPX4s) ; 4 * (2*16)
  ;Global xDelaysBlockT.l

  If Not *Delays
    Goto NoNo
    EndIf
  CompilerEndIf

MaxInit = 0.05
IncludeFile "LSDemo.pb"
CompilerIf #Delay
LocalFree_(*Delays)
NoNo:
CompilerEndIf

Procedure ResizedSelf()
  Define.RECT xType1
  GetClientRect_(xMainHWnd, @xType1)
  FillRect_(xMainDC, @xType1, hBrush)
  Global FullWindow_H2 = (xType1\Bottom - #PicSizeYOX) / (2 * #HeightPX)
  Global FullWindow_HP2 = xType1\Bottom
  Global FullWindow_W2 = xType1\Right / #WidthPX
  Global FullWindow_H2X = FullWindow_H2 - 1
  Global FullWindow_W2X = FullWindow_W2 - 1

  Define.f P1
  Define.l Row
  For Row = 0 To #DoWidth
    P1 = (Row + 1) / #DoWidthP
    FadeCons(Row) = Sqr(2 * P1 - P1*P1)
    Next
  EndProcedure


Declare.l RealFireOrBeamColor(xColor.f)


ProcedureDLL.l ReScaleRGB(xColor.l, xFactor.f)
  Define.l Result
  Define.l xLong1

  xLong1 = ((xColor & $ff0000) >> 16) * xFactor
  If xLong1 > $ff: xLong1 = $ff: EndIf
  Result = xLong1 * $10000

  xLong1 = ((xColor & $ff00) >> 8) * xFactor
  If xLong1 > $ff: xLong1 = $ff: EndIf
  Result + xLong1 * $100

  xLong1 = (xColor & $ff) * xFactor
  If xLong1 > $ff: xLong1 = $ff: EndIf
  Result + xLong1
  
  ProcedureReturn Result
  EndProcedure

Procedure RenderSelf(SkipFrame.l)
  Define.l Row
  Define.l Col
  Define.l xhBrush
  Define.RECT xType1
  Define.RECT xType2
  Define.RECT xType3
  Define.RECT xType4
  CompilerIf #Delay
    Define.f xPrev
    Define.f xColor
    CompilerEndIf
  ;Define.f RColor
  ;Define.l BColor

  If SkipFrame
    If MaxInit < 1.5
      MaxInit + 0.04
      EndIf
    EndIf

  Define.f Boost = MaxInit ; * 1.5 / 100 ;1.5;00
  ;If SkipFrame
    ;LineSpec_GetSpectrum(@xCurSpec(0), #WidthPX, 3, 2, @Boost)
    If SetWaveData(0)
      GetFullSpectrumStereoOpt(0)
      SpectrumCompactStereo(0, 1024, @xCurSpec(0), #WidthPX)
      EndIf
    CompilerIf #Shift
      BeamCurrent = (BeamCurrent + #Shift) % 766
      SplitCol(BeamCurrent)
      CompilerEndIf
  ;  EndIf

  xType2\Top = (#HeightPX - 1) * FullWindow_H2
  xType3\Top = FullWindow_HP2 - #HeightPX * FullWindow_H2 + 1
  xType4\Top = FullWindow_HP2 - FullWindow_H2X

  xType1\Bottom = FullWindow_H2X
  xType2\Bottom = xType2\Top + FullWindow_H2X
  xType3\Bottom = xType3\Top + FullWindow_H2X
  xType4\Bottom = xType4\Top + FullWindow_H2X

;!XOR eax, eax

  
  CompilerIf #Delay
    Row = *Delays
    CompilerEndIf
  For Col = 0 To #WidthPX_ForDimX
    CompilerIf #Delay
      xColor = xCurSpec(Col)
      xPrev = PeekF(Row)
      If xColor > xPrev
        If xColor > xPrev + #Delay2 ; 0.45 ;/ (Col + 1) ;/ 3 / 2
          xColor = xPrev + #Delay2 ;  0.45 ;/ (Col + 1) ;/ 3 / 2
          EndIf
        ElseIf xColor < xPrev - #Delay1 ; 0.15 ;/ (Col + 1) ;/ 8 / 2
        xColor = xPrev - #Delay1 ;0.15 ;/ (Col + 1) ;/ 8 / 2
        EndIf
      xCurSpec(Col) = RealFireOrBeamColor(xColor)
      PokeF(Row, xColor)
      Row + 4
      CompilerElse
      xCurSpec(Col) = RealFireOrBeamColor(xCurSpec(Col))
      CompilerEndIf
    Next


  For Row = 0 To #DoWidth
    xType1\Left = 0
    xType1\Right = FullWindow_W2X
    xType2\Left = 0
    xType2\Right = FullWindow_W2X
    xType3\Left = 0
    xType3\Right = FullWindow_W2X
    xType4\Left = 0
    xType4\Right = FullWindow_W2X

    For Col = 0 To #WidthPX - 1
;      Define.f P1 = (Row + 1) / #DoWidthP
;      BColor = ReScaleRGB(ColVals(Col), FadeCons(Row))
      
      xhBrush = CreateSolidBrush_(ReScaleRGB(xCurSpec(Col), FadeCons(Row)))
      If Not xhBrush = 0
        FillRect_(xMainDC, xType1, xhBrush)
        ;FillRect_(xMainDC, xType3, xhBrush)
        If Not (xType1\Top = xType2\Top)
          FillRect_(xMainDC, xType2, xhBrush)
          ;FillRect_(xMainDC, xType4, xhBrush)
          EndIf
        DeleteObject_(xhBrush)
        EndIf

      xhBrush = CreateSolidBrush_(ReScaleRGB(xCurSpec(Col + #WidthPX), FadeCons(Row)))
      If Not xhBrush = 0
        ;FillRect_(xMainDC, xType1, xhBrush)
        FillRect_(xMainDC, xType3, xhBrush)
        If Not (xType1\Top = xType2\Top)
          ;FillRect_(xMainDC, xType2, xhBrush)
          FillRect_(xMainDC, xType4, xhBrush)
          EndIf
        DeleteObject_(xhBrush)
        EndIf

      xType1\Left + FullWindow_W2
      xType2\Left + FullWindow_W2
      xType3\Left + FullWindow_W2
      xType4\Left + FullWindow_W2
      xType1\Right + FullWindow_W2
      xType2\Right + FullWindow_W2
      xType3\Right + FullWindow_W2
      xType4\Right + FullWindow_W2
      Next

    xType1\Top + FullWindow_H2
    xType2\Top - FullWindow_H2
    xType3\Top + FullWindow_H2
    xType4\Top - FullWindow_H2

    xType1\Bottom + FullWindow_H2
    xType2\Bottom - FullWindow_H2
    xType3\Bottom + FullWindow_H2
    xType4\Bottom - FullWindow_H2
    Next
  EndProcedure

;IncludeFile "LSDemo_PB_FireBeam.pb"

; IDE Options = PureBasic 4.61 (Windows - x64)
; CursorPosition = 146
; FirstLine = 140
; Folding = -
; EnableAsm
; Executable = C:\Q0501E\USERDATA\DESKTOP\test.exe
; DisableDebugger
; AddResource = D:\DEV\Software\Libraries\LineSpec\Demo applications\source\LSDemo (plasmabeam).rc