<commit_before>(ns game.gif
 (require [arcadia.core :as a]
          [hard.core :as hard]
          [tween.core :refer :all]
          [game.data :as data])
 (import [UnityEngine RenderTexture Camera Texture2D TextureFormat Rect Application Time Canvas]
         [GifEncoder AnimatedGifEncoder]
         GameUtils))

(def gif-cam (atom nil))
(def gif-tex (atom nil))
(def gif-encoder (atom false))

(defn setup-encoder []
 (let [encoder (AnimatedGifEncoder. (GameUtils/CreateDocumentFile))]
  (reset! gif-encoder encoder)))

(defn setup [w h b]
 (let [render-texture (RenderTexture. w h b)
       gif-canvas (hard/clone! :ui/gif-canvas)
       cam-go (hard/clone! :gif-cam)
       cam (a/cmpt cam-go Camera)]
  (reset! gif-cam cam)
  (reset! gif-tex render-texture)
  (if (false? @gif-encoder) (setup-encoder))
  (set! (.targetTexture cam) render-texture)
  (set! (.worldCamera (a/cmpt gif-canvas Canvas)) cam) 
  (a/hook+ cam-go :update
   (fn [go]
    (let [skatecam (a/object-tagged "skatecam")]
     (if (false? (a/null-obj? skatecam))
      (do
       (set! (.. go transform position) (.. (a/object-tagged "skatecam") transform position))
       (set! (.. go transform rotation) (.. (a/object-tagged "skatecam") transform rotation)))))))
  (.SetDelay @gif-encoder (float (/ 1000 24)))
  (add-watch data/recording? nil
   (fn [_ _ _ new-state]
    (if new-state
     (start-recording))))))

(defn start-recording []
 (let [start-time Time/time
       recording-canvas (hard/clone! :ui/recording-canvas)]
  (set! (.worldCamera (a/cmpt recording-canvas Canvas)) (a/cmpt (a/object-tagged "skatecam") Camera))
  (timeline* :loop
   (wait Time/deltaTime)
   #(do
     (if (or
          (false? @data/recording?)
          (>= (- Time/time start-time) 15))
      (do
       (.Finish @gif-encoder)
       (reset! data/recording? false)
       (setup-encoder)
       (a/destroy recording-canvas)
       (abort!)))
     (.Render @gif-cam)
     (set! RenderTexture/active @gif-tex)
     (let [frame-texture (Texture2D. (.width @gif-tex) (.height @gif-tex) (TextureFormat/RGB24) false)
           gif-rect (Rect. 0 0 (.width @gif-tex) (.height @gif-tex))]
      (.ReadPixels frame-texture gif-rect 0 0)
      (.AddFrame @gif-encoder frame-texture)
      (a/destroy frame-texture))
     nil))))

'(setup 800 480 24)
'(start-recording)
<commit_msg>only setup encoder on record<commit_after>(ns game.gif
 (require [arcadia.core :as a]
          [hard.core :as hard]
          [tween.core :refer :all]
          [game.data :as data])
 (import [UnityEngine RenderTexture Camera Texture2D TextureFormat Rect Application Time Canvas]
         [GifEncoder AnimatedGifEncoder]
         GameUtils))

(def gif-cam (atom nil))
(def gif-tex (atom nil))
(def gif-encoder (atom nil))

(defn setup-encoder []
 (let [encoder (AnimatedGifEncoder. (GameUtils/CreateDocumentFile))]
  (.SetDelay @gif-encoder (float (/ 1000 24)))
  (reset! gif-encoder encoder)))

(defn setup [w h b]
 (let [render-texture (RenderTexture. w h b)
       gif-canvas (hard/clone! :ui/gif-canvas)
       cam-go (hard/clone! :gif-cam)
       cam (a/cmpt cam-go Camera)]
  (reset! gif-cam cam)
  (reset! gif-tex render-texture)
  (set! (.targetTexture cam) render-texture)
  (set! (.worldCamera (a/cmpt gif-canvas Canvas)) cam) 
  (a/hook+ cam-go :update
   (fn [go]
    (let [skatecam (a/object-tagged "skatecam")]
     (if (false? (a/null-obj? skatecam))
      (do
       (set! (.. go transform position) (.. (a/object-tagged "skatecam") transform position))
       (set! (.. go transform rotation) (.. (a/object-tagged "skatecam") transform rotation)))))))
  (add-watch data/recording? nil
   (fn [_ _ _ new-state]
    (if new-state
     (start-recording))))))

(defn start-recording []
 (let [start-time Time/time
       recording-canvas (hard/clone! :ui/recording-canvas)]
  (setup-encoder)
  (set! (.worldCamera (a/cmpt recording-canvas Canvas)) (a/cmpt (a/object-tagged "skatecam") Camera))
  (timeline* :loop
   (wait Time/deltaTime)
   #(do
     (if (or
          (false? @data/recording?)
          (>= (- Time/time start-time) 15))
      (do
       (.Finish @gif-encoder)
       (reset! data/recording? false)
       (setup-encoder)
       (a/destroy recording-canvas)
       (abort!)))
     (.Render @gif-cam)
     (set! RenderTexture/active @gif-tex)
     (let [frame-texture (Texture2D. (.width @gif-tex) (.height @gif-tex) (TextureFormat/RGB24) false)
           gif-rect (Rect. 0 0 (.width @gif-tex) (.height @gif-tex))]
      (.ReadPixels frame-texture gif-rect 0 0)
      (.AddFrame @gif-encoder frame-texture)
      (a/destroy frame-texture))
     nil))))

'(setup 800 480 24)

{'fraction_non_alphanumeric': 0.16167922497308934, 'fraction_numerical': 0.010333692142088267, 'mean_word_length': 2.8428453267162945, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5157512', 'n_tokens_mistral': 1689, 'n_tokens_neox': 1609, 'n_words': 420}