<commit_before>(export start
        stop
        get-window-size
        ui
        resize-hooks
        frame-rate)

(define *last-render-time* 0)
(define ui #f)
(define *ui-camera* #f)

(define (render)
    (%swap-buffers (%window))
    (gl:clear (bitwise-ior gl:+color-buffer-bit+ gl:+depth-buffer-bit+))
    (scene:activate-camera *ui-camera*) ; always draw UI last
    (scene:render-cameras)
    (scene:deactivate-camera *ui-camera*))

(define (move-ui-camera w h)
  (scene:set-camera-position! *ui-camera*
                              (make-point (/ w 2) (/ h -2) 1)))

(define resize-hooks (make-parameter (list move-ui-camera)))

(define (resize _ w h)
  (gl:viewport 0 0 w h)
  (for-each (cut <> w h) (resize-hooks))
  (scene:resize-cameras))

(define (start* width height title . args)
  (%init)
  (scene:init (lambda () (%get-window-size (%window))))
  (%window-size-callback resize)
  (%mouse-button-callback mouse-click)
  (cond-expand
    (macosx (apply %make-window width height title
                   samples: 2
                   context-version-major: 3
                   context-version-minor: 2
                   opengl-forward-compat: #t
                   opengl-profile: %+opengl-core-profile+
                   args))
    (else (apply %make-window width height title samples: 2 args)))
  (%%set-cursor-pos-callback (%window) #$hpgCursorPositionCallback)
  (%%set-scroll-callback (%window) #$hpgScrollCallback)
  (%%set-key-callback (%window) #$hpgKeyCallback)
  (%%set-char-callback (%window) #$hpgCharCallback)
  (gl:init)
  (gl:enable gl:+depth-test+)
  (gl:enable gl:+blend+)
  (gl:blend-func gl:+src-alpha+ gl:+one-minus-src-alpha+)
  ;(gl:enable gl:+cull-face+)
  (cond-expand
    ((not gles) (gl:enable gl:+multisample+))
    (else))

  (set! ui (scene:make-scene))
  (set! *ui-camera* (scene:make-camera #:ortho #:position ui))
  (scene:deactivate-camera *ui-camera*)
  (resize #f width height)
  (glls:compile-pipelines)
  ((get-keyword init: args (lambda () (lambda () #f))))
  (gc)
  (set! *last-render-time* (%get-time))
  (let ((update (get-keyword update: args (lambda () (lambda (delta) #f)))))
    (let loop ()
      (let* ((time (%get-time))
             (delta (- time *last-render-time*)))
        (add-frame-time time)
        (update delta)
        (scene:update-scenes)
        (scene:activate-camera *ui-camera*)
        (scene:update-cameras)
        (scene:deactivate-camera *ui-camera*)
        (render)
        (check-error)
        (set! *last-render-time* time)
        (when (feature? csi:)
          (thread-yield!))
        (%poll-events))
      (unless (%window-should-close? (%window))
        (loop))))
  ((get-keyword cleanup: args (lambda () (lambda () #f))))
  (gc)
  (%destroy-window (%window))
  (%terminate))

(define (start width height title . args)
  (define start** (lambda () (apply start* width height title args)))
  (if (feature? csi:)
      (thread-start! start**)
      (start**)))

(define (stop)
  (%set-window-should-close (%window) #t))

(define (get-window-size)
  (%get-window-size (%window)))

;; Frame rate calculation
;; We use a circular buffer to track subsequent frame times
;; Frame rate is calculated averaging the total time between the most recent frame and oldest frame
(define n-frames-1 20) ; number of frame times we keep, minus one, because that's more useful
(define frame-times (make-list (add1 n-frames-1) 0)) ; circular buffer 
(define frame-times-counter 0) ; where are we in the frame-times list?

;; Call every frame
(define (add-frame-time time)
  (set! frame-times-counter
    (if (= frame-times-counter n-frames-1)
        0
        (add1 frame-times-counter)))
  (set! (list-ref frame-times frame-times-counter) time))

(define (frame-rate)
  (let ((oldest-frame (list-ref frame-times frame-times-counter))
        (newest-frame (list-ref frame-times (if (= frame-times-counter n-frames-1)
                                               0
                                               (add1 frame-times-counter)))))
    (/ n-frames-1 (- oldest-frame newest-frame))))<commit_msg>Add update-string-mesh!<commit_after>(export start
        stop
        get-window-size
        ui
        resize-hooks
        frame-rate
        update-string-mesh!)

(define *last-render-time* 0)
(define ui #f)
(define *ui-camera* #f)

(define (render)
    (%swap-buffers (%window))
    (gl:clear (bitwise-ior gl:+color-buffer-bit+ gl:+depth-buffer-bit+))
    (scene:activate-camera *ui-camera*) ; always draw UI last
    (scene:render-cameras)
    (scene:deactivate-camera *ui-camera*))

(define (move-ui-camera w h)
  (scene:set-camera-position! *ui-camera*
                              (make-point (/ w 2) (/ h -2) 1)))

(define resize-hooks (make-parameter (list move-ui-camera)))

(define (resize _ w h)
  (gl:viewport 0 0 w h)
  (for-each (cut <> w h) (resize-hooks))
  (scene:resize-cameras))

(define (start* width height title . args)
  (%init)
  (scene:init (lambda () (%get-window-size (%window))))
  (%window-size-callback resize)
  (%mouse-button-callback mouse-click)
  (cond-expand
    (macosx (apply %make-window width height title
                   samples: 2
                   context-version-major: 3
                   context-version-minor: 2
                   opengl-forward-compat: #t
                   opengl-profile: %+opengl-core-profile+
                   args))
    (else (apply %make-window width height title samples: 2 args)))
  (%%set-cursor-pos-callback (%window) #$hpgCursorPositionCallback)
  (%%set-scroll-callback (%window) #$hpgScrollCallback)
  (%%set-key-callback (%window) #$hpgKeyCallback)
  (%%set-char-callback (%window) #$hpgCharCallback)
  (gl:init)
  (gl:enable gl:+depth-test+)
  (gl:enable gl:+blend+)
  (gl:blend-func gl:+src-alpha+ gl:+one-minus-src-alpha+)
  ;(gl:enable gl:+cull-face+)
  (cond-expand
    ((not gles) (gl:enable gl:+multisample+))
    (else))

  (set! ui (scene:make-scene))
  (set! *ui-camera* (scene:make-camera #:ortho #:position ui))
  (scene:deactivate-camera *ui-camera*)
  (resize #f width height)
  (glls:compile-pipelines)
  ((get-keyword init: args (lambda () (lambda () #f))))
  (gc)
  (set! *last-render-time* (%get-time))
  (let ((update (get-keyword update: args (lambda () (lambda (delta) #f)))))
    (let loop ()
      (let* ((time (%get-time))
             (delta (- time *last-render-time*)))
        (add-frame-time time)
        (update delta)
        (scene:update-scenes)
        (scene:activate-camera *ui-camera*)
        (scene:update-cameras)
        (scene:deactivate-camera *ui-camera*)
        (render)
        (check-error)
        (set! *last-render-time* time)
        (when (feature? csi:)
          (thread-yield!))
        (%poll-events))
      (unless (%window-should-close? (%window))
        (loop))))
  ((get-keyword cleanup: args (lambda () (lambda () #f))))
  (gc)
  (%destroy-window (%window))
  (%terminate))

(define (start width height title . args)
  (define start** (lambda () (apply start* width height title args)))
  (if (feature? csi:)
      (thread-start! start**)
      (start**)))

(define (stop)
  (%set-window-should-close (%window) #t))

(define (get-window-size)
  (%get-window-size (%window)))

;; Frame rate calculation
;; We use a circular buffer to track subsequent frame times
;; Frame rate is calculated averaging the total time between the most recent frame and oldest frame
(define n-frames-1 20) ; number of frame times we keep, minus one, because that's more useful
(define frame-times (make-list (add1 n-frames-1) 0)) ; circular buffer 
(define frame-times-counter 0) ; where are we in the frame-times list?

;; Call every frame
(define (add-frame-time time)
  (set! frame-times-counter
    (if (= frame-times-counter n-frames-1)
        0
        (add1 frame-times-counter)))
  (set! (list-ref frame-times frame-times-counter) time))

(define (frame-rate)
  (let ((oldest-frame (list-ref frame-times frame-times-counter))
        (newest-frame (list-ref frame-times (if (= frame-times-counter n-frames-1)
                                               0
                                               (add1 frame-times-counter)))))
    (/ n-frames-1 (- oldest-frame newest-frame))))

(define (update-string-mesh! mesh renderable string face)
  (string-mesh string face mesh: mesh)
  (glls:set-renderable-n-elements! renderable (mesh-n-indices mesh)))
{'fraction_non_alphanumeric': 0.1625744934445769, 'fraction_numerical': 0.0057210965435041715, 'mean_word_length': 2.7111897390535162, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '42520', 'n_tokens_mistral': 2803, 'n_tokens_neox': 2716, 'n_words': 700}