<commit_before>(in-package :sdl2-examples)

(require :sdl2)
(require :cl-opengl)

(defun basic-test ()
  (sdl2:with-init (:everything)
    (let* ((win (sdl2:create-window :flags '(:shown :opengl)))
           (gl-context (sdl2:gl-create-context win))
           (controllers ()))

      ;; basic window/gl setup
      (sdl2:gl-make-current win gl-context)
      (gl:viewport 0 0 800 600)
      (gl:matrix-mode :projection)
      (gl:ortho -2 2 -2 2 -2 2)
      (gl:matrix-mode :modelview)
      (gl:load-identity)
      (gl:clear-color 0.0 0.0 1.0 1.0)
      (gl:clear :color-buffer)

      ;; open any game controllers
      (loop for i from 0 upto (- (sdl2:joystick-count) 1)
         do (when (sdl2:game-controller-p i)
              (format t "Found gamecontroller: ~a~%"
                      (sdl2:game-controller-name-for-index i))
              (setf controllers (acons i (sdl2:game-controller-open i) controllers))))
      
      (sdl2:with-event-loop (:method :poll)
        (:keydown (:keysym keysym)
          (let ((scancode (sdl2:scancode-value keysym))
                (sym (sdl2:sym-value keysym))
                (mod-value (sdl2:mod-value keysym)))
            (if (sdl2:scancode= scancode :scancode-w)
                (if (and
                     (sdl2::mod-value-p mod-value :kmod-rctrl :kmod-lctrl)
                     (sdl2::mod-value-p mod-value :kmod-rshift :kmod-lshift))
                    (print "BACKWARD")
                    (print "FORWARD"))
                (progn
                  (print (format nil "Key sym: ~a, code: ~a, mod: ~a"
                                 sym
                                 scancode
                                 mod-value))))))
        (:keyup (:keysym keysym)
          (when (sdl2:scancode= (sdl2:scancode-value keysym) :scancode-escape)
            (sdl2:push-event :quit)))
        (:mousemotion (:x x :y y :xrel xrel :yrel yrel :state state)
          (print (format
                  nil
                  "Mouse motion abs(rel): ~a (~a), ~a (~a)~%Mouse state: ~a"
                  x xrel y yrel state)))
        (:controlleraxismotion (:which controller-id :axis axis-id :value value)
           (format t "Controller axis motion: Controller: ~a, Axis: ~a, Value: ~a~%"
                   controller-id axis-id value))
        (:idle ()
          (gl:clear :color-buffer)
          (gl:begin :triangles)
          (gl:color 1.0 0.0 0.0)
          (gl:vertex 0.0 1.0)
          (gl:vertex -1.0 -1.0)
          (gl:vertex 1.0 -1.0)
          (gl:end)
          (gl:flush)
          (sdl2:gl-swap-window win))
        (:quit () t))

      ;; close any game controllers that were opened
      (loop for (_ . controller) in controllers
         do (sdl2:game-controller-close controller))
      
      (sdl2:gl-delete-context gl-context)
      (sdl2:destroy-window win))))<commit_msg>- some reformatting because I'm tired of fighting emacs - updated basic-test to rumble when a gamecontroller button is pressed on a supported gamepad<commit_after>(in-package :sdl2-examples)

(require :sdl2)
(require :cl-opengl)

(defun basic-test ()
  (sdl2:with-init (:everything)
    (let* ((win (sdl2:create-window :flags '(:shown :opengl)))
           (gl-context (sdl2:gl-create-context win))
           (controllers ())
           (haptic ()))

      ;; basic window/gl setup
      (sdl2:gl-make-current win gl-context)
      (gl:viewport 0 0 800 600)
      (gl:matrix-mode :projection)
      (gl:ortho -2 2 -2 2 -2 2)
      (gl:matrix-mode :modelview)
      (gl:load-identity)
      (gl:clear-color 0.0 0.0 1.0 1.0)
      (gl:clear :color-buffer)

      ;; open any game controllers
      (loop for i from 0 upto (- (sdl2:joystick-count) 1)
         do (when (sdl2:game-controller-p i)
              (format t "Found gamecontroller: ~a~%"
                      (sdl2:game-controller-name-for-index i))
              (let* ((gc (sdl2:game-controller-open i))
                     (joy (sdl2:game-controller-get-joystick gc)))
                (setf controllers (acons i gc controllers))
                (when (sdl2:joystick-is-haptic joy)
                  (let ((h (sdl2:haptic-open-from-joystick joy)))
                    (setf haptic (acons i h haptic))
                    (sdl2:rumble-init h))))))
      
      (sdl2:with-event-loop (:method :poll)
        (:keydown
         (:keysym keysym)
         (let ((scancode (sdl2:scancode-value keysym))
               (sym (sdl2:sym-value keysym))
               (mod-value (sdl2:mod-value keysym)))
           (if (sdl2:scancode= scancode :scancode-w)
               (if (and
                    (sdl2::mod-value-p mod-value :kmod-rctrl :kmod-lctrl)
                    (sdl2::mod-value-p mod-value :kmod-rshift :kmod-lshift))
                   (print "BACKWARD")
                   (print "FORWARD"))
               (progn
                 (print (format nil "Key sym: ~a, code: ~a, mod: ~a"
                                sym
                                scancode
                                mod-value))))))
        
        (:keyup
         (:keysym keysym)
         (when (sdl2:scancode= (sdl2:scancode-value keysym) :scancode-escape)
           (sdl2:push-event :quit)))
        
        (:mousemotion
         (:x x :y y :xrel xrel :yrel yrel :state state)
         (print (format
                 nil
                 "Mouse motion abs(rel): ~a (~a), ~a (~a)~%Mouse state: ~a"
                 x xrel y yrel state)))
        
        (:controlleraxismotion
         (:which controller-id :axis axis-id :value value)
         (format t "Controller axis motion: Controller: ~a, Axis: ~a, Value: ~a~%"
                 controller-id axis-id value))
        
        (:controllerbuttondown
         (:which controller-id)
         (let ((h (cdr (assoc controller-id haptic))))
           (when h
             (sdl2:rumble-play h 1.0 100))))
        
        (:idle
         ()
         (gl:clear :color-buffer)
         (gl:begin :triangles)
         (gl:color 1.0 0.0 0.0)
         (gl:vertex 0.0 1.0)
         (gl:vertex -1.0 -1.0)
         (gl:vertex 1.0 -1.0)
         (gl:end)
         (gl:flush)
         (sdl2:gl-swap-window win))
        
        (:quit () t))

      ;; close any game controllers that were opened
      ;; as well as any haptics
      (loop for (i . controller) in controllers
         do (progn
              (sdl2:game-controller-close controller)
              (sdl2:haptic-close (cdr (assoc i haptic)))))
      
      (sdl2:gl-delete-context gl-context)
      (sdl2:destroy-window win))))
{'fraction_non_alphanumeric': 0.13491214667685256, 'fraction_numerical': 0.021848739495798318, 'mean_word_length': 1.6131736526946108, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5588304', 'n_tokens_mistral': 2284, 'n_tokens_neox': 2068, 'n_words': 530}