# HG changeset patch # User Robert McIntyre # Date 1331342280 21600 # Node ID 95cb2152d7cd229720a69a7505b3eadff34c103b # Parent 04d539d26bdc8a06523925c5f9b525d01c774acc fleshing out functional gb interface diff -r 04d539d26bdc -r 95cb2152d7cd clojure/com/aurellem/gb_driver.clj --- a/clojure/com/aurellem/gb_driver.clj Fri Mar 09 13:24:02 2012 -0600 +++ b/clojure/com/aurellem/gb_driver.clj Fri Mar 09 19:18:00 2012 -0600 @@ -177,3 +177,30 @@ `(binding [*save-history* false] ~@forms)) + +(require '(clojure [zip :as zip])) + + + + +(defn tree->str [original] + (loop [s ".\n" loc (zip/down (zip/seq-zip (seq original)))] + (if (zip/end? loc) s + (let [d (count (zip/path loc)) + rep + (str + s + (if (and (zip/up loc) + (> (count (-> loc zip/up zip/rights)) 0)) + "|" "") + (apply str (repeat (dec d) " ")) + (if (= (count (zip/rights loc)) 0) + "`-- " + "|-- ") + (zip/node loc) + "\n")] + (recur rep (zip/next loc)))))) + + + + diff -r 04d539d26bdc -r 95cb2152d7cd clojure/com/aurellem/gb_funs.clj --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/clojure/com/aurellem/gb_funs.clj Fri Mar 09 19:18:00 2012 -0600 @@ -0,0 +1,104 @@ +(ns com.aurellem.gb-funs + (:import com.aurellem.gb.Gb) + (:import java.io.File) + (:import org.apache.commons.io.FileUtils) + (:import (java.nio IntBuffer ByteOrder))) + +;; Savestates +(defrecord SaveState [frame data]) + +(def ^:dynamic *save-state-cache* + (File. "/home/r/proj/pokemon-escape/save-states/")) + +(defn frame->filename [frame] + (File. *save-state-cache* (format "%07d.sav" frame))) + +(defn write-save! [^SaveState save] + (let [buf (:data save) + bytes (byte-array (.limit buf)) + dest (frame->filename (:frame save))] + (.get buf bytes) + (FileUtils/writeByteArrayToFile dest bytes) + (.rewind buf) + save)) + +(defn read-save [frame] + (let [save (frame->filename frame)] + (if (.exists save) + (let [buf (Gb/saveBuffer) + bytes (FileUtils/readFileToByteArray save)] + (.put buf bytes) + (.flip buf) + (SaveState. frame buf))))) +;;;;;;;;;;;;;;;; + +;; Gameboy management +(Gb/loadVBA) + +(def yellow-rom-image + (File. "/home/r/proj/pokemon-escape/roms/yellow.gbc")) + +(def yellow-save-file + (File. "/home/r/proj/pokemon-escape/roms/yellow.sav")) + +(def on? (atom nil)) + +(defn shutdown! [] (Gb/shutdown) (reset! on? false)) + +(defn restart! [] + (shutdown!) + (.delete yellow-save-file) + (Gb/startEmulator (.getCanonicalPath yellow-rom-image)) + (reset! on? true)) + +;;; The first state! +(defn gen-root! [] + (restart!) + (write-save! (SaveState. 0 (Gb/saveState)))) + +(defn root [] + (if (.exists (frame->filename 0)) + (read-save 0) + (gen-root!))) + +;;;; Press Buttons + +(def button-code + {;; main buttons + :a 0x0001 + :b 0x0002 + + ;; directional pad + :r 0x0010 + :l 0x0020 + :u 0x0040 + :d 0x0080 + + ;; meta buttons + :select 0x0004 + :start 0x0008 + + ;; pseudo-buttons + :restart 0x0800 ; hard reset -- not really a button + :listen -1 ; listen for user input + }) + +(defn button-mask [buttons] + (reduce bit-or 0x0000 (map button-code buttons))) + +(def current-state (atom nil)) + +(defn step + ([^SaveState state buttons] + (if (not @on?) (restart!)) + (if (not= @current-state state) + (Gb/loadState (:data state))) + (Gb/step (button-mask buttons)) + (reset! current-state + (SaveState. (inc (:frame state))(Gb/saveState))))) + +(defn play + ([^SaveState state] + (step state [:listen])) + ([] (step (if @current-state @current-state (root))))) + diff -r 04d539d26bdc -r 95cb2152d7cd java/src/com/aurellem/gb/Gb.java --- a/java/src/com/aurellem/gb/Gb.java Fri Mar 09 13:24:02 2012 -0600 +++ b/java/src/com/aurellem/gb/Gb.java Fri Mar 09 19:18:00 2012 -0600 @@ -28,7 +28,11 @@ public static native void step(); - public static native void step(int keymask); + public static native void nstep(int keymask); + + public static void step(int keymask){ + if (-1 == keymask) {step();} + else {nstep(keymask);}} public static native void shutdown(); diff -r 04d539d26bdc -r 95cb2152d7cd src/clojure/clojure.cpp --- a/src/clojure/clojure.cpp Fri Mar 09 13:24:02 2012 -0600 +++ b/src/clojure/clojure.cpp Fri Mar 09 19:18:00 2012 -0600 @@ -46,7 +46,7 @@ * Method: step * Signature: ()V */ -JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_step__ +JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_step (JNIEnv *env, jclass clazz){ step(); } @@ -56,7 +56,7 @@ * Method: step * Signature: (I)V */ -JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_step__I +JNIEXPORT void JNICALL Java_com_aurellem_gb_Gb_nstep (JNIEnv *env, jclass clazz, jint keymask){ step(keymask); }