Mercurial > vba-clojure
diff clojure/com/aurellem/gb_driver.clj @ 73:8a895ed4c0f9
added history facilities
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Thu, 08 Mar 2012 04:37:10 -0600 |
parents | 39928bf4622d |
children | aaddd7b72a0e |
line wrap: on
line diff
1.1 --- a/clojure/com/aurellem/gb_driver.clj Thu Mar 08 03:41:24 2012 -0600 1.2 +++ b/clojure/com/aurellem/gb_driver.clj Thu Mar 08 04:37:10 2012 -0600 1.3 @@ -11,7 +11,10 @@ 1.4 (def yellow-save-file 1.5 (File. "/home/r/proj/pokemon-escape/roms/yellow.sav")) 1.6 1.7 +(def current-frame (atom 0)) 1.8 + 1.9 (defn vba-init [] 1.10 + (reset! current-frame 0) 1.11 (.delete yellow-save-file) 1.12 (Gb/startEmulator (.getCanonicalPath yellow-rom-image))) 1.13 1.14 @@ -70,9 +73,41 @@ 1.15 (recur (conj buttons button) (rest masks)) 1.16 (recur buttons (rest masks))))))) 1.17 1.18 + 1.19 +(defn save-state [] (Gb/saveState)) 1.20 + 1.21 +(def history (atom {})) 1.22 + 1.23 +(defn goto [frame] 1.24 + (let [save (@history frame)] 1.25 + (if (not (nil? save)) 1.26 + (do 1.27 + (reset! current-frame frame) 1.28 + (Gb/loadState save)) 1.29 + (println "no backup state")))) 1.30 + 1.31 +(defn clear-history [] (reset! history {})) 1.32 + 1.33 +(defn rewind [] (goto (dec @current-frame))) 1.34 + 1.35 +(defn backup-state [frame] 1.36 + (swap! history #(assoc % frame (save-state)))) 1.37 + 1.38 +(def ^:dynamic *save-history* true) 1.39 + 1.40 +(defn advance [] 1.41 + (let [save (save-state)] 1.42 + (backup-state @current-frame) 1.43 + (swap! current-frame inc))) 1.44 + 1.45 (defn step 1.46 - ([] (Gb/step)) 1.47 + ([] (advance) (Gb/step)) 1.48 ([mask-or-buttons] 1.49 + (advance) 1.50 (if (number? mask-or-buttons) 1.51 (Gb/step mask-or-buttons) 1.52 (Gb/step (button-mask mask-or-buttons))))) 1.53 + 1.54 +(defn frame [] @current-frame) 1.55 + 1.56 +