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


Global ExcludeBottomP.l
Global ExcludeTopM.l
Global ParticleRenderCount.l
#HideText = 0 ; not very optimized!!! - for testing
#AnimPlugin = 2
#RenderSelf = 2
#PSFMode = 0
#FixedUse = 0 ; 1
#SpectrumArraySize = 3
#MMonitor = 1
#WarnMsg = 0
#HAlign = 1
#StereoBMP = 1
#OnPicRender = 0

CompilerIf #RenderMainInfoMySelf
  ;Procedure EraseBkGndSelf()
  ;  EndProcedure
  Global hBmpInfoSelfBk.i
  Global hBmpInfoSelfBkNow.i
  Global xWinRect3.RECT
  Global PaintNowR.RECT
  Global PaintNowB.l

  Global RenderMainInfoMySelf_RPLeft.l
  ;Global RenderMainInfoMySelf_RPTop.l

  ;Global ParticleRenderCountI.l
  ;Global Dim ParticleRenderDataI.RectAndBrush(#ParticleRenderCountMaxX)

  CompilerEndIf

Macro DimColorASMPrepared()
  CompilerIf #HideText
    CompilerElse

  FLD X1

    CompilerIf #PB_Compiler_Processor = #PB_Processor_x64
      XOR rax, rax
      MOV rcx, rax
      MOV eax, X2
      PUSH rax
      MOV rdx, rsp
      ADD rdx, 4
      MOV rax, rdx
      DEC rax
      MOV [rax], cl
      DEC rax
      FILD word [rax]
      FMUL st, st1
      FISTP word [rdx]
      MOV [rax], byte cl
      DEC rax
      INC rdx
      
      FILD word [rax]
      FMUL st, st1
      FISTP word [rdx]
      MOV [rax], byte cl
      DEC rax
      INC rdx
      
      FILD word [rax]
      FMUL st, st1
      FISTP word [rdx]
      MOV eax, [rax + 3]
      BSWAP eax
      ADD rsp, 8
      MOV X2, eax
      FSTP ST0
      CompilerElse
      MOV eax, X2
      XOR ecx, ecx
      PUSH ecx
      MOV edx, esp
      PUSH eax
      MOV eax, edx
      DEC eax
      MOV [eax], cl
      DEC eax
      FILD word [eax]
      FMUL st, st1
      FISTP word [edx]
      MOV [eax], byte cl
      DEC eax
      INC edx
      
      FILD word [eax]
      FMUL st, st1
      FISTP word [edx]
      MOV [eax], byte cl
      DEC eax
      INC edx
      
      FILD word [eax]
      FMUL st, st1
      FISTP word [edx]
      MOV eax, [eax + 3]
      BSWAP eax
      ADD esp, 8
      MOV X2, eax
      FSTP ST0
      CompilerEndIf
    CompilerEndIf
  EndMacro


CompilerIf #MaxTick
Declare RenderSelf(SkipFrame.l, xElapsedTicks.l)
CompilerElse
Declare RenderSelf(SkipFrame.l)
CompilerEndIf

Declare ResizedSelf()

CompilerIf #PartID = 51885
  Global MaxSize.l
  CompilerEndIf
Global ParticleCenterX.l
Global ParticleCenterY.l

Structure RectAndBrush
  Left.l
  Top.l
  Right.l
  Bottom.l
  Brush.i
  EndStructure

Global Dim ParticleRenderData.RectAndBrush(#ParticleRenderCountMaxX)
Global xWinRect.RECT
Global xWinRect2.RECT

Global MaxInit.l
MaxInit = 5

#MaxTickAND = #MaxTick - 1
#MaxTickExp = #MaxTick * #MaxTick / 100

IncludeFile "LSDemo.pb"

CompilerIf #RenderMainInfoMySelf
  #InfoIntHeightPPX = #InfoIntHeight + #BorderForImageX2 + 2
  CompilerEndIf

CompilerIf #MaxTick
Procedure RenderSelf(SkipFrame.l, xElapsedTicks.l)
CompilerElse
Procedure RenderSelf(SkipFrame.l)
CompilerEndIf
  Define.l xLong1
;  Define.l hBrush
  Define.i hBrush2
  CompilerIf #MaxTick
    CompilerElse
    Static OldTick.l
    CompilerEndIf

  If SkipFrame
    If Not MaxInit = 100
      MaxInit + 5
      EndIf
    EndIf

  xLong1 = (GetTickCount_() & #MaxTickAND)

CompilerIf #RenderMainInfoMySelf
  hBmpInfoSelfBkNow = 0
  ;hBrush = CreateSolidBrush_(0)
  ;If hBrush
    If hBmpInfoSelfBk
      If SelectObject_(hDCTemp, hBmpInfoSelfBk)
        hBmpInfoSelfBkNow = 1
        FillRect_(hDCTemp, @xWinRect3, hBrush)
        EndIf
      EndIf
    ;EndIf
CompilerEndIf

  CompilerIf #MaxTick
    RenderSelfPart(SkipFrame, xElapsedTicks)
    CompilerElse
    RenderSelfPart(SkipFrame, (xLong1 + #MaxTick - OldTick) & #MaxTickAND)
    OldTick = xLong1
    CompilerEndIf


  CompilerIf #RenderMainInfoMySelf
    CompilerIf #HideText
      CompilerElse
      If hBmpInfoSelfBkNow
;Debug Str(RenderMainInfoMySelf_RPLeft) + " / " + Str(#BorderForImage)
        SetTextColor_(hDCTemp, $ffffff)
        SetBkMode_(hDCTemp, 1)
        If *hBitmapTxt
          DrawBmp(*hBitmapTxt, RenderMainInfoMySelf_RPLeft, #BorderForImage)
          Else
          DrawBmp(*hBitmapGen, RenderMainInfoMySelf_RPLeft, #BorderForImage)
          EndIf
        EndIf
      CompilerEndIf
    CompilerEndIf
    
  If hBrush
    FillRect_(xMainDC, @xWinRect, hBrush)
    FillRect_(xMainDC, @xWinRect2, hBrush)
;    DeleteObject_(hBrush)
    EndIf

;Debug ParticleRenderCount
  If ParticleRenderCount
    ParticleRenderCount - 1
  ;  Me.Cls
  
    xLong1 = 0
;Debug Str(ParticleRenderCount) + ", " + Str(ParticleRenderData(xLong1)\Left) + ", " + Str(ParticleRenderData(xLong1)\Right) + ", " + Str(ParticleRenderData(xLong1)\Top) + ", " + Str(ParticleRenderData(xLong1)\Bottom) + ", " + Str(ParticleRenderData(xLong1)\Brush)
    For xLong1 = 0 To ParticleRenderCount
      hBrush2 = CreateSolidBrush_(ParticleRenderData(xLong1)\Brush)
      If hBrush2
        FillRect_(xMainDC, @ParticleRenderData(xLong1), hBrush2)
        DeleteObject_(hBrush2)
        EndIf
  ;    Me.Line (ParticleRenderData(xLong1).Left, ParticleRenderData(xLong1).Top)-(ParticleRenderData(xLong1).Right, ParticleRenderData(xLong1).Bottom), xRenderBrush(xLong1), BF
      Next
    ;ElseIf hBrush
    ;DeleteObject_(hBrush)
    EndIf
;hBmpInfoSelfBkNow =
  CompilerIf #RenderMainInfoMySelf
    If hBmpInfoSelfBkNow
      BitBlt_(xMainDC, 0, ExcludeTopM, xWinRect3\Right, #InfoIntHeightPPX, hDCTemp, 0, 0, #SRCCOPY)
      EndIf
    CompilerEndIf
  ParticleRenderCount = 0
  EndProcedure

CompilerIf #RenderMainInfoMySelf
CompilerIf #RenderMainInfoMySelf = 2
  Macro OptimizeAndAddRect_BrushCreator()
        X1 = TransparentCenterMul
        X2 = ParticleRenderData(ParticleRenderCount)\Brush
        DimColorASMPrepared()
        PaintNowB = CreateSolidBrush_(X2)
    EndMacro
CompilerElse
  Macro OptimizeAndAddRect_BrushCreator()
        PaintNowB = CreateSolidBrush_(ParticleRenderData(ParticleRenderCount)\Brush)
    EndMacro
CompilerEndIf
CompilerEndIf

Macro OptimizeAndAddRect_PartlyAbove_Macro()
    CompilerIf #RenderMainInfoMySelf
      If hBmpInfoSelfBkNow
        OptimizeAndAddRect_BrushCreator()
        If PaintNowB
          PaintNowR\Left = ParticleRenderData(ParticleRenderCount)\Left
          PaintNowR\Right = ParticleRenderData(ParticleRenderCount)\Right
          PaintNowR\Top = 0
          PaintNowR\Bottom = ParticleRenderData(ParticleRenderCount)\Bottom - ExcludeTopM
          FillRect_(hDCTemp, @PaintNowR, PaintNowB)
          DeleteObject_(PaintNowB)
          EndIf
        EndIf
      CompilerEndIf
EndMacro

Macro OptimizeAndAddRect()
  If (ParticleRenderData(ParticleRenderCount)\Brush & $f0f0f0) = 0
    ElseIf ParticleRenderData(ParticleRenderCount)\Bottom < ExcludeTop
    ; above
    ParticleRenderCount + 1
    ElseIf ParticleRenderData(ParticleRenderCount)\Top < ExcludeTop
    ; partly above




CompilerIf #LargeRects
    If ParticleRenderData(ParticleRenderCount)\Bottom > ExcludeBottom
      CompilerIf #RenderMainInfoMySelf
        If hBmpInfoSelfBkNow
          OptimizeAndAddRect_BrushCreator()
          If PaintNowB
            PaintNowR\Left = ParticleRenderData(ParticleRenderCount)\Left
            PaintNowR\Right = ParticleRenderData(ParticleRenderCount)\Right
            PaintNowR\Top = 0
            PaintNowR\Bottom = xWinRect3\Bottom
    ;    Debug "WithIn2 @ " + Str(PaintNowR\Top) + " / " + Str(PaintNowR\Bottom)
            FillRect_(hDCTemp, @PaintNowR, PaintNowB)
            DeleteObject_(PaintNowB)
            EndIf
          EndIf
        CompilerEndIf
      RtlMoveMemory_(@ParticleRenderData(ParticleRenderCount + 1), @ParticleRenderData(ParticleRenderCount), 20)
      ParticleRenderData(ParticleRenderCount)\Bottom = ExcludeTopM
      ParticleRenderCount + 1
      ParticleRenderData(ParticleRenderCount)\Top = ExcludeBottomP
      ParticleRenderCount + 1
      Else
OptimizeAndAddRect_PartlyAbove_Macro()
      ParticleRenderData(ParticleRenderCount)\Bottom = ExcludeTopM
      ParticleRenderCount + 1
      EndIf
CompilerElse
OptimizeAndAddRect_PartlyAbove_Macro()
    ParticleRenderData(ParticleRenderCount)\Bottom = ExcludeTopM
    ParticleRenderCount + 1
CompilerEndIf
    ElseIf ParticleRenderData(ParticleRenderCount)\Bottom < ExcludeBottomP
    ; within
    CompilerIf #RenderMainInfoMySelf
      If hBmpInfoSelfBkNow
        OptimizeAndAddRect_BrushCreator()
        If PaintNowB
          PaintNowR\Left = ParticleRenderData(ParticleRenderCount)\Left
          PaintNowR\Right = ParticleRenderData(ParticleRenderCount)\Right
          PaintNowR\Top = ParticleRenderData(ParticleRenderCount)\Top - ExcludeTopM
          PaintNowR\Bottom = ParticleRenderData(ParticleRenderCount)\Bottom - ExcludeTopM
  ;    Debug "WithIn2 @ " + Str(PaintNowR\Top) + " / " + Str(PaintNowR\Bottom)
          FillRect_(hDCTemp, @PaintNowR, PaintNowB)
          DeleteObject_(PaintNowB)
          EndIf
        EndIf
      CompilerEndIf
    ElseIf ParticleRenderData(ParticleRenderCount)\Top < ExcludeBottomP
    ; partly below
    CompilerIf #RenderMainInfoMySelf
      If hBmpInfoSelfBkNow
        OptimizeAndAddRect_BrushCreator()
        If PaintNowB
          PaintNowR\Left = ParticleRenderData(ParticleRenderCount)\Left
          PaintNowR\Right = ParticleRenderData(ParticleRenderCount)\Right
          PaintNowR\Top = ParticleRenderData(ParticleRenderCount)\Top - ExcludeTopM
          PaintNowR\Bottom = xWinRect3\Bottom
          FillRect_(hDCTemp, @PaintNowR, PaintNowB)
          DeleteObject_(PaintNowB)
          EndIf
        EndIf
      CompilerEndIf
    ParticleRenderData(ParticleRenderCount)\Top = ExcludeBottomP
    ParticleRenderCount + 1
    Else
    ; below
    ParticleRenderCount + 1
    EndIf
  EndMacro

Procedure ResizedSelf()
  ;Define.RECT xType1
 
  GetClientRect_(xMainHWnd, @xWinRect)

  FillRect_(xMainDC, @xWinRect, hBrush)

  xWinRect\Right = xWinRect\Right - xWinRect\Left
  xWinRect\Bottom = xWinRect\Bottom - xWinRect\Top
  xWinRect\Left = 0
  xWinRect\Top = 0
 

  ParticleCenterX = xWinRect\Right / 2
  ParticleCenterY = xWinRect\Bottom / 2

  ExcludeBottomP = ExcludeBottom + 1
  ExcludeTopM.l = ExcludeTop - 1

  xWinRect2\Bottom = xWinRect\Bottom
  xWinRect2\Right = xWinRect\Right
  xWinRect\Bottom = ExcludeTop + #BorderForImage
  xWinRect2\Top = ExcludeBottom - #BorderForImage

CompilerIf #RenderMainInfoMySelf
  If hBmpInfoSelfBk
    SelectObject_(hDCTemp, hBmpOld)
    DeleteObject_(hBmpInfoSelfBk)
    hBmpInfoSelfBk = 0
    EndIf
  Define.i xLong3 = GetDC_(xMainHWnd)
;#BorderForImageX2
  If xLong3
    hBmpInfoSelfBk = CreateCompatibleBitmap_(xLong3, xWinRect\Right, #InfoIntHeightPPX)
    ;xWinRect3\Left = 0
    xWinRect3\Right = xWinRect\Right
    ;xWinRect3\Top = 0
    xWinRect3\Bottom = #InfoIntHeightPPX
    ReleaseDC_(xMainHWnd, xLong3)
    EndIf
  CompilerEndIf

  ;Debug xWinRect2\Left
  ;Debug xWinRect2\Top
  ;Debug xWinRect2\Right
  ;Debug xWinRect2\Bottom

CompilerIf #PartID = 51885
  If ParticleCenterX > ParticleCenterY ;MaxSize
    MaxSize = ParticleCenterX * 1.6;1.4142135623731
    Else
    MaxSize = ParticleCenterY * 1.6;1.4142135623731
    EndIf
CompilerEndIf
  ;StretchSimple = MaxSize / 480 * 3
  EndProcedure


; IDE Options = PureBasic 4.61 Beta 1 (Windows - x64)
; CursorPosition = 33
; FirstLine = 20
; Folding = ----
; EnableAsm
; EnableXP