;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Flock of birds ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; SPH 2022 - PB6.0 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;é

;-CONSTANTS
Enumeration
  #MainWindow
  #OpenGLGadget
EndEnumeration

;These two GL constants are used for texture creation. Don't change their values.
#GL_BGR = $80E0
#GL_BGRA = $80E1

ExamineDesktops()
ddw=DesktopWidth(0)
ddh=DesktopHeight(0)

;-********************* DPI *********************
dpix.f=DesktopResolutionX()
dpiy.f=DesktopResolutionY()

ech_xf.f=(1920/ddw);*DesktopResolutionX()
ech_yf.f=(1080/ddh);*DesktopResolutionY()
;**********************************************


;-STRUCTURES
Structure Integer2
  X.i
  Y.i
EndStructure
Global.Integer2 WindowDim
WindowDim\X = ddw
WindowDim\Y = ddh


;-DEFINES
Define.i Event
Define.i WindowFlags = #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget


;-DECLARES
Declare Render()
Declare Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
Declare SetupOpenGL()
Declare SetupGLTexture(ImageHandle.i)


;-MAIN WINDOW
win=OpenWindow(#MainWindow, 0, 0,ddw,ddh, "Flock of birds",#PB_Window_Maximize|#PB_Window_BorderLess)
If win=0
  ;  Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenWindow() impossible")
  End
EndIf

screenGL=OpenGLGadget(#OpenGLGadget,0,0,ddw,ddh)
If screenGL=0
  ;  Beep_(500,250) : Delay(150) : Beep_(500,250)
  MessageRequester("Erreur","OpenGLGadget() impossible")
  End
EndIf


SetupOpenGL()

AddKeyboardShortcut(0,  #PB_Shortcut_Escape, 666) ; quitter


glOrtho_(0,ddw,ddh,0,-1,1)
glMatrixMode_(#GL_MODELVIEW)
glLoadIdentity_()
glClear_(0)

;;;;;;;;;;;
timer=ElapsedMilliseconds()

glClearColor_(0.6,0.6,1,0.8)
ShowCursor_(0)
;- **********************************************
;- **********************************************
;- **********************************************
;- **********************************************

nb=1000

Dim couleur.f(nb)
Dim sph_x1.f(nb)
Dim sph_y1.f(nb)
Dim sph_x2.f(nb)
Dim sph_y2.f(nb)
Dim sph_xx2.f(nb)
Dim sph_yy2.f(nb)

Dim sph_rx.f(nb)

Dim sph_x3.f(nb)
Dim sph_y3.f(nb)

group.f=3
xx3.f=(100+Random(900))/100000
yy3.f=(100+Random(900))/100000

;;
For i=0 To nb
  
  sph_x1.f(i)=0.001+Random(100000)/1000000
  sph_y1.f(i)=0.001+Random(100000)/1000000
  sph_x2.f(i)=0.001+Random(100000)/1000000
  sph_y2.f(i)=0.001+Random(100000)/1000000
  
  sph_x3.f(i)=0.001+Random(100000)/1000000
  sph_y3.f(i)=0.001+Random(100000)/1000000
  
  sph_rx.f(i)=0.005+Random(100)/20000
  couleur(i)=Random(Random(255))/255
Next


;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;##  D E B U T  ##**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************
;-;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;#################**********************************************

Repeat
  
  For i=0 To nb
    
    ddw21=Sin(sph_x1(i)+sph_x2(i))*ddw/5
    ddh21=Cos(sph_y1(i)+sph_y2(i))*ddh/5
    ddw22=ddw/5-ddw21
    ddh22=ddh/5-ddh21
    
    sph_xx=Cos(sph_x1(i))*ddw21
    sph_yy=Sin(sph_y1(i))*ddh21
    sph_xx+Cos(sph_x2(i)+sph_x3(i))*ddw22;+Cos(sph_x3(i))*120
    sph_yy+Cos(sph_y2(i)+sph_y3(i))*ddh22;+Sin(sph_y3(i))*120
    
    sph_xx2(i)+(sph_xx-sph_xx2(i))/(i/group)
    sph_yy2(i)+(sph_yy-sph_yy2(i))/(i/group)
    
    ;===========
    
    sph_x1(i)+sph_rx(i)
    sph_y1(i)+sph_rx(i)
    
    sph_x3(i)+xx3
    sph_y3(i)+yy3
  Next
  
  
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  
  
  ;*****
  Repeat
    Event = WindowEvent()
    
    Select Event
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 1
            Resx = GetGadgetAttribute(1, #PB_OpenGL_MouseX)
            Resy = GetGadgetAttribute(1, #PB_OpenGL_MouseY)
            ;;;;;;;         
            Select EventType()
                ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;             
            EndSelect
        EndSelect
      Case #PB_Event_Menu
        Select EventMenu()
          Case 666
            timer=ElapsedMilliseconds()-timer
            ;MessageRequester("Timer", Str(timer)+" ms - "+Str(scene)+" frames",2)
            ;           Debug scene
            ;           Debug timer
            ShowCursor_(1)
            End
        EndSelect
    EndSelect
    
  Until Event = 0
  
  
  ;##############################################
  ;##############################################
  ;############    polygones :   ################
  glBegin_(#GL_POLYGON)
  glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
  glColor4f_(1,1,1,0.05)
  glVertex2f_(0/ech_xf/dpix,200/ech_yf/dpiy)
  glVertex2f_(0/ech_xf/dpix,400/ech_yf/dpiy)
  glVertex2f_(300/ech_xf/dpix,450/ech_yf/dpiy)
  glVertex2f_(700/ech_xf/dpix,400/ech_yf/dpiy)
  glVertex2f_(720/ech_xf/dpix,350/ech_yf/dpiy)
  glVertex2f_(680/ech_xf/dpix,250/ech_yf/dpiy)
  glVertex2f_(300/ech_xf/dpix,180/ech_yf/dpiy)
  glEnd_()
  
  glBegin_(#GL_POLYGON)
  glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
  glColor4f_(1,1,1,0.05)
  glVertex2f_(350/ech_xf/dpix,300/ech_yf/dpiy)
  glVertex2f_(320/ech_xf/dpix,400/ech_yf/dpiy)
  glVertex2f_(360/ech_xf/dpix,500/ech_yf/dpiy)
  glVertex2f_(900/ech_xf/dpix,550/ech_yf/dpiy)
  glVertex2f_(1200/ech_xf/dpix,500/ech_yf/dpiy)
  glVertex2f_(1220/ech_xf/dpix,450/ech_yf/dpiy)
  glVertex2f_(1180/ech_xf/dpix,350/ech_yf/dpiy)
  glVertex2f_(800/ech_xf/dpix,280/ech_yf/dpiy)
  glEnd_()
  
  glBegin_(#GL_POLYGON)
  glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
  glColor4f_(1,1,1,0.05)
  glVertex2f_(1150/ech_xf/dpix,800/ech_yf/dpiy)
  glVertex2f_(1100/ech_xf/dpix,900/ech_yf/dpiy)
  glVertex2f_(1160/ech_xf/dpix,1000/ech_yf/dpiy)
  glVertex2f_(2000/ech_xf/dpix,1050/ech_yf/dpiy)
  glVertex2f_(2700/ech_xf/dpix,1000/ech_yf/dpiy)
  glVertex2f_(2720/ech_xf/dpix,950/ech_yf/dpiy)
  glVertex2f_(2480/ech_xf/dpix,850/ech_yf/dpiy)
  glVertex2f_(2100/ech_xf/dpix,780/ech_yf/dpiy)
  glEnd_()
  
  glBegin_(#GL_POLYGON)
  glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
  glColor4f_(1,1,1,0.04)
  glVertex2f_(800/ech_xf/dpix,500/ech_yf/dpiy)
  glVertex2f_(750/ech_xf/dpix,600/ech_yf/dpiy)
  glVertex2f_(805/ech_xf/dpix,800/ech_yf/dpiy)
  glVertex2f_(1800/ech_xf/dpix,850/ech_yf/dpiy)
  glVertex2f_(2400/ech_xf/dpix,800/ech_yf/dpiy)
  glVertex2f_(2490/ech_xf/dpix,650/ech_yf/dpiy)
  glVertex2f_(2380/ech_xf/dpix,550/ech_yf/dpiy)
  glVertex2f_(1550/ech_xf/dpix,440/ech_yf/dpiy)
  glEnd_()
  
  glBegin_(#GL_POLYGON)
  glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
  glColor4f_(1,1,1,0.05)
  glVertex2f_(1400/ech_xf/dpix,0/ech_yf/dpiy)
  glVertex2f_(1300/ech_xf/dpix,75/ech_yf/dpiy)
  glVertex2f_(1420/ech_xf/dpix,150/ech_yf/dpiy)
  glVertex2f_(1800/ech_xf/dpix,168/ech_yf/dpiy)
  glVertex2f_(2400/ech_xf/dpix,150/ech_yf/dpiy)
  glVertex2f_(2490/ech_xf/dpix,0/ech_yf/dpiy)
  glEnd_()
  
  glBegin_(#GL_POLYGON)
  glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
  glColor4f_(1,1,1,0.05)
  glVertex2f_(0/ech_xf/dpix,800/ech_yf/dpiy)
  glVertex2f_(0/ech_xf/dpix,1200/ech_yf/dpiy)
  glVertex2f_(420/ech_xf/dpix,1000/ech_yf/dpiy)
  glVertex2f_(600/ech_xf/dpix,868/ech_yf/dpiy)
  glVertex2f_(580/ech_xf/dpix,750/ech_yf/dpiy)
  glVertex2f_(50/ech_xf/dpix,800/ech_yf/dpiy)
  glEnd_()
  
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SRC_ALPHA)
    glColor4f_(0,0,0,0.3)
    glVertex2f_(0,ddh/dpiy)
    glVertex2f_(ddw/dpix,0)
    glEnd_()
    
    ;-polyline      
    glBegin_(#GL_LINES);glBegin_(#GL_POLYGON)
    glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
    glColor4f_(0,0,0,1)
    glVertex2f_((ddw/2)/dpix-5,(ddh/2)/dpiy);*ech_yf)
    glVertex2f_((ddw/2)/dpix+5,(ddh/2)/dpiy);*ech_yf)
    glEnd_()
  EndIf
  ;=====
  ;=====
  ;=====
  For i=0 To nb
    If Random(3)>0
      rand=Random(1)*2
      glBegin_(#GL_LINES);glBegin_(#GL_POLYGON)
      glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
      glColor4f_(couleur(i),couleur(i),couleur(i),0.6)
      glVertex2f_(((ddw/2)/dpix+sph_xx2(i))-rand,((ddh/2)/dpiy+sph_yy2(i)))
      glVertex2f_(((ddw/2)/dpix+sph_xx2(i))+4+rand,((ddh/2)/dpiy+sph_yy2(i)))
      glEnd_()
    Else
      rand2=Random(1)*2-1
      glBegin_(#GL_LINES);glBegin_(#GL_POLYGON)
      glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
      glColor4f_(couleur(i),couleur(i),couleur(i),0.6)
      glVertex2f_((ddw/2)/dpix+sph_xx2(i)-1,(ddh/2)/dpiy+sph_yy2(i)+rand2)
      glVertex2f_((ddw/2)/dpix+sph_xx2(i)+2,(ddh/2)/dpiy+sph_yy2(i))
      glEnd_()
      glBegin_(#GL_LINES);glBegin_(#GL_POLYGON)
      glBlendFunc_(#GL_SRC_ALPHA,#GL_ONE_MINUS_SRC_ALPHA)
      glColor4f_(couleur(i),couleur(i),couleur(i),0.6)
      glVertex2f_((ddw/2)/dpix+sph_xx2(i)+2,(ddh/2)/dpiy+sph_yy2(i))
      glVertex2f_((ddw/2)/dpix+sph_xx2(i)+5,(ddh/2)/dpiy+sph_yy2(i)+rand2)
      glEnd_()
    EndIf
    
    
  Next
  
  SetGadgetAttribute(#OpenGLGadget, #PB_OpenGL_FlipBuffers, #True)
  
ForEver


Procedure Render()
  
  ;Clearing buffers and resetting clear color to remove old graphics from the last frame.
  glClear_(#GL_COLOR_BUFFER_BIT | #GL_DEPTH_BUFFER_BIT)
  ;  glClearColor_(0.2, 0.2, 0.2, 1.0)
  glClearColor_(0,0,0,1)
  
  ;## DRAWING TEXTURES/IMAGES
  ;First enable the Texture system.
  glEnable_(#GL_TEXTURE_2D)
  
  ;This procedure will create a quad and apply a texture to it.
  ;The Texture variable contains the texture created earlier using SetupGLTexture().
  Render2DQuad(Texture1, 0, 0, ImageWidth(Image1), ImageHeight(Image1), -2)
  ; Render2DQuad(Texture2, 0, 0, ImageWidth(Image2), ImageHeight(Image2), -1)
  
  ;After all the textures have been displayed disable the texture system.
  ;Otherwise it will conflict with the non texture graphics.
  glDisable_(#GL_TEXTURE_2D)
  
EndProcedure

Procedure Render2DQuad(OGLTexture.i, StartX.d, StartY.d, Width.i, Height.i, Z.d)
  
  ;The texture is first bound which tells OpenGL to use this texture for any future rendering.
  glBindTexture_(#GL_TEXTURE_2D, OGLTexture)
  glBegin_(#GL_QUADS)
  glColor4f_   (1,1,1,1)
  glNormal3f_  (0,0,1.0)
  glTexCoord2f_(1.0,1.0)
  glVertex3f_  (StartX+Width,StartY,Z)
  glTexCoord2f_(0.0,1.0)
  glVertex3f_  (StartX,StartY,Z)
  glTexCoord2f_(0.0,0.0)
  glVertex3f_  (StartX,StartY+Height,Z)
  glTexCoord2f_(1.0,0.0)
  glVertex3f_  (StartX+Width,StartY+Height,Z)
  glEnd_()
  
EndProcedure

Procedure SetupOpenGL()
  
  glMatrixMode_(#GL_PROJECTION)
  
  glOrtho_(0.0, WindowDim\X, WindowDim\Y, 0.0, -1000.0, 1000.0)
  
  glMatrixMode_(#GL_MODELVIEW)
  
  ; glEnable_(#GL_DEPTH_TEST)
  
  glEnable_(#GL_BLEND)
  glBlendFunc_(#GL_SRC_ALPHA, #GL_ONE_MINUS_SRC_ALPHA)
  
EndProcedure