EnableExplicit

Define.i width = 2000, height = 1400
Define.i x, y, iteration, max_iteration = 500
Define.d zx, zy, cx, cy, temp_zx
Define.i color

; Fenster öffnen
If OpenWindow(0, 0, 0, width, height, "Mandelbrot - macOS Fix", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  
  ; Wir nutzen ein Canvas statt eines Screens, das vermeidet die AGL-Abhängigkeit
  CanvasGadget(0, 0, 0, width, height)
  
  ; Zeichnen auf dem Canvas starten
  If StartDrawing(CanvasOutput(0))
    For y = 0 To height - 1
      For x = 0 To width - 1
        
        ; Koordinaten-Transformation
        cx = (x - width / 1.5) * 3.0 / width
        cy = (y - height / 2.0) * 3.0 / width
        
        zx = 0
        zy = 0
        iteration = 0
        
        ; Die Mandelbrot-Formel: z = z^2 + c
        While (zx * zx + zy * zy < 4) And (iteration < max_iteration)
          temp_zx = zx * zx - zy * zy + cx
          zy = 2 * zx * zy + cy
          zx = temp_zx
          iteration + 1
        Wend
        
        ; Farbgebung
        If iteration = max_iteration
          Plot(x, y, RGB(0, 0, 0))
        Else
          color = iteration * 255 / max_iteration
          Plot(x, y, RGB(color, color / 2, 255 - color))
        EndIf
        
      Next x
    Next y
    StopDrawing()
  EndIf

  ; Einfache Event-Schleife
  Repeat
    Define Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf
; IDE Options = PureBasic 6.21 - C Backend (MacOS X - arm64)
; CursorPosition = 2
; EnableXP
; DPIAware