<commit_before>(in-package :glaw)

;; Framerate utils
;; TODO: add min/avg/max
(defstruct frame-counter
  (nb-frames 0 :type fixnum)
  (sample-size 10)
  (min most-positive-single-float)
  (max most-negative-single-float)
  (sum 0.0)
  (nb-samples 0)
  (rate 0.0)
  (last-render-time (get-internal-real-time)))

(defvar *frame-counter* (make-frame-counter)
  "Framerate counter.")

(defun update-fps ()
  "Updates framerate informations."
  (incf (frame-counter-nb-frames *frame-counter*))
  (when (> (frame-counter-nb-frames *frame-counter*) (frame-counter-sample-size *frame-counter*))
    (let* ((elapsed-time (- (get-internal-real-time)
                            (frame-counter-last-render-time *frame-counter*)))
           (fps (* (/ (frame-counter-nb-frames *frame-counter*)
                      (/ elapsed-time internal-time-units-per-second)) 1.0)))
      (incf (frame-counter-nb-samples *frame-counter*))
      (incf (frame-counter-sum *frame-counter*) fps)
      (when (< fps (frame-counter-min *frame-counter*))
        (setf (frame-counter-min *frame-counter*) fps))
      (when (> fps (frame-counter-max *frame-counter*))
        (setf (frame-counter-max *frame-counter*) fps))
      (setf (frame-counter-rate *frame-counter*) fps)
      (format t "FPS(min/current/max/total avg): ~D / ~D / ~D / ~S~%"
              (frame-counter-min *frame-counter*)
              (frame-counter-rate *frame-counter*)
              (frame-counter-max *frame-counter*)
              (/ (frame-counter-sum *frame-counter*)
                 (if (zerop (frame-counter-nb-samples *frame-counter*))
                     1.0
                     (frame-counter-nb-samples *frame-counter*))))
      (setf (frame-counter-last-render-time *frame-counter*)
            (get-internal-real-time))
      (setf (frame-counter-nb-frames *frame-counter*) 0))))

(defun current-fps ()
  (frame-counter-rate *frame-counter*))

(defun min-fps ()
  (frame-counter-min *frame-counter*))

(defun max-fps ()
  (frame-counter-max *frame-counter*))

(defun avg-fps ()
  (/ (frame-counter-sum *frame-counter*)
     (if (zerop (frame-counter-nb-samples *frame-counter*))
         1.0
         (frame-counter-nb-samples *frame-counter*))))

;;; General rendering
(defvar *display-width* 0)
(defvar *display-height* 0)

(defun setup-gl-defaults ()
  (gl:clear-color 0.3 0.3 0.3 0)
  (gl:disable :depth-test)
  (gl:disable :cull-face)
  (gl:disable :lighting)
  (gl:enable :blend)
  (gl:blend-func :src-alpha :one-minus-src-alpha)
  (gl:enable :normalize)
  (gl:shade-model :smooth)
  (gl:enable :texture-2d))

(defun reshape (width height)
  ;; set viewport to full window
  (gl:viewport 0 0 width height)
  (setf *display-width* width)
  (setf *display-height* height))


(defun begin-draw ()
  (gl:clear :color-buffer))

(defun end-draw ()
  (update-fps)
  (gl:flush))

(defstruct 2d-view
  left right bottom top
  (zoom 1.0))

(defun create-2d-view (x y width height)
  (make-2d-view :left x :right (+ x width)
                :bottom y :top (+ y height)))

(defun 2d-view-width (view)
  (- (2d-view-right view) (2d-view-left view)))

(defmethod (setf 2d-view-width) (value (view 2d-view))
  (setf (2d-view-right view) (+ (2d-view-left view)
                                value)))

(defun 2d-view-height (view)
  (- (2d-view-top view) (2d-view-bottom view)))

(defmethod (setf 2d-view-height) (value (view 2d-view))
  (setf (2d-view-top view) (+ (2d-view-bottom view)
                                 value)))

(defun zoom-2d-view (view dfactor)
  (let ((width-diff (* dfactor (2d-view-width view)))
        (height-diff (* dfactor (2d-view-height view))))
    (decf (2d-view-left view) width-diff)
    (incf (2d-view-right view) width-diff)
    (decf (2d-view-bottom view) height-diff)
    (incf (2d-view-top view) height-diff)
    (incf (2d-view-zoom view) dfactor)))

(defun move-2d-view (view dx dy)
  (incf (2d-view-left view) dx)
  (incf (2d-view-right view) dx)
  (incf (2d-view-bottom view) dy)
  (incf (2d-view-top view) dy))

(defun update-2d-view (view x y width height)
  (setf (2d-view-left view) x)
  (setf (2d-view-bottom view) y)
  (setf (2d-view-right view) (+ x width))
  (setf (2d-view-top view) (+ y height)))

(defun set-view-2d (view)
  (gl:matrix-mode :projection)
  (gl:load-identity)
  (glu:ortho-2d (2d-view-left view) (2d-view-right view)
                (2d-view-bottom view) (2d-view-top view))
  (gl:matrix-mode :modelview)
  (gl:load-identity))

(defun screen-to-view (x y view)
  (unless (or (zerop (2d-view-width view)) (zerop (2d-view-height view))
              (zerop *display-width*) (zerop *display-height*))
    (let ((width-factor (/ (2d-view-width view) *display-width*))
          (height-factor (/ (2d-view-height view) *display-height*)))
      (values (+ (2d-view-left view)
                 (* x width-factor))
              (+ (2d-view-bottom view)
                 (* (- *display-height* y) height-factor))))))


;;; Colors helpers
(defstruct color
  (r 1.0)
  (g 1.0)
  (b 1.0)
  (a 1.0))

(declaim (inline set-color))
(defun set-color (col)
  (gl:color (color-r col) (color-g col) (color-b col) (color-a col)))

(declaim (inline set-color/rgb))
(defun set-color/rgb (r g b &optional (a 1.0))
  (gl:color r g b a))

(defun create-color (r g b &optional (a 1.0))
  (make-color :r r :g g :b b :a a))

(defun copy-color (src dest)
  (setf (color-r dest) (color-r src)
        (color-g dest) (color-g src)
        (color-b dest) (color-b src)
        (color-a dest) (color-a src)))
<commit_msg>Make sure we return float coords for screen-to-view conversion.<commit_after>(in-package :glaw)

;; Framerate utils
;; TODO: add min/avg/max
(defstruct frame-counter
  (nb-frames 0 :type fixnum)
  (sample-size 10)
  (min most-positive-single-float)
  (max most-negative-single-float)
  (sum 0.0)
  (nb-samples 0)
  (rate 0.0)
  (last-render-time (get-internal-real-time)))

(defvar *frame-counter* (make-frame-counter)
  "Framerate counter.")

(defun update-fps ()
  "Updates framerate informations."
  (incf (frame-counter-nb-frames *frame-counter*))
  (when (> (frame-counter-nb-frames *frame-counter*) (frame-counter-sample-size *frame-counter*))
    (let* ((elapsed-time (- (get-internal-real-time)
                            (frame-counter-last-render-time *frame-counter*)))
           (fps (* (/ (frame-counter-nb-frames *frame-counter*)
                      (/ elapsed-time internal-time-units-per-second)) 1.0)))
      (incf (frame-counter-nb-samples *frame-counter*))
      (incf (frame-counter-sum *frame-counter*) fps)
      (when (< fps (frame-counter-min *frame-counter*))
        (setf (frame-counter-min *frame-counter*) fps))
      (when (> fps (frame-counter-max *frame-counter*))
        (setf (frame-counter-max *frame-counter*) fps))
      (setf (frame-counter-rate *frame-counter*) fps)
      (format t "FPS(min/current/max/total avg): ~D / ~D / ~D / ~S~%"
              (frame-counter-min *frame-counter*)
              (frame-counter-rate *frame-counter*)
              (frame-counter-max *frame-counter*)
              (/ (frame-counter-sum *frame-counter*)
                 (if (zerop (frame-counter-nb-samples *frame-counter*))
                     1.0
                     (frame-counter-nb-samples *frame-counter*))))
      (setf (frame-counter-last-render-time *frame-counter*)
            (get-internal-real-time))
      (setf (frame-counter-nb-frames *frame-counter*) 0))))

(defun current-fps ()
  (frame-counter-rate *frame-counter*))

(defun min-fps ()
  (frame-counter-min *frame-counter*))

(defun max-fps ()
  (frame-counter-max *frame-counter*))

(defun avg-fps ()
  (/ (frame-counter-sum *frame-counter*)
     (if (zerop (frame-counter-nb-samples *frame-counter*))
         1.0
         (frame-counter-nb-samples *frame-counter*))))

;;; General rendering
(defvar *display-width* 0)
(defvar *display-height* 0)

(defun setup-gl-defaults ()
  (gl:clear-color 0.3 0.3 0.3 0)
  (gl:disable :depth-test)
  (gl:disable :cull-face)
  (gl:disable :lighting)
  (gl:enable :blend)
  (gl:blend-func :src-alpha :one-minus-src-alpha)
  (gl:enable :normalize)
  (gl:shade-model :smooth)
  (gl:enable :texture-2d))

(defun reshape (width height)
  ;; set viewport to full window
  (gl:viewport 0 0 width height)
  (setf *display-width* width)
  (setf *display-height* height))


(defun begin-draw ()
  (gl:clear :color-buffer))

(defun end-draw ()
  (update-fps)
  (gl:flush))

(defstruct 2d-view
  left right bottom top
  (zoom 1.0))

(defun create-2d-view (x y width height)
  (make-2d-view :left x :right (+ x width)
                :bottom y :top (+ y height)))

(defun 2d-view-width (view)
  (- (2d-view-right view) (2d-view-left view)))

(defmethod (setf 2d-view-width) (value (view 2d-view))
  (setf (2d-view-right view) (+ (2d-view-left view)
                                value)))

(defun 2d-view-height (view)
  (- (2d-view-top view) (2d-view-bottom view)))

(defmethod (setf 2d-view-height) (value (view 2d-view))
  (setf (2d-view-top view) (+ (2d-view-bottom view)
                                 value)))

(defun zoom-2d-view (view dfactor)
  (let ((width-diff (* dfactor (2d-view-width view)))
        (height-diff (* dfactor (2d-view-height view))))
    (decf (2d-view-left view) width-diff)
    (incf (2d-view-right view) width-diff)
    (decf (2d-view-bottom view) height-diff)
    (incf (2d-view-top view) height-diff)
    (incf (2d-view-zoom view) dfactor)))

(defun move-2d-view (view dx dy)
  (incf (2d-view-left view) dx)
  (incf (2d-view-right view) dx)
  (incf (2d-view-bottom view) dy)
  (incf (2d-view-top view) dy))

(defun update-2d-view (view x y width height)
  (setf (2d-view-left view) x)
  (setf (2d-view-bottom view) y)
  (setf (2d-view-right view) (+ x width))
  (setf (2d-view-top view) (+ y height)))

(defun set-view-2d (view)
  (gl:matrix-mode :projection)
  (gl:load-identity)
  (glu:ortho-2d (2d-view-left view) (2d-view-right view)
                (2d-view-bottom view) (2d-view-top view))
  (gl:matrix-mode :modelview)
  (gl:load-identity))

(defun screen-to-view (x y view)
  (unless (or (zerop (2d-view-width view)) (zerop (2d-view-height view))
              (zerop *display-width*) (zerop *display-height*))
    (let ((width-factor (/ (2d-view-width view) *display-width*))
          (height-factor (/ (2d-view-height view) *display-height*)))
      (values (float (+ (2d-view-left view)
                        (* x width-factor)))
              (float (+ (2d-view-bottom view)
                        (* (- *display-height* y) height-factor)))))))


;;; Colors helpers
(defstruct color
  (r 1.0)
  (g 1.0)
  (b 1.0)
  (a 1.0))

(declaim (inline set-color))
(defun set-color (col)
  (gl:color (color-r col) (color-g col) (color-b col) (color-a col)))

(declaim (inline set-color/rgb))
(defun set-color/rgb (r g b &optional (a 1.0))
  (gl:color r g b a))

(defun create-color (r g b &optional (a 1.0))
  (make-color :r r :g g :b b :a a))

(defun copy-color (src dest)
  (setf (color-r dest) (color-r src)
        (color-g dest) (color-g src)
        (color-b dest) (color-b src)
        (color-a dest) (color-a src)))

{'fraction_non_alphanumeric': 0.1773758099352052, 'fraction_numerical': 0.01583873290136789, 'mean_word_length': 2.6822398939695162, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4841112', 'n_tokens_mistral': 4284, 'n_tokens_neox': 4173, 'n_words': 958}