<commit_before>(define-library (lib gl)
(import
   (otus lisp) (otus ffi)
   (OpenGL) (lib gl config))

(export
   gl:set-window-title gl:set-window-size
   gl:set-context-version ; recreate OpenGL with version
   gl:set-userdata
   gl:set-renderer
   gl:finish ; if renderer exists - wait for window close, else just glFinish

   ; this library automatically creates window
   gl:enable-context gl:disable-context
   *atexit*)

(begin
   (setq OS (ref (uname) 1))

   (setq win32? (string-ci=? OS "Windows"))
   (setq linux? (string-ci=? OS "Linux"))
   (setq x32? (eq? (size nullptr) 4))

; check the platform
(or win32? linux?
   (runtime-error "Unsupported platform" OS))

; -- some debug staff -----
(define (vector->vptr vec offset)
   (let ((vptr (vm:cast 0 type-vptr)))
      (for-each (lambda (i)
            (set-ref! vptr i (ref vec (+ i offset))))
         (iota (size nullptr)))
      vptr))
(define (vector-set-int! vec offset int)
   (for-each (lambda (i)
         (set-ref! vec (+ offset i) (band #xFF (>> int (* i 8)))))
      (iota (size nullptr)))
   vec)
(define (vector-set-vptr! vec offset vptr)
   (for-each (lambda (i)
         (set-ref! vec (+ offset i) (ref vptr i)))
      (iota (size nullptr)))
   vec)

; --

(define WIDTH (get config 'width 640))
(define HEIGHT (get config 'height 480))
(define CONFIG (list->ff '( ; todo: move to config
   (red .    8)
   (green .  8)
   (blue .   8)
   (depth . 24)
)))

; ===================================================
(define gl:enable-context (cond
   (win32?  (lambda (context)
               (let ((dc   (ref context 1))
                     (glrc (ref context 2)))
                  (gl:MakeCurrent dc glrc))))
   (linux?  (lambda (context)
               (let ((display (ref context 1))
                    ;(screen  (ref context 2))
                     (window  (ref context 3))
                     (cx      (ref context 4)))
                  (gl:MakeCurrent display window cx))))))

(define gl:disable-context (cond
   (win32?  (lambda (context)
                  (gl:MakeCurrent #f #f)))
   (linux?  (lambda (context)
               (let ((display (ref context 1)))
                  (gl:MakeCurrent display #f #f))))))

(define gl:create-context (cond<commit_msg>default gl windows size changed to 16:9<commit_after>(define-library (lib gl)
(import
   (otus lisp) (otus ffi)
   (OpenGL) (lib gl config))

(export
   gl:set-window-title gl:set-window-size
   gl:set-context-version ; recreate OpenGL with version
   gl:set-userdata
   gl:set-renderer
   gl:finish ; if renderer exists - wait for window close, else just glFinish

   ; this library automatically creates window
   gl:enable-context gl:disable-context
   *atexit*)

(begin
   (setq OS (ref (uname) 1))

   (setq win32? (string-ci=? OS "Windows"))
   (setq linux? (string-ci=? OS "Linux"))
   (setq x32? (eq? (size nullptr) 4))

; check the platform
(or win32? linux?
   (runtime-error "Unsupported platform" OS))

; -- some debug staff -----
(define (vector->vptr vec offset)
   (let ((vptr (vm:cast 0 type-vptr)))
      (for-each (lambda (i)
            (set-ref! vptr i (ref vec (+ i offset))))
         (iota (size nullptr)))
      vptr))
(define (vector-set-int! vec offset int)
   (for-each (lambda (i)
         (set-ref! vec (+ offset i) (band #xFF (>> int (* i 8)))))
      (iota (size nullptr)))
   vec)
(define (vector-set-vptr! vec offset vptr)
   (for-each (lambda (i)
         (set-ref! vec (+ offset i) (ref vptr i)))
      (iota (size nullptr)))
   vec)

; --

(define WIDTH  (get config 'width  854))
(define HEIGHT (get config 'height 480))
(define CONFIG (list->ff '( ; todo: move to config
   (red   .  8)
   (green .  8)
   (blue  .  8)
   (depth . 24)
)))

; ===================================================
(define gl:enable-context (cond
   (win32?  (lambda (context)
               (let ((dc   (ref context 1))
                     (glrc (ref context 2)))
                  (gl:MakeCurrent dc glrc))))
   (linux?  (lambda (context)
               (let ((display (ref context 1))
                    ;(screen  (ref context 2))
                     (window  (ref context 3))
                     (cx      (ref context 4)))
                  (gl:MakeCurrent display window cx))))))

(define gl:disable-context (cond
   (win32?  (lambda (context)
                  (gl:MakeCurrent #f #f)))
   (linux?  (lambda (context)
               (let ((display (ref context 1)))
                  (gl:MakeCurrent display #f #f))))))

(define gl:create-context (cond
{'fraction_non_alphanumeric': 0.1699072028281043, 'fraction_numerical': 0.014803358373840036, 'mean_word_length': 2.326230712711242, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '617772', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1450, 'n_words': 469}